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
41 #include "framebuffer.h"
42 #include "glformats.h"
45 #include "mfeatures.h"
47 #include "renderbuffer.h"
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
60 * None of the GL_EXT_framebuffer_object functions are compiled into
67 * When glGenRender/FramebuffersEXT() is called we insert pointers to
68 * these placeholder objects into the hash table.
69 * Later, when the object ID is first bound, we replace the placeholder
70 * with the real frame/renderbuffer.
72 static struct gl_framebuffer DummyFramebuffer
;
73 static struct gl_renderbuffer DummyRenderbuffer
;
75 /* We bind this framebuffer when applications pass a NULL
76 * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer
;
81 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
87 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
94 _mesa_init_fbobjects(struct gl_context
*ctx
)
96 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
97 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
98 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
99 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
101 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
104 struct gl_framebuffer
*
105 _mesa_get_incomplete_framebuffer(void)
107 return &IncompleteFramebuffer
;
111 * Helper routine for getting a gl_renderbuffer.
113 struct gl_renderbuffer
*
114 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
116 struct gl_renderbuffer
*rb
;
121 rb
= (struct gl_renderbuffer
*)
122 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
128 * Helper routine for getting a gl_framebuffer.
130 struct gl_framebuffer
*
131 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
133 struct gl_framebuffer
*fb
;
138 fb
= (struct gl_framebuffer
*)
139 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
145 * Mark the given framebuffer as invalid. This will force the
146 * test for framebuffer completeness to be done before the framebuffer
150 invalidate_framebuffer(struct gl_framebuffer
*fb
)
152 fb
->_Status
= 0; /* "indeterminate" */
157 * Return the gl_framebuffer object which corresponds to the given
158 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159 * Check support for GL_EXT_framebuffer_blit to determine if certain
161 * \return gl_framebuffer pointer or NULL if target is illegal
163 static struct gl_framebuffer
*
164 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
166 bool have_fb_blit
= _mesa_is_gles3(ctx
) ||
167 (ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
));
169 case GL_DRAW_FRAMEBUFFER
:
170 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
171 case GL_READ_FRAMEBUFFER
:
172 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
173 case GL_FRAMEBUFFER_EXT
:
174 return ctx
->DrawBuffer
;
182 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183 * gl_renderbuffer_attachment object.
184 * This function is only used for user-created FB objects, not the
185 * default / window-system FB object.
186 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187 * the depth buffer attachment point.
189 struct gl_renderbuffer_attachment
*
190 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
195 assert(_mesa_is_user_fbo(fb
));
197 switch (attachment
) {
198 case GL_COLOR_ATTACHMENT0_EXT
:
199 case GL_COLOR_ATTACHMENT1_EXT
:
200 case GL_COLOR_ATTACHMENT2_EXT
:
201 case GL_COLOR_ATTACHMENT3_EXT
:
202 case GL_COLOR_ATTACHMENT4_EXT
:
203 case GL_COLOR_ATTACHMENT5_EXT
:
204 case GL_COLOR_ATTACHMENT6_EXT
:
205 case GL_COLOR_ATTACHMENT7_EXT
:
206 case GL_COLOR_ATTACHMENT8_EXT
:
207 case GL_COLOR_ATTACHMENT9_EXT
:
208 case GL_COLOR_ATTACHMENT10_EXT
:
209 case GL_COLOR_ATTACHMENT11_EXT
:
210 case GL_COLOR_ATTACHMENT12_EXT
:
211 case GL_COLOR_ATTACHMENT13_EXT
:
212 case GL_COLOR_ATTACHMENT14_EXT
:
213 case GL_COLOR_ATTACHMENT15_EXT
:
214 /* Only OpenGL ES 1.x forbids color attachments other than
215 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
218 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
219 if (i
>= ctx
->Const
.MaxColorAttachments
220 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
223 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
224 case GL_DEPTH_STENCIL_ATTACHMENT
:
225 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
228 case GL_DEPTH_ATTACHMENT_EXT
:
229 return &fb
->Attachment
[BUFFER_DEPTH
];
230 case GL_STENCIL_ATTACHMENT_EXT
:
231 return &fb
->Attachment
[BUFFER_STENCIL
];
239 * As above, but only used for getting attachments of the default /
240 * window-system framebuffer (not user-created framebuffer objects).
242 static struct gl_renderbuffer_attachment
*
243 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
246 assert(_mesa_is_winsys_fbo(fb
));
248 if (_mesa_is_gles3(ctx
)) {
249 assert(attachment
== GL_BACK
||
250 attachment
== GL_DEPTH
||
251 attachment
== GL_STENCIL
);
252 switch (attachment
) {
254 /* Since there is no stereo rendering in ES 3.0, only return the
257 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
258 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
259 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
261 return &fb
->Attachment
[BUFFER_DEPTH
];
263 return &fb
->Attachment
[BUFFER_STENCIL
];
267 switch (attachment
) {
269 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
271 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
273 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
275 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
277 if (fb
->Visual
.numAuxBuffers
== 1) {
278 return &fb
->Attachment
[BUFFER_AUX0
];
282 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
284 * "If the default framebuffer is bound to target, then attachment must
285 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
286 * identifying a color buffer; DEPTH, identifying the depth buffer; or
287 * STENCIL, identifying the stencil buffer."
289 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
290 * language. However, revision #33 of the ARB_framebuffer_object spec
293 * "If the default framebuffer is bound to <target>, then <attachment>
294 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
295 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
296 * depth buffer, or the stencil buffer, and <pname> may be
297 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
298 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
300 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
301 * from glext.h, so shipping apps should not use those values.
303 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
304 * support queries of the window system FBO.
307 return &fb
->Attachment
[BUFFER_DEPTH
];
309 return &fb
->Attachment
[BUFFER_STENCIL
];
318 * Remove any texture or renderbuffer attached to the given attachment
319 * point. Update reference counts, etc.
322 _mesa_remove_attachment(struct gl_context
*ctx
,
323 struct gl_renderbuffer_attachment
*att
)
325 if (att
->Type
== GL_TEXTURE
) {
326 ASSERT(att
->Texture
);
327 if (ctx
->Driver
.FinishRenderTexture
) {
328 /* tell driver that we're done rendering to this texture. */
329 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
331 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
332 ASSERT(!att
->Texture
);
334 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
335 ASSERT(!att
->Texture
);
336 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
337 ASSERT(!att
->Renderbuffer
);
340 att
->Complete
= GL_TRUE
;
345 * Bind a texture object to an attachment point.
346 * The previous binding, if any, will be removed first.
349 _mesa_set_texture_attachment(struct gl_context
*ctx
,
350 struct gl_framebuffer
*fb
,
351 struct gl_renderbuffer_attachment
*att
,
352 struct gl_texture_object
*texObj
,
353 GLenum texTarget
, GLuint level
, GLuint zoffset
)
355 if (att
->Texture
== texObj
) {
356 /* re-attaching same texture */
357 ASSERT(att
->Type
== GL_TEXTURE
);
358 if (ctx
->Driver
.FinishRenderTexture
)
359 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
363 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
364 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
365 _mesa_remove_attachment(ctx
, att
);
366 att
->Type
= GL_TEXTURE
;
367 assert(!att
->Texture
);
368 _mesa_reference_texobj(&att
->Texture
, texObj
);
371 /* always update these fields */
372 att
->TextureLevel
= level
;
373 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
374 att
->Zoffset
= zoffset
;
375 att
->Complete
= GL_FALSE
;
377 if (_mesa_get_attachment_teximage(att
)) {
378 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
381 invalidate_framebuffer(fb
);
386 * Bind a renderbuffer to an attachment point.
387 * The previous binding, if any, will be removed first.
390 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
391 struct gl_renderbuffer_attachment
*att
,
392 struct gl_renderbuffer
*rb
)
394 /* XXX check if re-doing same attachment, exit early */
395 _mesa_remove_attachment(ctx
, att
);
396 att
->Type
= GL_RENDERBUFFER_EXT
;
397 att
->Texture
= NULL
; /* just to be safe */
398 att
->Complete
= GL_FALSE
;
399 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
404 * Fallback for ctx->Driver.FramebufferRenderbuffer()
405 * Attach a renderbuffer object to a framebuffer object.
408 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
409 struct gl_framebuffer
*fb
,
410 GLenum attachment
, struct gl_renderbuffer
*rb
)
412 struct gl_renderbuffer_attachment
*att
;
414 _glthread_LOCK_MUTEX(fb
->Mutex
);
416 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
419 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
420 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
421 /* do stencil attachment here (depth already done above) */
422 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
424 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
426 rb
->AttachedAnytime
= GL_TRUE
;
429 _mesa_remove_attachment(ctx
, att
);
432 invalidate_framebuffer(fb
);
434 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
439 * Fallback for ctx->Driver.ValidateFramebuffer()
440 * Check if the renderbuffer's formats are supported by the software
442 * Drivers should probably override this.
445 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
448 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
449 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
451 switch (rb
->_BaseFormat
) {
453 case GL_LUMINANCE_ALPHA
:
458 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
462 switch (rb
->Format
) {
463 /* XXX This list is likely incomplete. */
464 case MESA_FORMAT_RGB9_E5_FLOAT
:
465 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
468 /* render buffer format is supported by software rendering */
480 att_incomplete(const char *msg
)
482 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
483 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
492 fbo_incomplete(const char *msg
, int index
)
494 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
495 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
501 * Is the given base format a legal format for a color renderbuffer?
504 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
506 switch (baseFormat
) {
511 case GL_LUMINANCE_ALPHA
:
514 return ctx
->API
== API_OPENGL_COMPAT
&&
515 ctx
->Extensions
.ARB_framebuffer_object
;
518 return ctx
->Extensions
.ARB_texture_rg
;
526 * Is the given base format a legal format for a color renderbuffer?
529 is_format_color_renderable(const struct gl_context
*ctx
, gl_format format
, GLenum internalFormat
)
531 const GLenum baseFormat
=
532 _mesa_get_format_base_format(format
);
535 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
536 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
540 /* Reject additional cases for GLES */
541 switch (internalFormat
) {
561 if (format
== MESA_FORMAT_ARGB2101010
&& internalFormat
!= GL_RGB10_A2
) {
570 * Is the given base format a legal format for a depth/stencil renderbuffer?
573 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
575 switch (baseFormat
) {
576 case GL_DEPTH_COMPONENT
:
577 case GL_DEPTH_STENCIL_EXT
:
586 * Test if an attachment point is complete and update its Complete field.
587 * \param format if GL_COLOR, this is a color attachment point,
588 * if GL_DEPTH, this is a depth component attachment point,
589 * if GL_STENCIL, this is a stencil component attachment point.
592 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
593 struct gl_renderbuffer_attachment
*att
)
595 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
597 /* assume complete */
598 att
->Complete
= GL_TRUE
;
600 /* Look for reasons why the attachment might be incomplete */
601 if (att
->Type
== GL_TEXTURE
) {
602 const struct gl_texture_object
*texObj
= att
->Texture
;
603 struct gl_texture_image
*texImage
;
607 att_incomplete("no texobj");
608 att
->Complete
= GL_FALSE
;
612 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
614 att_incomplete("no teximage");
615 att
->Complete
= GL_FALSE
;
618 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
619 att_incomplete("teximage width/height=0");
620 printf("texobj = %u\n", texObj
->Name
);
621 printf("level = %d\n", att
->TextureLevel
);
622 att
->Complete
= GL_FALSE
;
625 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
626 att_incomplete("bad z offset");
627 att
->Complete
= GL_FALSE
;
631 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
633 if (format
== GL_COLOR
) {
634 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
635 att_incomplete("bad format");
636 att
->Complete
= GL_FALSE
;
639 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
640 att_incomplete("compressed internalformat");
641 att
->Complete
= GL_FALSE
;
645 else if (format
== GL_DEPTH
) {
646 if (baseFormat
== GL_DEPTH_COMPONENT
) {
649 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
650 ctx
->Extensions
.ARB_depth_texture
&&
651 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
655 att
->Complete
= GL_FALSE
;
656 att_incomplete("bad depth format");
661 ASSERT(format
== GL_STENCIL
);
662 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
663 ctx
->Extensions
.ARB_depth_texture
&&
664 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
668 /* no such thing as stencil-only textures */
669 att_incomplete("illegal stencil texture");
670 att
->Complete
= GL_FALSE
;
675 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
676 const GLenum baseFormat
=
677 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
679 ASSERT(att
->Renderbuffer
);
680 if (!att
->Renderbuffer
->InternalFormat
||
681 att
->Renderbuffer
->Width
< 1 ||
682 att
->Renderbuffer
->Height
< 1) {
683 att_incomplete("0x0 renderbuffer");
684 att
->Complete
= GL_FALSE
;
687 if (format
== GL_COLOR
) {
688 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
689 att_incomplete("bad renderbuffer color format");
690 att
->Complete
= GL_FALSE
;
694 else if (format
== GL_DEPTH
) {
695 if (baseFormat
== GL_DEPTH_COMPONENT
) {
698 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
699 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
703 att_incomplete("bad renderbuffer depth format");
704 att
->Complete
= GL_FALSE
;
709 assert(format
== GL_STENCIL
);
710 if (baseFormat
== GL_STENCIL_INDEX
) {
713 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
714 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
718 att
->Complete
= GL_FALSE
;
719 att_incomplete("bad renderbuffer stencil format");
725 ASSERT(att
->Type
== GL_NONE
);
733 * Test if the given framebuffer object is complete and update its
734 * Status field with the results.
735 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
736 * driver to make hardware-specific validation/completeness checks.
737 * Also update the framebuffer's Width and Height fields if the
738 * framebuffer is complete.
741 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
742 struct gl_framebuffer
*fb
)
745 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
746 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
747 GLint numSamples
= -1;
748 GLint fixedSampleLocations
= -1;
752 assert(_mesa_is_user_fbo(fb
));
754 /* we're changing framebuffer fields here */
755 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
761 /* Start at -2 to more easily loop over all attachment points.
766 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
767 struct gl_renderbuffer_attachment
*att
;
772 * XXX for ARB_fbo, only check color buffers that are named by
773 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
776 /* check for attachment completeness
779 att
= &fb
->Attachment
[BUFFER_DEPTH
];
780 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
781 if (!att
->Complete
) {
782 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
783 fbo_incomplete("depth attachment incomplete", -1);
788 att
= &fb
->Attachment
[BUFFER_STENCIL
];
789 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
790 if (!att
->Complete
) {
791 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
792 fbo_incomplete("stencil attachment incomplete", -1);
797 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
798 test_attachment_completeness(ctx
, GL_COLOR
, att
);
799 if (!att
->Complete
) {
800 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
801 fbo_incomplete("color attachment incomplete", i
);
806 /* get width, height, format of the renderbuffer/texture
808 if (att
->Type
== GL_TEXTURE
) {
809 const struct gl_texture_image
*texImg
=
810 _mesa_get_attachment_teximage(att
);
811 minWidth
= MIN2(minWidth
, texImg
->Width
);
812 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
813 minHeight
= MIN2(minHeight
, texImg
->Height
);
814 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
815 f
= texImg
->_BaseFormat
;
816 attFormat
= texImg
->TexFormat
;
819 if (!is_format_color_renderable(ctx
, attFormat
, texImg
->InternalFormat
) &&
820 !is_legal_depth_format(ctx
, f
)) {
821 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
822 fbo_incomplete("texture attachment incomplete", -1);
827 numSamples
= texImg
->NumSamples
;
828 else if (numSamples
!= texImg
->NumSamples
) {
829 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
830 fbo_incomplete("inconsistent sample count", -1);
834 if (fixedSampleLocations
< 0)
835 fixedSampleLocations
= texImg
->FixedSampleLocations
;
836 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
837 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
838 fbo_incomplete("inconsistent fixed sample locations", -1);
842 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
843 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
844 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
845 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
846 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
847 f
= att
->Renderbuffer
->InternalFormat
;
848 attFormat
= att
->Renderbuffer
->Format
;
852 numSamples
= att
->Renderbuffer
->NumSamples
;
853 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
854 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
855 fbo_incomplete("inconsistent sample count", -1);
859 /* RENDERBUFFER has fixedSampleLocations implicitly true */
860 if (fixedSampleLocations
< 0)
861 fixedSampleLocations
= GL_TRUE
;
862 else if (fixedSampleLocations
!= GL_TRUE
) {
863 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
864 fbo_incomplete("inconsistent fixed sample locations", -1);
869 assert(att
->Type
== GL_NONE
);
873 /* check if integer color */
874 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
876 /* Error-check width, height, format */
877 if (numImages
== 1) {
884 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
885 /* check that width, height, format are same */
886 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
887 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
888 fbo_incomplete("width or height mismatch", -1);
891 /* check that all color buffers are the same format */
892 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
893 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
894 fbo_incomplete("format mismatch", -1);
900 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
902 if (att
->Type
== GL_RENDERBUFFER
&&
903 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
904 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
905 fbo_incomplete("unsupported renderbuffer format", i
);
910 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
911 /* Check that all DrawBuffers are present */
912 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
913 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
914 const struct gl_renderbuffer_attachment
*att
915 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
917 if (att
->Type
== GL_NONE
) {
918 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
919 fbo_incomplete("missing drawbuffer", j
);
925 /* Check that the ReadBuffer is present */
926 if (fb
->ColorReadBuffer
!= GL_NONE
) {
927 const struct gl_renderbuffer_attachment
*att
928 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
930 if (att
->Type
== GL_NONE
) {
931 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
932 fbo_incomplete("missing readbuffer", -1);
938 if (numImages
== 0) {
939 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
940 fbo_incomplete("no attachments", -1);
944 /* Provisionally set status = COMPLETE ... */
945 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
947 /* ... but the driver may say the FB is incomplete.
948 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
951 if (ctx
->Driver
.ValidateFramebuffer
) {
952 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
953 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
954 fbo_incomplete("driver marked FBO as incomplete", -1);
958 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
960 * Note that if ARB_framebuffer_object is supported and the attached
961 * renderbuffers/textures are different sizes, the framebuffer
962 * width/height will be set to the smallest width/height.
964 fb
->Width
= minWidth
;
965 fb
->Height
= minHeight
;
967 /* finally, update the visual info for the framebuffer */
968 _mesa_update_framebuffer_visual(ctx
, fb
);
974 _mesa_IsRenderbuffer(GLuint renderbuffer
)
976 GET_CURRENT_CONTEXT(ctx
);
977 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
979 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
980 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
988 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
990 struct gl_renderbuffer
*newRb
;
991 GET_CURRENT_CONTEXT(ctx
);
993 if (target
!= GL_RENDERBUFFER_EXT
) {
994 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
998 /* No need to flush here since the render buffer binding has no
999 * effect on rendering state.
1003 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1004 if (newRb
== &DummyRenderbuffer
) {
1005 /* ID was reserved, but no real renderbuffer object made yet */
1009 && _mesa_is_desktop_gl(ctx
)
1010 && ctx
->Extensions
.ARB_framebuffer_object
) {
1011 /* All RB IDs must be Gen'd */
1012 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1017 /* create new renderbuffer object */
1018 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1020 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
1023 ASSERT(newRb
->AllocStorage
);
1024 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1025 newRb
->RefCount
= 1; /* referenced by hash table */
1032 ASSERT(newRb
!= &DummyRenderbuffer
);
1034 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1039 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1041 * This is used when a renderbuffer object is deleted.
1042 * The spec calls for unbinding.
1045 detach_renderbuffer(struct gl_context
*ctx
,
1046 struct gl_framebuffer
*fb
,
1047 struct gl_renderbuffer
*rb
)
1050 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1051 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
1052 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
1055 invalidate_framebuffer(fb
);
1060 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1063 GET_CURRENT_CONTEXT(ctx
);
1065 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1067 for (i
= 0; i
< n
; i
++) {
1068 if (renderbuffers
[i
] > 0) {
1069 struct gl_renderbuffer
*rb
;
1070 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1072 /* check if deleting currently bound renderbuffer object */
1073 if (rb
== ctx
->CurrentRenderbuffer
) {
1075 ASSERT(rb
->RefCount
>= 2);
1076 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1079 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1080 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1082 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1083 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1084 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1087 /* Remove from hash table immediately, to free the ID.
1088 * But the object will not be freed until it's no longer
1089 * referenced anywhere else.
1091 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1093 if (rb
!= &DummyRenderbuffer
) {
1094 /* no longer referenced by hash table */
1095 _mesa_reference_renderbuffer(&rb
, NULL
);
1104 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1106 GET_CURRENT_CONTEXT(ctx
);
1111 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1118 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1120 for (i
= 0; i
< n
; i
++) {
1121 GLuint name
= first
+ i
;
1122 renderbuffers
[i
] = name
;
1123 /* insert dummy placeholder into hash table */
1124 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1125 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1126 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1132 * Given an internal format token for a render buffer, return the
1133 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1134 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1135 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1137 * This is similar to _mesa_base_tex_format() but the set of valid
1138 * internal formats is different.
1140 * Note that even if a format is determined to be legal here, validation
1141 * of the FBO may fail if the format is not supported by the driver/GPU.
1143 * \param internalFormat as passed to glRenderbufferStorage()
1144 * \return the base internal format, or 0 if internalFormat is illegal
1147 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1150 * Notes: some formats such as alpha, luminance, etc. were added
1151 * with GL_ARB_framebuffer_object.
1153 switch (internalFormat
) {
1159 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1164 case GL_LUMINANCE12
:
1165 case GL_LUMINANCE16
:
1166 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1168 case GL_LUMINANCE_ALPHA
:
1169 case GL_LUMINANCE4_ALPHA4
:
1170 case GL_LUMINANCE6_ALPHA2
:
1171 case GL_LUMINANCE8_ALPHA8
:
1172 case GL_LUMINANCE12_ALPHA4
:
1173 case GL_LUMINANCE12_ALPHA12
:
1174 case GL_LUMINANCE16_ALPHA16
:
1175 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1176 ? GL_LUMINANCE_ALPHA
: 0;
1180 case GL_INTENSITY12
:
1181 case GL_INTENSITY16
:
1182 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1193 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1195 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1204 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1206 case GL_SRGB8_ALPHA8_EXT
:
1207 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1208 case GL_STENCIL_INDEX
:
1209 case GL_STENCIL_INDEX1_EXT
:
1210 case GL_STENCIL_INDEX4_EXT
:
1211 case GL_STENCIL_INDEX16_EXT
:
1212 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1213 * OpenGL ES, but Mesa does not currently support them.
1215 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1216 case GL_STENCIL_INDEX8_EXT
:
1217 return GL_STENCIL_INDEX
;
1218 case GL_DEPTH_COMPONENT
:
1219 case GL_DEPTH_COMPONENT32
:
1220 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1221 case GL_DEPTH_COMPONENT16
:
1222 case GL_DEPTH_COMPONENT24
:
1223 return GL_DEPTH_COMPONENT
;
1224 case GL_DEPTH_STENCIL_EXT
:
1225 return _mesa_is_desktop_gl(ctx
)
1226 && ctx
->Extensions
.EXT_packed_depth_stencil
1227 ? GL_DEPTH_STENCIL_EXT
: 0;
1228 case GL_DEPTH24_STENCIL8_EXT
:
1229 return ctx
->Extensions
.EXT_packed_depth_stencil
1230 ? GL_DEPTH_STENCIL_EXT
: 0;
1231 case GL_DEPTH_COMPONENT32F
:
1232 return ctx
->Version
>= 30
1233 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1234 ? GL_DEPTH_COMPONENT
: 0;
1235 case GL_DEPTH32F_STENCIL8
:
1236 return ctx
->Version
>= 30
1237 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1238 ? GL_DEPTH_STENCIL
: 0;
1241 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1244 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1248 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1251 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1253 /* signed normalized texture formats */
1257 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1262 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1266 case GL_RGB16_SNORM
:
1267 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1270 case GL_RGBA8_SNORM
:
1271 case GL_RGBA16_SNORM
:
1272 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1274 case GL_ALPHA_SNORM
:
1275 case GL_ALPHA8_SNORM
:
1276 case GL_ALPHA16_SNORM
:
1277 return ctx
->API
== API_OPENGL_COMPAT
&&
1278 ctx
->Extensions
.EXT_texture_snorm
&&
1279 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1282 return ((_mesa_is_desktop_gl(ctx
) &&
1283 ctx
->Extensions
.ARB_texture_rg
&&
1284 ctx
->Extensions
.ARB_texture_float
) ||
1285 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1289 return ((_mesa_is_desktop_gl(ctx
) &&
1290 ctx
->Extensions
.ARB_texture_rg
&&
1291 ctx
->Extensions
.ARB_texture_float
) ||
1292 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1296 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1300 return ((_mesa_is_desktop_gl(ctx
) &&
1301 ctx
->Extensions
.ARB_texture_float
) ||
1302 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1304 case GL_ALPHA16F_ARB
:
1305 case GL_ALPHA32F_ARB
:
1306 return ctx
->API
== API_OPENGL_COMPAT
&&
1307 ctx
->Extensions
.ARB_texture_float
&&
1308 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1309 case GL_LUMINANCE16F_ARB
:
1310 case GL_LUMINANCE32F_ARB
:
1311 return ctx
->API
== API_OPENGL_COMPAT
&&
1312 ctx
->Extensions
.ARB_texture_float
&&
1313 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1314 case GL_LUMINANCE_ALPHA16F_ARB
:
1315 case GL_LUMINANCE_ALPHA32F_ARB
:
1316 return ctx
->API
== API_OPENGL_COMPAT
&&
1317 ctx
->Extensions
.ARB_texture_float
&&
1318 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1319 case GL_INTENSITY16F_ARB
:
1320 case GL_INTENSITY32F_ARB
:
1321 return ctx
->API
== API_OPENGL_COMPAT
&&
1322 ctx
->Extensions
.ARB_texture_float
&&
1323 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1325 return (_mesa_is_desktop_gl(ctx
)
1326 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1328 case GL_R11F_G11F_B10F
:
1329 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1330 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1333 case GL_RGBA8UI_EXT
:
1334 case GL_RGBA16UI_EXT
:
1335 case GL_RGBA32UI_EXT
:
1337 case GL_RGBA16I_EXT
:
1338 case GL_RGBA32I_EXT
:
1339 return ctx
->Version
>= 30
1340 || (_mesa_is_desktop_gl(ctx
) &&
1341 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1344 case GL_RGB16UI_EXT
:
1345 case GL_RGB32UI_EXT
:
1349 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1357 return ctx
->Version
>= 30
1358 || (_mesa_is_desktop_gl(ctx
) &&
1359 ctx
->Extensions
.ARB_texture_rg
&&
1360 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1368 return ctx
->Version
>= 30
1369 || (_mesa_is_desktop_gl(ctx
) &&
1370 ctx
->Extensions
.ARB_texture_rg
&&
1371 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1373 case GL_INTENSITY8I_EXT
:
1374 case GL_INTENSITY8UI_EXT
:
1375 case GL_INTENSITY16I_EXT
:
1376 case GL_INTENSITY16UI_EXT
:
1377 case GL_INTENSITY32I_EXT
:
1378 case GL_INTENSITY32UI_EXT
:
1379 return ctx
->API
== API_OPENGL_COMPAT
&&
1380 ctx
->Extensions
.EXT_texture_integer
&&
1381 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1383 case GL_LUMINANCE8I_EXT
:
1384 case GL_LUMINANCE8UI_EXT
:
1385 case GL_LUMINANCE16I_EXT
:
1386 case GL_LUMINANCE16UI_EXT
:
1387 case GL_LUMINANCE32I_EXT
:
1388 case GL_LUMINANCE32UI_EXT
:
1389 return ctx
->API
== API_OPENGL_COMPAT
&&
1390 ctx
->Extensions
.EXT_texture_integer
&&
1391 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1393 case GL_LUMINANCE_ALPHA8I_EXT
:
1394 case GL_LUMINANCE_ALPHA8UI_EXT
:
1395 case GL_LUMINANCE_ALPHA16I_EXT
:
1396 case GL_LUMINANCE_ALPHA16UI_EXT
:
1397 case GL_LUMINANCE_ALPHA32I_EXT
:
1398 case GL_LUMINANCE_ALPHA32UI_EXT
:
1399 return ctx
->API
== API_OPENGL_COMPAT
&&
1400 ctx
->Extensions
.EXT_texture_integer
&&
1401 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1403 case GL_ALPHA8I_EXT
:
1404 case GL_ALPHA8UI_EXT
:
1405 case GL_ALPHA16I_EXT
:
1406 case GL_ALPHA16UI_EXT
:
1407 case GL_ALPHA32I_EXT
:
1408 case GL_ALPHA32UI_EXT
:
1409 return ctx
->API
== API_OPENGL_COMPAT
&&
1410 ctx
->Extensions
.EXT_texture_integer
&&
1411 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1414 return (_mesa_is_desktop_gl(ctx
) &&
1415 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1416 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1419 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1428 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1431 invalidate_rb(GLuint key
, void *data
, void *userData
)
1433 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1434 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1436 /* If this is a user-created FBO */
1437 if (_mesa_is_user_fbo(fb
)) {
1439 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1440 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1441 if (att
->Type
== GL_RENDERBUFFER
&&
1442 att
->Renderbuffer
== rb
) {
1443 /* Mark fb status as indeterminate to force re-validation */
1452 /** sentinal value, see below */
1453 #define NO_SAMPLES 1000
1457 * Helper function used by _mesa_RenderbufferStorage() and
1458 * _mesa_RenderbufferStorageMultisample().
1459 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1462 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1463 GLsizei width
, GLsizei height
, GLsizei samples
)
1465 const char *func
= samples
== NO_SAMPLES
?
1466 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1467 struct gl_renderbuffer
*rb
;
1469 GET_CURRENT_CONTEXT(ctx
);
1471 if (MESA_VERBOSE
& VERBOSE_API
) {
1472 if (samples
== NO_SAMPLES
)
1473 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1475 _mesa_lookup_enum_by_nr(target
),
1476 _mesa_lookup_enum_by_nr(internalFormat
),
1479 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1481 _mesa_lookup_enum_by_nr(target
),
1482 _mesa_lookup_enum_by_nr(internalFormat
),
1483 width
, height
, samples
);
1486 if (target
!= GL_RENDERBUFFER_EXT
) {
1487 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1491 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1492 if (baseFormat
== 0) {
1493 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1494 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1498 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1499 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1503 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1504 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1508 if (samples
== NO_SAMPLES
) {
1509 /* NumSamples == 0 indicates non-multisampling */
1512 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1513 /* note: driver may choose to use more samples than what's requested */
1514 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1518 rb
= ctx
->CurrentRenderbuffer
;
1520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1524 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1526 if (rb
->InternalFormat
== internalFormat
&&
1527 rb
->Width
== (GLuint
) width
&&
1528 rb
->Height
== (GLuint
) height
&&
1529 rb
->NumSamples
== samples
) {
1530 /* no change in allocation needed */
1534 /* These MUST get set by the AllocStorage func */
1535 rb
->Format
= MESA_FORMAT_NONE
;
1536 rb
->NumSamples
= samples
;
1538 /* Now allocate the storage */
1539 ASSERT(rb
->AllocStorage
);
1540 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1541 /* No error - check/set fields now */
1542 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1543 assert(rb
->Width
== (GLuint
) width
);
1544 assert(rb
->Height
== (GLuint
) height
);
1545 rb
->InternalFormat
= internalFormat
;
1546 rb
->_BaseFormat
= baseFormat
;
1547 assert(rb
->_BaseFormat
!= 0);
1550 /* Probably ran out of memory - clear the fields */
1553 rb
->Format
= MESA_FORMAT_NONE
;
1554 rb
->InternalFormat
= GL_NONE
;
1555 rb
->_BaseFormat
= GL_NONE
;
1559 /* Invalidate the framebuffers the renderbuffer is attached in. */
1560 if (rb
->AttachedAnytime
) {
1561 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1567 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1569 struct gl_renderbuffer
*rb
;
1570 GET_CURRENT_CONTEXT(ctx
);
1572 if (!ctx
->Extensions
.OES_EGL_image
) {
1573 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1574 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1578 if (target
!= GL_RENDERBUFFER
) {
1579 _mesa_error(ctx
, GL_INVALID_ENUM
,
1580 "EGLImageTargetRenderbufferStorageOES");
1584 rb
= ctx
->CurrentRenderbuffer
;
1586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1587 "EGLImageTargetRenderbufferStorageOES");
1591 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1593 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1598 * Helper function for _mesa_GetRenderbufferParameteriv() and
1599 * _mesa_GetFramebufferAttachmentParameteriv()
1600 * We have to be careful to respect the base format. For example, if a
1601 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1602 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1603 * we need to return zero.
1606 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1608 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1609 return _mesa_get_format_bits(format
, pname
);
1617 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1618 GLsizei width
, GLsizei height
)
1620 /* GL_ARB_fbo says calling this function is equivalent to calling
1621 * glRenderbufferStorageMultisample() with samples=0. We pass in
1622 * a token value here just for error reporting purposes.
1624 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1629 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1630 GLenum internalFormat
,
1631 GLsizei width
, GLsizei height
)
1633 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1638 * OpenGL ES version of glRenderBufferStorage.
1641 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1642 GLsizei width
, GLsizei height
)
1644 switch (internalFormat
) {
1646 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1647 /* choose a closest format */
1648 internalFormat
= GL_RGB5
;
1654 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1659 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1661 struct gl_renderbuffer
*rb
;
1662 GET_CURRENT_CONTEXT(ctx
);
1664 if (target
!= GL_RENDERBUFFER_EXT
) {
1665 _mesa_error(ctx
, GL_INVALID_ENUM
,
1666 "glGetRenderbufferParameterivEXT(target)");
1670 rb
= ctx
->CurrentRenderbuffer
;
1672 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1673 "glGetRenderbufferParameterivEXT");
1677 /* No need to flush here since we're just quering state which is
1678 * not effected by rendering.
1682 case GL_RENDERBUFFER_WIDTH_EXT
:
1683 *params
= rb
->Width
;
1685 case GL_RENDERBUFFER_HEIGHT_EXT
:
1686 *params
= rb
->Height
;
1688 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1689 *params
= rb
->InternalFormat
;
1691 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1692 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1693 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1694 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1695 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1696 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1697 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1699 case GL_RENDERBUFFER_SAMPLES
:
1700 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1701 || _mesa_is_gles3(ctx
)) {
1702 *params
= rb
->NumSamples
;
1707 _mesa_error(ctx
, GL_INVALID_ENUM
,
1708 "glGetRenderbufferParameterivEXT(target)");
1714 GLboolean GLAPIENTRY
1715 _mesa_IsFramebuffer(GLuint framebuffer
)
1717 GET_CURRENT_CONTEXT(ctx
);
1718 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1720 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1721 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1729 * Check if any of the attachments of the given framebuffer are textures
1730 * (render to texture). Call ctx->Driver.RenderTexture() for such
1734 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1737 ASSERT(ctx
->Driver
.RenderTexture
);
1739 if (_mesa_is_winsys_fbo(fb
))
1740 return; /* can't render to texture with winsys framebuffers */
1742 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1743 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1744 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1745 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1752 * Examine all the framebuffer's attachments to see if any are textures.
1753 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1754 * notify the device driver that the texture image may have changed.
1757 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1759 if (_mesa_is_winsys_fbo(fb
))
1760 return; /* can't render to texture with winsys framebuffers */
1762 if (ctx
->Driver
.FinishRenderTexture
) {
1764 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1765 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1766 if (att
->Texture
&& att
->Renderbuffer
) {
1767 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1775 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
1777 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1778 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1779 GLboolean bindReadBuf
, bindDrawBuf
;
1780 GET_CURRENT_CONTEXT(ctx
);
1783 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1784 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1785 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1789 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1790 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1791 "glBindFramebufferEXT(unsupported)");
1796 case GL_DRAW_FRAMEBUFFER_EXT
:
1797 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1798 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1801 bindDrawBuf
= GL_TRUE
;
1802 bindReadBuf
= GL_FALSE
;
1804 case GL_READ_FRAMEBUFFER_EXT
:
1805 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1806 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1809 bindDrawBuf
= GL_FALSE
;
1810 bindReadBuf
= GL_TRUE
;
1812 case GL_FRAMEBUFFER_EXT
:
1813 bindDrawBuf
= GL_TRUE
;
1814 bindReadBuf
= GL_TRUE
;
1817 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1822 /* Binding a user-created framebuffer object */
1823 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1824 if (newDrawFb
== &DummyFramebuffer
) {
1825 /* ID was reserved, but no real framebuffer object made yet */
1829 && _mesa_is_desktop_gl(ctx
)
1830 && ctx
->Extensions
.ARB_framebuffer_object
) {
1831 /* All FBO IDs must be Gen'd */
1832 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1837 /* create new framebuffer object */
1838 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1840 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1843 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1845 newReadFb
= newDrawFb
;
1848 /* Binding the window system framebuffer (which was originally set
1849 * with MakeCurrent).
1851 newDrawFb
= ctx
->WinSysDrawBuffer
;
1852 newReadFb
= ctx
->WinSysReadBuffer
;
1856 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1858 /* save pointers to current/old framebuffers */
1859 oldDrawFb
= ctx
->DrawBuffer
;
1860 oldReadFb
= ctx
->ReadBuffer
;
1862 /* check if really changing bindings */
1863 if (oldDrawFb
== newDrawFb
)
1864 bindDrawBuf
= GL_FALSE
;
1865 if (oldReadFb
== newReadFb
)
1866 bindReadBuf
= GL_FALSE
;
1869 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1871 * We also check if we're beginning and/or ending render-to-texture.
1872 * When a framebuffer with texture attachments is unbound, call
1873 * ctx->Driver.FinishRenderTexture().
1874 * When a framebuffer with texture attachments is bound, call
1875 * ctx->Driver.RenderTexture().
1877 * Note that if the ReadBuffer has texture attachments we don't consider
1878 * that a render-to-texture case.
1881 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1883 /* check if old readbuffer was render-to-texture */
1884 check_end_texture_render(ctx
, oldReadFb
);
1886 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1890 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1892 /* check if old framebuffer had any texture attachments */
1894 check_end_texture_render(ctx
, oldDrawFb
);
1896 /* check if newly bound framebuffer has any texture attachments */
1897 check_begin_texture_render(ctx
, newDrawFb
);
1899 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1902 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1903 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1909 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
1912 GET_CURRENT_CONTEXT(ctx
);
1914 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1916 for (i
= 0; i
< n
; i
++) {
1917 if (framebuffers
[i
] > 0) {
1918 struct gl_framebuffer
*fb
;
1919 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1921 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1923 /* check if deleting currently bound framebuffer object */
1924 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1925 /* separate draw/read binding points */
1926 if (fb
== ctx
->DrawBuffer
) {
1928 ASSERT(fb
->RefCount
>= 2);
1929 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1931 if (fb
== ctx
->ReadBuffer
) {
1933 ASSERT(fb
->RefCount
>= 2);
1934 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, 0);
1938 /* only one binding point for read/draw buffers */
1939 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1941 ASSERT(fb
->RefCount
>= 2);
1942 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
1946 /* remove from hash table immediately, to free the ID */
1947 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1949 if (fb
!= &DummyFramebuffer
) {
1950 /* But the object will not be freed until it's no longer
1951 * bound in any context.
1953 _mesa_reference_framebuffer(&fb
, NULL
);
1962 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
1964 GET_CURRENT_CONTEXT(ctx
);
1969 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1976 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1978 for (i
= 0; i
< n
; i
++) {
1979 GLuint name
= first
+ i
;
1980 framebuffers
[i
] = name
;
1981 /* insert dummy placeholder into hash table */
1982 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1983 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1984 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1991 _mesa_CheckFramebufferStatus(GLenum target
)
1993 struct gl_framebuffer
*buffer
;
1994 GET_CURRENT_CONTEXT(ctx
);
1996 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1998 if (MESA_VERBOSE
& VERBOSE_API
)
1999 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2000 _mesa_lookup_enum_by_nr(target
));
2002 buffer
= get_framebuffer_target(ctx
, target
);
2004 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2008 if (_mesa_is_winsys_fbo(buffer
)) {
2009 /* The window system / default framebuffer is always complete */
2010 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2013 /* No need to flush here */
2015 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2016 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2019 return buffer
->_Status
;
2024 * Replicate the src attachment point. Used by framebuffer_texture() when
2025 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2026 * GL_STENCIL_ATTACHMENT.
2029 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2030 gl_buffer_index dst
,
2031 gl_buffer_index src
)
2033 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2034 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2036 assert(src_att
->Texture
!= NULL
);
2037 assert(src_att
->Renderbuffer
!= NULL
);
2039 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2040 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2041 dst_att
->Type
= src_att
->Type
;
2042 dst_att
->Complete
= src_att
->Complete
;
2043 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2044 dst_att
->Zoffset
= src_att
->Zoffset
;
2049 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2050 * glFramebufferTextureLayerEXT().
2051 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2052 * get textarget=0 in that case.
2055 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2056 GLenum attachment
, GLenum textarget
, GLuint texture
,
2057 GLint level
, GLint zoffset
)
2059 struct gl_renderbuffer_attachment
*att
;
2060 struct gl_texture_object
*texObj
= NULL
;
2061 struct gl_framebuffer
*fb
;
2062 GLenum maxLevelsTarget
;
2064 fb
= get_framebuffer_target(ctx
, target
);
2066 _mesa_error(ctx
, GL_INVALID_ENUM
,
2067 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2071 /* check framebuffer binding */
2072 if (_mesa_is_winsys_fbo(fb
)) {
2073 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2074 "glFramebufferTexture%sEXT", caller
);
2078 /* The textarget, level, and zoffset parameters are only validated if
2079 * texture is non-zero.
2082 GLboolean err
= GL_TRUE
;
2084 texObj
= _mesa_lookup_texture(ctx
, texture
);
2085 if (texObj
!= NULL
) {
2086 if (textarget
== 0) {
2087 /* If textarget == 0 it means we're being called by
2088 * glFramebufferTextureLayer() and textarget is not used.
2089 * The only legal texture types for that function are 3D and
2090 * 1D/2D arrays textures.
2092 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2093 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2094 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2095 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2096 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2099 /* Make sure textarget is consistent with the texture's type */
2100 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2101 ? !_mesa_is_cube_face(textarget
)
2102 : (texObj
->Target
!= textarget
);
2106 /* can't render to a non-existant texture */
2107 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2108 "glFramebufferTexture%sEXT(non existant texture)",
2114 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2115 "glFramebufferTexture%sEXT(texture target mismatch)",
2120 if (texObj
->Target
== GL_TEXTURE_3D
) {
2121 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2122 if (zoffset
< 0 || zoffset
>= maxSize
) {
2123 _mesa_error(ctx
, GL_INVALID_VALUE
,
2124 "glFramebufferTexture%sEXT(zoffset)", caller
);
2128 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2129 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2130 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2131 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2133 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2134 _mesa_error(ctx
, GL_INVALID_VALUE
,
2135 "glFramebufferTexture%sEXT(layer)", caller
);
2140 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2142 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2143 _mesa_error(ctx
, GL_INVALID_VALUE
,
2144 "glFramebufferTexture%sEXT(level)", caller
);
2149 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2151 _mesa_error(ctx
, GL_INVALID_ENUM
,
2152 "glFramebufferTexture%sEXT(attachment)", caller
);
2156 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2158 _glthread_LOCK_MUTEX(fb
->Mutex
);
2160 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2161 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2162 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2163 _mesa_tex_target_to_face(textarget
) ==
2164 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2165 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2166 /* The texture object is already attached to the stencil attachment
2167 * point. Don't create a new renderbuffer; just reuse the stencil
2168 * attachment's. This is required to prevent a GL error in
2169 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2171 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2173 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2174 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2175 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2176 _mesa_tex_target_to_face(textarget
) ==
2177 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2178 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2179 /* As above, but with depth and stencil transposed. */
2180 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2183 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2185 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2186 /* Above we created a new renderbuffer and attached it to the
2187 * depth attachment point. Now attach it to the stencil attachment
2190 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2191 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2196 /* Set the render-to-texture flag. We'll check this flag in
2197 * glTexImage() and friends to determine if we need to revalidate
2198 * any FBOs that might be rendering into this texture.
2199 * This flag never gets cleared since it's non-trivial to determine
2200 * when all FBOs might be done rendering to this texture. That's OK
2201 * though since it's uncommon to render to a texture then repeatedly
2202 * call glTexImage() to change images in the texture.
2204 texObj
->_RenderToTexture
= GL_TRUE
;
2207 _mesa_remove_attachment(ctx
, att
);
2208 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2209 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2210 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2214 invalidate_framebuffer(fb
);
2216 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2222 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2223 GLenum textarget
, GLuint texture
, GLint level
)
2225 GET_CURRENT_CONTEXT(ctx
);
2230 switch (textarget
) {
2234 case GL_TEXTURE_1D_ARRAY
:
2235 error
= !ctx
->Extensions
.EXT_texture_array
;
2242 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2243 "glFramebufferTexture1DEXT(textarget=%s)",
2244 _mesa_lookup_enum_by_nr(textarget
));
2249 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2255 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2256 GLenum textarget
, GLuint texture
, GLint level
)
2258 GET_CURRENT_CONTEXT(ctx
);
2263 switch (textarget
) {
2267 case GL_TEXTURE_RECTANGLE
:
2268 error
= _mesa_is_gles(ctx
)
2269 || !ctx
->Extensions
.NV_texture_rectangle
;
2271 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2272 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2273 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2274 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2275 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2276 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2277 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2279 case GL_TEXTURE_2D_ARRAY
:
2280 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2281 || !ctx
->Extensions
.EXT_texture_array
;
2283 case GL_TEXTURE_2D_MULTISAMPLE
:
2284 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2285 error
= _mesa_is_gles(ctx
)
2286 || !ctx
->Extensions
.ARB_texture_multisample
;
2293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2294 "glFramebufferTexture2DEXT(textarget=%s)",
2295 _mesa_lookup_enum_by_nr(textarget
));
2300 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2306 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2307 GLenum textarget
, GLuint texture
,
2308 GLint level
, GLint zoffset
)
2310 GET_CURRENT_CONTEXT(ctx
);
2312 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2313 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2314 "glFramebufferTexture3DEXT(textarget)");
2318 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2324 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2325 GLuint texture
, GLint level
, GLint layer
)
2327 GET_CURRENT_CONTEXT(ctx
);
2329 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2335 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2336 GLenum renderbufferTarget
,
2337 GLuint renderbuffer
)
2339 struct gl_renderbuffer_attachment
*att
;
2340 struct gl_framebuffer
*fb
;
2341 struct gl_renderbuffer
*rb
;
2342 GET_CURRENT_CONTEXT(ctx
);
2344 fb
= get_framebuffer_target(ctx
, target
);
2346 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2350 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2351 _mesa_error(ctx
, GL_INVALID_ENUM
,
2352 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2356 if (_mesa_is_winsys_fbo(fb
)) {
2357 /* Can't attach new renderbuffers to a window system framebuffer */
2358 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2362 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2364 _mesa_error(ctx
, GL_INVALID_ENUM
,
2365 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2366 _mesa_lookup_enum_by_nr(attachment
));
2371 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2373 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2374 "glFramebufferRenderbufferEXT(non-existant"
2375 " renderbuffer %u)", renderbuffer
);
2378 else if (rb
== &DummyRenderbuffer
) {
2379 /* This is what NVIDIA does */
2380 _mesa_error(ctx
, GL_INVALID_VALUE
,
2381 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2387 /* remove renderbuffer attachment */
2391 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2392 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2393 /* make sure the renderbuffer is a depth/stencil format */
2394 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2395 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2397 "glFramebufferRenderbufferEXT(renderbuffer"
2398 " is not DEPTH_STENCIL format)");
2404 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2406 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2407 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2409 /* Some subsequent GL commands may depend on the framebuffer's visual
2410 * after the binding is updated. Update visual info now.
2412 _mesa_update_framebuffer_visual(ctx
, fb
);
2417 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2418 GLenum pname
, GLint
*params
)
2420 const struct gl_renderbuffer_attachment
*att
;
2421 struct gl_framebuffer
*buffer
;
2423 GET_CURRENT_CONTEXT(ctx
);
2425 /* The error differs in GL and GLES. */
2426 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2428 buffer
= get_framebuffer_target(ctx
, target
);
2430 _mesa_error(ctx
, GL_INVALID_ENUM
,
2431 "glGetFramebufferAttachmentParameterivEXT(target)");
2435 if (_mesa_is_winsys_fbo(buffer
)) {
2436 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2439 * "If the framebuffer currently bound to target is zero, then
2440 * INVALID_OPERATION is generated."
2442 * The EXT_framebuffer_object spec has the same wording, and the
2443 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2446 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2447 && !_mesa_is_gles3(ctx
)) {
2448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2449 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2453 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2454 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2456 "glGetFramebufferAttachmentParameteriv(attachment)");
2459 /* the default / window-system FBO */
2460 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2463 /* user-created framebuffer FBO */
2464 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2468 _mesa_error(ctx
, GL_INVALID_ENUM
,
2469 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2473 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2474 /* the depth and stencil attachments must point to the same buffer */
2475 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2476 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2477 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2478 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2480 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2481 " attachments differ)");
2486 /* No need to flush here */
2489 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2490 *params
= _mesa_is_winsys_fbo(buffer
)
2491 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2493 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2494 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2495 *params
= att
->Renderbuffer
->Name
;
2497 else if (att
->Type
== GL_TEXTURE
) {
2498 *params
= att
->Texture
->Name
;
2501 assert(att
->Type
== GL_NONE
);
2502 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2505 goto invalid_pname_enum
;
2509 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2510 if (att
->Type
== GL_TEXTURE
) {
2511 *params
= att
->TextureLevel
;
2513 else if (att
->Type
== GL_NONE
) {
2514 _mesa_error(ctx
, err
,
2515 "glGetFramebufferAttachmentParameterivEXT(pname)");
2518 goto invalid_pname_enum
;
2521 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2522 if (att
->Type
== GL_TEXTURE
) {
2523 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2524 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2530 else if (att
->Type
== GL_NONE
) {
2531 _mesa_error(ctx
, err
,
2532 "glGetFramebufferAttachmentParameterivEXT(pname)");
2535 goto invalid_pname_enum
;
2538 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2539 if (ctx
->API
== API_OPENGLES
) {
2540 goto invalid_pname_enum
;
2541 } else if (att
->Type
== GL_NONE
) {
2542 _mesa_error(ctx
, err
,
2543 "glGetFramebufferAttachmentParameterivEXT(pname)");
2544 } else if (att
->Type
== GL_TEXTURE
) {
2545 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2546 *params
= att
->Zoffset
;
2553 goto invalid_pname_enum
;
2556 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2557 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2558 && !_mesa_is_gles3(ctx
)) {
2559 goto invalid_pname_enum
;
2561 else if (att
->Type
== GL_NONE
) {
2562 _mesa_error(ctx
, err
,
2563 "glGetFramebufferAttachmentParameterivEXT(pname)");
2566 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2567 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2570 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2571 * if the sRGB conversion is unsupported. */
2572 *params
= GL_LINEAR
;
2576 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2577 if ((ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2578 && ctx
->API
!= API_OPENGL_CORE
2579 && !_mesa_is_gles3(ctx
)) {
2580 goto invalid_pname_enum
;
2582 else if (att
->Type
== GL_NONE
) {
2583 _mesa_error(ctx
, err
,
2584 "glGetFramebufferAttachmentParameterivEXT(pname)");
2587 gl_format format
= att
->Renderbuffer
->Format
;
2589 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2592 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2593 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2594 * generate an INVALID_OPERATION error.
2596 if (_mesa_is_gles3(ctx
) && attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2597 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2598 "glGetFramebufferAttachmentParameteriv(cannot query "
2599 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2600 "GL_DEPTH_STENCIL_ATTACHMENT");
2604 if (format
== MESA_FORMAT_S8
) {
2608 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2609 /* depends on the attachment parameter */
2610 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2618 *params
= _mesa_get_format_datatype(format
);
2622 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2623 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2624 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2625 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2626 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2627 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2628 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2629 && !_mesa_is_gles3(ctx
)) {
2630 goto invalid_pname_enum
;
2632 else if (att
->Type
== GL_NONE
) {
2633 _mesa_error(ctx
, err
,
2634 "glGetFramebufferAttachmentParameterivEXT(pname)");
2636 else if (att
->Texture
) {
2637 const struct gl_texture_image
*texImage
=
2638 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2641 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2642 texImage
->TexFormat
);
2648 else if (att
->Renderbuffer
) {
2649 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2650 att
->Renderbuffer
->Format
);
2653 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2654 " invalid FBO attachment structure");
2658 goto invalid_pname_enum
;
2664 _mesa_error(ctx
, GL_INVALID_ENUM
,
2665 "glGetFramebufferAttachmentParameteriv(pname)");
2671 _mesa_GenerateMipmap(GLenum target
)
2673 struct gl_texture_image
*srcImage
;
2674 struct gl_texture_object
*texObj
;
2677 GET_CURRENT_CONTEXT(ctx
);
2679 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2683 error
= _mesa_is_gles(ctx
);
2689 error
= ctx
->API
== API_OPENGLES
;
2691 case GL_TEXTURE_CUBE_MAP
:
2692 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2694 case GL_TEXTURE_1D_ARRAY
:
2695 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2697 case GL_TEXTURE_2D_ARRAY
:
2698 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2699 || !ctx
->Extensions
.EXT_texture_array
;
2706 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2707 _mesa_lookup_enum_by_nr(target
));
2711 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2713 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2718 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2719 !_mesa_cube_complete(texObj
)) {
2720 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2721 "glGenerateMipmap(incomplete cube map)");
2725 _mesa_lock_texture(ctx
, texObj
);
2727 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2729 _mesa_unlock_texture(ctx
, texObj
);
2730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2731 "glGenerateMipmap(zero size base image)");
2735 if (_mesa_is_enum_format_integer(srcImage
->InternalFormat
) ||
2736 _mesa_is_depthstencil_format(srcImage
->InternalFormat
) ||
2737 _mesa_is_stencil_format(srcImage
->InternalFormat
)) {
2738 _mesa_unlock_texture(ctx
, texObj
);
2739 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2740 "glGenerateMipmap(invalid internal format)");
2744 if (target
== GL_TEXTURE_CUBE_MAP
) {
2746 for (face
= 0; face
< 6; face
++)
2747 ctx
->Driver
.GenerateMipmap(ctx
,
2748 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2752 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2754 _mesa_unlock_texture(ctx
, texObj
);
2758 static const struct gl_renderbuffer_attachment
*
2759 find_attachment(const struct gl_framebuffer
*fb
,
2760 const struct gl_renderbuffer
*rb
)
2763 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2764 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2765 return &fb
->Attachment
[i
];
2772 * Helper function for checking if the datatypes of color buffers are
2773 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2775 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2776 * and any of the following conditions hold:
2777 * - The read buffer contains fixed-point or floating-point values and any
2778 * draw buffer contains neither fixed-point nor floating-point values.
2779 * - The read buffer contains unsigned integer values and any draw buffer
2780 * does not contain unsigned integer values.
2781 * - The read buffer contains signed integer values and any draw buffer
2782 * does not contain signed integer values."
2785 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2787 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2788 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2790 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2791 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2792 srcType
== GL_SIGNED_NORMALIZED
||
2793 srcType
== GL_FLOAT
);
2794 /* Boil any of those types down to GL_FLOAT */
2798 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2799 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2800 dstType
== GL_SIGNED_NORMALIZED
||
2801 dstType
== GL_FLOAT
);
2802 /* Boil any of those types down to GL_FLOAT */
2806 return srcType
== dstType
;
2811 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
2812 const struct gl_renderbuffer
*drawRb
)
2814 GLenum readFormat
, drawFormat
;
2816 /* The simple case where we know the backing Mesa formats are the same.
2818 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
2819 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
2823 /* The Mesa formats are different, so we must check whether the internal
2824 * formats are compatible.
2826 * Under some circumstances, the user may request e.g. two GL_RGBA8
2827 * textures and get two entirely different Mesa formats like RGBA8888 and
2828 * ARGB8888. Drivers behaving like that should be able to cope with
2829 * non-matching formats by themselves, because it's not the user's fault.
2831 * Blits between linear and sRGB formats are also allowed.
2833 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
2834 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
2835 readFormat
= _mesa_get_linear_internalformat(readFormat
);
2836 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
2838 if (readFormat
== drawFormat
) {
2847 * Blit rectangular region, optionally from one framebuffer to another.
2849 * Note, if the src buffer is multisampled and the dest is not, this is
2850 * when the samples must be resolved to a single color.
2853 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2854 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2855 GLbitfield mask
, GLenum filter
)
2857 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2858 GL_DEPTH_BUFFER_BIT
|
2859 GL_STENCIL_BUFFER_BIT
);
2860 const struct gl_framebuffer
*readFb
, *drawFb
;
2861 GET_CURRENT_CONTEXT(ctx
);
2863 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2865 if (MESA_VERBOSE
& VERBOSE_API
)
2867 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2868 srcX0
, srcY0
, srcX1
, srcY1
,
2869 dstX0
, dstY0
, dstX1
, dstY1
,
2870 mask
, _mesa_lookup_enum_by_nr(filter
));
2872 if (ctx
->NewState
) {
2873 _mesa_update_state(ctx
);
2876 readFb
= ctx
->ReadBuffer
;
2877 drawFb
= ctx
->DrawBuffer
;
2879 if (!readFb
|| !drawFb
) {
2880 /* This will normally never happen but someday we may want to
2881 * support MakeCurrent() with no drawables.
2886 /* check for complete framebuffers */
2887 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2888 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2889 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2890 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2894 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2895 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2899 if (mask
& ~legalMaskBits
) {
2900 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2904 /* depth/stencil must be blitted with nearest filtering */
2905 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2906 && filter
!= GL_NEAREST
) {
2907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2908 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2912 /* get color read/draw renderbuffers */
2913 if (mask
& GL_COLOR_BUFFER_BIT
) {
2914 const GLuint numColorDrawBuffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
2915 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
2916 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
2919 /* From the EXT_framebuffer_object spec:
2921 * "If a buffer is specified in <mask> and does not exist in both
2922 * the read and draw framebuffers, the corresponding bit is silently
2925 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
2926 mask
&= ~GL_COLOR_BUFFER_BIT
;
2929 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
2930 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
2934 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
2935 * ES 3.0.1 spec says:
2937 * "If the source and destination buffers are identical, an
2938 * INVALID_OPERATION error is generated. Different mipmap
2939 * levels of a texture, different layers of a three-
2940 * dimensional texture or two-dimensional array texture, and
2941 * different faces of a cube map texture do not constitute
2942 * identical buffers."
2944 if (_mesa_is_gles3(ctx
) && (colorDrawRb
== colorReadRb
)) {
2945 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2946 "glBlitFramebuffer(source and destination color "
2947 "buffer cannot be the same)");
2951 if (!compatible_color_datatypes(colorReadRb
->Format
,
2952 colorDrawRb
->Format
)) {
2953 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2954 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2957 /* extra checks for multisample copies... */
2958 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2959 /* color formats must match */
2960 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2961 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2962 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2967 if (filter
== GL_LINEAR
) {
2968 /* 3.1 spec, page 199:
2969 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2970 * if filter is LINEAR and read buffer contains integer data."
2972 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2973 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2975 "glBlitFramebufferEXT(integer color type)");
2982 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2983 struct gl_renderbuffer
*readRb
=
2984 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2985 struct gl_renderbuffer
*drawRb
=
2986 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2988 /* From the EXT_framebuffer_object spec:
2990 * "If a buffer is specified in <mask> and does not exist in both
2991 * the read and draw framebuffers, the corresponding bit is silently
2994 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2995 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2998 int read_z_bits
, draw_z_bits
;
3000 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
3001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3002 "glBlitFramebuffer(source and destination stencil "
3003 "buffer cannot be the same)");
3007 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
3008 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
3009 /* There is no need to check the stencil datatype here, because
3010 * there is only one: GL_UNSIGNED_INT.
3012 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3013 "glBlitFramebuffer(stencil attachment format mismatch)");
3017 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
3018 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
3020 /* If both buffers also have depth data, the depth formats must match
3021 * as well. If one doesn't have depth, it's not blitted, so we should
3022 * ignore the depth format check.
3024 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
3025 (read_z_bits
!= draw_z_bits
||
3026 _mesa_get_format_datatype(readRb
->Format
) !=
3027 _mesa_get_format_datatype(drawRb
->Format
))) {
3029 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
3030 "(stencil attachment depth format mismatch)");
3036 if (mask
& GL_DEPTH_BUFFER_BIT
) {
3037 struct gl_renderbuffer
*readRb
=
3038 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3039 struct gl_renderbuffer
*drawRb
=
3040 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3042 /* From the EXT_framebuffer_object spec:
3044 * "If a buffer is specified in <mask> and does not exist in both
3045 * the read and draw framebuffers, the corresponding bit is silently
3048 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
3049 mask
&= ~GL_DEPTH_BUFFER_BIT
;
3052 int read_s_bit
, draw_s_bit
;
3054 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
3055 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3056 "glBlitFramebuffer(source and destination depth "
3057 "buffer cannot be the same)");
3061 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
3062 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
3063 (_mesa_get_format_datatype(readRb
->Format
) !=
3064 _mesa_get_format_datatype(drawRb
->Format
))) {
3065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3066 "glBlitFramebuffer(depth attachment format mismatch)");
3070 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
3071 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
3073 /* If both buffers also have stencil data, the stencil formats must
3074 * match as well. If one doesn't have stencil, it's not blitted, so
3075 * we should ignore the stencil format check.
3077 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
3078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
3079 "(depth attachment stencil bits mismatch)");
3086 if (_mesa_is_gles3(ctx
)) {
3087 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3090 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
3091 * an INVALID_OPERATION error is generated."
3093 if (drawFb
->Visual
.samples
> 0) {
3094 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3095 "glBlitFramebuffer(destination samples must be 0)");
3099 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3102 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
3103 * no copy is performed and an INVALID_OPERATION error is generated
3104 * if the formats of the read and draw framebuffers are not
3105 * identical or if the source and destination rectangles are not
3106 * defined with the same (X0, Y0) and (X1, Y1) bounds."
3108 * The format check was made above because desktop OpenGL has the same
3111 if (readFb
->Visual
.samples
> 0
3112 && (srcX0
!= dstX0
|| srcY0
!= dstY0
3113 || srcX1
!= dstX1
|| srcY1
!= dstY1
)) {
3114 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3115 "glBlitFramebuffer(bad src/dst multisample region)");
3119 if (readFb
->Visual
.samples
> 0 &&
3120 drawFb
->Visual
.samples
> 0 &&
3121 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
3122 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3123 "glBlitFramebufferEXT(mismatched samples)");
3127 /* extra checks for multisample copies... */
3128 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
3129 /* src and dest region sizes must be the same */
3130 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
3131 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
3132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3133 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3139 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
3140 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
3146 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
3147 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
3150 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3152 srcX0
, srcY0
, srcX1
, srcY1
,
3153 dstX0
, dstY0
, dstX1
, dstY1
,
3156 const struct gl_renderbuffer_attachment
*att
;
3158 att
= find_attachment(readFb
, colorReadRb
);
3159 printf(" Src FBO %u RB %u (%dx%d) ",
3160 readFb
->Name
, colorReadRb
->Name
,
3161 colorReadRb
->Width
, colorReadRb
->Height
);
3162 if (att
&& att
->Texture
) {
3163 printf("Tex %u tgt 0x%x level %u face %u",
3165 att
->Texture
->Target
,
3171 /* Print all active color render buffers */
3172 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
3173 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
3177 att
= find_attachment(drawFb
, colorDrawRb
);
3178 printf(" Dst FBO %u RB %u (%dx%d) ",
3179 drawFb
->Name
, colorDrawRb
->Name
,
3180 colorDrawRb
->Width
, colorDrawRb
->Height
);
3181 if (att
&& att
->Texture
) {
3182 printf("Tex %u tgt 0x%x level %u face %u",
3184 att
->Texture
->Target
,
3197 ASSERT(ctx
->Driver
.BlitFramebuffer
);
3198 ctx
->Driver
.BlitFramebuffer(ctx
,
3199 srcX0
, srcY0
, srcX1
, srcY1
,
3200 dstX0
, dstY0
, dstX1
, dstY1
,
3206 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3207 const GLenum
*attachments
, GLint x
, GLint y
,
3208 GLsizei width
, GLsizei height
, const char *name
)
3211 struct gl_framebuffer
*fb
;
3212 GET_CURRENT_CONTEXT(ctx
);
3214 fb
= get_framebuffer_target(ctx
, target
);
3216 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3220 if (numAttachments
< 0) {
3221 _mesa_error(ctx
, GL_INVALID_VALUE
,
3222 "%s(numAttachments < 0)", name
);
3226 /* The GL_ARB_invalidate_subdata spec says:
3228 * "If an attachment is specified that does not exist in the
3229 * framebuffer bound to <target>, it is ignored."
3233 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3234 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3235 * INVALID_OPERATION is generated."
3237 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3238 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3239 * set of retrictions).
3241 for (i
= 0; i
< numAttachments
; i
++) {
3242 if (_mesa_is_winsys_fbo(fb
)) {
3243 switch (attachments
[i
]) {
3249 /* Accumulation buffers and auxilary buffers were removed in
3250 * OpenGL 3.1, and they never existed in OpenGL ES.
3252 if (ctx
->API
!= API_OPENGL_COMPAT
)
3262 case GL_FRONT_RIGHT
:
3263 if (!_mesa_is_desktop_gl(ctx
))
3270 switch (attachments
[i
]) {
3271 case GL_DEPTH_ATTACHMENT
:
3272 case GL_STENCIL_ATTACHMENT
:
3274 case GL_COLOR_ATTACHMENT0
:
3275 case GL_COLOR_ATTACHMENT1
:
3276 case GL_COLOR_ATTACHMENT2
:
3277 case GL_COLOR_ATTACHMENT3
:
3278 case GL_COLOR_ATTACHMENT4
:
3279 case GL_COLOR_ATTACHMENT5
:
3280 case GL_COLOR_ATTACHMENT6
:
3281 case GL_COLOR_ATTACHMENT7
:
3282 case GL_COLOR_ATTACHMENT8
:
3283 case GL_COLOR_ATTACHMENT9
:
3284 case GL_COLOR_ATTACHMENT10
:
3285 case GL_COLOR_ATTACHMENT11
:
3286 case GL_COLOR_ATTACHMENT12
:
3287 case GL_COLOR_ATTACHMENT13
:
3288 case GL_COLOR_ATTACHMENT14
:
3289 case GL_COLOR_ATTACHMENT15
: {
3290 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3291 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3292 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3293 "%s(attachment >= max. color attachments)", name
);
3303 /* We don't actually do anything for this yet. Just return after
3304 * validating the parameters and generating the required errors.
3309 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3314 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3315 const GLenum
*attachments
, GLint x
, GLint y
,
3316 GLsizei width
, GLsizei height
)
3318 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3319 x
, y
, width
, height
,
3320 "glInvalidateSubFramebuffer");
3324 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3325 const GLenum
*attachments
)
3327 /* The GL_ARB_invalidate_subdata spec says:
3331 * void InvalidateFramebuffer(enum target,
3332 * sizei numAttachments,
3333 * const enum *attachments);
3335 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3336 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3337 * <MAX_VIEWPORT_DIMS[1]> respectively."
3339 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3340 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3341 "glInvalidateFramebuffer");
3345 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3346 const GLenum
*attachments
)
3348 struct gl_framebuffer
*fb
;
3351 GET_CURRENT_CONTEXT(ctx
);
3353 fb
= get_framebuffer_target(ctx
, target
);
3355 _mesa_error(ctx
, GL_INVALID_ENUM
,
3356 "glDiscardFramebufferEXT(target %s)",
3357 _mesa_lookup_enum_by_nr(target
));
3361 if (numAttachments
< 0) {
3362 _mesa_error(ctx
, GL_INVALID_VALUE
,
3363 "glDiscardFramebufferEXT(numAttachments < 0)");
3367 for (i
= 0; i
< numAttachments
; i
++) {
3368 switch (attachments
[i
]) {
3372 if (_mesa_is_user_fbo(fb
))
3375 case GL_COLOR_ATTACHMENT0
:
3376 case GL_DEPTH_ATTACHMENT
:
3377 case GL_STENCIL_ATTACHMENT
:
3378 if (_mesa_is_winsys_fbo(fb
))
3386 if (ctx
->Driver
.DiscardFramebuffer
)
3387 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3392 _mesa_error(ctx
, GL_INVALID_ENUM
,
3393 "glDiscardFramebufferEXT(attachment %s)",
3394 _mesa_lookup_enum_by_nr(attachments
[i
]));