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;
751 assert(_mesa_is_user_fbo(fb
));
753 /* we're changing framebuffer fields here */
754 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
760 /* Start at -2 to more easily loop over all attachment points.
765 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
766 struct gl_renderbuffer_attachment
*att
;
771 * XXX for ARB_fbo, only check color buffers that are named by
772 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
775 /* check for attachment completeness
778 att
= &fb
->Attachment
[BUFFER_DEPTH
];
779 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
780 if (!att
->Complete
) {
781 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
782 fbo_incomplete("depth attachment incomplete", -1);
787 att
= &fb
->Attachment
[BUFFER_STENCIL
];
788 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
789 if (!att
->Complete
) {
790 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
791 fbo_incomplete("stencil attachment incomplete", -1);
796 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
797 test_attachment_completeness(ctx
, GL_COLOR
, att
);
798 if (!att
->Complete
) {
799 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
800 fbo_incomplete("color attachment incomplete", i
);
805 /* get width, height, format of the renderbuffer/texture
807 if (att
->Type
== GL_TEXTURE
) {
808 const struct gl_texture_image
*texImg
=
809 _mesa_get_attachment_teximage(att
);
810 minWidth
= MIN2(minWidth
, texImg
->Width
);
811 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
812 minHeight
= MIN2(minHeight
, texImg
->Height
);
813 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
814 f
= texImg
->_BaseFormat
;
815 attFormat
= texImg
->TexFormat
;
817 if (!is_format_color_renderable(ctx
, attFormat
, texImg
->InternalFormat
) &&
818 !is_legal_depth_format(ctx
, f
)) {
819 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
820 fbo_incomplete("texture attachment incomplete", -1);
824 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
825 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
826 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
827 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
828 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
829 f
= att
->Renderbuffer
->InternalFormat
;
830 attFormat
= att
->Renderbuffer
->Format
;
834 assert(att
->Type
== GL_NONE
);
838 if (att
->Renderbuffer
&& numSamples
< 0) {
840 numSamples
= att
->Renderbuffer
->NumSamples
;
843 /* check if integer color */
844 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
846 /* Error-check width, height, format, samples
848 if (numImages
== 1) {
849 /* save format, num samples */
855 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
856 /* check that width, height, format are same */
857 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
858 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
859 fbo_incomplete("width or height mismatch", -1);
862 /* check that all color buffers are the same format */
863 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
864 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
865 fbo_incomplete("format mismatch", -1);
869 if (att
->Renderbuffer
&&
870 att
->Renderbuffer
->NumSamples
!= numSamples
) {
871 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
872 fbo_incomplete("inconsistant number of samples", i
);
877 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
879 if (att
->Type
== GL_RENDERBUFFER
&&
880 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
881 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
882 fbo_incomplete("unsupported renderbuffer format", i
);
887 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
888 /* Check that all DrawBuffers are present */
889 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
890 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
891 const struct gl_renderbuffer_attachment
*att
892 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
894 if (att
->Type
== GL_NONE
) {
895 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
896 fbo_incomplete("missing drawbuffer", j
);
902 /* Check that the ReadBuffer is present */
903 if (fb
->ColorReadBuffer
!= GL_NONE
) {
904 const struct gl_renderbuffer_attachment
*att
905 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
907 if (att
->Type
== GL_NONE
) {
908 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
909 fbo_incomplete("missing readbuffer", -1);
915 if (numImages
== 0) {
916 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
917 fbo_incomplete("no attachments", -1);
921 /* Provisionally set status = COMPLETE ... */
922 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
924 /* ... but the driver may say the FB is incomplete.
925 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
928 if (ctx
->Driver
.ValidateFramebuffer
) {
929 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
930 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
931 fbo_incomplete("driver marked FBO as incomplete", -1);
935 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
937 * Note that if ARB_framebuffer_object is supported and the attached
938 * renderbuffers/textures are different sizes, the framebuffer
939 * width/height will be set to the smallest width/height.
941 fb
->Width
= minWidth
;
942 fb
->Height
= minHeight
;
944 /* finally, update the visual info for the framebuffer */
945 _mesa_update_framebuffer_visual(ctx
, fb
);
951 _mesa_IsRenderbuffer(GLuint renderbuffer
)
953 GET_CURRENT_CONTEXT(ctx
);
954 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
956 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
957 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
965 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
967 struct gl_renderbuffer
*newRb
;
968 GET_CURRENT_CONTEXT(ctx
);
970 ASSERT_OUTSIDE_BEGIN_END(ctx
);
972 if (target
!= GL_RENDERBUFFER_EXT
) {
973 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
977 /* No need to flush here since the render buffer binding has no
978 * effect on rendering state.
982 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
983 if (newRb
== &DummyRenderbuffer
) {
984 /* ID was reserved, but no real renderbuffer object made yet */
988 && _mesa_is_desktop_gl(ctx
)
989 && ctx
->Extensions
.ARB_framebuffer_object
) {
990 /* All RB IDs must be Gen'd */
991 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
996 /* create new renderbuffer object */
997 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
999 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
1002 ASSERT(newRb
->AllocStorage
);
1003 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1004 newRb
->RefCount
= 1; /* referenced by hash table */
1011 ASSERT(newRb
!= &DummyRenderbuffer
);
1013 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1018 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1020 * This is used when a renderbuffer object is deleted.
1021 * The spec calls for unbinding.
1024 detach_renderbuffer(struct gl_context
*ctx
,
1025 struct gl_framebuffer
*fb
,
1026 struct gl_renderbuffer
*rb
)
1029 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1030 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
1031 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
1034 invalidate_framebuffer(fb
);
1039 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1042 GET_CURRENT_CONTEXT(ctx
);
1044 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1045 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1047 for (i
= 0; i
< n
; i
++) {
1048 if (renderbuffers
[i
] > 0) {
1049 struct gl_renderbuffer
*rb
;
1050 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1052 /* check if deleting currently bound renderbuffer object */
1053 if (rb
== ctx
->CurrentRenderbuffer
) {
1055 ASSERT(rb
->RefCount
>= 2);
1056 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1059 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1060 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1062 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1063 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1064 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1067 /* Remove from hash table immediately, to free the ID.
1068 * But the object will not be freed until it's no longer
1069 * referenced anywhere else.
1071 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1073 if (rb
!= &DummyRenderbuffer
) {
1074 /* no longer referenced by hash table */
1075 _mesa_reference_renderbuffer(&rb
, NULL
);
1084 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1086 GET_CURRENT_CONTEXT(ctx
);
1090 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1093 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1100 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1102 for (i
= 0; i
< n
; i
++) {
1103 GLuint name
= first
+ i
;
1104 renderbuffers
[i
] = name
;
1105 /* insert dummy placeholder into hash table */
1106 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1107 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1108 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1114 * Given an internal format token for a render buffer, return the
1115 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1116 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1117 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1119 * This is similar to _mesa_base_tex_format() but the set of valid
1120 * internal formats is different.
1122 * Note that even if a format is determined to be legal here, validation
1123 * of the FBO may fail if the format is not supported by the driver/GPU.
1125 * \param internalFormat as passed to glRenderbufferStorage()
1126 * \return the base internal format, or 0 if internalFormat is illegal
1129 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1132 * Notes: some formats such as alpha, luminance, etc. were added
1133 * with GL_ARB_framebuffer_object.
1135 switch (internalFormat
) {
1141 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1146 case GL_LUMINANCE12
:
1147 case GL_LUMINANCE16
:
1148 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1150 case GL_LUMINANCE_ALPHA
:
1151 case GL_LUMINANCE4_ALPHA4
:
1152 case GL_LUMINANCE6_ALPHA2
:
1153 case GL_LUMINANCE8_ALPHA8
:
1154 case GL_LUMINANCE12_ALPHA4
:
1155 case GL_LUMINANCE12_ALPHA12
:
1156 case GL_LUMINANCE16_ALPHA16
:
1157 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1158 ? GL_LUMINANCE_ALPHA
: 0;
1162 case GL_INTENSITY12
:
1163 case GL_INTENSITY16
:
1164 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1175 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1177 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1186 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1188 case GL_SRGB8_ALPHA8_EXT
:
1189 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1190 case GL_STENCIL_INDEX
:
1191 case GL_STENCIL_INDEX1_EXT
:
1192 case GL_STENCIL_INDEX4_EXT
:
1193 case GL_STENCIL_INDEX16_EXT
:
1194 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1195 * OpenGL ES, but Mesa does not currently support them.
1197 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1198 case GL_STENCIL_INDEX8_EXT
:
1199 return GL_STENCIL_INDEX
;
1200 case GL_DEPTH_COMPONENT
:
1201 case GL_DEPTH_COMPONENT32
:
1202 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1203 case GL_DEPTH_COMPONENT16
:
1204 case GL_DEPTH_COMPONENT24
:
1205 return GL_DEPTH_COMPONENT
;
1206 case GL_DEPTH_STENCIL_EXT
:
1207 return _mesa_is_desktop_gl(ctx
)
1208 && ctx
->Extensions
.EXT_packed_depth_stencil
1209 ? GL_DEPTH_STENCIL_EXT
: 0;
1210 case GL_DEPTH24_STENCIL8_EXT
:
1211 return ctx
->Extensions
.EXT_packed_depth_stencil
1212 ? GL_DEPTH_STENCIL_EXT
: 0;
1213 case GL_DEPTH_COMPONENT32F
:
1214 return ctx
->Version
>= 30
1215 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1216 ? GL_DEPTH_COMPONENT
: 0;
1217 case GL_DEPTH32F_STENCIL8
:
1218 return ctx
->Version
>= 30
1219 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1220 ? GL_DEPTH_STENCIL
: 0;
1223 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1226 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1230 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1233 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1235 /* signed normalized texture formats */
1239 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1244 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1248 case GL_RGB16_SNORM
:
1249 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1252 case GL_RGBA8_SNORM
:
1253 case GL_RGBA16_SNORM
:
1254 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1256 case GL_ALPHA_SNORM
:
1257 case GL_ALPHA8_SNORM
:
1258 case GL_ALPHA16_SNORM
:
1259 return ctx
->API
== API_OPENGL_COMPAT
&&
1260 ctx
->Extensions
.EXT_texture_snorm
&&
1261 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1264 return ((_mesa_is_desktop_gl(ctx
) &&
1265 ctx
->Extensions
.ARB_texture_rg
&&
1266 ctx
->Extensions
.ARB_texture_float
) ||
1267 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1271 return ((_mesa_is_desktop_gl(ctx
) &&
1272 ctx
->Extensions
.ARB_texture_rg
&&
1273 ctx
->Extensions
.ARB_texture_float
) ||
1274 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1278 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1282 return ((_mesa_is_desktop_gl(ctx
) &&
1283 ctx
->Extensions
.ARB_texture_float
) ||
1284 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1286 case GL_ALPHA16F_ARB
:
1287 case GL_ALPHA32F_ARB
:
1288 return ctx
->API
== API_OPENGL_COMPAT
&&
1289 ctx
->Extensions
.ARB_texture_float
&&
1290 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1291 case GL_LUMINANCE16F_ARB
:
1292 case GL_LUMINANCE32F_ARB
:
1293 return ctx
->API
== API_OPENGL_COMPAT
&&
1294 ctx
->Extensions
.ARB_texture_float
&&
1295 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1296 case GL_LUMINANCE_ALPHA16F_ARB
:
1297 case GL_LUMINANCE_ALPHA32F_ARB
:
1298 return ctx
->API
== API_OPENGL_COMPAT
&&
1299 ctx
->Extensions
.ARB_texture_float
&&
1300 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1301 case GL_INTENSITY16F_ARB
:
1302 case GL_INTENSITY32F_ARB
:
1303 return ctx
->API
== API_OPENGL_COMPAT
&&
1304 ctx
->Extensions
.ARB_texture_float
&&
1305 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1307 return (_mesa_is_desktop_gl(ctx
)
1308 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1310 case GL_R11F_G11F_B10F
:
1311 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1312 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1315 case GL_RGBA8UI_EXT
:
1316 case GL_RGBA16UI_EXT
:
1317 case GL_RGBA32UI_EXT
:
1319 case GL_RGBA16I_EXT
:
1320 case GL_RGBA32I_EXT
:
1321 return ctx
->Version
>= 30
1322 || (_mesa_is_desktop_gl(ctx
) &&
1323 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1326 case GL_RGB16UI_EXT
:
1327 case GL_RGB32UI_EXT
:
1331 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1339 return ctx
->Version
>= 30
1340 || (_mesa_is_desktop_gl(ctx
) &&
1341 ctx
->Extensions
.ARB_texture_rg
&&
1342 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1350 return ctx
->Version
>= 30
1351 || (_mesa_is_desktop_gl(ctx
) &&
1352 ctx
->Extensions
.ARB_texture_rg
&&
1353 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1355 case GL_INTENSITY8I_EXT
:
1356 case GL_INTENSITY8UI_EXT
:
1357 case GL_INTENSITY16I_EXT
:
1358 case GL_INTENSITY16UI_EXT
:
1359 case GL_INTENSITY32I_EXT
:
1360 case GL_INTENSITY32UI_EXT
:
1361 return ctx
->API
== API_OPENGL_COMPAT
&&
1362 ctx
->Extensions
.EXT_texture_integer
&&
1363 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1365 case GL_LUMINANCE8I_EXT
:
1366 case GL_LUMINANCE8UI_EXT
:
1367 case GL_LUMINANCE16I_EXT
:
1368 case GL_LUMINANCE16UI_EXT
:
1369 case GL_LUMINANCE32I_EXT
:
1370 case GL_LUMINANCE32UI_EXT
:
1371 return ctx
->API
== API_OPENGL_COMPAT
&&
1372 ctx
->Extensions
.EXT_texture_integer
&&
1373 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1375 case GL_LUMINANCE_ALPHA8I_EXT
:
1376 case GL_LUMINANCE_ALPHA8UI_EXT
:
1377 case GL_LUMINANCE_ALPHA16I_EXT
:
1378 case GL_LUMINANCE_ALPHA16UI_EXT
:
1379 case GL_LUMINANCE_ALPHA32I_EXT
:
1380 case GL_LUMINANCE_ALPHA32UI_EXT
:
1381 return ctx
->API
== API_OPENGL_COMPAT
&&
1382 ctx
->Extensions
.EXT_texture_integer
&&
1383 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1385 case GL_ALPHA8I_EXT
:
1386 case GL_ALPHA8UI_EXT
:
1387 case GL_ALPHA16I_EXT
:
1388 case GL_ALPHA16UI_EXT
:
1389 case GL_ALPHA32I_EXT
:
1390 case GL_ALPHA32UI_EXT
:
1391 return ctx
->API
== API_OPENGL_COMPAT
&&
1392 ctx
->Extensions
.EXT_texture_integer
&&
1393 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1396 return (_mesa_is_desktop_gl(ctx
) &&
1397 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1398 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1401 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1410 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1413 invalidate_rb(GLuint key
, void *data
, void *userData
)
1415 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1416 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1418 /* If this is a user-created FBO */
1419 if (_mesa_is_user_fbo(fb
)) {
1421 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1422 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1423 if (att
->Type
== GL_RENDERBUFFER
&&
1424 att
->Renderbuffer
== rb
) {
1425 /* Mark fb status as indeterminate to force re-validation */
1434 /** sentinal value, see below */
1435 #define NO_SAMPLES 1000
1439 * Helper function used by _mesa_RenderbufferStorage() and
1440 * _mesa_RenderbufferStorageMultisample().
1441 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1444 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1445 GLsizei width
, GLsizei height
, GLsizei samples
)
1447 const char *func
= samples
== NO_SAMPLES
?
1448 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1449 struct gl_renderbuffer
*rb
;
1451 GET_CURRENT_CONTEXT(ctx
);
1453 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1455 if (MESA_VERBOSE
& VERBOSE_API
) {
1456 if (samples
== NO_SAMPLES
)
1457 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1459 _mesa_lookup_enum_by_nr(target
),
1460 _mesa_lookup_enum_by_nr(internalFormat
),
1463 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1465 _mesa_lookup_enum_by_nr(target
),
1466 _mesa_lookup_enum_by_nr(internalFormat
),
1467 width
, height
, samples
);
1470 if (target
!= GL_RENDERBUFFER_EXT
) {
1471 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1475 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1476 if (baseFormat
== 0) {
1477 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1478 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1482 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1483 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1487 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1488 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1492 if (samples
== NO_SAMPLES
) {
1493 /* NumSamples == 0 indicates non-multisampling */
1496 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1497 /* note: driver may choose to use more samples than what's requested */
1498 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1502 rb
= ctx
->CurrentRenderbuffer
;
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1508 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1510 if (rb
->InternalFormat
== internalFormat
&&
1511 rb
->Width
== (GLuint
) width
&&
1512 rb
->Height
== (GLuint
) height
&&
1513 rb
->NumSamples
== samples
) {
1514 /* no change in allocation needed */
1518 /* These MUST get set by the AllocStorage func */
1519 rb
->Format
= MESA_FORMAT_NONE
;
1520 rb
->NumSamples
= samples
;
1522 /* Now allocate the storage */
1523 ASSERT(rb
->AllocStorage
);
1524 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1525 /* No error - check/set fields now */
1526 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1527 assert(rb
->Width
== (GLuint
) width
);
1528 assert(rb
->Height
== (GLuint
) height
);
1529 rb
->InternalFormat
= internalFormat
;
1530 rb
->_BaseFormat
= baseFormat
;
1531 assert(rb
->_BaseFormat
!= 0);
1534 /* Probably ran out of memory - clear the fields */
1537 rb
->Format
= MESA_FORMAT_NONE
;
1538 rb
->InternalFormat
= GL_NONE
;
1539 rb
->_BaseFormat
= GL_NONE
;
1543 /* Invalidate the framebuffers the renderbuffer is attached in. */
1544 if (rb
->AttachedAnytime
) {
1545 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1551 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1553 struct gl_renderbuffer
*rb
;
1554 GET_CURRENT_CONTEXT(ctx
);
1555 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1557 if (!ctx
->Extensions
.OES_EGL_image
) {
1558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1559 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1563 if (target
!= GL_RENDERBUFFER
) {
1564 _mesa_error(ctx
, GL_INVALID_ENUM
,
1565 "EGLImageTargetRenderbufferStorageOES");
1569 rb
= ctx
->CurrentRenderbuffer
;
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "EGLImageTargetRenderbufferStorageOES");
1576 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1578 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1583 * Helper function for _mesa_GetRenderbufferParameteriv() and
1584 * _mesa_GetFramebufferAttachmentParameteriv()
1585 * We have to be careful to respect the base format. For example, if a
1586 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1587 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1588 * we need to return zero.
1591 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1593 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1594 return _mesa_get_format_bits(format
, pname
);
1602 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1603 GLsizei width
, GLsizei height
)
1605 /* GL_ARB_fbo says calling this function is equivalent to calling
1606 * glRenderbufferStorageMultisample() with samples=0. We pass in
1607 * a token value here just for error reporting purposes.
1609 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1614 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1615 GLenum internalFormat
,
1616 GLsizei width
, GLsizei height
)
1618 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1623 * OpenGL ES version of glRenderBufferStorage.
1626 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1627 GLsizei width
, GLsizei height
)
1629 switch (internalFormat
) {
1631 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1632 /* choose a closest format */
1633 internalFormat
= GL_RGB5
;
1639 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1644 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1646 struct gl_renderbuffer
*rb
;
1647 GET_CURRENT_CONTEXT(ctx
);
1649 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1651 if (target
!= GL_RENDERBUFFER_EXT
) {
1652 _mesa_error(ctx
, GL_INVALID_ENUM
,
1653 "glGetRenderbufferParameterivEXT(target)");
1657 rb
= ctx
->CurrentRenderbuffer
;
1659 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1660 "glGetRenderbufferParameterivEXT");
1664 /* No need to flush here since we're just quering state which is
1665 * not effected by rendering.
1669 case GL_RENDERBUFFER_WIDTH_EXT
:
1670 *params
= rb
->Width
;
1672 case GL_RENDERBUFFER_HEIGHT_EXT
:
1673 *params
= rb
->Height
;
1675 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1676 *params
= rb
->InternalFormat
;
1678 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1679 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1680 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1681 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1682 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1683 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1684 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1686 case GL_RENDERBUFFER_SAMPLES
:
1687 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1688 || _mesa_is_gles3(ctx
)) {
1689 *params
= rb
->NumSamples
;
1694 _mesa_error(ctx
, GL_INVALID_ENUM
,
1695 "glGetRenderbufferParameterivEXT(target)");
1701 GLboolean GLAPIENTRY
1702 _mesa_IsFramebuffer(GLuint framebuffer
)
1704 GET_CURRENT_CONTEXT(ctx
);
1705 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1707 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1708 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1716 * Check if any of the attachments of the given framebuffer are textures
1717 * (render to texture). Call ctx->Driver.RenderTexture() for such
1721 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1724 ASSERT(ctx
->Driver
.RenderTexture
);
1726 if (_mesa_is_winsys_fbo(fb
))
1727 return; /* can't render to texture with winsys framebuffers */
1729 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1730 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1731 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1732 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1739 * Examine all the framebuffer's attachments to see if any are textures.
1740 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1741 * notify the device driver that the texture image may have changed.
1744 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1746 if (_mesa_is_winsys_fbo(fb
))
1747 return; /* can't render to texture with winsys framebuffers */
1749 if (ctx
->Driver
.FinishRenderTexture
) {
1751 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1752 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1753 if (att
->Texture
&& att
->Renderbuffer
) {
1754 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1762 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
1764 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1765 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1766 GLboolean bindReadBuf
, bindDrawBuf
;
1767 GET_CURRENT_CONTEXT(ctx
);
1770 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1771 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1772 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1776 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1778 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glBindFramebufferEXT(unsupported)");
1785 case GL_DRAW_FRAMEBUFFER_EXT
:
1786 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1787 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1790 bindDrawBuf
= GL_TRUE
;
1791 bindReadBuf
= GL_FALSE
;
1793 case GL_READ_FRAMEBUFFER_EXT
:
1794 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1795 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1798 bindDrawBuf
= GL_FALSE
;
1799 bindReadBuf
= GL_TRUE
;
1801 case GL_FRAMEBUFFER_EXT
:
1802 bindDrawBuf
= GL_TRUE
;
1803 bindReadBuf
= GL_TRUE
;
1806 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1811 /* Binding a user-created framebuffer object */
1812 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1813 if (newDrawFb
== &DummyFramebuffer
) {
1814 /* ID was reserved, but no real framebuffer object made yet */
1818 && _mesa_is_desktop_gl(ctx
)
1819 && ctx
->Extensions
.ARB_framebuffer_object
) {
1820 /* All FBO IDs must be Gen'd */
1821 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1826 /* create new framebuffer object */
1827 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1829 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1832 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1834 newReadFb
= newDrawFb
;
1837 /* Binding the window system framebuffer (which was originally set
1838 * with MakeCurrent).
1840 newDrawFb
= ctx
->WinSysDrawBuffer
;
1841 newReadFb
= ctx
->WinSysReadBuffer
;
1845 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1847 /* save pointers to current/old framebuffers */
1848 oldDrawFb
= ctx
->DrawBuffer
;
1849 oldReadFb
= ctx
->ReadBuffer
;
1851 /* check if really changing bindings */
1852 if (oldDrawFb
== newDrawFb
)
1853 bindDrawBuf
= GL_FALSE
;
1854 if (oldReadFb
== newReadFb
)
1855 bindReadBuf
= GL_FALSE
;
1858 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1860 * We also check if we're beginning and/or ending render-to-texture.
1861 * When a framebuffer with texture attachments is unbound, call
1862 * ctx->Driver.FinishRenderTexture().
1863 * When a framebuffer with texture attachments is bound, call
1864 * ctx->Driver.RenderTexture().
1866 * Note that if the ReadBuffer has texture attachments we don't consider
1867 * that a render-to-texture case.
1870 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1872 /* check if old readbuffer was render-to-texture */
1873 check_end_texture_render(ctx
, oldReadFb
);
1875 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1879 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1881 /* check if old framebuffer had any texture attachments */
1883 check_end_texture_render(ctx
, oldDrawFb
);
1885 /* check if newly bound framebuffer has any texture attachments */
1886 check_begin_texture_render(ctx
, newDrawFb
);
1888 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1891 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1892 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1898 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
1901 GET_CURRENT_CONTEXT(ctx
);
1903 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1904 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1906 for (i
= 0; i
< n
; i
++) {
1907 if (framebuffers
[i
] > 0) {
1908 struct gl_framebuffer
*fb
;
1909 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1911 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1913 /* check if deleting currently bound framebuffer object */
1914 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1915 /* separate draw/read binding points */
1916 if (fb
== ctx
->DrawBuffer
) {
1918 ASSERT(fb
->RefCount
>= 2);
1919 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1921 if (fb
== ctx
->ReadBuffer
) {
1923 ASSERT(fb
->RefCount
>= 2);
1924 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, 0);
1928 /* only one binding point for read/draw buffers */
1929 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1931 ASSERT(fb
->RefCount
>= 2);
1932 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
1936 /* remove from hash table immediately, to free the ID */
1937 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1939 if (fb
!= &DummyFramebuffer
) {
1940 /* But the object will not be freed until it's no longer
1941 * bound in any context.
1943 _mesa_reference_framebuffer(&fb
, NULL
);
1952 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
1954 GET_CURRENT_CONTEXT(ctx
);
1958 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1961 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1968 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1970 for (i
= 0; i
< n
; i
++) {
1971 GLuint name
= first
+ i
;
1972 framebuffers
[i
] = name
;
1973 /* insert dummy placeholder into hash table */
1974 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1975 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1976 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1983 _mesa_CheckFramebufferStatus(GLenum target
)
1985 struct gl_framebuffer
*buffer
;
1986 GET_CURRENT_CONTEXT(ctx
);
1988 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1990 if (MESA_VERBOSE
& VERBOSE_API
)
1991 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
1992 _mesa_lookup_enum_by_nr(target
));
1994 buffer
= get_framebuffer_target(ctx
, target
);
1996 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2000 if (_mesa_is_winsys_fbo(buffer
)) {
2001 /* The window system / default framebuffer is always complete */
2002 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2005 /* No need to flush here */
2007 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2008 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2011 return buffer
->_Status
;
2016 * Replicate the src attachment point. Used by framebuffer_texture() when
2017 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2018 * GL_STENCIL_ATTACHMENT.
2021 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2022 gl_buffer_index dst
,
2023 gl_buffer_index src
)
2025 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2026 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2028 assert(src_att
->Texture
!= NULL
);
2029 assert(src_att
->Renderbuffer
!= NULL
);
2031 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2032 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2033 dst_att
->Type
= src_att
->Type
;
2034 dst_att
->Complete
= src_att
->Complete
;
2035 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2036 dst_att
->Zoffset
= src_att
->Zoffset
;
2041 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2042 * glFramebufferTextureLayerEXT().
2043 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2044 * get textarget=0 in that case.
2047 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2048 GLenum attachment
, GLenum textarget
, GLuint texture
,
2049 GLint level
, GLint zoffset
)
2051 struct gl_renderbuffer_attachment
*att
;
2052 struct gl_texture_object
*texObj
= NULL
;
2053 struct gl_framebuffer
*fb
;
2054 GLenum maxLevelsTarget
;
2056 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2058 fb
= get_framebuffer_target(ctx
, target
);
2060 _mesa_error(ctx
, GL_INVALID_ENUM
,
2061 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2065 /* check framebuffer binding */
2066 if (_mesa_is_winsys_fbo(fb
)) {
2067 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2068 "glFramebufferTexture%sEXT", caller
);
2072 /* The textarget, level, and zoffset parameters are only validated if
2073 * texture is non-zero.
2076 GLboolean err
= GL_TRUE
;
2078 texObj
= _mesa_lookup_texture(ctx
, texture
);
2079 if (texObj
!= NULL
) {
2080 if (textarget
== 0) {
2081 /* If textarget == 0 it means we're being called by
2082 * glFramebufferTextureLayer() and textarget is not used.
2083 * The only legal texture types for that function are 3D and
2084 * 1D/2D arrays textures.
2086 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2087 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2088 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2089 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
);
2092 /* Make sure textarget is consistent with the texture's type */
2093 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2094 ? !_mesa_is_cube_face(textarget
)
2095 : (texObj
->Target
!= textarget
);
2099 /* can't render to a non-existant texture */
2100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2101 "glFramebufferTexture%sEXT(non existant texture)",
2107 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2108 "glFramebufferTexture%sEXT(texture target mismatch)",
2113 if (texObj
->Target
== GL_TEXTURE_3D
) {
2114 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2115 if (zoffset
< 0 || zoffset
>= maxSize
) {
2116 _mesa_error(ctx
, GL_INVALID_VALUE
,
2117 "glFramebufferTexture%sEXT(zoffset)", caller
);
2121 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2122 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2123 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)) {
2125 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2126 _mesa_error(ctx
, GL_INVALID_VALUE
,
2127 "glFramebufferTexture%sEXT(layer)", caller
);
2132 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2134 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2135 _mesa_error(ctx
, GL_INVALID_VALUE
,
2136 "glFramebufferTexture%sEXT(level)", caller
);
2141 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2143 _mesa_error(ctx
, GL_INVALID_ENUM
,
2144 "glFramebufferTexture%sEXT(attachment)", caller
);
2148 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2150 _glthread_LOCK_MUTEX(fb
->Mutex
);
2152 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2153 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2154 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2155 _mesa_tex_target_to_face(textarget
) ==
2156 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2157 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2158 /* The texture object is already attached to the stencil attachment
2159 * point. Don't create a new renderbuffer; just reuse the stencil
2160 * attachment's. This is required to prevent a GL error in
2161 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2163 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2165 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2166 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2167 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2168 _mesa_tex_target_to_face(textarget
) ==
2169 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2170 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2171 /* As above, but with depth and stencil transposed. */
2172 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2175 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2177 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2178 /* Above we created a new renderbuffer and attached it to the
2179 * depth attachment point. Now attach it to the stencil attachment
2182 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2183 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2188 /* Set the render-to-texture flag. We'll check this flag in
2189 * glTexImage() and friends to determine if we need to revalidate
2190 * any FBOs that might be rendering into this texture.
2191 * This flag never gets cleared since it's non-trivial to determine
2192 * when all FBOs might be done rendering to this texture. That's OK
2193 * though since it's uncommon to render to a texture then repeatedly
2194 * call glTexImage() to change images in the texture.
2196 texObj
->_RenderToTexture
= GL_TRUE
;
2199 _mesa_remove_attachment(ctx
, att
);
2200 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2201 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2202 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2206 invalidate_framebuffer(fb
);
2208 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2214 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2215 GLenum textarget
, GLuint texture
, GLint level
)
2217 GET_CURRENT_CONTEXT(ctx
);
2222 switch (textarget
) {
2226 case GL_TEXTURE_1D_ARRAY
:
2227 error
= !ctx
->Extensions
.EXT_texture_array
;
2234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2235 "glFramebufferTexture1DEXT(textarget=%s)",
2236 _mesa_lookup_enum_by_nr(textarget
));
2241 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2247 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2248 GLenum textarget
, GLuint texture
, GLint level
)
2250 GET_CURRENT_CONTEXT(ctx
);
2255 switch (textarget
) {
2259 case GL_TEXTURE_RECTANGLE
:
2260 error
= _mesa_is_gles(ctx
)
2261 || !ctx
->Extensions
.NV_texture_rectangle
;
2263 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2264 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2265 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2266 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2267 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2268 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2269 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2271 case GL_TEXTURE_2D_ARRAY
:
2272 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2273 || !ctx
->Extensions
.EXT_texture_array
;
2280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2281 "glFramebufferTexture2DEXT(textarget=%s)",
2282 _mesa_lookup_enum_by_nr(textarget
));
2287 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2293 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2294 GLenum textarget
, GLuint texture
,
2295 GLint level
, GLint zoffset
)
2297 GET_CURRENT_CONTEXT(ctx
);
2299 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2301 "glFramebufferTexture3DEXT(textarget)");
2305 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2311 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2312 GLuint texture
, GLint level
, GLint layer
)
2314 GET_CURRENT_CONTEXT(ctx
);
2316 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2322 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2323 GLenum renderbufferTarget
,
2324 GLuint renderbuffer
)
2326 struct gl_renderbuffer_attachment
*att
;
2327 struct gl_framebuffer
*fb
;
2328 struct gl_renderbuffer
*rb
;
2329 GET_CURRENT_CONTEXT(ctx
);
2331 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2333 fb
= get_framebuffer_target(ctx
, target
);
2335 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2339 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2340 _mesa_error(ctx
, GL_INVALID_ENUM
,
2341 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2345 if (_mesa_is_winsys_fbo(fb
)) {
2346 /* Can't attach new renderbuffers to a window system framebuffer */
2347 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2351 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2353 _mesa_error(ctx
, GL_INVALID_ENUM
,
2354 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2355 _mesa_lookup_enum_by_nr(attachment
));
2360 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2362 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2363 "glFramebufferRenderbufferEXT(non-existant"
2364 " renderbuffer %u)", renderbuffer
);
2367 else if (rb
== &DummyRenderbuffer
) {
2368 /* This is what NVIDIA does */
2369 _mesa_error(ctx
, GL_INVALID_VALUE
,
2370 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2376 /* remove renderbuffer attachment */
2380 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2381 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2382 /* make sure the renderbuffer is a depth/stencil format */
2383 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2384 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2386 "glFramebufferRenderbufferEXT(renderbuffer"
2387 " is not DEPTH_STENCIL format)");
2393 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2395 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2396 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2398 /* Some subsequent GL commands may depend on the framebuffer's visual
2399 * after the binding is updated. Update visual info now.
2401 _mesa_update_framebuffer_visual(ctx
, fb
);
2406 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2407 GLenum pname
, GLint
*params
)
2409 const struct gl_renderbuffer_attachment
*att
;
2410 struct gl_framebuffer
*buffer
;
2412 GET_CURRENT_CONTEXT(ctx
);
2414 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2416 /* The error differs in GL and GLES. */
2417 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2419 buffer
= get_framebuffer_target(ctx
, target
);
2421 _mesa_error(ctx
, GL_INVALID_ENUM
,
2422 "glGetFramebufferAttachmentParameterivEXT(target)");
2426 if (_mesa_is_winsys_fbo(buffer
)) {
2427 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2430 * "If the framebuffer currently bound to target is zero, then
2431 * INVALID_OPERATION is generated."
2433 * The EXT_framebuffer_object spec has the same wording, and the
2434 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2437 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2438 && !_mesa_is_gles3(ctx
)) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2444 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2445 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2446 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2447 "glGetFramebufferAttachmentParameteriv(attachment)");
2450 /* the default / window-system FBO */
2451 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2454 /* user-created framebuffer FBO */
2455 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2459 _mesa_error(ctx
, GL_INVALID_ENUM
,
2460 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2464 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2465 /* the depth and stencil attachments must point to the same buffer */
2466 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2467 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2468 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2469 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2470 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2471 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2472 " attachments differ)");
2477 /* No need to flush here */
2480 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2481 *params
= _mesa_is_winsys_fbo(buffer
)
2482 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2484 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2485 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2486 *params
= att
->Renderbuffer
->Name
;
2488 else if (att
->Type
== GL_TEXTURE
) {
2489 *params
= att
->Texture
->Name
;
2492 assert(att
->Type
== GL_NONE
);
2493 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2496 goto invalid_pname_enum
;
2500 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2501 if (att
->Type
== GL_TEXTURE
) {
2502 *params
= att
->TextureLevel
;
2504 else if (att
->Type
== GL_NONE
) {
2505 _mesa_error(ctx
, err
,
2506 "glGetFramebufferAttachmentParameterivEXT(pname)");
2509 goto invalid_pname_enum
;
2512 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2513 if (att
->Type
== GL_TEXTURE
) {
2514 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2515 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2521 else if (att
->Type
== GL_NONE
) {
2522 _mesa_error(ctx
, err
,
2523 "glGetFramebufferAttachmentParameterivEXT(pname)");
2526 goto invalid_pname_enum
;
2529 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2530 if (ctx
->API
== API_OPENGLES
) {
2531 goto invalid_pname_enum
;
2532 } else if (att
->Type
== GL_NONE
) {
2533 _mesa_error(ctx
, err
,
2534 "glGetFramebufferAttachmentParameterivEXT(pname)");
2535 } else if (att
->Type
== GL_TEXTURE
) {
2536 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2537 *params
= att
->Zoffset
;
2544 goto invalid_pname_enum
;
2547 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2548 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2549 && !_mesa_is_gles3(ctx
)) {
2550 goto invalid_pname_enum
;
2552 else if (att
->Type
== GL_NONE
) {
2553 _mesa_error(ctx
, err
,
2554 "glGetFramebufferAttachmentParameterivEXT(pname)");
2557 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2558 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2561 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2562 * if the sRGB conversion is unsupported. */
2563 *params
= GL_LINEAR
;
2567 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2568 if ((ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2569 && ctx
->API
!= API_OPENGL_CORE
2570 && !_mesa_is_gles3(ctx
)) {
2571 goto invalid_pname_enum
;
2573 else if (att
->Type
== GL_NONE
) {
2574 _mesa_error(ctx
, err
,
2575 "glGetFramebufferAttachmentParameterivEXT(pname)");
2578 gl_format format
= att
->Renderbuffer
->Format
;
2579 if (format
== MESA_FORMAT_S8
) {
2583 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2584 /* depends on the attachment parameter */
2585 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2593 *params
= _mesa_get_format_datatype(format
);
2597 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2598 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2599 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2600 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2601 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2602 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2603 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2604 && !_mesa_is_gles3(ctx
)) {
2605 goto invalid_pname_enum
;
2607 else if (att
->Type
== GL_NONE
) {
2608 _mesa_error(ctx
, err
,
2609 "glGetFramebufferAttachmentParameterivEXT(pname)");
2611 else if (att
->Texture
) {
2612 const struct gl_texture_image
*texImage
=
2613 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2616 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2617 texImage
->TexFormat
);
2623 else if (att
->Renderbuffer
) {
2624 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2625 att
->Renderbuffer
->Format
);
2628 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2629 " invalid FBO attachment structure");
2633 goto invalid_pname_enum
;
2639 _mesa_error(ctx
, GL_INVALID_ENUM
,
2640 "glGetFramebufferAttachmentParameteriv(pname)");
2646 _mesa_GenerateMipmap(GLenum target
)
2648 struct gl_texture_image
*srcImage
;
2649 struct gl_texture_object
*texObj
;
2652 GET_CURRENT_CONTEXT(ctx
);
2654 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2655 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2659 error
= _mesa_is_gles(ctx
);
2665 error
= ctx
->API
== API_OPENGLES
;
2667 case GL_TEXTURE_CUBE_MAP
:
2668 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2670 case GL_TEXTURE_1D_ARRAY
:
2671 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2673 case GL_TEXTURE_2D_ARRAY
:
2674 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2675 || !ctx
->Extensions
.EXT_texture_array
;
2682 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2683 _mesa_lookup_enum_by_nr(target
));
2687 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2689 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2694 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2695 !_mesa_cube_complete(texObj
)) {
2696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2697 "glGenerateMipmap(incomplete cube map)");
2701 _mesa_lock_texture(ctx
, texObj
);
2703 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2705 _mesa_unlock_texture(ctx
, texObj
);
2706 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2707 "glGenerateMipmap(zero size base image)");
2711 if (_mesa_is_enum_format_integer(srcImage
->InternalFormat
) ||
2712 _mesa_is_depthstencil_format(srcImage
->InternalFormat
) ||
2713 _mesa_is_stencil_format(srcImage
->InternalFormat
)) {
2714 _mesa_unlock_texture(ctx
, texObj
);
2715 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2716 "glGenerateMipmap(invalid internal format)");
2720 if (target
== GL_TEXTURE_CUBE_MAP
) {
2722 for (face
= 0; face
< 6; face
++)
2723 ctx
->Driver
.GenerateMipmap(ctx
,
2724 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2728 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2730 _mesa_unlock_texture(ctx
, texObj
);
2734 static const struct gl_renderbuffer_attachment
*
2735 find_attachment(const struct gl_framebuffer
*fb
,
2736 const struct gl_renderbuffer
*rb
)
2739 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2740 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2741 return &fb
->Attachment
[i
];
2748 * Helper function for checking if the datatypes of color buffers are
2749 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2751 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2752 * and any of the following conditions hold:
2753 * - The read buffer contains fixed-point or floating-point values and any
2754 * draw buffer contains neither fixed-point nor floating-point values.
2755 * - The read buffer contains unsigned integer values and any draw buffer
2756 * does not contain unsigned integer values.
2757 * - The read buffer contains signed integer values and any draw buffer
2758 * does not contain signed integer values."
2761 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2763 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2764 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2766 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2767 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2768 srcType
== GL_SIGNED_NORMALIZED
||
2769 srcType
== GL_FLOAT
);
2770 /* Boil any of those types down to GL_FLOAT */
2774 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2775 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2776 dstType
== GL_SIGNED_NORMALIZED
||
2777 dstType
== GL_FLOAT
);
2778 /* Boil any of those types down to GL_FLOAT */
2782 return srcType
== dstType
;
2787 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
2788 const struct gl_renderbuffer
*drawRb
)
2790 GLenum readFormat
, drawFormat
;
2792 /* The simple case where we know the backing Mesa formats are the same.
2794 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
2795 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
2799 /* The Mesa formats are different, so we must check whether the internal
2800 * formats are compatible.
2802 * Under some circumstances, the user may request e.g. two GL_RGBA8
2803 * textures and get two entirely different Mesa formats like RGBA8888 and
2804 * ARGB8888. Drivers behaving like that should be able to cope with
2805 * non-matching formats by themselves, because it's not the user's fault.
2807 * Blits between linear and sRGB formats are also allowed.
2809 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
2810 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
2811 readFormat
= _mesa_get_linear_internalformat(readFormat
);
2812 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
2814 if (readFormat
== drawFormat
) {
2823 * Blit rectangular region, optionally from one framebuffer to another.
2825 * Note, if the src buffer is multisampled and the dest is not, this is
2826 * when the samples must be resolved to a single color.
2829 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2830 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2831 GLbitfield mask
, GLenum filter
)
2833 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2834 GL_DEPTH_BUFFER_BIT
|
2835 GL_STENCIL_BUFFER_BIT
);
2836 const struct gl_framebuffer
*readFb
, *drawFb
;
2837 GET_CURRENT_CONTEXT(ctx
);
2839 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2840 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2842 if (MESA_VERBOSE
& VERBOSE_API
)
2844 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2845 srcX0
, srcY0
, srcX1
, srcY1
,
2846 dstX0
, dstY0
, dstX1
, dstY1
,
2847 mask
, _mesa_lookup_enum_by_nr(filter
));
2849 if (ctx
->NewState
) {
2850 _mesa_update_state(ctx
);
2853 readFb
= ctx
->ReadBuffer
;
2854 drawFb
= ctx
->DrawBuffer
;
2856 if (!readFb
|| !drawFb
) {
2857 /* This will normally never happen but someday we may want to
2858 * support MakeCurrent() with no drawables.
2863 /* check for complete framebuffers */
2864 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2865 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2866 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2867 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2871 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2872 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2876 if (mask
& ~legalMaskBits
) {
2877 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2881 /* depth/stencil must be blitted with nearest filtering */
2882 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2883 && filter
!= GL_NEAREST
) {
2884 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2885 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2889 /* get color read/draw renderbuffers */
2890 if (mask
& GL_COLOR_BUFFER_BIT
) {
2891 const GLuint numColorDrawBuffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
2892 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
2893 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
2896 /* From the EXT_framebuffer_object spec:
2898 * "If a buffer is specified in <mask> and does not exist in both
2899 * the read and draw framebuffers, the corresponding bit is silently
2902 if (!colorReadRb
|| numColorDrawBuffers
== 0) {
2903 mask
&= ~GL_COLOR_BUFFER_BIT
;
2906 for (i
= 0; i
< numColorDrawBuffers
; i
++) {
2907 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
2911 if (!compatible_color_datatypes(colorReadRb
->Format
,
2912 colorDrawRb
->Format
)) {
2913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2914 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2917 /* extra checks for multisample copies... */
2918 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2919 /* color formats must match */
2920 if (!compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2921 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2922 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2927 if (filter
== GL_LINEAR
) {
2928 /* 3.1 spec, page 199:
2929 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2930 * if filter is LINEAR and read buffer contains integer data."
2932 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2933 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2934 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2935 "glBlitFramebufferEXT(integer color type)");
2942 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2943 struct gl_renderbuffer
*readRb
=
2944 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2945 struct gl_renderbuffer
*drawRb
=
2946 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2948 /* From the EXT_framebuffer_object spec:
2950 * "If a buffer is specified in <mask> and does not exist in both
2951 * the read and draw framebuffers, the corresponding bit is silently
2954 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2955 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2958 int read_z_bits
, draw_z_bits
;
2960 if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2961 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2962 /* There is no need to check the stencil datatype here, because
2963 * there is only one: GL_UNSIGNED_INT.
2965 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2966 "glBlitFramebuffer(stencil attachment format mismatch)");
2970 read_z_bits
= _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
);
2971 draw_z_bits
= _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
);
2973 /* If both buffers also have depth data, the depth formats must match
2974 * as well. If one doesn't have depth, it's not blitted, so we should
2975 * ignore the depth format check.
2977 if (read_z_bits
> 0 && draw_z_bits
> 0 &&
2978 (read_z_bits
!= draw_z_bits
||
2979 _mesa_get_format_datatype(readRb
->Format
) !=
2980 _mesa_get_format_datatype(drawRb
->Format
))) {
2982 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
2983 "(stencil attachment depth format mismatch)");
2989 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2990 struct gl_renderbuffer
*readRb
=
2991 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2992 struct gl_renderbuffer
*drawRb
=
2993 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2995 /* From the EXT_framebuffer_object spec:
2997 * "If a buffer is specified in <mask> and does not exist in both
2998 * the read and draw framebuffers, the corresponding bit is silently
3001 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
3002 mask
&= ~GL_DEPTH_BUFFER_BIT
;
3005 int read_s_bit
, draw_s_bit
;
3007 if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
3008 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
3009 (_mesa_get_format_datatype(readRb
->Format
) !=
3010 _mesa_get_format_datatype(drawRb
->Format
))) {
3011 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3012 "glBlitFramebuffer(depth attachment format mismatch)");
3016 read_s_bit
= _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
);
3017 draw_s_bit
= _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
);
3019 /* If both buffers also have stencil data, the stencil formats must
3020 * match as well. If one doesn't have stencil, it's not blitted, so
3021 * we should ignore the stencil format check.
3023 if (read_s_bit
> 0 && draw_s_bit
> 0 && read_s_bit
!= draw_s_bit
) {
3024 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebuffer"
3025 "(depth attachment stencil bits mismatch)");
3031 if (readFb
->Visual
.samples
> 0 &&
3032 drawFb
->Visual
.samples
> 0 &&
3033 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
3034 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3035 "glBlitFramebufferEXT(mismatched samples)");
3039 /* extra checks for multisample copies... */
3040 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
3041 /* src and dest region sizes must be the same */
3042 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
3043 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
3044 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3045 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3050 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
3051 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
3057 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
3058 const struct gl_renderbuffer
*colorDrawRb
= NULL
;
3061 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3063 srcX0
, srcY0
, srcX1
, srcY1
,
3064 dstX0
, dstY0
, dstX1
, dstY1
,
3067 const struct gl_renderbuffer_attachment
*att
;
3069 att
= find_attachment(readFb
, colorReadRb
);
3070 printf(" Src FBO %u RB %u (%dx%d) ",
3071 readFb
->Name
, colorReadRb
->Name
,
3072 colorReadRb
->Width
, colorReadRb
->Height
);
3073 if (att
&& att
->Texture
) {
3074 printf("Tex %u tgt 0x%x level %u face %u",
3076 att
->Texture
->Target
,
3082 /* Print all active color render buffers */
3083 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
3084 colorDrawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
3088 att
= find_attachment(drawFb
, colorDrawRb
);
3089 printf(" Dst FBO %u RB %u (%dx%d) ",
3090 drawFb
->Name
, colorDrawRb
->Name
,
3091 colorDrawRb
->Width
, colorDrawRb
->Height
);
3092 if (att
&& att
->Texture
) {
3093 printf("Tex %u tgt 0x%x level %u face %u",
3095 att
->Texture
->Target
,
3108 ASSERT(ctx
->Driver
.BlitFramebuffer
);
3109 ctx
->Driver
.BlitFramebuffer(ctx
,
3110 srcX0
, srcY0
, srcX1
, srcY1
,
3111 dstX0
, dstY0
, dstX1
, dstY1
,
3117 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3118 const GLenum
*attachments
, GLint x
, GLint y
,
3119 GLsizei width
, GLsizei height
, const char *name
)
3122 struct gl_framebuffer
*fb
;
3123 GET_CURRENT_CONTEXT(ctx
);
3125 ASSERT_OUTSIDE_BEGIN_END(ctx
);
3127 fb
= get_framebuffer_target(ctx
, target
);
3129 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3133 if (numAttachments
< 0) {
3134 _mesa_error(ctx
, GL_INVALID_VALUE
,
3135 "%s(numAttachments < 0)", name
);
3139 /* The GL_ARB_invalidate_subdata spec says:
3141 * "If an attachment is specified that does not exist in the
3142 * framebuffer bound to <target>, it is ignored."
3146 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3147 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3148 * INVALID_OPERATION is generated."
3150 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3151 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3152 * set of retrictions).
3154 for (i
= 0; i
< numAttachments
; i
++) {
3155 if (_mesa_is_winsys_fbo(fb
)) {
3156 switch (attachments
[i
]) {
3162 /* Accumulation buffers and auxilary buffers were removed in
3163 * OpenGL 3.1, and they never existed in OpenGL ES.
3165 if (ctx
->API
!= API_OPENGL_COMPAT
)
3175 case GL_FRONT_RIGHT
:
3176 if (!_mesa_is_desktop_gl(ctx
))
3183 switch (attachments
[i
]) {
3184 case GL_DEPTH_ATTACHMENT
:
3185 case GL_STENCIL_ATTACHMENT
:
3187 case GL_COLOR_ATTACHMENT0
:
3188 case GL_COLOR_ATTACHMENT1
:
3189 case GL_COLOR_ATTACHMENT2
:
3190 case GL_COLOR_ATTACHMENT3
:
3191 case GL_COLOR_ATTACHMENT4
:
3192 case GL_COLOR_ATTACHMENT5
:
3193 case GL_COLOR_ATTACHMENT6
:
3194 case GL_COLOR_ATTACHMENT7
:
3195 case GL_COLOR_ATTACHMENT8
:
3196 case GL_COLOR_ATTACHMENT9
:
3197 case GL_COLOR_ATTACHMENT10
:
3198 case GL_COLOR_ATTACHMENT11
:
3199 case GL_COLOR_ATTACHMENT12
:
3200 case GL_COLOR_ATTACHMENT13
:
3201 case GL_COLOR_ATTACHMENT14
:
3202 case GL_COLOR_ATTACHMENT15
: {
3203 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3204 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3206 "%s(attachment >= max. color attachments)", name
);
3216 /* We don't actually do anything for this yet. Just return after
3217 * validating the parameters and generating the required errors.
3222 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3227 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3228 const GLenum
*attachments
, GLint x
, GLint y
,
3229 GLsizei width
, GLsizei height
)
3231 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3232 x
, y
, width
, height
,
3233 "glInvalidateSubFramebuffer");
3237 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3238 const GLenum
*attachments
)
3240 /* The GL_ARB_invalidate_subdata spec says:
3244 * void InvalidateFramebuffer(enum target,
3245 * sizei numAttachments,
3246 * const enum *attachments);
3248 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3249 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3250 * <MAX_VIEWPORT_DIMS[1]> respectively."
3252 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3253 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3254 "glInvalidateFramebuffer");