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"
46 #include "multisample.h"
48 #include "renderbuffer.h"
54 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
61 * None of the GL_EXT_framebuffer_object functions are compiled into
68 * When glGenRender/FramebuffersEXT() is called we insert pointers to
69 * these placeholder objects into the hash table.
70 * Later, when the object ID is first bound, we replace the placeholder
71 * with the real frame/renderbuffer.
73 static struct gl_framebuffer DummyFramebuffer
;
74 static struct gl_renderbuffer DummyRenderbuffer
;
76 /* We bind this framebuffer when applications pass a NULL
77 * drawable/surface in make current. */
78 static struct gl_framebuffer IncompleteFramebuffer
;
82 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
88 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
95 _mesa_init_fbobjects(struct gl_context
*ctx
)
97 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
98 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
99 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
100 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
101 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
102 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
105 struct gl_framebuffer
*
106 _mesa_get_incomplete_framebuffer(void)
108 return &IncompleteFramebuffer
;
112 * Helper routine for getting a gl_renderbuffer.
114 struct gl_renderbuffer
*
115 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
117 struct gl_renderbuffer
*rb
;
122 rb
= (struct gl_renderbuffer
*)
123 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
129 * Helper routine for getting a gl_framebuffer.
131 struct gl_framebuffer
*
132 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
134 struct gl_framebuffer
*fb
;
139 fb
= (struct gl_framebuffer
*)
140 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
146 * Mark the given framebuffer as invalid. This will force the
147 * test for framebuffer completeness to be done before the framebuffer
151 invalidate_framebuffer(struct gl_framebuffer
*fb
)
153 fb
->_Status
= 0; /* "indeterminate" */
158 * Return the gl_framebuffer object which corresponds to the given
159 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
160 * Check support for GL_EXT_framebuffer_blit to determine if certain
162 * \return gl_framebuffer pointer or NULL if target is illegal
164 static struct gl_framebuffer
*
165 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
167 bool have_fb_blit
= _mesa_is_gles3(ctx
) ||
168 (ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
));
170 case GL_DRAW_FRAMEBUFFER
:
171 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
172 case GL_READ_FRAMEBUFFER
:
173 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
174 case GL_FRAMEBUFFER_EXT
:
175 return ctx
->DrawBuffer
;
183 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
184 * gl_renderbuffer_attachment object.
185 * This function is only used for user-created FB objects, not the
186 * default / window-system FB object.
187 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
188 * the depth buffer attachment point.
190 struct gl_renderbuffer_attachment
*
191 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
196 assert(_mesa_is_user_fbo(fb
));
198 switch (attachment
) {
199 case GL_COLOR_ATTACHMENT0_EXT
:
200 case GL_COLOR_ATTACHMENT1_EXT
:
201 case GL_COLOR_ATTACHMENT2_EXT
:
202 case GL_COLOR_ATTACHMENT3_EXT
:
203 case GL_COLOR_ATTACHMENT4_EXT
:
204 case GL_COLOR_ATTACHMENT5_EXT
:
205 case GL_COLOR_ATTACHMENT6_EXT
:
206 case GL_COLOR_ATTACHMENT7_EXT
:
207 case GL_COLOR_ATTACHMENT8_EXT
:
208 case GL_COLOR_ATTACHMENT9_EXT
:
209 case GL_COLOR_ATTACHMENT10_EXT
:
210 case GL_COLOR_ATTACHMENT11_EXT
:
211 case GL_COLOR_ATTACHMENT12_EXT
:
212 case GL_COLOR_ATTACHMENT13_EXT
:
213 case GL_COLOR_ATTACHMENT14_EXT
:
214 case GL_COLOR_ATTACHMENT15_EXT
:
215 /* Only OpenGL ES 1.x forbids color attachments other than
216 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
219 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
220 if (i
>= ctx
->Const
.MaxColorAttachments
221 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
224 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
225 case GL_DEPTH_STENCIL_ATTACHMENT
:
226 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
229 case GL_DEPTH_ATTACHMENT_EXT
:
230 return &fb
->Attachment
[BUFFER_DEPTH
];
231 case GL_STENCIL_ATTACHMENT_EXT
:
232 return &fb
->Attachment
[BUFFER_STENCIL
];
240 * As above, but only used for getting attachments of the default /
241 * window-system framebuffer (not user-created framebuffer objects).
243 static struct gl_renderbuffer_attachment
*
244 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
247 assert(_mesa_is_winsys_fbo(fb
));
249 if (_mesa_is_gles3(ctx
)) {
250 assert(attachment
== GL_BACK
||
251 attachment
== GL_DEPTH
||
252 attachment
== GL_STENCIL
);
253 switch (attachment
) {
255 /* Since there is no stereo rendering in ES 3.0, only return the
258 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
259 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
260 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
262 return &fb
->Attachment
[BUFFER_DEPTH
];
264 return &fb
->Attachment
[BUFFER_STENCIL
];
268 switch (attachment
) {
270 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
272 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
274 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
276 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
278 if (fb
->Visual
.numAuxBuffers
== 1) {
279 return &fb
->Attachment
[BUFFER_AUX0
];
283 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
285 * "If the default framebuffer is bound to target, then attachment must
286 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
287 * identifying a color buffer; DEPTH, identifying the depth buffer; or
288 * STENCIL, identifying the stencil buffer."
290 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
291 * language. However, revision #33 of the ARB_framebuffer_object spec
294 * "If the default framebuffer is bound to <target>, then <attachment>
295 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
296 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
297 * depth buffer, or the stencil buffer, and <pname> may be
298 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
299 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
301 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
302 * from glext.h, so shipping apps should not use those values.
304 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
305 * support queries of the window system FBO.
308 return &fb
->Attachment
[BUFFER_DEPTH
];
310 return &fb
->Attachment
[BUFFER_STENCIL
];
319 * Remove any texture or renderbuffer attached to the given attachment
320 * point. Update reference counts, etc.
323 _mesa_remove_attachment(struct gl_context
*ctx
,
324 struct gl_renderbuffer_attachment
*att
)
326 if (att
->Type
== GL_TEXTURE
) {
327 ASSERT(att
->Texture
);
328 if (ctx
->Driver
.FinishRenderTexture
) {
329 /* tell driver that we're done rendering to this texture. */
330 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
332 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
333 ASSERT(!att
->Texture
);
335 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
336 ASSERT(!att
->Texture
);
337 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
338 ASSERT(!att
->Renderbuffer
);
341 att
->Complete
= GL_TRUE
;
346 * Bind a texture object to an attachment point.
347 * The previous binding, if any, will be removed first.
350 _mesa_set_texture_attachment(struct gl_context
*ctx
,
351 struct gl_framebuffer
*fb
,
352 struct gl_renderbuffer_attachment
*att
,
353 struct gl_texture_object
*texObj
,
354 GLenum texTarget
, GLuint level
, GLuint zoffset
)
356 if (att
->Texture
== texObj
) {
357 /* re-attaching same texture */
358 ASSERT(att
->Type
== GL_TEXTURE
);
359 if (ctx
->Driver
.FinishRenderTexture
)
360 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
364 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
365 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
366 _mesa_remove_attachment(ctx
, att
);
367 att
->Type
= GL_TEXTURE
;
368 assert(!att
->Texture
);
369 _mesa_reference_texobj(&att
->Texture
, texObj
);
372 /* always update these fields */
373 att
->TextureLevel
= level
;
374 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
375 att
->Zoffset
= zoffset
;
376 att
->Complete
= GL_FALSE
;
378 if (_mesa_get_attachment_teximage(att
)) {
379 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
382 invalidate_framebuffer(fb
);
387 * Bind a renderbuffer to an attachment point.
388 * The previous binding, if any, will be removed first.
391 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
392 struct gl_renderbuffer_attachment
*att
,
393 struct gl_renderbuffer
*rb
)
395 /* XXX check if re-doing same attachment, exit early */
396 _mesa_remove_attachment(ctx
, att
);
397 att
->Type
= GL_RENDERBUFFER_EXT
;
398 att
->Texture
= NULL
; /* just to be safe */
399 att
->Complete
= GL_FALSE
;
400 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
405 * Fallback for ctx->Driver.FramebufferRenderbuffer()
406 * Attach a renderbuffer object to a framebuffer object.
409 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
410 struct gl_framebuffer
*fb
,
411 GLenum attachment
, struct gl_renderbuffer
*rb
)
413 struct gl_renderbuffer_attachment
*att
;
415 _glthread_LOCK_MUTEX(fb
->Mutex
);
417 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
420 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
421 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
422 /* do stencil attachment here (depth already done above) */
423 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
425 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
427 rb
->AttachedAnytime
= GL_TRUE
;
430 _mesa_remove_attachment(ctx
, att
);
433 invalidate_framebuffer(fb
);
435 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
440 * Fallback for ctx->Driver.ValidateFramebuffer()
441 * Check if the renderbuffer's formats are supported by the software
443 * Drivers should probably override this.
446 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
449 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
450 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
452 switch (rb
->_BaseFormat
) {
454 case GL_LUMINANCE_ALPHA
:
459 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
463 switch (rb
->Format
) {
464 /* XXX This list is likely incomplete. */
465 case MESA_FORMAT_RGB9_E5_FLOAT
:
466 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
469 /* render buffer format is supported by software rendering */
478 * Return true if the framebuffer has a combined depth/stencil
479 * renderbuffer attached.
482 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
484 const struct gl_renderbuffer_attachment
*depth
=
485 &fb
->Attachment
[BUFFER_DEPTH
];
486 const struct gl_renderbuffer_attachment
*stencil
=
487 &fb
->Attachment
[BUFFER_STENCIL
];
489 if (depth
->Type
== stencil
->Type
) {
490 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
491 depth
->Renderbuffer
== stencil
->Renderbuffer
)
494 if (depth
->Type
== GL_TEXTURE
&&
495 depth
->Texture
== stencil
->Texture
)
507 att_incomplete(const char *msg
)
509 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
510 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
519 fbo_incomplete(const char *msg
, int index
)
521 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
522 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
528 * Is the given base format a legal format for a color renderbuffer?
531 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
533 switch (baseFormat
) {
538 case GL_LUMINANCE_ALPHA
:
541 return ctx
->API
== API_OPENGL_COMPAT
&&
542 ctx
->Extensions
.ARB_framebuffer_object
;
545 return ctx
->Extensions
.ARB_texture_rg
;
553 * Is the given base format a legal format for a color renderbuffer?
556 is_format_color_renderable(const struct gl_context
*ctx
, gl_format format
, GLenum internalFormat
)
558 const GLenum baseFormat
=
559 _mesa_get_format_base_format(format
);
562 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
563 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
567 /* Reject additional cases for GLES */
568 switch (internalFormat
) {
588 if (format
== MESA_FORMAT_ARGB2101010
&& internalFormat
!= GL_RGB10_A2
) {
597 * Is the given base format a legal format for a depth/stencil renderbuffer?
600 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
602 switch (baseFormat
) {
603 case GL_DEPTH_COMPONENT
:
604 case GL_DEPTH_STENCIL_EXT
:
613 * Test if an attachment point is complete and update its Complete field.
614 * \param format if GL_COLOR, this is a color attachment point,
615 * if GL_DEPTH, this is a depth component attachment point,
616 * if GL_STENCIL, this is a stencil component attachment point.
619 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
620 struct gl_renderbuffer_attachment
*att
)
622 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
624 /* assume complete */
625 att
->Complete
= GL_TRUE
;
627 /* Look for reasons why the attachment might be incomplete */
628 if (att
->Type
== GL_TEXTURE
) {
629 const struct gl_texture_object
*texObj
= att
->Texture
;
630 struct gl_texture_image
*texImage
;
634 att_incomplete("no texobj");
635 att
->Complete
= GL_FALSE
;
639 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
641 att_incomplete("no teximage");
642 att
->Complete
= GL_FALSE
;
645 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
646 att_incomplete("teximage width/height=0");
647 printf("texobj = %u\n", texObj
->Name
);
648 printf("level = %d\n", att
->TextureLevel
);
649 att
->Complete
= GL_FALSE
;
652 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
653 att_incomplete("bad z offset");
654 att
->Complete
= GL_FALSE
;
658 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
660 if (format
== GL_COLOR
) {
661 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
662 att_incomplete("bad format");
663 att
->Complete
= GL_FALSE
;
666 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
667 att_incomplete("compressed internalformat");
668 att
->Complete
= GL_FALSE
;
672 else if (format
== GL_DEPTH
) {
673 if (baseFormat
== GL_DEPTH_COMPONENT
) {
676 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
677 ctx
->Extensions
.ARB_depth_texture
&&
678 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
682 att
->Complete
= GL_FALSE
;
683 att_incomplete("bad depth format");
688 ASSERT(format
== GL_STENCIL
);
689 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
690 ctx
->Extensions
.ARB_depth_texture
&&
691 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
695 /* no such thing as stencil-only textures */
696 att_incomplete("illegal stencil texture");
697 att
->Complete
= GL_FALSE
;
702 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
703 const GLenum baseFormat
=
704 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
706 ASSERT(att
->Renderbuffer
);
707 if (!att
->Renderbuffer
->InternalFormat
||
708 att
->Renderbuffer
->Width
< 1 ||
709 att
->Renderbuffer
->Height
< 1) {
710 att_incomplete("0x0 renderbuffer");
711 att
->Complete
= GL_FALSE
;
714 if (format
== GL_COLOR
) {
715 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
716 att_incomplete("bad renderbuffer color format");
717 att
->Complete
= GL_FALSE
;
721 else if (format
== GL_DEPTH
) {
722 if (baseFormat
== GL_DEPTH_COMPONENT
) {
725 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
726 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
730 att_incomplete("bad renderbuffer depth format");
731 att
->Complete
= GL_FALSE
;
736 assert(format
== GL_STENCIL
);
737 if (baseFormat
== GL_STENCIL_INDEX
) {
740 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
741 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
745 att
->Complete
= GL_FALSE
;
746 att_incomplete("bad renderbuffer stencil format");
752 ASSERT(att
->Type
== GL_NONE
);
760 * Test if the given framebuffer object is complete and update its
761 * Status field with the results.
762 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
763 * driver to make hardware-specific validation/completeness checks.
764 * Also update the framebuffer's Width and Height fields if the
765 * framebuffer is complete.
768 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
769 struct gl_framebuffer
*fb
)
772 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
773 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
774 GLint numSamples
= -1;
775 GLint fixedSampleLocations
= -1;
779 assert(_mesa_is_user_fbo(fb
));
781 /* we're changing framebuffer fields here */
782 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
788 /* Start at -2 to more easily loop over all attachment points.
793 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
794 struct gl_renderbuffer_attachment
*att
;
799 * XXX for ARB_fbo, only check color buffers that are named by
800 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
803 /* check for attachment completeness
806 att
= &fb
->Attachment
[BUFFER_DEPTH
];
807 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
808 if (!att
->Complete
) {
809 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
810 fbo_incomplete("depth attachment incomplete", -1);
815 att
= &fb
->Attachment
[BUFFER_STENCIL
];
816 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
817 if (!att
->Complete
) {
818 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
819 fbo_incomplete("stencil attachment incomplete", -1);
824 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
825 test_attachment_completeness(ctx
, GL_COLOR
, att
);
826 if (!att
->Complete
) {
827 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
828 fbo_incomplete("color attachment incomplete", i
);
833 /* get width, height, format of the renderbuffer/texture
835 if (att
->Type
== GL_TEXTURE
) {
836 const struct gl_texture_image
*texImg
=
837 _mesa_get_attachment_teximage(att
);
838 minWidth
= MIN2(minWidth
, texImg
->Width
);
839 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
840 minHeight
= MIN2(minHeight
, texImg
->Height
);
841 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
842 f
= texImg
->_BaseFormat
;
843 attFormat
= texImg
->TexFormat
;
846 if (!is_format_color_renderable(ctx
, attFormat
, texImg
->InternalFormat
) &&
847 !is_legal_depth_format(ctx
, f
)) {
848 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
849 fbo_incomplete("texture attachment incomplete", -1);
854 numSamples
= texImg
->NumSamples
;
855 else if (numSamples
!= texImg
->NumSamples
) {
856 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
857 fbo_incomplete("inconsistent sample count", -1);
861 if (fixedSampleLocations
< 0)
862 fixedSampleLocations
= texImg
->FixedSampleLocations
;
863 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
864 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
865 fbo_incomplete("inconsistent fixed sample locations", -1);
869 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
870 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
871 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
872 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
873 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
874 f
= att
->Renderbuffer
->InternalFormat
;
875 attFormat
= att
->Renderbuffer
->Format
;
879 numSamples
= att
->Renderbuffer
->NumSamples
;
880 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
881 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
882 fbo_incomplete("inconsistent sample count", -1);
886 /* RENDERBUFFER has fixedSampleLocations implicitly true */
887 if (fixedSampleLocations
< 0)
888 fixedSampleLocations
= GL_TRUE
;
889 else if (fixedSampleLocations
!= GL_TRUE
) {
890 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
891 fbo_incomplete("inconsistent fixed sample locations", -1);
896 assert(att
->Type
== GL_NONE
);
900 /* check if integer color */
901 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
903 /* Error-check width, height, format */
904 if (numImages
== 1) {
911 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
912 /* check that width, height, format are same */
913 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
914 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
915 fbo_incomplete("width or height mismatch", -1);
918 /* check that all color buffers are the same format */
919 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
920 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
921 fbo_incomplete("format mismatch", -1);
927 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
929 if (att
->Type
== GL_RENDERBUFFER
&&
930 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
931 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
932 fbo_incomplete("unsupported renderbuffer format", i
);
937 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
938 /* Check that all DrawBuffers are present */
939 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
940 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
941 const struct gl_renderbuffer_attachment
*att
942 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
944 if (att
->Type
== GL_NONE
) {
945 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
946 fbo_incomplete("missing drawbuffer", j
);
952 /* Check that the ReadBuffer is present */
953 if (fb
->ColorReadBuffer
!= GL_NONE
) {
954 const struct gl_renderbuffer_attachment
*att
955 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
957 if (att
->Type
== GL_NONE
) {
958 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
959 fbo_incomplete("missing readbuffer", -1);
965 if (numImages
== 0) {
966 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
967 fbo_incomplete("no attachments", -1);
971 /* Provisionally set status = COMPLETE ... */
972 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
974 /* ... but the driver may say the FB is incomplete.
975 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
978 if (ctx
->Driver
.ValidateFramebuffer
) {
979 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
980 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
981 fbo_incomplete("driver marked FBO as incomplete", -1);
985 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
987 * Note that if ARB_framebuffer_object is supported and the attached
988 * renderbuffers/textures are different sizes, the framebuffer
989 * width/height will be set to the smallest width/height.
991 fb
->Width
= minWidth
;
992 fb
->Height
= minHeight
;
994 /* finally, update the visual info for the framebuffer */
995 _mesa_update_framebuffer_visual(ctx
, fb
);
1000 GLboolean GLAPIENTRY
1001 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1003 GET_CURRENT_CONTEXT(ctx
);
1004 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1006 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1007 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1015 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1017 struct gl_renderbuffer
*newRb
;
1018 GET_CURRENT_CONTEXT(ctx
);
1020 if (target
!= GL_RENDERBUFFER_EXT
) {
1021 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1025 /* No need to flush here since the render buffer binding has no
1026 * effect on rendering state.
1030 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1031 if (newRb
== &DummyRenderbuffer
) {
1032 /* ID was reserved, but no real renderbuffer object made yet */
1036 && _mesa_is_desktop_gl(ctx
)
1037 && ctx
->Extensions
.ARB_framebuffer_object
) {
1038 /* All RB IDs must be Gen'd */
1039 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1044 /* create new renderbuffer object */
1045 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1047 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
1050 ASSERT(newRb
->AllocStorage
);
1051 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1052 newRb
->RefCount
= 1; /* referenced by hash table */
1059 ASSERT(newRb
!= &DummyRenderbuffer
);
1061 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1066 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1068 * This is used when a renderbuffer object is deleted.
1069 * The spec calls for unbinding.
1072 detach_renderbuffer(struct gl_context
*ctx
,
1073 struct gl_framebuffer
*fb
,
1074 struct gl_renderbuffer
*rb
)
1077 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1078 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
1079 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
1082 invalidate_framebuffer(fb
);
1087 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1090 GET_CURRENT_CONTEXT(ctx
);
1092 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1094 for (i
= 0; i
< n
; i
++) {
1095 if (renderbuffers
[i
] > 0) {
1096 struct gl_renderbuffer
*rb
;
1097 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1099 /* check if deleting currently bound renderbuffer object */
1100 if (rb
== ctx
->CurrentRenderbuffer
) {
1102 ASSERT(rb
->RefCount
>= 2);
1103 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1106 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1107 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1109 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1110 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1111 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1114 /* Remove from hash table immediately, to free the ID.
1115 * But the object will not be freed until it's no longer
1116 * referenced anywhere else.
1118 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1120 if (rb
!= &DummyRenderbuffer
) {
1121 /* no longer referenced by hash table */
1122 _mesa_reference_renderbuffer(&rb
, NULL
);
1131 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1133 GET_CURRENT_CONTEXT(ctx
);
1138 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1145 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1147 for (i
= 0; i
< n
; i
++) {
1148 GLuint name
= first
+ i
;
1149 renderbuffers
[i
] = name
;
1150 /* insert dummy placeholder into hash table */
1151 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1152 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1153 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1159 * Given an internal format token for a render buffer, return the
1160 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1161 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1162 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1164 * This is similar to _mesa_base_tex_format() but the set of valid
1165 * internal formats is different.
1167 * Note that even if a format is determined to be legal here, validation
1168 * of the FBO may fail if the format is not supported by the driver/GPU.
1170 * \param internalFormat as passed to glRenderbufferStorage()
1171 * \return the base internal format, or 0 if internalFormat is illegal
1174 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1177 * Notes: some formats such as alpha, luminance, etc. were added
1178 * with GL_ARB_framebuffer_object.
1180 switch (internalFormat
) {
1186 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1191 case GL_LUMINANCE12
:
1192 case GL_LUMINANCE16
:
1193 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1195 case GL_LUMINANCE_ALPHA
:
1196 case GL_LUMINANCE4_ALPHA4
:
1197 case GL_LUMINANCE6_ALPHA2
:
1198 case GL_LUMINANCE8_ALPHA8
:
1199 case GL_LUMINANCE12_ALPHA4
:
1200 case GL_LUMINANCE12_ALPHA12
:
1201 case GL_LUMINANCE16_ALPHA16
:
1202 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1203 ? GL_LUMINANCE_ALPHA
: 0;
1207 case GL_INTENSITY12
:
1208 case GL_INTENSITY16
:
1209 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1220 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1222 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1231 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1233 case GL_SRGB8_ALPHA8_EXT
:
1234 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1235 case GL_STENCIL_INDEX
:
1236 case GL_STENCIL_INDEX1_EXT
:
1237 case GL_STENCIL_INDEX4_EXT
:
1238 case GL_STENCIL_INDEX16_EXT
:
1239 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1240 * OpenGL ES, but Mesa does not currently support them.
1242 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1243 case GL_STENCIL_INDEX8_EXT
:
1244 return GL_STENCIL_INDEX
;
1245 case GL_DEPTH_COMPONENT
:
1246 case GL_DEPTH_COMPONENT32
:
1247 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1248 case GL_DEPTH_COMPONENT16
:
1249 case GL_DEPTH_COMPONENT24
:
1250 return GL_DEPTH_COMPONENT
;
1251 case GL_DEPTH_STENCIL_EXT
:
1252 return _mesa_is_desktop_gl(ctx
)
1253 && ctx
->Extensions
.EXT_packed_depth_stencil
1254 ? GL_DEPTH_STENCIL_EXT
: 0;
1255 case GL_DEPTH24_STENCIL8_EXT
:
1256 return ctx
->Extensions
.EXT_packed_depth_stencil
1257 ? GL_DEPTH_STENCIL_EXT
: 0;
1258 case GL_DEPTH_COMPONENT32F
:
1259 return ctx
->Version
>= 30
1260 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1261 ? GL_DEPTH_COMPONENT
: 0;
1262 case GL_DEPTH32F_STENCIL8
:
1263 return ctx
->Version
>= 30
1264 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1265 ? GL_DEPTH_STENCIL
: 0;
1268 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1271 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1275 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1278 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1280 /* signed normalized texture formats */
1284 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1289 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1293 case GL_RGB16_SNORM
:
1294 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1297 case GL_RGBA8_SNORM
:
1298 case GL_RGBA16_SNORM
:
1299 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1301 case GL_ALPHA_SNORM
:
1302 case GL_ALPHA8_SNORM
:
1303 case GL_ALPHA16_SNORM
:
1304 return ctx
->API
== API_OPENGL_COMPAT
&&
1305 ctx
->Extensions
.EXT_texture_snorm
&&
1306 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1309 return ((_mesa_is_desktop_gl(ctx
) &&
1310 ctx
->Extensions
.ARB_texture_rg
&&
1311 ctx
->Extensions
.ARB_texture_float
) ||
1312 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1316 return ((_mesa_is_desktop_gl(ctx
) &&
1317 ctx
->Extensions
.ARB_texture_rg
&&
1318 ctx
->Extensions
.ARB_texture_float
) ||
1319 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1323 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1327 return ((_mesa_is_desktop_gl(ctx
) &&
1328 ctx
->Extensions
.ARB_texture_float
) ||
1329 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1331 case GL_ALPHA16F_ARB
:
1332 case GL_ALPHA32F_ARB
:
1333 return ctx
->API
== API_OPENGL_COMPAT
&&
1334 ctx
->Extensions
.ARB_texture_float
&&
1335 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1336 case GL_LUMINANCE16F_ARB
:
1337 case GL_LUMINANCE32F_ARB
:
1338 return ctx
->API
== API_OPENGL_COMPAT
&&
1339 ctx
->Extensions
.ARB_texture_float
&&
1340 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1341 case GL_LUMINANCE_ALPHA16F_ARB
:
1342 case GL_LUMINANCE_ALPHA32F_ARB
:
1343 return ctx
->API
== API_OPENGL_COMPAT
&&
1344 ctx
->Extensions
.ARB_texture_float
&&
1345 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1346 case GL_INTENSITY16F_ARB
:
1347 case GL_INTENSITY32F_ARB
:
1348 return ctx
->API
== API_OPENGL_COMPAT
&&
1349 ctx
->Extensions
.ARB_texture_float
&&
1350 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1352 return (_mesa_is_desktop_gl(ctx
)
1353 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1355 case GL_R11F_G11F_B10F
:
1356 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1357 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1360 case GL_RGBA8UI_EXT
:
1361 case GL_RGBA16UI_EXT
:
1362 case GL_RGBA32UI_EXT
:
1364 case GL_RGBA16I_EXT
:
1365 case GL_RGBA32I_EXT
:
1366 return ctx
->Version
>= 30
1367 || (_mesa_is_desktop_gl(ctx
) &&
1368 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1371 case GL_RGB16UI_EXT
:
1372 case GL_RGB32UI_EXT
:
1376 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1384 return ctx
->Version
>= 30
1385 || (_mesa_is_desktop_gl(ctx
) &&
1386 ctx
->Extensions
.ARB_texture_rg
&&
1387 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1395 return ctx
->Version
>= 30
1396 || (_mesa_is_desktop_gl(ctx
) &&
1397 ctx
->Extensions
.ARB_texture_rg
&&
1398 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1400 case GL_INTENSITY8I_EXT
:
1401 case GL_INTENSITY8UI_EXT
:
1402 case GL_INTENSITY16I_EXT
:
1403 case GL_INTENSITY16UI_EXT
:
1404 case GL_INTENSITY32I_EXT
:
1405 case GL_INTENSITY32UI_EXT
:
1406 return ctx
->API
== API_OPENGL_COMPAT
&&
1407 ctx
->Extensions
.EXT_texture_integer
&&
1408 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1410 case GL_LUMINANCE8I_EXT
:
1411 case GL_LUMINANCE8UI_EXT
:
1412 case GL_LUMINANCE16I_EXT
:
1413 case GL_LUMINANCE16UI_EXT
:
1414 case GL_LUMINANCE32I_EXT
:
1415 case GL_LUMINANCE32UI_EXT
:
1416 return ctx
->API
== API_OPENGL_COMPAT
&&
1417 ctx
->Extensions
.EXT_texture_integer
&&
1418 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1420 case GL_LUMINANCE_ALPHA8I_EXT
:
1421 case GL_LUMINANCE_ALPHA8UI_EXT
:
1422 case GL_LUMINANCE_ALPHA16I_EXT
:
1423 case GL_LUMINANCE_ALPHA16UI_EXT
:
1424 case GL_LUMINANCE_ALPHA32I_EXT
:
1425 case GL_LUMINANCE_ALPHA32UI_EXT
:
1426 return ctx
->API
== API_OPENGL_COMPAT
&&
1427 ctx
->Extensions
.EXT_texture_integer
&&
1428 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1430 case GL_ALPHA8I_EXT
:
1431 case GL_ALPHA8UI_EXT
:
1432 case GL_ALPHA16I_EXT
:
1433 case GL_ALPHA16UI_EXT
:
1434 case GL_ALPHA32I_EXT
:
1435 case GL_ALPHA32UI_EXT
:
1436 return ctx
->API
== API_OPENGL_COMPAT
&&
1437 ctx
->Extensions
.EXT_texture_integer
&&
1438 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1441 return (_mesa_is_desktop_gl(ctx
) &&
1442 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1443 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1446 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1455 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1458 invalidate_rb(GLuint key
, void *data
, void *userData
)
1460 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1461 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1463 /* If this is a user-created FBO */
1464 if (_mesa_is_user_fbo(fb
)) {
1466 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1467 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1468 if (att
->Type
== GL_RENDERBUFFER
&&
1469 att
->Renderbuffer
== rb
) {
1470 /* Mark fb status as indeterminate to force re-validation */
1479 /** sentinal value, see below */
1480 #define NO_SAMPLES 1000
1484 * Helper function used by _mesa_RenderbufferStorage() and
1485 * _mesa_RenderbufferStorageMultisample().
1486 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1489 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1490 GLsizei width
, GLsizei height
, GLsizei samples
)
1492 const char *func
= samples
== NO_SAMPLES
?
1493 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1494 struct gl_renderbuffer
*rb
;
1496 GLenum sample_count_error
;
1497 GET_CURRENT_CONTEXT(ctx
);
1499 if (MESA_VERBOSE
& VERBOSE_API
) {
1500 if (samples
== NO_SAMPLES
)
1501 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1503 _mesa_lookup_enum_by_nr(target
),
1504 _mesa_lookup_enum_by_nr(internalFormat
),
1507 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1509 _mesa_lookup_enum_by_nr(target
),
1510 _mesa_lookup_enum_by_nr(internalFormat
),
1511 width
, height
, samples
);
1514 if (target
!= GL_RENDERBUFFER_EXT
) {
1515 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1519 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1520 if (baseFormat
== 0) {
1521 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1522 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1526 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1527 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1531 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1532 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1536 if (samples
== NO_SAMPLES
) {
1537 /* NumSamples == 0 indicates non-multisampling */
1541 /* check the sample count;
1542 * note: driver may choose to use more samples than what's requested
1544 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
1545 internalFormat
, samples
);
1546 if (sample_count_error
!= GL_NO_ERROR
) {
1547 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
1552 rb
= ctx
->CurrentRenderbuffer
;
1554 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1558 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1560 if (rb
->InternalFormat
== internalFormat
&&
1561 rb
->Width
== (GLuint
) width
&&
1562 rb
->Height
== (GLuint
) height
&&
1563 rb
->NumSamples
== samples
) {
1564 /* no change in allocation needed */
1568 /* These MUST get set by the AllocStorage func */
1569 rb
->Format
= MESA_FORMAT_NONE
;
1570 rb
->NumSamples
= samples
;
1572 /* Now allocate the storage */
1573 ASSERT(rb
->AllocStorage
);
1574 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1575 /* No error - check/set fields now */
1576 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1577 assert(rb
->Width
== (GLuint
) width
);
1578 assert(rb
->Height
== (GLuint
) height
);
1579 rb
->InternalFormat
= internalFormat
;
1580 rb
->_BaseFormat
= baseFormat
;
1581 assert(rb
->_BaseFormat
!= 0);
1584 /* Probably ran out of memory - clear the fields */
1587 rb
->Format
= MESA_FORMAT_NONE
;
1588 rb
->InternalFormat
= GL_NONE
;
1589 rb
->_BaseFormat
= GL_NONE
;
1593 /* Invalidate the framebuffers the renderbuffer is attached in. */
1594 if (rb
->AttachedAnytime
) {
1595 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1601 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1603 struct gl_renderbuffer
*rb
;
1604 GET_CURRENT_CONTEXT(ctx
);
1606 if (!ctx
->Extensions
.OES_EGL_image
) {
1607 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1608 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1612 if (target
!= GL_RENDERBUFFER
) {
1613 _mesa_error(ctx
, GL_INVALID_ENUM
,
1614 "EGLImageTargetRenderbufferStorageOES");
1618 rb
= ctx
->CurrentRenderbuffer
;
1620 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1621 "EGLImageTargetRenderbufferStorageOES");
1625 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1627 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1632 * Helper function for _mesa_GetRenderbufferParameteriv() and
1633 * _mesa_GetFramebufferAttachmentParameteriv()
1634 * We have to be careful to respect the base format. For example, if a
1635 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1636 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1637 * we need to return zero.
1640 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1642 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1643 return _mesa_get_format_bits(format
, pname
);
1651 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1652 GLsizei width
, GLsizei height
)
1654 /* GL_ARB_fbo says calling this function is equivalent to calling
1655 * glRenderbufferStorageMultisample() with samples=0. We pass in
1656 * a token value here just for error reporting purposes.
1658 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1663 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1664 GLenum internalFormat
,
1665 GLsizei width
, GLsizei height
)
1667 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1672 * OpenGL ES version of glRenderBufferStorage.
1675 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1676 GLsizei width
, GLsizei height
)
1678 switch (internalFormat
) {
1680 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1681 /* choose a closest format */
1682 internalFormat
= GL_RGB5
;
1688 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1693 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1695 struct gl_renderbuffer
*rb
;
1696 GET_CURRENT_CONTEXT(ctx
);
1698 if (target
!= GL_RENDERBUFFER_EXT
) {
1699 _mesa_error(ctx
, GL_INVALID_ENUM
,
1700 "glGetRenderbufferParameterivEXT(target)");
1704 rb
= ctx
->CurrentRenderbuffer
;
1706 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1707 "glGetRenderbufferParameterivEXT");
1711 /* No need to flush here since we're just quering state which is
1712 * not effected by rendering.
1716 case GL_RENDERBUFFER_WIDTH_EXT
:
1717 *params
= rb
->Width
;
1719 case GL_RENDERBUFFER_HEIGHT_EXT
:
1720 *params
= rb
->Height
;
1722 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1723 *params
= rb
->InternalFormat
;
1725 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1726 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1727 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1728 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1729 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1730 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1731 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1733 case GL_RENDERBUFFER_SAMPLES
:
1734 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1735 || _mesa_is_gles3(ctx
)) {
1736 *params
= rb
->NumSamples
;
1741 _mesa_error(ctx
, GL_INVALID_ENUM
,
1742 "glGetRenderbufferParameterivEXT(target)");
1748 GLboolean GLAPIENTRY
1749 _mesa_IsFramebuffer(GLuint framebuffer
)
1751 GET_CURRENT_CONTEXT(ctx
);
1752 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1754 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1755 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1763 * Check if any of the attachments of the given framebuffer are textures
1764 * (render to texture). Call ctx->Driver.RenderTexture() for such
1768 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1771 ASSERT(ctx
->Driver
.RenderTexture
);
1773 if (_mesa_is_winsys_fbo(fb
))
1774 return; /* can't render to texture with winsys framebuffers */
1776 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1777 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1778 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1779 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1786 * Examine all the framebuffer's attachments to see if any are textures.
1787 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1788 * notify the device driver that the texture image may have changed.
1791 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1793 if (_mesa_is_winsys_fbo(fb
))
1794 return; /* can't render to texture with winsys framebuffers */
1796 if (ctx
->Driver
.FinishRenderTexture
) {
1798 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1799 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1800 if (att
->Texture
&& att
->Renderbuffer
) {
1801 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1809 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
1811 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1812 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1813 GLboolean bindReadBuf
, bindDrawBuf
;
1814 GET_CURRENT_CONTEXT(ctx
);
1817 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1818 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1819 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1823 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1824 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1825 "glBindFramebufferEXT(unsupported)");
1830 case GL_DRAW_FRAMEBUFFER_EXT
:
1831 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1832 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1835 bindDrawBuf
= GL_TRUE
;
1836 bindReadBuf
= GL_FALSE
;
1838 case GL_READ_FRAMEBUFFER_EXT
:
1839 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1840 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1843 bindDrawBuf
= GL_FALSE
;
1844 bindReadBuf
= GL_TRUE
;
1846 case GL_FRAMEBUFFER_EXT
:
1847 bindDrawBuf
= GL_TRUE
;
1848 bindReadBuf
= GL_TRUE
;
1851 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1856 /* Binding a user-created framebuffer object */
1857 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1858 if (newDrawFb
== &DummyFramebuffer
) {
1859 /* ID was reserved, but no real framebuffer object made yet */
1863 && _mesa_is_desktop_gl(ctx
)
1864 && ctx
->Extensions
.ARB_framebuffer_object
) {
1865 /* All FBO IDs must be Gen'd */
1866 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1871 /* create new framebuffer object */
1872 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1874 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1877 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1879 newReadFb
= newDrawFb
;
1882 /* Binding the window system framebuffer (which was originally set
1883 * with MakeCurrent).
1885 newDrawFb
= ctx
->WinSysDrawBuffer
;
1886 newReadFb
= ctx
->WinSysReadBuffer
;
1890 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1892 /* save pointers to current/old framebuffers */
1893 oldDrawFb
= ctx
->DrawBuffer
;
1894 oldReadFb
= ctx
->ReadBuffer
;
1896 /* check if really changing bindings */
1897 if (oldDrawFb
== newDrawFb
)
1898 bindDrawBuf
= GL_FALSE
;
1899 if (oldReadFb
== newReadFb
)
1900 bindReadBuf
= GL_FALSE
;
1903 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1905 * We also check if we're beginning and/or ending render-to-texture.
1906 * When a framebuffer with texture attachments is unbound, call
1907 * ctx->Driver.FinishRenderTexture().
1908 * When a framebuffer with texture attachments is bound, call
1909 * ctx->Driver.RenderTexture().
1911 * Note that if the ReadBuffer has texture attachments we don't consider
1912 * that a render-to-texture case.
1915 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1917 /* check if old readbuffer was render-to-texture */
1918 check_end_texture_render(ctx
, oldReadFb
);
1920 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1924 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1926 /* check if old framebuffer had any texture attachments */
1928 check_end_texture_render(ctx
, oldDrawFb
);
1930 /* check if newly bound framebuffer has any texture attachments */
1931 check_begin_texture_render(ctx
, newDrawFb
);
1933 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1936 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1937 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1943 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
1946 GET_CURRENT_CONTEXT(ctx
);
1948 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1950 for (i
= 0; i
< n
; i
++) {
1951 if (framebuffers
[i
] > 0) {
1952 struct gl_framebuffer
*fb
;
1953 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1955 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1957 /* check if deleting currently bound framebuffer object */
1958 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1959 /* separate draw/read binding points */
1960 if (fb
== ctx
->DrawBuffer
) {
1962 ASSERT(fb
->RefCount
>= 2);
1963 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1965 if (fb
== ctx
->ReadBuffer
) {
1967 ASSERT(fb
->RefCount
>= 2);
1968 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, 0);
1972 /* only one binding point for read/draw buffers */
1973 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1975 ASSERT(fb
->RefCount
>= 2);
1976 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
1980 /* remove from hash table immediately, to free the ID */
1981 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1983 if (fb
!= &DummyFramebuffer
) {
1984 /* But the object will not be freed until it's no longer
1985 * bound in any context.
1987 _mesa_reference_framebuffer(&fb
, NULL
);
1996 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
1998 GET_CURRENT_CONTEXT(ctx
);
2003 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
2010 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2012 for (i
= 0; i
< n
; i
++) {
2013 GLuint name
= first
+ i
;
2014 framebuffers
[i
] = name
;
2015 /* insert dummy placeholder into hash table */
2016 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
2017 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
2018 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
2025 _mesa_CheckFramebufferStatus(GLenum target
)
2027 struct gl_framebuffer
*buffer
;
2028 GET_CURRENT_CONTEXT(ctx
);
2030 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2032 if (MESA_VERBOSE
& VERBOSE_API
)
2033 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2034 _mesa_lookup_enum_by_nr(target
));
2036 buffer
= get_framebuffer_target(ctx
, target
);
2038 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2042 if (_mesa_is_winsys_fbo(buffer
)) {
2043 /* The window system / default framebuffer is always complete */
2044 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2047 /* No need to flush here */
2049 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2050 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2053 return buffer
->_Status
;
2058 * Replicate the src attachment point. Used by framebuffer_texture() when
2059 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2060 * GL_STENCIL_ATTACHMENT.
2063 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2064 gl_buffer_index dst
,
2065 gl_buffer_index src
)
2067 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2068 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2070 assert(src_att
->Texture
!= NULL
);
2071 assert(src_att
->Renderbuffer
!= NULL
);
2073 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2074 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2075 dst_att
->Type
= src_att
->Type
;
2076 dst_att
->Complete
= src_att
->Complete
;
2077 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2078 dst_att
->Zoffset
= src_att
->Zoffset
;
2083 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2084 * glFramebufferTextureLayerEXT().
2085 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2086 * get textarget=0 in that case.
2089 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2090 GLenum attachment
, GLenum textarget
, GLuint texture
,
2091 GLint level
, GLint zoffset
)
2093 struct gl_renderbuffer_attachment
*att
;
2094 struct gl_texture_object
*texObj
= NULL
;
2095 struct gl_framebuffer
*fb
;
2096 GLenum maxLevelsTarget
;
2098 fb
= get_framebuffer_target(ctx
, target
);
2100 _mesa_error(ctx
, GL_INVALID_ENUM
,
2101 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2105 /* check framebuffer binding */
2106 if (_mesa_is_winsys_fbo(fb
)) {
2107 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2108 "glFramebufferTexture%sEXT", caller
);
2112 /* The textarget, level, and zoffset parameters are only validated if
2113 * texture is non-zero.
2116 GLboolean err
= GL_TRUE
;
2118 texObj
= _mesa_lookup_texture(ctx
, texture
);
2119 if (texObj
!= NULL
) {
2120 if (textarget
== 0) {
2121 /* If textarget == 0 it means we're being called by
2122 * glFramebufferTextureLayer() and textarget is not used.
2123 * The only legal texture types for that function are 3D and
2124 * 1D/2D arrays textures.
2126 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2127 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2128 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2129 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2130 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2133 /* Make sure textarget is consistent with the texture's type */
2134 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2135 ? !_mesa_is_cube_face(textarget
)
2136 : (texObj
->Target
!= textarget
);
2140 /* can't render to a non-existant texture */
2141 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2142 "glFramebufferTexture%sEXT(non existant texture)",
2148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2149 "glFramebufferTexture%sEXT(texture target mismatch)",
2154 if (texObj
->Target
== GL_TEXTURE_3D
) {
2155 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2156 if (zoffset
< 0 || zoffset
>= maxSize
) {
2157 _mesa_error(ctx
, GL_INVALID_VALUE
,
2158 "glFramebufferTexture%sEXT(zoffset)", caller
);
2162 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2163 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2164 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2165 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2167 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2168 _mesa_error(ctx
, GL_INVALID_VALUE
,
2169 "glFramebufferTexture%sEXT(layer)", caller
);
2174 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2176 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2177 _mesa_error(ctx
, GL_INVALID_VALUE
,
2178 "glFramebufferTexture%sEXT(level)", caller
);
2183 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2185 _mesa_error(ctx
, GL_INVALID_ENUM
,
2186 "glFramebufferTexture%sEXT(attachment)", caller
);
2190 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2192 _glthread_LOCK_MUTEX(fb
->Mutex
);
2194 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2195 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2196 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2197 _mesa_tex_target_to_face(textarget
) ==
2198 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2199 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2200 /* The texture object is already attached to the stencil attachment
2201 * point. Don't create a new renderbuffer; just reuse the stencil
2202 * attachment's. This is required to prevent a GL error in
2203 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2205 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2207 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2208 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2209 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2210 _mesa_tex_target_to_face(textarget
) ==
2211 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2212 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2213 /* As above, but with depth and stencil transposed. */
2214 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2217 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2219 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2220 /* Above we created a new renderbuffer and attached it to the
2221 * depth attachment point. Now attach it to the stencil attachment
2224 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2225 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2230 /* Set the render-to-texture flag. We'll check this flag in
2231 * glTexImage() and friends to determine if we need to revalidate
2232 * any FBOs that might be rendering into this texture.
2233 * This flag never gets cleared since it's non-trivial to determine
2234 * when all FBOs might be done rendering to this texture. That's OK
2235 * though since it's uncommon to render to a texture then repeatedly
2236 * call glTexImage() to change images in the texture.
2238 texObj
->_RenderToTexture
= GL_TRUE
;
2241 _mesa_remove_attachment(ctx
, att
);
2242 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2243 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2244 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2248 invalidate_framebuffer(fb
);
2250 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2256 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2257 GLenum textarget
, GLuint texture
, GLint level
)
2259 GET_CURRENT_CONTEXT(ctx
);
2264 switch (textarget
) {
2268 case GL_TEXTURE_1D_ARRAY
:
2269 error
= !ctx
->Extensions
.EXT_texture_array
;
2276 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2277 "glFramebufferTexture1DEXT(textarget=%s)",
2278 _mesa_lookup_enum_by_nr(textarget
));
2283 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2289 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2290 GLenum textarget
, GLuint texture
, GLint level
)
2292 GET_CURRENT_CONTEXT(ctx
);
2297 switch (textarget
) {
2301 case GL_TEXTURE_RECTANGLE
:
2302 error
= _mesa_is_gles(ctx
)
2303 || !ctx
->Extensions
.NV_texture_rectangle
;
2305 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2306 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2307 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2308 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2309 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2310 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2311 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2313 case GL_TEXTURE_2D_ARRAY
:
2314 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2315 || !ctx
->Extensions
.EXT_texture_array
;
2317 case GL_TEXTURE_2D_MULTISAMPLE
:
2318 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2319 error
= _mesa_is_gles(ctx
)
2320 || !ctx
->Extensions
.ARB_texture_multisample
;
2327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2328 "glFramebufferTexture2DEXT(textarget=%s)",
2329 _mesa_lookup_enum_by_nr(textarget
));
2334 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2340 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2341 GLenum textarget
, GLuint texture
,
2342 GLint level
, GLint zoffset
)
2344 GET_CURRENT_CONTEXT(ctx
);
2346 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2347 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2348 "glFramebufferTexture3DEXT(textarget)");
2352 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2358 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2359 GLuint texture
, GLint level
, GLint layer
)
2361 GET_CURRENT_CONTEXT(ctx
);
2363 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2369 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2370 GLenum renderbufferTarget
,
2371 GLuint renderbuffer
)
2373 struct gl_renderbuffer_attachment
*att
;
2374 struct gl_framebuffer
*fb
;
2375 struct gl_renderbuffer
*rb
;
2376 GET_CURRENT_CONTEXT(ctx
);
2378 fb
= get_framebuffer_target(ctx
, target
);
2380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2384 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2385 _mesa_error(ctx
, GL_INVALID_ENUM
,
2386 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2390 if (_mesa_is_winsys_fbo(fb
)) {
2391 /* Can't attach new renderbuffers to a window system framebuffer */
2392 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2396 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2398 _mesa_error(ctx
, GL_INVALID_ENUM
,
2399 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2400 _mesa_lookup_enum_by_nr(attachment
));
2405 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2408 "glFramebufferRenderbufferEXT(non-existant"
2409 " renderbuffer %u)", renderbuffer
);
2412 else if (rb
== &DummyRenderbuffer
) {
2413 /* This is what NVIDIA does */
2414 _mesa_error(ctx
, GL_INVALID_VALUE
,
2415 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2421 /* remove renderbuffer attachment */
2425 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2426 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2427 /* make sure the renderbuffer is a depth/stencil format */
2428 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2429 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2431 "glFramebufferRenderbufferEXT(renderbuffer"
2432 " is not DEPTH_STENCIL format)");
2438 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2440 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2441 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2443 /* Some subsequent GL commands may depend on the framebuffer's visual
2444 * after the binding is updated. Update visual info now.
2446 _mesa_update_framebuffer_visual(ctx
, fb
);
2451 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2452 GLenum pname
, GLint
*params
)
2454 const struct gl_renderbuffer_attachment
*att
;
2455 struct gl_framebuffer
*buffer
;
2457 GET_CURRENT_CONTEXT(ctx
);
2459 /* The error differs in GL and GLES. */
2460 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2462 buffer
= get_framebuffer_target(ctx
, target
);
2464 _mesa_error(ctx
, GL_INVALID_ENUM
,
2465 "glGetFramebufferAttachmentParameterivEXT(target)");
2469 if (_mesa_is_winsys_fbo(buffer
)) {
2470 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2473 * "If the framebuffer currently bound to target is zero, then
2474 * INVALID_OPERATION is generated."
2476 * The EXT_framebuffer_object spec has the same wording, and the
2477 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2480 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2481 && !_mesa_is_gles3(ctx
)) {
2482 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2483 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2487 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2488 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2490 "glGetFramebufferAttachmentParameteriv(attachment)");
2493 /* the default / window-system FBO */
2494 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2497 /* user-created framebuffer FBO */
2498 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2502 _mesa_error(ctx
, GL_INVALID_ENUM
,
2503 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2507 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2508 /* the depth and stencil attachments must point to the same buffer */
2509 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2510 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2511 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2512 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2514 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2515 " attachments differ)");
2520 /* No need to flush here */
2523 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2524 *params
= _mesa_is_winsys_fbo(buffer
)
2525 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2527 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2528 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2529 *params
= att
->Renderbuffer
->Name
;
2531 else if (att
->Type
== GL_TEXTURE
) {
2532 *params
= att
->Texture
->Name
;
2535 assert(att
->Type
== GL_NONE
);
2536 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2539 goto invalid_pname_enum
;
2543 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2544 if (att
->Type
== GL_TEXTURE
) {
2545 *params
= att
->TextureLevel
;
2547 else if (att
->Type
== GL_NONE
) {
2548 _mesa_error(ctx
, err
,
2549 "glGetFramebufferAttachmentParameterivEXT(pname)");
2552 goto invalid_pname_enum
;
2555 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2556 if (att
->Type
== GL_TEXTURE
) {
2557 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2558 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2564 else if (att
->Type
== GL_NONE
) {
2565 _mesa_error(ctx
, err
,
2566 "glGetFramebufferAttachmentParameterivEXT(pname)");
2569 goto invalid_pname_enum
;
2572 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2573 if (ctx
->API
== API_OPENGLES
) {
2574 goto invalid_pname_enum
;
2575 } else if (att
->Type
== GL_NONE
) {
2576 _mesa_error(ctx
, err
,
2577 "glGetFramebufferAttachmentParameterivEXT(pname)");
2578 } else if (att
->Type
== GL_TEXTURE
) {
2579 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2580 *params
= att
->Zoffset
;
2587 goto invalid_pname_enum
;
2590 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2591 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2592 && !_mesa_is_gles3(ctx
)) {
2593 goto invalid_pname_enum
;
2595 else if (att
->Type
== GL_NONE
) {
2596 _mesa_error(ctx
, err
,
2597 "glGetFramebufferAttachmentParameterivEXT(pname)");
2600 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2601 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2604 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2605 * if the sRGB conversion is unsupported. */
2606 *params
= GL_LINEAR
;
2610 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2611 if ((ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2612 && ctx
->API
!= API_OPENGL_CORE
2613 && !_mesa_is_gles3(ctx
)) {
2614 goto invalid_pname_enum
;
2616 else if (att
->Type
== GL_NONE
) {
2617 _mesa_error(ctx
, err
,
2618 "glGetFramebufferAttachmentParameterivEXT(pname)");
2621 gl_format format
= att
->Renderbuffer
->Format
;
2623 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2626 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2627 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2628 * generate an INVALID_OPERATION error.
2630 if (_mesa_is_gles3(ctx
) && attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2631 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2632 "glGetFramebufferAttachmentParameteriv(cannot query "
2633 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2634 "GL_DEPTH_STENCIL_ATTACHMENT");
2638 if (format
== MESA_FORMAT_S8
) {
2642 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2643 /* depends on the attachment parameter */
2644 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2652 *params
= _mesa_get_format_datatype(format
);
2656 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2657 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2658 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2659 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2660 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2661 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2662 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2663 && !_mesa_is_gles3(ctx
)) {
2664 goto invalid_pname_enum
;
2666 else if (att
->Type
== GL_NONE
) {
2667 _mesa_error(ctx
, err
,
2668 "glGetFramebufferAttachmentParameterivEXT(pname)");
2670 else if (att
->Texture
) {
2671 const struct gl_texture_image
*texImage
=
2672 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2675 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2676 texImage
->TexFormat
);
2682 else if (att
->Renderbuffer
) {
2683 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2684 att
->Renderbuffer
->Format
);
2687 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2688 " invalid FBO attachment structure");
2692 goto invalid_pname_enum
;
2698 _mesa_error(ctx
, GL_INVALID_ENUM
,
2699 "glGetFramebufferAttachmentParameteriv(pname)");
2705 _mesa_GenerateMipmap(GLenum target
)
2707 struct gl_texture_image
*srcImage
;
2708 struct gl_texture_object
*texObj
;
2711 GET_CURRENT_CONTEXT(ctx
);
2713 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2717 error
= _mesa_is_gles(ctx
);
2723 error
= ctx
->API
== API_OPENGLES
;
2725 case GL_TEXTURE_CUBE_MAP
:
2726 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2728 case GL_TEXTURE_1D_ARRAY
:
2729 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2731 case GL_TEXTURE_2D_ARRAY
:
2732 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2733 || !ctx
->Extensions
.EXT_texture_array
;
2740 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2741 _mesa_lookup_enum_by_nr(target
));
2745 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2747 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2752 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2753 !_mesa_cube_complete(texObj
)) {
2754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2755 "glGenerateMipmap(incomplete cube map)");
2759 _mesa_lock_texture(ctx
, texObj
);
2761 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2763 _mesa_unlock_texture(ctx
, texObj
);
2764 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2765 "glGenerateMipmap(zero size base image)");
2769 if (_mesa_is_enum_format_integer(srcImage
->InternalFormat
) ||
2770 _mesa_is_depthstencil_format(srcImage
->InternalFormat
) ||
2771 _mesa_is_stencil_format(srcImage
->InternalFormat
)) {
2772 _mesa_unlock_texture(ctx
, texObj
);
2773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2774 "glGenerateMipmap(invalid internal format)");
2778 if (target
== GL_TEXTURE_CUBE_MAP
) {
2780 for (face
= 0; face
< 6; face
++)
2781 ctx
->Driver
.GenerateMipmap(ctx
,
2782 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2786 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2788 _mesa_unlock_texture(ctx
, texObj
);
2792 static const struct gl_renderbuffer_attachment
*
2793 find_attachment(const struct gl_framebuffer
*fb
,
2794 const struct gl_renderbuffer
*rb
)
2797 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2798 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2799 return &fb
->Attachment
[i
];
2806 * Helper function for checking if the datatypes of color buffers are
2807 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2809 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2810 * and any of the following conditions hold:
2811 * - The read buffer contains fixed-point or floating-point values and any
2812 * draw buffer contains neither fixed-point nor floating-point values.
2813 * - The read buffer contains unsigned integer values and any draw buffer
2814 * does not contain unsigned integer values.
2815 * - The read buffer contains signed integer values and any draw buffer
2816 * does not contain signed integer values."
2819 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2821 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2822 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2824 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2825 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2826 srcType
== GL_SIGNED_NORMALIZED
||
2827 srcType
== GL_FLOAT
);
2828 /* Boil any of those types down to GL_FLOAT */
2832 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2833 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2834 dstType
== GL_SIGNED_NORMALIZED
||
2835 dstType
== GL_FLOAT
);
2836 /* Boil any of those types down to GL_FLOAT */
2840 return srcType
== dstType
;
2845 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
2846 const struct gl_renderbuffer
*drawRb
)
2848 GLenum readFormat
, drawFormat
;
2850 /* The simple case where we know the backing Mesa formats are the same.
2852 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
2853 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
2857 /* The Mesa formats are different, so we must check whether the internal
2858 * formats are compatible.
2860 * Under some circumstances, the user may request e.g. two GL_RGBA8
2861 * textures and get two entirely different Mesa formats like RGBA8888 and
2862 * ARGB8888. Drivers behaving like that should be able to cope with
2863 * non-matching formats by themselves, because it's not the user's fault.
2865 * Blits between linear and sRGB formats are also allowed.
2867 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
2868 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
2869 readFormat
= _mesa_get_linear_internalformat(readFormat
);
2870 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
2872 if (readFormat
== drawFormat
) {
2881 * Blit rectangular region, optionally from one framebuffer to another.
2883 * Note, if the src buffer is multisampled and the dest is not, this is
2884 * when the samples must be resolved to a single color.
2887 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2888 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2889 GLbitfield mask
, GLenum filter
)
2891 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2892 GL_DEPTH_BUFFER_BIT
|
2893 GL_STENCIL_BUFFER_BIT
);
2894 const struct gl_framebuffer
*readFb
, *drawFb
;
2895 GET_CURRENT_CONTEXT(ctx
);
2897 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2899 if (MESA_VERBOSE
& VERBOSE_API
)
2901 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2902 srcX0
, srcY0
, srcX1
, srcY1
,
2903 dstX0
, dstY0
, dstX1
, dstY1
,
2904 mask
, _mesa_lookup_enum_by_nr(filter
));
2906 if (ctx
->NewState
) {
2907 _mesa_update_state(ctx
);
2910 readFb
= ctx
->ReadBuffer
;
2911 drawFb
= ctx
->DrawBuffer
;
2913 if (!readFb
|| !drawFb
) {
2914 /* This will normally never happen but someday we may want to
2915 * support MakeCurrent() with no drawables.
2920 /* check for complete framebuffers */
2921 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2922 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2923 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2924 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2928 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2929 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2933 if (mask
& ~legalMaskBits
) {
2934 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2938 /* depth/stencil must be blitted with nearest filtering */
2939 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2940 && filter
!= GL_NEAREST
) {
2941 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2942 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2946 /* get color read/draw renderbuffers */
2947 if (mask
& GL_COLOR_BUFFER_BIT
) {
2948 const GLuint numColorDrawBuffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
2949 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
2950 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
2953 /* From the EXT_framebuffer_object spec:
2955 * "If a buffer is specified in <mask> and does not exist in both
2956 * the read and draw framebuffers, the corresponding bit is silently
2959 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
2960 mask
&= ~GL_COLOR_BUFFER_BIT
;
2963 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
2964 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
2968 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
2969 * ES 3.0.1 spec says:
2971 * "If the source and destination buffers are identical, an
2972 * INVALID_OPERATION error is generated. Different mipmap
2973 * levels of a texture, different layers of a three-
2974 * dimensional texture or two-dimensional array texture, and
2975 * different faces of a cube map texture do not constitute
2976 * identical buffers."
2978 if (_mesa_is_gles3(ctx
) && (colorDrawRb
== colorReadRb
)) {
2979 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2980 "glBlitFramebuffer(source and destination color "
2981 "buffer cannot be the same)");
2985 if (!compatible_color_datatypes(colorReadRb
->Format
,
2986 colorDrawRb
->Format
)) {
2987 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2988 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2991 /* extra checks for multisample copies... */
2992 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2993 /* color formats must match */
2994 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2996 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
3001 if (filter
== GL_LINEAR
) {
3002 /* 3.1 spec, page 199:
3003 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
3004 * if filter is LINEAR and read buffer contains integer data."
3006 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
3007 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
3008 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3009 "glBlitFramebufferEXT(integer color type)");
3016 if (mask
& GL_STENCIL_BUFFER_BIT
) {
3017 struct gl_renderbuffer
*readRb
=
3018 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
3019 struct gl_renderbuffer
*drawRb
=
3020 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
3022 /* From the EXT_framebuffer_object spec:
3024 * "If a buffer is specified in <mask> and does not exist in both
3025 * the read and draw framebuffers, the corresponding bit is silently
3028 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
3029 mask
&= ~GL_STENCIL_BUFFER_BIT
;
3032 int read_z_bits
, draw_z_bits
;
3034 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
3035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3036 "glBlitFramebuffer(source and destination stencil "
3037 "buffer cannot be the same)");
3041 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
3042 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
3043 /* There is no need to check the stencil datatype here, because
3044 * there is only one: GL_UNSIGNED_INT.
3046 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3047 "glBlitFramebuffer(stencil attachment format mismatch)");
3051 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
3052 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
3054 /* If both buffers also have depth data, the depth formats must match
3055 * as well. If one doesn't have depth, it's not blitted, so we should
3056 * ignore the depth format check.
3058 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
3059 (read_z_bits
!= draw_z_bits
||
3060 _mesa_get_format_datatype(readRb
->Format
) !=
3061 _mesa_get_format_datatype(drawRb
->Format
))) {
3063 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
3064 "(stencil attachment depth format mismatch)");
3070 if (mask
& GL_DEPTH_BUFFER_BIT
) {
3071 struct gl_renderbuffer
*readRb
=
3072 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3073 struct gl_renderbuffer
*drawRb
=
3074 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3076 /* From the EXT_framebuffer_object spec:
3078 * "If a buffer is specified in <mask> and does not exist in both
3079 * the read and draw framebuffers, the corresponding bit is silently
3082 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
3083 mask
&= ~GL_DEPTH_BUFFER_BIT
;
3086 int read_s_bit
, draw_s_bit
;
3088 if (_mesa_is_gles3(ctx
) && (drawRb
== readRb
)) {
3089 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3090 "glBlitFramebuffer(source and destination depth "
3091 "buffer cannot be the same)");
3095 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
3096 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
3097 (_mesa_get_format_datatype(readRb
->Format
) !=
3098 _mesa_get_format_datatype(drawRb
->Format
))) {
3099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3100 "glBlitFramebuffer(depth attachment format mismatch)");
3104 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
3105 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
3107 /* If both buffers also have stencil data, the stencil formats must
3108 * match as well. If one doesn't have stencil, it's not blitted, so
3109 * we should ignore the stencil format check.
3111 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
3112 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
3113 "(depth attachment stencil bits mismatch)");
3120 if (_mesa_is_gles3(ctx
)) {
3121 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3124 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
3125 * an INVALID_OPERATION error is generated."
3127 if (drawFb
->Visual
.samples
> 0) {
3128 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3129 "glBlitFramebuffer(destination samples must be 0)");
3133 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3136 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
3137 * no copy is performed and an INVALID_OPERATION error is generated
3138 * if the formats of the read and draw framebuffers are not
3139 * identical or if the source and destination rectangles are not
3140 * defined with the same (X0, Y0) and (X1, Y1) bounds."
3142 * The format check was made above because desktop OpenGL has the same
3145 if (readFb
->Visual
.samples
> 0
3146 && (srcX0
!= dstX0
|| srcY0
!= dstY0
3147 || srcX1
!= dstX1
|| srcY1
!= dstY1
)) {
3148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3149 "glBlitFramebuffer(bad src/dst multisample region)");
3153 if (readFb
->Visual
.samples
> 0 &&
3154 drawFb
->Visual
.samples
> 0 &&
3155 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
3156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3157 "glBlitFramebufferEXT(mismatched samples)");
3161 /* extra checks for multisample copies... */
3162 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
3163 /* src and dest region sizes must be the same */
3164 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
3165 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
3166 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3167 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3173 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
3174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
3180 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
3181 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
3184 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3186 srcX0
, srcY0
, srcX1
, srcY1
,
3187 dstX0
, dstY0
, dstX1
, dstY1
,
3190 const struct gl_renderbuffer_attachment
*att
;
3192 att
= find_attachment(readFb
, colorReadRb
);
3193 printf(" Src FBO %u RB %u (%dx%d) ",
3194 readFb
->Name
, colorReadRb
->Name
,
3195 colorReadRb
->Width
, colorReadRb
->Height
);
3196 if (att
&& att
->Texture
) {
3197 printf("Tex %u tgt 0x%x level %u face %u",
3199 att
->Texture
->Target
,
3205 /* Print all active color render buffers */
3206 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
3207 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
3211 att
= find_attachment(drawFb
, colorDrawRb
);
3212 printf(" Dst FBO %u RB %u (%dx%d) ",
3213 drawFb
->Name
, colorDrawRb
->Name
,
3214 colorDrawRb
->Width
, colorDrawRb
->Height
);
3215 if (att
&& att
->Texture
) {
3216 printf("Tex %u tgt 0x%x level %u face %u",
3218 att
->Texture
->Target
,
3228 (srcX1
- srcX0
) == 0 || (srcY1
- srcY0
) == 0 ||
3229 (dstX1
- dstX0
) == 0 || (dstY1
- dstY0
) == 0) {
3233 ASSERT(ctx
->Driver
.BlitFramebuffer
);
3234 ctx
->Driver
.BlitFramebuffer(ctx
,
3235 srcX0
, srcY0
, srcX1
, srcY1
,
3236 dstX0
, dstY0
, dstX1
, dstY1
,
3242 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3243 const GLenum
*attachments
, GLint x
, GLint y
,
3244 GLsizei width
, GLsizei height
, const char *name
)
3247 struct gl_framebuffer
*fb
;
3248 GET_CURRENT_CONTEXT(ctx
);
3250 fb
= get_framebuffer_target(ctx
, target
);
3252 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3256 if (numAttachments
< 0) {
3257 _mesa_error(ctx
, GL_INVALID_VALUE
,
3258 "%s(numAttachments < 0)", name
);
3262 /* The GL_ARB_invalidate_subdata spec says:
3264 * "If an attachment is specified that does not exist in the
3265 * framebuffer bound to <target>, it is ignored."
3269 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3270 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3271 * INVALID_OPERATION is generated."
3273 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3274 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3275 * set of retrictions).
3277 for (i
= 0; i
< numAttachments
; i
++) {
3278 if (_mesa_is_winsys_fbo(fb
)) {
3279 switch (attachments
[i
]) {
3285 /* Accumulation buffers and auxilary buffers were removed in
3286 * OpenGL 3.1, and they never existed in OpenGL ES.
3288 if (ctx
->API
!= API_OPENGL_COMPAT
)
3298 case GL_FRONT_RIGHT
:
3299 if (!_mesa_is_desktop_gl(ctx
))
3306 switch (attachments
[i
]) {
3307 case GL_DEPTH_ATTACHMENT
:
3308 case GL_STENCIL_ATTACHMENT
:
3310 case GL_COLOR_ATTACHMENT0
:
3311 case GL_COLOR_ATTACHMENT1
:
3312 case GL_COLOR_ATTACHMENT2
:
3313 case GL_COLOR_ATTACHMENT3
:
3314 case GL_COLOR_ATTACHMENT4
:
3315 case GL_COLOR_ATTACHMENT5
:
3316 case GL_COLOR_ATTACHMENT6
:
3317 case GL_COLOR_ATTACHMENT7
:
3318 case GL_COLOR_ATTACHMENT8
:
3319 case GL_COLOR_ATTACHMENT9
:
3320 case GL_COLOR_ATTACHMENT10
:
3321 case GL_COLOR_ATTACHMENT11
:
3322 case GL_COLOR_ATTACHMENT12
:
3323 case GL_COLOR_ATTACHMENT13
:
3324 case GL_COLOR_ATTACHMENT14
:
3325 case GL_COLOR_ATTACHMENT15
: {
3326 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3327 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3328 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3329 "%s(attachment >= max. color attachments)", name
);
3339 /* We don't actually do anything for this yet. Just return after
3340 * validating the parameters and generating the required errors.
3345 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3350 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3351 const GLenum
*attachments
, GLint x
, GLint y
,
3352 GLsizei width
, GLsizei height
)
3354 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3355 x
, y
, width
, height
,
3356 "glInvalidateSubFramebuffer");
3360 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3361 const GLenum
*attachments
)
3363 /* The GL_ARB_invalidate_subdata spec says:
3367 * void InvalidateFramebuffer(enum target,
3368 * sizei numAttachments,
3369 * const enum *attachments);
3371 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3372 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3373 * <MAX_VIEWPORT_DIMS[1]> respectively."
3375 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3376 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3377 "glInvalidateFramebuffer");
3381 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3382 const GLenum
*attachments
)
3384 struct gl_framebuffer
*fb
;
3387 GET_CURRENT_CONTEXT(ctx
);
3389 fb
= get_framebuffer_target(ctx
, target
);
3391 _mesa_error(ctx
, GL_INVALID_ENUM
,
3392 "glDiscardFramebufferEXT(target %s)",
3393 _mesa_lookup_enum_by_nr(target
));
3397 if (numAttachments
< 0) {
3398 _mesa_error(ctx
, GL_INVALID_VALUE
,
3399 "glDiscardFramebufferEXT(numAttachments < 0)");
3403 for (i
= 0; i
< numAttachments
; i
++) {
3404 switch (attachments
[i
]) {
3408 if (_mesa_is_user_fbo(fb
))
3411 case GL_COLOR_ATTACHMENT0
:
3412 case GL_DEPTH_ATTACHMENT
:
3413 case GL_STENCIL_ATTACHMENT
:
3414 if (_mesa_is_winsys_fbo(fb
))
3422 if (ctx
->Driver
.DiscardFramebuffer
)
3423 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3428 _mesa_error(ctx
, GL_INVALID_ENUM
,
3429 "glDiscardFramebufferEXT(attachment %s)",
3430 _mesa_lookup_enum_by_nr(attachments
[i
]));