2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * GL_EXT/ARB_framebuffer_object extensions
41 #include "framebuffer.h"
42 #include "glformats.h"
45 #include "multisample.h"
47 #include "renderbuffer.h"
56 * None of the GL_EXT_framebuffer_object functions are compiled into
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
68 static struct gl_framebuffer DummyFramebuffer
;
69 static struct gl_renderbuffer DummyRenderbuffer
;
71 /* We bind this framebuffer when applications pass a NULL
72 * drawable/surface in make current. */
73 static struct gl_framebuffer IncompleteFramebuffer
;
77 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
83 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
90 _mesa_init_fbobjects(struct gl_context
*ctx
)
92 mtx_init(&DummyFramebuffer
.Mutex
, mtx_plain
);
93 mtx_init(&DummyRenderbuffer
.Mutex
, mtx_plain
);
94 mtx_init(&IncompleteFramebuffer
.Mutex
, mtx_plain
);
95 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
96 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
97 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 struct gl_framebuffer
*
101 _mesa_get_incomplete_framebuffer(void)
103 return &IncompleteFramebuffer
;
107 * Helper routine for getting a gl_renderbuffer.
109 struct gl_renderbuffer
*
110 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
112 struct gl_renderbuffer
*rb
;
117 rb
= (struct gl_renderbuffer
*)
118 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
124 * A convenience function for direct state access that throws
125 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
127 struct gl_renderbuffer
*
128 _mesa_lookup_renderbuffer_err(struct gl_context
*ctx
, GLuint id
,
131 struct gl_renderbuffer
*rb
;
133 rb
= _mesa_lookup_renderbuffer(ctx
, id
);
134 if (!rb
|| rb
== &DummyRenderbuffer
) {
135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
136 "%s(non-existent renderbuffer %u)", func
, id
);
145 * Helper routine for getting a gl_framebuffer.
147 struct gl_framebuffer
*
148 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
150 struct gl_framebuffer
*fb
;
155 fb
= (struct gl_framebuffer
*)
156 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
162 * A convenience function for direct state access that throws
163 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
165 struct gl_framebuffer
*
166 _mesa_lookup_framebuffer_err(struct gl_context
*ctx
, GLuint id
,
169 struct gl_framebuffer
*fb
;
171 fb
= _mesa_lookup_framebuffer(ctx
, id
);
172 if (!fb
|| fb
== &DummyFramebuffer
) {
173 _mesa_error(ctx
, GL_INVALID_OPERATION
,
174 "%s(non-existent framebuffer %u)", func
, id
);
183 * Mark the given framebuffer as invalid. This will force the
184 * test for framebuffer completeness to be done before the framebuffer
188 invalidate_framebuffer(struct gl_framebuffer
*fb
)
190 fb
->_Status
= 0; /* "indeterminate" */
195 * Return the gl_framebuffer object which corresponds to the given
196 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
197 * Check support for GL_EXT_framebuffer_blit to determine if certain
199 * \return gl_framebuffer pointer or NULL if target is illegal
201 static struct gl_framebuffer
*
202 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
204 bool have_fb_blit
= _mesa_is_gles3(ctx
) || _mesa_is_desktop_gl(ctx
);
206 case GL_DRAW_FRAMEBUFFER
:
207 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
208 case GL_READ_FRAMEBUFFER
:
209 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
210 case GL_FRAMEBUFFER_EXT
:
211 return ctx
->DrawBuffer
;
219 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
220 * gl_renderbuffer_attachment object.
221 * This function is only used for user-created FB objects, not the
222 * default / window-system FB object.
223 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
224 * the depth buffer attachment point.
226 static struct gl_renderbuffer_attachment
*
227 get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
232 assert(_mesa_is_user_fbo(fb
));
234 switch (attachment
) {
235 case GL_COLOR_ATTACHMENT0_EXT
:
236 case GL_COLOR_ATTACHMENT1_EXT
:
237 case GL_COLOR_ATTACHMENT2_EXT
:
238 case GL_COLOR_ATTACHMENT3_EXT
:
239 case GL_COLOR_ATTACHMENT4_EXT
:
240 case GL_COLOR_ATTACHMENT5_EXT
:
241 case GL_COLOR_ATTACHMENT6_EXT
:
242 case GL_COLOR_ATTACHMENT7_EXT
:
243 case GL_COLOR_ATTACHMENT8_EXT
:
244 case GL_COLOR_ATTACHMENT9_EXT
:
245 case GL_COLOR_ATTACHMENT10_EXT
:
246 case GL_COLOR_ATTACHMENT11_EXT
:
247 case GL_COLOR_ATTACHMENT12_EXT
:
248 case GL_COLOR_ATTACHMENT13_EXT
:
249 case GL_COLOR_ATTACHMENT14_EXT
:
250 case GL_COLOR_ATTACHMENT15_EXT
:
251 /* Only OpenGL ES 1.x forbids color attachments other than
252 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
255 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
256 if (i
>= ctx
->Const
.MaxColorAttachments
257 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
260 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
261 case GL_DEPTH_STENCIL_ATTACHMENT
:
262 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
265 case GL_DEPTH_ATTACHMENT_EXT
:
266 return &fb
->Attachment
[BUFFER_DEPTH
];
267 case GL_STENCIL_ATTACHMENT_EXT
:
268 return &fb
->Attachment
[BUFFER_STENCIL
];
276 * As above, but only used for getting attachments of the default /
277 * window-system framebuffer (not user-created framebuffer objects).
279 static struct gl_renderbuffer_attachment
*
280 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
283 assert(_mesa_is_winsys_fbo(fb
));
285 if (_mesa_is_gles3(ctx
)) {
286 assert(attachment
== GL_BACK
||
287 attachment
== GL_DEPTH
||
288 attachment
== GL_STENCIL
);
289 switch (attachment
) {
291 /* Since there is no stereo rendering in ES 3.0, only return the
294 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
295 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
296 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
298 return &fb
->Attachment
[BUFFER_DEPTH
];
300 return &fb
->Attachment
[BUFFER_STENCIL
];
304 switch (attachment
) {
306 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
308 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
310 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
312 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
314 if (fb
->Visual
.numAuxBuffers
== 1) {
315 return &fb
->Attachment
[BUFFER_AUX0
];
319 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
321 * "If the default framebuffer is bound to target, then attachment must
322 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
323 * identifying a color buffer; DEPTH, identifying the depth buffer; or
324 * STENCIL, identifying the stencil buffer."
326 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
327 * language. However, revision #33 of the ARB_framebuffer_object spec
330 * "If the default framebuffer is bound to <target>, then <attachment>
331 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
332 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
333 * depth buffer, or the stencil buffer, and <pname> may be
334 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
335 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
337 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
338 * from glext.h, so shipping apps should not use those values.
340 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
341 * support queries of the window system FBO.
344 return &fb
->Attachment
[BUFFER_DEPTH
];
346 return &fb
->Attachment
[BUFFER_STENCIL
];
355 * Remove any texture or renderbuffer attached to the given attachment
356 * point. Update reference counts, etc.
359 remove_attachment(struct gl_context
*ctx
,
360 struct gl_renderbuffer_attachment
*att
)
362 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
364 /* tell driver that we're done rendering to this texture. */
365 if (rb
&& rb
->NeedsFinishRenderTexture
)
366 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
368 if (att
->Type
== GL_TEXTURE
) {
369 assert(att
->Texture
);
370 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
371 assert(!att
->Texture
);
373 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
374 assert(!att
->Texture
);
375 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
376 assert(!att
->Renderbuffer
);
379 att
->Complete
= GL_TRUE
;
383 * Verify a couple error conditions that will lead to an incomplete FBO and
384 * may cause problems for the driver's RenderTexture path.
387 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment
*att
)
389 const struct gl_texture_image
*const texImage
=
390 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
392 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
395 if ((texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
396 && att
->Zoffset
>= texImage
->Height
)
397 || (texImage
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
398 && att
->Zoffset
>= texImage
->Depth
))
405 * Create a renderbuffer which will be set up by the driver to wrap the
406 * texture image slice.
408 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
409 * to share most of their framebuffer rendering code between winsys,
410 * renderbuffer, and texture attachments.
412 * The allocated renderbuffer uses a non-zero Name so that drivers can check
413 * it for determining vertical orientation, but we use ~0 to make it fairly
414 * unambiguous with actual user (non-texture) renderbuffers.
417 _mesa_update_texture_renderbuffer(struct gl_context
*ctx
,
418 struct gl_framebuffer
*fb
,
419 struct gl_renderbuffer_attachment
*att
)
421 struct gl_texture_image
*texImage
;
422 struct gl_renderbuffer
*rb
;
424 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
426 rb
= att
->Renderbuffer
;
428 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, ~0);
430 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
433 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
435 /* This can't get called on a texture renderbuffer, so set it to NULL
436 * for clarity compared to user renderbuffers.
438 rb
->AllocStorage
= NULL
;
440 rb
->NeedsFinishRenderTexture
= ctx
->Driver
.FinishRenderTexture
!= NULL
;
446 rb
->_BaseFormat
= texImage
->_BaseFormat
;
447 rb
->Format
= texImage
->TexFormat
;
448 rb
->InternalFormat
= texImage
->InternalFormat
;
449 rb
->Width
= texImage
->Width2
;
450 rb
->Height
= texImage
->Height2
;
451 rb
->Depth
= texImage
->Depth2
;
452 rb
->NumSamples
= texImage
->NumSamples
;
453 rb
->TexImage
= texImage
;
455 if (driver_RenderTexture_is_safe(att
))
456 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
460 * Bind a texture object to an attachment point.
461 * The previous binding, if any, will be removed first.
464 set_texture_attachment(struct gl_context
*ctx
,
465 struct gl_framebuffer
*fb
,
466 struct gl_renderbuffer_attachment
*att
,
467 struct gl_texture_object
*texObj
,
468 GLenum texTarget
, GLuint level
, GLuint zoffset
,
471 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
473 if (rb
&& rb
->NeedsFinishRenderTexture
)
474 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
476 if (att
->Texture
== texObj
) {
477 /* re-attaching same texture */
478 assert(att
->Type
== GL_TEXTURE
);
482 remove_attachment(ctx
, att
);
483 att
->Type
= GL_TEXTURE
;
484 assert(!att
->Texture
);
485 _mesa_reference_texobj(&att
->Texture
, texObj
);
487 invalidate_framebuffer(fb
);
489 /* always update these fields */
490 att
->TextureLevel
= level
;
491 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
492 att
->Zoffset
= zoffset
;
493 att
->Layered
= layered
;
494 att
->Complete
= GL_FALSE
;
496 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
501 * Bind a renderbuffer to an attachment point.
502 * The previous binding, if any, will be removed first.
505 set_renderbuffer_attachment(struct gl_context
*ctx
,
506 struct gl_renderbuffer_attachment
*att
,
507 struct gl_renderbuffer
*rb
)
509 /* XXX check if re-doing same attachment, exit early */
510 remove_attachment(ctx
, att
);
511 att
->Type
= GL_RENDERBUFFER_EXT
;
512 att
->Texture
= NULL
; /* just to be safe */
513 att
->Layered
= GL_FALSE
;
514 att
->Complete
= GL_FALSE
;
515 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
520 * Fallback for ctx->Driver.FramebufferRenderbuffer()
521 * Attach a renderbuffer object to a framebuffer object.
524 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
525 struct gl_framebuffer
*fb
,
526 GLenum attachment
, struct gl_renderbuffer
*rb
)
528 struct gl_renderbuffer_attachment
*att
;
530 mtx_lock(&fb
->Mutex
);
532 att
= get_attachment(ctx
, fb
, attachment
);
535 set_renderbuffer_attachment(ctx
, att
, rb
);
536 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
537 /* do stencil attachment here (depth already done above) */
538 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
540 set_renderbuffer_attachment(ctx
, att
, rb
);
542 rb
->AttachedAnytime
= GL_TRUE
;
545 remove_attachment(ctx
, att
);
546 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
547 /* detach stencil (depth was detached above) */
548 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
550 remove_attachment(ctx
, att
);
554 invalidate_framebuffer(fb
);
556 mtx_unlock(&fb
->Mutex
);
561 * Fallback for ctx->Driver.ValidateFramebuffer()
562 * Check if the renderbuffer's formats are supported by the software
564 * Drivers should probably override this.
567 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
570 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
571 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
573 switch (rb
->_BaseFormat
) {
575 case GL_LUMINANCE_ALPHA
:
580 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
584 switch (rb
->Format
) {
585 /* XXX This list is likely incomplete. */
586 case MESA_FORMAT_R9G9B9E5_FLOAT
:
587 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
590 /* render buffer format is supported by software rendering */
599 * Return true if the framebuffer has a combined depth/stencil
600 * renderbuffer attached.
603 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
605 const struct gl_renderbuffer_attachment
*depth
=
606 &fb
->Attachment
[BUFFER_DEPTH
];
607 const struct gl_renderbuffer_attachment
*stencil
=
608 &fb
->Attachment
[BUFFER_STENCIL
];
610 if (depth
->Type
== stencil
->Type
) {
611 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
612 depth
->Renderbuffer
== stencil
->Renderbuffer
)
615 if (depth
->Type
== GL_TEXTURE
&&
616 depth
->Texture
== stencil
->Texture
)
628 att_incomplete(const char *msg
)
630 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
631 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
640 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
642 static GLuint msg_id
;
644 _mesa_gl_debug(ctx
, &msg_id
,
645 MESA_DEBUG_SOURCE_API
,
646 MESA_DEBUG_TYPE_OTHER
,
647 MESA_DEBUG_SEVERITY_MEDIUM
,
648 "FBO incomplete: %s [%d]\n", msg
, index
);
650 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
651 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
657 * Is the given base format a legal format for a color renderbuffer?
660 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
662 switch (baseFormat
) {
667 case GL_LUMINANCE_ALPHA
:
670 return ctx
->API
== API_OPENGL_COMPAT
&&
671 ctx
->Extensions
.ARB_framebuffer_object
;
674 return ctx
->Extensions
.ARB_texture_rg
;
682 * Is the given base format a legal format for a color renderbuffer?
685 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
686 GLenum internalFormat
)
688 const GLenum baseFormat
=
689 _mesa_get_format_base_format(format
);
692 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
693 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
697 /* Reject additional cases for GLES */
698 switch (internalFormat
) {
718 if (format
== MESA_FORMAT_B10G10R10A2_UNORM
&&
719 internalFormat
!= GL_RGB10_A2
) {
728 * Is the given base format a legal format for a depth/stencil renderbuffer?
731 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
733 switch (baseFormat
) {
734 case GL_DEPTH_COMPONENT
:
735 case GL_DEPTH_STENCIL_EXT
:
744 * Test if an attachment point is complete and update its Complete field.
745 * \param format if GL_COLOR, this is a color attachment point,
746 * if GL_DEPTH, this is a depth component attachment point,
747 * if GL_STENCIL, this is a stencil component attachment point.
750 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
751 struct gl_renderbuffer_attachment
*att
)
753 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
755 /* assume complete */
756 att
->Complete
= GL_TRUE
;
758 /* Look for reasons why the attachment might be incomplete */
759 if (att
->Type
== GL_TEXTURE
) {
760 const struct gl_texture_object
*texObj
= att
->Texture
;
761 struct gl_texture_image
*texImage
;
765 att_incomplete("no texobj");
766 att
->Complete
= GL_FALSE
;
770 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
772 att_incomplete("no teximage");
773 att
->Complete
= GL_FALSE
;
776 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
777 att_incomplete("teximage width/height=0");
778 att
->Complete
= GL_FALSE
;
782 switch (texObj
->Target
) {
784 if (att
->Zoffset
>= texImage
->Depth
) {
785 att_incomplete("bad z offset");
786 att
->Complete
= GL_FALSE
;
790 case GL_TEXTURE_1D_ARRAY
:
791 if (att
->Zoffset
>= texImage
->Height
) {
792 att_incomplete("bad 1D-array layer");
793 att
->Complete
= GL_FALSE
;
797 case GL_TEXTURE_2D_ARRAY
:
798 if (att
->Zoffset
>= texImage
->Depth
) {
799 att_incomplete("bad 2D-array layer");
800 att
->Complete
= GL_FALSE
;
804 case GL_TEXTURE_CUBE_MAP_ARRAY
:
805 if (att
->Zoffset
>= texImage
->Depth
) {
806 att_incomplete("bad cube-array layer");
807 att
->Complete
= GL_FALSE
;
813 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
815 if (format
== GL_COLOR
) {
816 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
817 att_incomplete("bad format");
818 att
->Complete
= GL_FALSE
;
821 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
822 att_incomplete("compressed internalformat");
823 att
->Complete
= GL_FALSE
;
827 /* OES_texture_float allows creation and use of floating point
828 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
829 * these textures to be used as a render target, this is done via
830 * GL_EXT_color_buffer(_half)_float with set of new sized types.
832 if (_mesa_is_gles(ctx
) && (texImage
->TexObject
->_IsFloat
||
833 texImage
->TexObject
->_IsHalfFloat
)) {
834 att_incomplete("bad internal format");
835 att
->Complete
= GL_FALSE
;
839 else if (format
== GL_DEPTH
) {
840 if (baseFormat
== GL_DEPTH_COMPONENT
) {
843 else if (ctx
->Extensions
.ARB_depth_texture
&&
844 baseFormat
== GL_DEPTH_STENCIL
) {
848 att
->Complete
= GL_FALSE
;
849 att_incomplete("bad depth format");
854 assert(format
== GL_STENCIL
);
855 if (ctx
->Extensions
.ARB_depth_texture
&&
856 baseFormat
== GL_DEPTH_STENCIL
) {
858 } else if (ctx
->Extensions
.ARB_texture_stencil8
&&
859 baseFormat
== GL_STENCIL_INDEX
) {
862 /* no such thing as stencil-only textures */
863 att_incomplete("illegal stencil texture");
864 att
->Complete
= GL_FALSE
;
869 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
870 const GLenum baseFormat
=
871 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
873 assert(att
->Renderbuffer
);
874 if (!att
->Renderbuffer
->InternalFormat
||
875 att
->Renderbuffer
->Width
< 1 ||
876 att
->Renderbuffer
->Height
< 1) {
877 att_incomplete("0x0 renderbuffer");
878 att
->Complete
= GL_FALSE
;
881 if (format
== GL_COLOR
) {
882 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
883 att_incomplete("bad renderbuffer color format");
884 att
->Complete
= GL_FALSE
;
888 else if (format
== GL_DEPTH
) {
889 if (baseFormat
== GL_DEPTH_COMPONENT
) {
892 else if (baseFormat
== GL_DEPTH_STENCIL
) {
896 att_incomplete("bad renderbuffer depth format");
897 att
->Complete
= GL_FALSE
;
902 assert(format
== GL_STENCIL
);
903 if (baseFormat
== GL_STENCIL_INDEX
||
904 baseFormat
== GL_DEPTH_STENCIL
) {
908 att
->Complete
= GL_FALSE
;
909 att_incomplete("bad renderbuffer stencil format");
915 assert(att
->Type
== GL_NONE
);
923 * Test if the given framebuffer object is complete and update its
924 * Status field with the results.
925 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
926 * driver to make hardware-specific validation/completeness checks.
927 * Also update the framebuffer's Width and Height fields if the
928 * framebuffer is complete.
931 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
932 struct gl_framebuffer
*fb
)
935 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
936 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
937 GLint numSamples
= -1;
938 GLint fixedSampleLocations
= -1;
941 /* Covers max_layer_count, is_layered, and layer_tex_target */
942 bool layer_info_valid
= false;
943 GLuint max_layer_count
= 0, att_layer_count
;
944 bool is_layered
= false;
945 GLenum layer_tex_target
= 0;
946 bool has_depth_attachment
= false;
947 bool has_stencil_attachment
= false;
949 assert(_mesa_is_user_fbo(fb
));
951 /* we're changing framebuffer fields here */
952 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
957 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
958 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
960 /* Start at -2 to more easily loop over all attachment points.
965 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
966 struct gl_renderbuffer_attachment
*att
;
968 mesa_format attFormat
;
969 GLenum att_tex_target
= GL_NONE
;
972 * XXX for ARB_fbo, only check color buffers that are named by
973 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
976 /* check for attachment completeness
979 att
= &fb
->Attachment
[BUFFER_DEPTH
];
980 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
981 if (!att
->Complete
) {
982 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
983 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
985 } else if (att
->Type
!= GL_NONE
) {
986 has_depth_attachment
= true;
990 att
= &fb
->Attachment
[BUFFER_STENCIL
];
991 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
992 if (!att
->Complete
) {
993 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
994 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
996 } else if (att
->Type
!= GL_NONE
) {
997 has_stencil_attachment
= true;
1001 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
1002 test_attachment_completeness(ctx
, GL_COLOR
, att
);
1003 if (!att
->Complete
) {
1004 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1005 fbo_incomplete(ctx
, "color attachment incomplete", i
);
1010 /* get width, height, format of the renderbuffer/texture
1012 if (att
->Type
== GL_TEXTURE
) {
1013 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
1014 att_tex_target
= att
->Texture
->Target
;
1015 minWidth
= MIN2(minWidth
, texImg
->Width
);
1016 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
1017 minHeight
= MIN2(minHeight
, texImg
->Height
);
1018 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
1019 f
= texImg
->_BaseFormat
;
1020 attFormat
= texImg
->TexFormat
;
1023 if (!is_format_color_renderable(ctx
, attFormat
,
1024 texImg
->InternalFormat
) &&
1025 !is_legal_depth_format(ctx
, f
) &&
1026 f
!= GL_STENCIL_INDEX
) {
1027 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
1028 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
1033 numSamples
= texImg
->NumSamples
;
1034 else if (numSamples
!= texImg
->NumSamples
) {
1035 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1036 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1040 if (fixedSampleLocations
< 0)
1041 fixedSampleLocations
= texImg
->FixedSampleLocations
;
1042 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
1043 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1044 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1048 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1049 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1050 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1051 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1052 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1053 f
= att
->Renderbuffer
->InternalFormat
;
1054 attFormat
= att
->Renderbuffer
->Format
;
1058 numSamples
= att
->Renderbuffer
->NumSamples
;
1059 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1060 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1061 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1065 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1066 if (fixedSampleLocations
< 0)
1067 fixedSampleLocations
= GL_TRUE
;
1068 else if (fixedSampleLocations
!= GL_TRUE
) {
1069 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1070 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1075 assert(att
->Type
== GL_NONE
);
1079 /* check if integer color */
1080 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
1082 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1084 GLenum type
= _mesa_get_format_datatype(attFormat
);
1086 fb
->_AllColorBuffersFixedPoint
=
1087 fb
->_AllColorBuffersFixedPoint
&&
1088 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1090 fb
->_HasSNormOrFloatColorBuffer
=
1091 fb
->_HasSNormOrFloatColorBuffer
||
1092 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1095 /* Error-check width, height, format */
1096 if (numImages
== 1) {
1103 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1104 /* check that width, height, format are same */
1105 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1106 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1107 fbo_incomplete(ctx
, "width or height mismatch", -1);
1110 /* check that all color buffers are the same format */
1111 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1112 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1113 fbo_incomplete(ctx
, "format mismatch", -1);
1119 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1121 if (att
->Type
== GL_RENDERBUFFER
&&
1122 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1123 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1124 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1128 /* Check that layered rendering is consistent. */
1130 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1131 att_layer_count
= 6;
1132 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1133 att_layer_count
= att
->Renderbuffer
->Height
;
1135 att_layer_count
= att
->Renderbuffer
->Depth
;
1137 att_layer_count
= 0;
1139 if (!layer_info_valid
) {
1140 is_layered
= att
->Layered
;
1141 max_layer_count
= att_layer_count
;
1142 layer_tex_target
= att_tex_target
;
1143 layer_info_valid
= true;
1144 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1145 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1146 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1148 } else if (is_layered
!= att
->Layered
) {
1149 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1151 "framebuffer attachment layer mode is inconsistent",
1154 } else if (att_layer_count
> max_layer_count
) {
1155 max_layer_count
= att_layer_count
;
1159 fb
->MaxNumLayers
= max_layer_count
;
1161 if (numImages
== 0) {
1162 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1163 fbo_incomplete(ctx
, "no attachments", -1);
1167 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1168 /* Check that all DrawBuffers are present */
1169 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1170 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1171 const struct gl_renderbuffer_attachment
*att
1172 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
1174 if (att
->Type
== GL_NONE
) {
1175 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1176 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1182 /* Check that the ReadBuffer is present */
1183 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1184 const struct gl_renderbuffer_attachment
*att
1185 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
1187 if (att
->Type
== GL_NONE
) {
1188 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1189 fbo_incomplete(ctx
, "missing readbuffer", -1);
1195 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1197 * "Depth and stencil attachments, if present, are the same image."
1199 * This restriction is not present in the OpenGL ES2 spec.
1201 if (_mesa_is_gles3(ctx
) &&
1202 has_stencil_attachment
&& has_depth_attachment
&&
1203 !_mesa_has_depthstencil_combined(fb
)) {
1204 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1205 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1209 /* Provisionally set status = COMPLETE ... */
1210 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1212 /* ... but the driver may say the FB is incomplete.
1213 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1216 if (ctx
->Driver
.ValidateFramebuffer
) {
1217 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1218 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1219 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1223 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
1225 * Note that if ARB_framebuffer_object is supported and the attached
1226 * renderbuffers/textures are different sizes, the framebuffer
1227 * width/height will be set to the smallest width/height.
1229 fb
->Width
= minWidth
;
1230 fb
->Height
= minHeight
;
1232 /* finally, update the visual info for the framebuffer */
1233 _mesa_update_framebuffer_visual(ctx
, fb
);
1238 GLboolean GLAPIENTRY
1239 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1244 struct gl_renderbuffer
*rb
=
1245 _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1246 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1253 static struct gl_renderbuffer
*
1254 allocate_renderbuffer(struct gl_context
*ctx
, GLuint renderbuffer
,
1257 struct gl_renderbuffer
*newRb
;
1259 /* create new renderbuffer object */
1260 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1265 assert(newRb
->AllocStorage
);
1266 mtx_lock(&ctx
->Shared
->Mutex
);
1267 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1268 newRb
->RefCount
= 1; /* referenced by hash table */
1269 mtx_unlock(&ctx
->Shared
->Mutex
);
1276 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1278 struct gl_renderbuffer
*newRb
;
1279 GET_CURRENT_CONTEXT(ctx
);
1281 if (target
!= GL_RENDERBUFFER_EXT
) {
1282 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1286 /* No need to flush here since the render buffer binding has no
1287 * effect on rendering state.
1291 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1292 if (newRb
== &DummyRenderbuffer
) {
1293 /* ID was reserved, but no real renderbuffer object made yet */
1296 else if (!newRb
&& !allow_user_names
) {
1297 /* All RB IDs must be Gen'd */
1298 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1303 newRb
= allocate_renderbuffer(ctx
, renderbuffer
, "glBindRenderbufferEXT");
1310 assert(newRb
!= &DummyRenderbuffer
);
1312 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1316 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1318 GET_CURRENT_CONTEXT(ctx
);
1320 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1321 * entry point, but they allow the use of user-generated names.
1323 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1327 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1329 /* This function should not be in the dispatch table for core profile /
1330 * OpenGL 3.1, so execution should never get here in those cases -- no
1331 * need for an explicit test.
1333 bind_renderbuffer(target
, renderbuffer
, true);
1338 * Remove the specified renderbuffer or texture from any attachment point in
1342 * \c true if the renderbuffer was detached from an attachment point. \c
1346 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1347 struct gl_framebuffer
*fb
,
1351 bool progress
= false;
1353 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1354 if (fb
->Attachment
[i
].Texture
== att
1355 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1356 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1361 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1362 * Completeness," of the OpenGL 3.1 spec says:
1364 * "Performing any of the following actions may change whether the
1365 * framebuffer is considered complete or incomplete:
1369 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1370 * containing an image that is attached to a framebuffer object
1371 * that is bound to the framebuffer."
1374 invalidate_framebuffer(fb
);
1381 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1384 GET_CURRENT_CONTEXT(ctx
);
1387 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1391 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1393 for (i
= 0; i
< n
; i
++) {
1394 if (renderbuffers
[i
] > 0) {
1395 struct gl_renderbuffer
*rb
;
1396 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1398 /* check if deleting currently bound renderbuffer object */
1399 if (rb
== ctx
->CurrentRenderbuffer
) {
1401 assert(rb
->RefCount
>= 2);
1402 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1405 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1406 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1407 * of the OpenGL 3.1 spec says:
1409 * "If a renderbuffer object is deleted while its image is
1410 * attached to one or more attachment points in the currently
1411 * bound framebuffer, then it is as if FramebufferRenderbuffer
1412 * had been called, with a renderbuffer of 0, for each
1413 * attachment point to which this image was attached in the
1414 * currently bound framebuffer. In other words, this
1415 * renderbuffer image is first detached from all attachment
1416 * points in the currently bound framebuffer. Note that the
1417 * renderbuffer image is specifically not detached from any
1418 * non-bound framebuffers. Detaching the image from any
1419 * non-bound framebuffers is the responsibility of the
1422 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1423 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1425 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1426 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1427 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1430 /* Remove from hash table immediately, to free the ID.
1431 * But the object will not be freed until it's no longer
1432 * referenced anywhere else.
1434 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1436 if (rb
!= &DummyRenderbuffer
) {
1437 /* no longer referenced by hash table */
1438 _mesa_reference_renderbuffer(&rb
, NULL
);
1446 create_render_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*renderbuffers
,
1449 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1454 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n<0)", func
);
1461 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1463 for (i
= 0; i
< n
; i
++) {
1464 GLuint name
= first
+ i
;
1465 renderbuffers
[i
] = name
;
1468 allocate_renderbuffer(ctx
, name
, func
);
1470 /* insert a dummy renderbuffer into the hash table */
1471 mtx_lock(&ctx
->Shared
->Mutex
);
1472 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1473 mtx_unlock(&ctx
->Shared
->Mutex
);
1480 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1482 GET_CURRENT_CONTEXT(ctx
);
1483 create_render_buffers(ctx
, n
, renderbuffers
, false);
1488 _mesa_CreateRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1490 GET_CURRENT_CONTEXT(ctx
);
1491 create_render_buffers(ctx
, n
, renderbuffers
, true);
1496 * Given an internal format token for a render buffer, return the
1497 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1498 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1499 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1501 * This is similar to _mesa_base_tex_format() but the set of valid
1502 * internal formats is different.
1504 * Note that even if a format is determined to be legal here, validation
1505 * of the FBO may fail if the format is not supported by the driver/GPU.
1507 * \param internalFormat as passed to glRenderbufferStorage()
1508 * \return the base internal format, or 0 if internalFormat is illegal
1511 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1514 * Notes: some formats such as alpha, luminance, etc. were added
1515 * with GL_ARB_framebuffer_object.
1517 switch (internalFormat
) {
1523 return (ctx
->API
== API_OPENGL_COMPAT
&&
1524 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1528 case GL_LUMINANCE12
:
1529 case GL_LUMINANCE16
:
1530 return (ctx
->API
== API_OPENGL_COMPAT
&&
1531 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1532 case GL_LUMINANCE_ALPHA
:
1533 case GL_LUMINANCE4_ALPHA4
:
1534 case GL_LUMINANCE6_ALPHA2
:
1535 case GL_LUMINANCE8_ALPHA8
:
1536 case GL_LUMINANCE12_ALPHA4
:
1537 case GL_LUMINANCE12_ALPHA12
:
1538 case GL_LUMINANCE16_ALPHA16
:
1539 return (ctx
->API
== API_OPENGL_COMPAT
&&
1540 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1544 case GL_INTENSITY12
:
1545 case GL_INTENSITY16
:
1546 return (ctx
->API
== API_OPENGL_COMPAT
&&
1547 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1557 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1559 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1568 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1570 case GL_SRGB8_ALPHA8_EXT
:
1571 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1572 case GL_STENCIL_INDEX
:
1573 case GL_STENCIL_INDEX1_EXT
:
1574 case GL_STENCIL_INDEX4_EXT
:
1575 case GL_STENCIL_INDEX16_EXT
:
1576 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1577 * OpenGL ES, but Mesa does not currently support them.
1579 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1580 case GL_STENCIL_INDEX8_EXT
:
1581 return GL_STENCIL_INDEX
;
1582 case GL_DEPTH_COMPONENT
:
1583 case GL_DEPTH_COMPONENT32
:
1584 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1585 case GL_DEPTH_COMPONENT16
:
1586 case GL_DEPTH_COMPONENT24
:
1587 return GL_DEPTH_COMPONENT
;
1588 case GL_DEPTH_STENCIL
:
1589 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1590 case GL_DEPTH24_STENCIL8
:
1591 return GL_DEPTH_STENCIL
;
1592 case GL_DEPTH_COMPONENT32F
:
1593 return ctx
->Version
>= 30
1594 || (ctx
->API
== API_OPENGL_COMPAT
&&
1595 ctx
->Extensions
.ARB_depth_buffer_float
)
1596 ? GL_DEPTH_COMPONENT
: 0;
1597 case GL_DEPTH32F_STENCIL8
:
1598 return ctx
->Version
>= 30
1599 || (ctx
->API
== API_OPENGL_COMPAT
&&
1600 ctx
->Extensions
.ARB_depth_buffer_float
)
1601 ? GL_DEPTH_STENCIL
: 0;
1604 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1607 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1611 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1614 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1616 /* signed normalized texture formats */
1620 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1625 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1629 case GL_RGB16_SNORM
:
1630 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1633 case GL_RGBA8_SNORM
:
1634 case GL_RGBA16_SNORM
:
1635 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1637 case GL_ALPHA_SNORM
:
1638 case GL_ALPHA8_SNORM
:
1639 case GL_ALPHA16_SNORM
:
1640 return ctx
->API
== API_OPENGL_COMPAT
&&
1641 ctx
->Extensions
.EXT_texture_snorm
&&
1642 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1643 case GL_LUMINANCE_SNORM
:
1644 case GL_LUMINANCE8_SNORM
:
1645 case GL_LUMINANCE16_SNORM
:
1646 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1648 case GL_LUMINANCE_ALPHA_SNORM
:
1649 case GL_LUMINANCE8_ALPHA8_SNORM
:
1650 case GL_LUMINANCE16_ALPHA16_SNORM
:
1651 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1652 ? GL_LUMINANCE_ALPHA
: 0;
1653 case GL_INTENSITY_SNORM
:
1654 case GL_INTENSITY8_SNORM
:
1655 case GL_INTENSITY16_SNORM
:
1656 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1661 return ((_mesa_is_desktop_gl(ctx
) &&
1662 ctx
->Extensions
.ARB_texture_rg
&&
1663 ctx
->Extensions
.ARB_texture_float
) ||
1664 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1668 return ((_mesa_is_desktop_gl(ctx
) &&
1669 ctx
->Extensions
.ARB_texture_rg
&&
1670 ctx
->Extensions
.ARB_texture_float
) ||
1671 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1675 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1679 return ((_mesa_is_desktop_gl(ctx
) &&
1680 ctx
->Extensions
.ARB_texture_float
) ||
1681 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1683 case GL_ALPHA16F_ARB
:
1684 case GL_ALPHA32F_ARB
:
1685 return ctx
->API
== API_OPENGL_COMPAT
&&
1686 ctx
->Extensions
.ARB_texture_float
&&
1687 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1688 case GL_LUMINANCE16F_ARB
:
1689 case GL_LUMINANCE32F_ARB
:
1690 return ctx
->API
== API_OPENGL_COMPAT
&&
1691 ctx
->Extensions
.ARB_texture_float
&&
1692 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1693 case GL_LUMINANCE_ALPHA16F_ARB
:
1694 case GL_LUMINANCE_ALPHA32F_ARB
:
1695 return ctx
->API
== API_OPENGL_COMPAT
&&
1696 ctx
->Extensions
.ARB_texture_float
&&
1697 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1698 case GL_INTENSITY16F_ARB
:
1699 case GL_INTENSITY32F_ARB
:
1700 return ctx
->API
== API_OPENGL_COMPAT
&&
1701 ctx
->Extensions
.ARB_texture_float
&&
1702 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1703 case GL_R11F_G11F_B10F
:
1704 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1705 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1708 case GL_RGBA8UI_EXT
:
1709 case GL_RGBA16UI_EXT
:
1710 case GL_RGBA32UI_EXT
:
1712 case GL_RGBA16I_EXT
:
1713 case GL_RGBA32I_EXT
:
1714 return ctx
->Version
>= 30
1715 || (_mesa_is_desktop_gl(ctx
) &&
1716 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1719 case GL_RGB16UI_EXT
:
1720 case GL_RGB32UI_EXT
:
1724 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1732 return ctx
->Version
>= 30
1733 || (_mesa_is_desktop_gl(ctx
) &&
1734 ctx
->Extensions
.ARB_texture_rg
&&
1735 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1743 return ctx
->Version
>= 30
1744 || (_mesa_is_desktop_gl(ctx
) &&
1745 ctx
->Extensions
.ARB_texture_rg
&&
1746 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1748 case GL_INTENSITY8I_EXT
:
1749 case GL_INTENSITY8UI_EXT
:
1750 case GL_INTENSITY16I_EXT
:
1751 case GL_INTENSITY16UI_EXT
:
1752 case GL_INTENSITY32I_EXT
:
1753 case GL_INTENSITY32UI_EXT
:
1754 return ctx
->API
== API_OPENGL_COMPAT
&&
1755 ctx
->Extensions
.EXT_texture_integer
&&
1756 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1758 case GL_LUMINANCE8I_EXT
:
1759 case GL_LUMINANCE8UI_EXT
:
1760 case GL_LUMINANCE16I_EXT
:
1761 case GL_LUMINANCE16UI_EXT
:
1762 case GL_LUMINANCE32I_EXT
:
1763 case GL_LUMINANCE32UI_EXT
:
1764 return ctx
->API
== API_OPENGL_COMPAT
&&
1765 ctx
->Extensions
.EXT_texture_integer
&&
1766 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1768 case GL_LUMINANCE_ALPHA8I_EXT
:
1769 case GL_LUMINANCE_ALPHA8UI_EXT
:
1770 case GL_LUMINANCE_ALPHA16I_EXT
:
1771 case GL_LUMINANCE_ALPHA16UI_EXT
:
1772 case GL_LUMINANCE_ALPHA32I_EXT
:
1773 case GL_LUMINANCE_ALPHA32UI_EXT
:
1774 return ctx
->API
== API_OPENGL_COMPAT
&&
1775 ctx
->Extensions
.EXT_texture_integer
&&
1776 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1778 case GL_ALPHA8I_EXT
:
1779 case GL_ALPHA8UI_EXT
:
1780 case GL_ALPHA16I_EXT
:
1781 case GL_ALPHA16UI_EXT
:
1782 case GL_ALPHA32I_EXT
:
1783 case GL_ALPHA32UI_EXT
:
1784 return ctx
->API
== API_OPENGL_COMPAT
&&
1785 ctx
->Extensions
.EXT_texture_integer
&&
1786 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1789 return (_mesa_is_desktop_gl(ctx
) &&
1790 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1791 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1794 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1803 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1806 invalidate_rb(GLuint key
, void *data
, void *userData
)
1808 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1809 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1811 /* If this is a user-created FBO */
1812 if (_mesa_is_user_fbo(fb
)) {
1814 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1815 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1816 if (att
->Type
== GL_RENDERBUFFER
&&
1817 att
->Renderbuffer
== rb
) {
1818 /* Mark fb status as indeterminate to force re-validation */
1827 /** sentinal value, see below */
1828 #define NO_SAMPLES 1000
1832 * Helper function used by renderbuffer_storage_direct() and
1833 * renderbuffer_storage_target().
1834 * samples will be NO_SAMPLES if called by a non-multisample function.
1837 renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1838 GLenum internalFormat
, GLsizei width
,
1839 GLsizei height
, GLsizei samples
, const char *func
)
1842 GLenum sample_count_error
;
1844 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1845 if (baseFormat
== 0) {
1846 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1847 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1851 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1852 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid width %d)", func
,
1857 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1858 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid height %d)", func
,
1863 if (samples
== NO_SAMPLES
) {
1864 /* NumSamples == 0 indicates non-multisampling */
1868 /* check the sample count;
1869 * note: driver may choose to use more samples than what's requested
1871 sample_count_error
= _mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
1872 internalFormat
, samples
);
1873 if (sample_count_error
!= GL_NO_ERROR
) {
1874 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
1879 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1881 if (rb
->InternalFormat
== internalFormat
&&
1882 rb
->Width
== (GLuint
) width
&&
1883 rb
->Height
== (GLuint
) height
&&
1884 rb
->NumSamples
== samples
) {
1885 /* no change in allocation needed */
1889 /* These MUST get set by the AllocStorage func */
1890 rb
->Format
= MESA_FORMAT_NONE
;
1891 rb
->NumSamples
= samples
;
1893 /* Now allocate the storage */
1894 assert(rb
->AllocStorage
);
1895 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1896 /* No error - check/set fields now */
1897 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1898 assert(rb
->Width
== (GLuint
) width
);
1899 assert(rb
->Height
== (GLuint
) height
);
1900 rb
->InternalFormat
= internalFormat
;
1901 rb
->_BaseFormat
= baseFormat
;
1902 assert(rb
->_BaseFormat
!= 0);
1905 /* Probably ran out of memory - clear the fields */
1908 rb
->Format
= MESA_FORMAT_NONE
;
1909 rb
->InternalFormat
= GL_NONE
;
1910 rb
->_BaseFormat
= GL_NONE
;
1914 /* Invalidate the framebuffers the renderbuffer is attached in. */
1915 if (rb
->AttachedAnytime
) {
1916 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1921 * Helper function used by _mesa_NamedRenderbufferStorage*().
1922 * samples will be NO_SAMPLES if called by a non-multisample function.
1925 renderbuffer_storage_named(GLuint renderbuffer
, GLenum internalFormat
,
1926 GLsizei width
, GLsizei height
, GLsizei samples
,
1929 GET_CURRENT_CONTEXT(ctx
);
1931 if (MESA_VERBOSE
& VERBOSE_API
) {
1932 if (samples
== NO_SAMPLES
)
1933 _mesa_debug(ctx
, "%s(%u, %s, %d, %d)\n",
1935 _mesa_lookup_enum_by_nr(internalFormat
),
1938 _mesa_debug(ctx
, "%s(%u, %s, %d, %d, %d)\n",
1940 _mesa_lookup_enum_by_nr(internalFormat
),
1941 width
, height
, samples
);
1944 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1945 if (!rb
|| rb
== &DummyRenderbuffer
) {
1946 /* ID was reserved, but no real renderbuffer object made yet */
1947 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid renderbuffer %u)",
1948 func
, renderbuffer
);
1952 renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
, func
);
1956 * Helper function used by _mesa_RenderbufferStorage() and
1957 * _mesa_RenderbufferStorageMultisample().
1958 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1961 renderbuffer_storage_target(GLenum target
, GLenum internalFormat
,
1962 GLsizei width
, GLsizei height
, GLsizei samples
,
1965 GET_CURRENT_CONTEXT(ctx
);
1967 if (MESA_VERBOSE
& VERBOSE_API
) {
1968 if (samples
== NO_SAMPLES
)
1969 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1971 _mesa_lookup_enum_by_nr(target
),
1972 _mesa_lookup_enum_by_nr(internalFormat
),
1975 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1977 _mesa_lookup_enum_by_nr(target
),
1978 _mesa_lookup_enum_by_nr(internalFormat
),
1979 width
, height
, samples
);
1982 if (target
!= GL_RENDERBUFFER_EXT
) {
1983 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1987 if (!ctx
->CurrentRenderbuffer
) {
1988 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no renderbuffer bound)",
1993 renderbuffer_storage(ctx
, ctx
->CurrentRenderbuffer
, internalFormat
, width
,
1994 height
, samples
, func
);
1999 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
2001 struct gl_renderbuffer
*rb
;
2002 GET_CURRENT_CONTEXT(ctx
);
2004 if (!ctx
->Extensions
.OES_EGL_image
) {
2005 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2006 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2010 if (target
!= GL_RENDERBUFFER
) {
2011 _mesa_error(ctx
, GL_INVALID_ENUM
,
2012 "EGLImageTargetRenderbufferStorageOES");
2016 rb
= ctx
->CurrentRenderbuffer
;
2018 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2019 "EGLImageTargetRenderbufferStorageOES");
2023 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2025 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
2030 * Helper function for _mesa_GetRenderbufferParameteriv() and
2031 * _mesa_GetFramebufferAttachmentParameteriv()
2032 * We have to be careful to respect the base format. For example, if a
2033 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2034 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2035 * we need to return zero.
2038 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
2040 if (_mesa_base_format_has_channel(baseFormat
, pname
))
2041 return _mesa_get_format_bits(format
, pname
);
2049 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
2050 GLsizei width
, GLsizei height
)
2052 /* GL_ARB_fbo says calling this function is equivalent to calling
2053 * glRenderbufferStorageMultisample() with samples=0. We pass in
2054 * a token value here just for error reporting purposes.
2056 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2057 NO_SAMPLES
, "glRenderbufferStorage");
2062 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
2063 GLenum internalFormat
,
2064 GLsizei width
, GLsizei height
)
2066 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2067 samples
, "glRenderbufferStorageMultisample");
2072 * OpenGL ES version of glRenderBufferStorage.
2075 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
2076 GLsizei width
, GLsizei height
)
2078 switch (internalFormat
) {
2080 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2081 /* choose a closest format */
2082 internalFormat
= GL_RGB5
;
2088 renderbuffer_storage_target(target
, internalFormat
, width
, height
, 0,
2089 "glRenderbufferStorageEXT");
2093 _mesa_NamedRenderbufferStorage(GLuint renderbuffer
, GLenum internalformat
,
2094 GLsizei width
, GLsizei height
)
2096 /* GL_ARB_fbo says calling this function is equivalent to calling
2097 * glRenderbufferStorageMultisample() with samples=0. We pass in
2098 * a token value here just for error reporting purposes.
2100 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2101 NO_SAMPLES
, "glNamedRenderbufferStorage");
2105 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer
, GLsizei samples
,
2106 GLenum internalformat
,
2107 GLsizei width
, GLsizei height
)
2109 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2111 "glNamedRenderbufferStorageMultisample");
2116 get_render_buffer_parameteriv(struct gl_context
*ctx
,
2117 struct gl_renderbuffer
*rb
, GLenum pname
,
2118 GLint
*params
, const char *func
)
2120 /* No need to flush here since we're just quering state which is
2121 * not effected by rendering.
2125 case GL_RENDERBUFFER_WIDTH_EXT
:
2126 *params
= rb
->Width
;
2128 case GL_RENDERBUFFER_HEIGHT_EXT
:
2129 *params
= rb
->Height
;
2131 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
2132 *params
= rb
->InternalFormat
;
2134 case GL_RENDERBUFFER_RED_SIZE_EXT
:
2135 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
2136 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
2137 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
2138 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
2139 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
2140 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2142 case GL_RENDERBUFFER_SAMPLES
:
2143 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2144 || _mesa_is_gles3(ctx
)) {
2145 *params
= rb
->NumSamples
;
2150 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname=%s)", func
,
2151 _mesa_lookup_enum_by_nr(pname
));
2158 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2160 GET_CURRENT_CONTEXT(ctx
);
2162 if (target
!= GL_RENDERBUFFER_EXT
) {
2163 _mesa_error(ctx
, GL_INVALID_ENUM
,
2164 "glGetRenderbufferParameterivEXT(target)");
2168 if (!ctx
->CurrentRenderbuffer
) {
2169 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetRenderbufferParameterivEXT"
2170 "(no renderbuffer bound)");
2174 get_render_buffer_parameteriv(ctx
, ctx
->CurrentRenderbuffer
, pname
,
2175 params
, "glGetRenderbufferParameteriv");
2180 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer
, GLenum pname
,
2183 GET_CURRENT_CONTEXT(ctx
);
2185 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2186 if (!rb
|| rb
== &DummyRenderbuffer
) {
2187 /* ID was reserved, but no real renderbuffer object made yet */
2188 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetNamedRenderbufferParameteriv"
2189 "(invalid renderbuffer %i)", renderbuffer
);
2193 get_render_buffer_parameteriv(ctx
, rb
, pname
, params
,
2194 "glGetNamedRenderbufferParameteriv");
2198 GLboolean GLAPIENTRY
2199 _mesa_IsFramebuffer(GLuint framebuffer
)
2201 GET_CURRENT_CONTEXT(ctx
);
2202 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2204 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2205 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2213 * Check if any of the attachments of the given framebuffer are textures
2214 * (render to texture). Call ctx->Driver.RenderTexture() for such
2218 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2221 assert(ctx
->Driver
.RenderTexture
);
2223 if (_mesa_is_winsys_fbo(fb
))
2224 return; /* can't render to texture with winsys framebuffers */
2226 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2227 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2228 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2229 && driver_RenderTexture_is_safe(att
)) {
2230 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2237 * Examine all the framebuffer's attachments to see if any are textures.
2238 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2239 * notify the device driver that the texture image may have changed.
2242 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2244 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2245 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2248 if (ctx
->Driver
.FinishRenderTexture
) {
2250 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2251 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2252 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2253 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2254 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2262 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2264 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2265 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
2266 GLboolean bindReadBuf
, bindDrawBuf
;
2267 GET_CURRENT_CONTEXT(ctx
);
2270 case GL_DRAW_FRAMEBUFFER_EXT
:
2271 bindDrawBuf
= GL_TRUE
;
2272 bindReadBuf
= GL_FALSE
;
2274 case GL_READ_FRAMEBUFFER_EXT
:
2275 bindDrawBuf
= GL_FALSE
;
2276 bindReadBuf
= GL_TRUE
;
2278 case GL_FRAMEBUFFER_EXT
:
2279 bindDrawBuf
= GL_TRUE
;
2280 bindReadBuf
= GL_TRUE
;
2283 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2288 /* Binding a user-created framebuffer object */
2289 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2290 if (newDrawFb
== &DummyFramebuffer
) {
2291 /* ID was reserved, but no real framebuffer object made yet */
2294 else if (!newDrawFb
&& !allow_user_names
) {
2295 /* All FBO IDs must be Gen'd */
2296 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2301 /* create new framebuffer object */
2302 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2307 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2309 newReadFb
= newDrawFb
;
2312 /* Binding the window system framebuffer (which was originally set
2313 * with MakeCurrent).
2315 newDrawFb
= ctx
->WinSysDrawBuffer
;
2316 newReadFb
= ctx
->WinSysReadBuffer
;
2320 assert(newDrawFb
!= &DummyFramebuffer
);
2322 /* save pointers to current/old framebuffers */
2323 oldDrawFb
= ctx
->DrawBuffer
;
2324 oldReadFb
= ctx
->ReadBuffer
;
2326 /* check if really changing bindings */
2327 if (oldDrawFb
== newDrawFb
)
2328 bindDrawBuf
= GL_FALSE
;
2329 if (oldReadFb
== newReadFb
)
2330 bindReadBuf
= GL_FALSE
;
2333 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2335 * We also check if we're beginning and/or ending render-to-texture.
2336 * When a framebuffer with texture attachments is unbound, call
2337 * ctx->Driver.FinishRenderTexture().
2338 * When a framebuffer with texture attachments is bound, call
2339 * ctx->Driver.RenderTexture().
2341 * Note that if the ReadBuffer has texture attachments we don't consider
2342 * that a render-to-texture case.
2345 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2347 /* check if old readbuffer was render-to-texture */
2348 check_end_texture_render(ctx
, oldReadFb
);
2350 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2354 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2356 /* check if old framebuffer had any texture attachments */
2358 check_end_texture_render(ctx
, oldDrawFb
);
2360 /* check if newly bound framebuffer has any texture attachments */
2361 check_begin_texture_render(ctx
, newDrawFb
);
2363 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2366 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2367 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
2372 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2374 GET_CURRENT_CONTEXT(ctx
);
2376 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2377 * point, but they allow the use of user-generated names.
2379 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2384 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2386 /* This function should not be in the dispatch table for core profile /
2387 * OpenGL 3.1, so execution should never get here in those cases -- no
2388 * need for an explicit test.
2390 bind_framebuffer(target
, framebuffer
, true);
2395 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2398 GET_CURRENT_CONTEXT(ctx
);
2401 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2405 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2407 for (i
= 0; i
< n
; i
++) {
2408 if (framebuffers
[i
] > 0) {
2409 struct gl_framebuffer
*fb
;
2410 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2412 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2414 /* check if deleting currently bound framebuffer object */
2415 if (fb
== ctx
->DrawBuffer
) {
2417 assert(fb
->RefCount
>= 2);
2418 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2420 if (fb
== ctx
->ReadBuffer
) {
2422 assert(fb
->RefCount
>= 2);
2423 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2426 /* remove from hash table immediately, to free the ID */
2427 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2429 if (fb
!= &DummyFramebuffer
) {
2430 /* But the object will not be freed until it's no longer
2431 * bound in any context.
2433 _mesa_reference_framebuffer(&fb
, NULL
);
2442 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2443 * It is not exposed to the rest of Mesa to encourage the use of
2444 * nameless buffers in driver internals.
2447 create_framebuffers(GLsizei n
, GLuint
*framebuffers
, bool dsa
)
2449 GET_CURRENT_CONTEXT(ctx
);
2452 struct gl_framebuffer
*fb
;
2454 const char *func
= dsa
? "glCreateFramebuffers" : "glGenFramebuffers";
2457 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
2464 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2466 for (i
= 0; i
< n
; i
++) {
2467 GLuint name
= first
+ i
;
2468 framebuffers
[i
] = name
;
2471 fb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffers
[i
]);
2473 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2478 fb
= &DummyFramebuffer
;
2480 mtx_lock(&ctx
->Shared
->Mutex
);
2481 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, fb
);
2482 mtx_unlock(&ctx
->Shared
->Mutex
);
2488 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2490 create_framebuffers(n
, framebuffers
, false);
2495 _mesa_CreateFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2497 create_framebuffers(n
, framebuffers
, true);
2502 _mesa_CheckFramebufferStatus(GLenum target
)
2504 struct gl_framebuffer
*buffer
;
2505 GET_CURRENT_CONTEXT(ctx
);
2507 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2509 if (MESA_VERBOSE
& VERBOSE_API
)
2510 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2511 _mesa_lookup_enum_by_nr(target
));
2513 buffer
= get_framebuffer_target(ctx
, target
);
2515 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2519 if (_mesa_is_winsys_fbo(buffer
)) {
2520 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2521 if (buffer
!= &IncompleteFramebuffer
) {
2522 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2524 return GL_FRAMEBUFFER_UNDEFINED
;
2528 /* No need to flush here */
2530 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2531 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2534 return buffer
->_Status
;
2539 * Replicate the src attachment point. Used by framebuffer_texture() when
2540 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2541 * GL_STENCIL_ATTACHMENT.
2544 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2545 gl_buffer_index dst
,
2546 gl_buffer_index src
)
2548 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2549 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2551 assert(src_att
->Texture
!= NULL
);
2552 assert(src_att
->Renderbuffer
!= NULL
);
2554 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2555 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2556 dst_att
->Type
= src_att
->Type
;
2557 dst_att
->Complete
= src_att
->Complete
;
2558 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2559 dst_att
->Zoffset
= src_att
->Zoffset
;
2564 * Common code called by glFramebufferTexture1D/2D/3D() and
2565 * glFramebufferTextureLayer().
2567 * \param textarget is the textarget that was passed to the
2568 * glFramebufferTexture...() function, or 0 if the corresponding function
2569 * doesn't have a textarget parameter.
2571 * \param layered is true if this function was called from
2572 * glFramebufferTexture(), false otherwise.
2575 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2576 GLenum attachment
, GLenum textarget
, GLuint texture
,
2577 GLint level
, GLuint zoffset
, GLboolean layered
)
2579 struct gl_renderbuffer_attachment
*att
;
2580 struct gl_texture_object
*texObj
= NULL
;
2581 struct gl_framebuffer
*fb
;
2582 GLenum maxLevelsTarget
;
2584 fb
= get_framebuffer_target(ctx
, target
);
2586 _mesa_error(ctx
, GL_INVALID_ENUM
,
2587 "glFramebufferTexture%s(target=0x%x)", caller
, target
);
2591 /* check framebuffer binding */
2592 if (_mesa_is_winsys_fbo(fb
)) {
2593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2594 "glFramebufferTexture%s", caller
);
2598 /* The textarget, level, and zoffset parameters are only validated if
2599 * texture is non-zero.
2602 GLboolean err
= GL_TRUE
;
2604 texObj
= _mesa_lookup_texture(ctx
, texture
);
2605 if (texObj
!= NULL
) {
2606 if (textarget
== 0) {
2608 /* We're being called by glFramebufferTexture() and textarget
2611 switch (texObj
->Target
) {
2613 case GL_TEXTURE_1D_ARRAY_EXT
:
2614 case GL_TEXTURE_2D_ARRAY_EXT
:
2615 case GL_TEXTURE_CUBE_MAP
:
2616 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2617 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2622 case GL_TEXTURE_RECTANGLE
:
2623 case GL_TEXTURE_2D_MULTISAMPLE
:
2624 /* These texture types are valid to pass to
2625 * glFramebufferTexture(), but since they aren't layered, it
2626 * is equivalent to calling glFramebufferTexture{1D,2D}().
2630 textarget
= texObj
->Target
;
2637 /* We're being called by glFramebufferTextureLayer() and
2638 * textarget is not used. The only legal texture types for
2639 * that function are 3D and 1D/2D arrays textures.
2641 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2642 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2643 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2644 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2645 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2649 /* Make sure textarget is consistent with the texture's type */
2650 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2651 ? !_mesa_is_cube_face(textarget
)
2652 : (texObj
->Target
!= textarget
);
2656 /* can't render to a non-existant texture */
2657 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2658 "glFramebufferTexture%s(non existant texture)",
2664 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2665 "glFramebufferTexture%s(texture target mismatch)",
2670 if (texObj
->Target
== GL_TEXTURE_3D
) {
2671 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2672 if (zoffset
>= maxSize
) {
2673 _mesa_error(ctx
, GL_INVALID_VALUE
,
2674 "glFramebufferTexture%s(zoffset)", caller
);
2678 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2679 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2680 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2681 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2682 if (zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
2683 _mesa_error(ctx
, GL_INVALID_VALUE
,
2684 "glFramebufferTexture%s(layer)", caller
);
2689 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2691 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2692 _mesa_error(ctx
, GL_INVALID_VALUE
,
2693 "glFramebufferTexture%s(level)", caller
);
2698 att
= get_attachment(ctx
, fb
, attachment
);
2700 _mesa_error(ctx
, GL_INVALID_ENUM
,
2701 "glFramebufferTexture%s(attachment)", caller
);
2705 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2707 mtx_lock(&fb
->Mutex
);
2709 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2710 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2711 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2712 _mesa_tex_target_to_face(textarget
) ==
2713 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2714 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2715 /* The texture object is already attached to the stencil attachment
2716 * point. Don't create a new renderbuffer; just reuse the stencil
2717 * attachment's. This is required to prevent a GL error in
2718 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2720 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2722 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2723 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2724 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2725 _mesa_tex_target_to_face(textarget
) ==
2726 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2727 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2728 /* As above, but with depth and stencil transposed. */
2729 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2732 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2733 level
, zoffset
, layered
);
2734 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2735 /* Above we created a new renderbuffer and attached it to the
2736 * depth attachment point. Now attach it to the stencil attachment
2739 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2740 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2745 /* Set the render-to-texture flag. We'll check this flag in
2746 * glTexImage() and friends to determine if we need to revalidate
2747 * any FBOs that might be rendering into this texture.
2748 * This flag never gets cleared since it's non-trivial to determine
2749 * when all FBOs might be done rendering to this texture. That's OK
2750 * though since it's uncommon to render to a texture then repeatedly
2751 * call glTexImage() to change images in the texture.
2753 texObj
->_RenderToTexture
= GL_TRUE
;
2756 remove_attachment(ctx
, att
);
2757 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2758 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2759 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2763 invalidate_framebuffer(fb
);
2765 mtx_unlock(&fb
->Mutex
);
2770 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2771 GLenum textarget
, GLuint texture
, GLint level
)
2773 GET_CURRENT_CONTEXT(ctx
);
2778 switch (textarget
) {
2782 case GL_TEXTURE_1D_ARRAY
:
2783 error
= !ctx
->Extensions
.EXT_texture_array
;
2790 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2791 "glFramebufferTexture1D(textarget=%s)",
2792 _mesa_lookup_enum_by_nr(textarget
));
2797 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2798 level
, 0, GL_FALSE
);
2803 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2804 GLenum textarget
, GLuint texture
, GLint level
)
2806 GET_CURRENT_CONTEXT(ctx
);
2811 switch (textarget
) {
2815 case GL_TEXTURE_RECTANGLE
:
2816 error
= _mesa_is_gles(ctx
)
2817 || !ctx
->Extensions
.NV_texture_rectangle
;
2819 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2820 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2821 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2822 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2823 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2824 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2825 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2827 case GL_TEXTURE_2D_ARRAY
:
2828 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2829 || !ctx
->Extensions
.EXT_texture_array
;
2831 case GL_TEXTURE_2D_MULTISAMPLE
:
2832 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2833 error
= _mesa_is_gles(ctx
)
2834 || !ctx
->Extensions
.ARB_texture_multisample
;
2841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2842 "glFramebufferTexture2D(textarget=%s)",
2843 _mesa_lookup_enum_by_nr(textarget
));
2848 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2849 level
, 0, GL_FALSE
);
2854 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2855 GLenum textarget
, GLuint texture
,
2856 GLint level
, GLint zoffset
)
2858 GET_CURRENT_CONTEXT(ctx
);
2860 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2862 "glFramebufferTexture3D(textarget)");
2866 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2867 level
, zoffset
, GL_FALSE
);
2872 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2873 GLuint texture
, GLint level
, GLint layer
)
2875 GET_CURRENT_CONTEXT(ctx
);
2877 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2878 level
, layer
, GL_FALSE
);
2883 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
2884 GLuint texture
, GLint level
)
2886 GET_CURRENT_CONTEXT(ctx
);
2888 if (_mesa_has_geometry_shaders(ctx
)) {
2889 framebuffer_texture(ctx
, "", target
, attachment
, 0, texture
,
2892 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2893 "unsupported function (glFramebufferTexture) called");
2899 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2900 GLenum renderbufferTarget
,
2901 GLuint renderbuffer
)
2903 struct gl_renderbuffer_attachment
*att
;
2904 struct gl_framebuffer
*fb
;
2905 struct gl_renderbuffer
*rb
;
2906 GET_CURRENT_CONTEXT(ctx
);
2908 fb
= get_framebuffer_target(ctx
, target
);
2910 _mesa_error(ctx
, GL_INVALID_ENUM
,
2911 "glFramebufferRenderbuffer(target)");
2915 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2916 _mesa_error(ctx
, GL_INVALID_ENUM
,
2917 "glFramebufferRenderbuffer(renderbufferTarget)");
2921 if (_mesa_is_winsys_fbo(fb
)) {
2922 /* Can't attach new renderbuffers to a window system framebuffer */
2923 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbuffer");
2927 att
= get_attachment(ctx
, fb
, attachment
);
2929 _mesa_error(ctx
, GL_INVALID_ENUM
,
2930 "glFramebufferRenderbuffer(invalid attachment %s)",
2931 _mesa_lookup_enum_by_nr(attachment
));
2936 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2939 "glFramebufferRenderbuffer(non-existant"
2940 " renderbuffer %u)", renderbuffer
);
2943 else if (rb
== &DummyRenderbuffer
) {
2944 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2945 "glFramebufferRenderbuffer(renderbuffer %u)",
2951 /* remove renderbuffer attachment */
2955 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2956 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2957 /* make sure the renderbuffer is a depth/stencil format */
2958 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2959 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2960 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2961 "glFramebufferRenderbuffer(renderbuffer"
2962 " is not DEPTH_STENCIL format)");
2967 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2969 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2970 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2972 /* Some subsequent GL commands may depend on the framebuffer's visual
2973 * after the binding is updated. Update visual info now.
2975 _mesa_update_framebuffer_visual(ctx
, fb
);
2980 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2981 GLenum pname
, GLint
*params
)
2983 const struct gl_renderbuffer_attachment
*att
;
2984 struct gl_framebuffer
*buffer
;
2986 GET_CURRENT_CONTEXT(ctx
);
2988 /* The error differs in GL and GLES. */
2989 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2991 buffer
= get_framebuffer_target(ctx
, target
);
2993 _mesa_error(ctx
, GL_INVALID_ENUM
,
2994 "glGetFramebufferAttachmentParameteriv(target)");
2998 if (_mesa_is_winsys_fbo(buffer
)) {
2999 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3002 * "If the framebuffer currently bound to target is zero, then
3003 * INVALID_OPERATION is generated."
3005 * The EXT_framebuffer_object spec has the same wording, and the
3006 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3009 if ((!_mesa_is_desktop_gl(ctx
) ||
3010 !ctx
->Extensions
.ARB_framebuffer_object
)
3011 && !_mesa_is_gles3(ctx
)) {
3012 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3013 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
3017 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
3018 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
3019 _mesa_error(ctx
, GL_INVALID_ENUM
,
3020 "glGetFramebufferAttachmentParameteriv(attachment)");
3023 /* the default / window-system FBO */
3024 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
3027 /* user-created framebuffer FBO */
3028 att
= get_attachment(ctx
, buffer
, attachment
);
3032 _mesa_error(ctx
, GL_INVALID_ENUM
,
3033 "glGetFramebufferAttachmentParameteriv(attachment)");
3037 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3038 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
3039 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
3040 /* This behavior is first specified in OpenGL 4.4 specification.
3042 * From the OpenGL 4.4 spec page 275:
3043 * "This query cannot be performed for a combined depth+stencil
3044 * attachment, since it does not have a single format."
3046 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3047 "glGetFramebufferAttachmentParameteriv("
3048 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3049 " is invalid for depth+stencil attachment)");
3052 /* the depth and stencil attachments must point to the same buffer */
3053 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
3054 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
3055 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
3056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3057 "glGetFramebufferAttachmentParameteriv(DEPTH/STENCIL"
3058 " attachments differ)");
3063 /* No need to flush here */
3066 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
3067 *params
= _mesa_is_winsys_fbo(buffer
)
3068 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
3070 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
3071 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
3072 *params
= att
->Renderbuffer
->Name
;
3074 else if (att
->Type
== GL_TEXTURE
) {
3075 *params
= att
->Texture
->Name
;
3078 assert(att
->Type
== GL_NONE
);
3079 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
3082 goto invalid_pname_enum
;
3086 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
3087 if (att
->Type
== GL_TEXTURE
) {
3088 *params
= att
->TextureLevel
;
3090 else if (att
->Type
== GL_NONE
) {
3091 _mesa_error(ctx
, err
,
3092 "glGetFramebufferAttachmentParameteriv(pname)");
3095 goto invalid_pname_enum
;
3098 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
3099 if (att
->Type
== GL_TEXTURE
) {
3100 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
3101 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
3107 else if (att
->Type
== GL_NONE
) {
3108 _mesa_error(ctx
, err
,
3109 "glGetFramebufferAttachmentParameteriv(pname)");
3112 goto invalid_pname_enum
;
3115 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
3116 if (ctx
->API
== API_OPENGLES
) {
3117 goto invalid_pname_enum
;
3118 } else if (att
->Type
== GL_NONE
) {
3119 _mesa_error(ctx
, err
,
3120 "glGetFramebufferAttachmentParameteriv(pname)");
3121 } else if (att
->Type
== GL_TEXTURE
) {
3122 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
3123 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
3124 *params
= att
->Zoffset
;
3131 goto invalid_pname_enum
;
3134 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
3135 if ((!_mesa_is_desktop_gl(ctx
) ||
3136 !ctx
->Extensions
.ARB_framebuffer_object
)
3137 && !_mesa_is_gles3(ctx
)) {
3138 goto invalid_pname_enum
;
3140 else if (att
->Type
== GL_NONE
) {
3141 _mesa_error(ctx
, err
,
3142 "glGetFramebufferAttachmentParameteriv(pname)");
3145 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3147 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
3150 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3151 * if the sRGB conversion is unsupported. */
3152 *params
= GL_LINEAR
;
3156 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
3157 if ((ctx
->API
!= API_OPENGL_COMPAT
||
3158 !ctx
->Extensions
.ARB_framebuffer_object
)
3159 && ctx
->API
!= API_OPENGL_CORE
3160 && !_mesa_is_gles3(ctx
)) {
3161 goto invalid_pname_enum
;
3163 else if (att
->Type
== GL_NONE
) {
3164 _mesa_error(ctx
, err
,
3165 "glGetFramebufferAttachmentParameteriv(pname)");
3168 mesa_format format
= att
->Renderbuffer
->Format
;
3170 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3173 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3174 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3175 * generate an INVALID_OPERATION error.
3177 if (_mesa_is_gles3(ctx
) &&
3178 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3179 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3180 "glGetFramebufferAttachmentParameteriv(cannot query "
3181 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3182 "GL_DEPTH_STENCIL_ATTACHMENT");
3186 if (format
== MESA_FORMAT_S_UINT8
) {
3190 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
3191 /* depends on the attachment parameter */
3192 if (attachment
== GL_STENCIL_ATTACHMENT
) {
3200 *params
= _mesa_get_format_datatype(format
);
3204 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
3205 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
3206 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
3207 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
3208 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
3209 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
3210 if ((!_mesa_is_desktop_gl(ctx
) ||
3211 !ctx
->Extensions
.ARB_framebuffer_object
)
3212 && !_mesa_is_gles3(ctx
)) {
3213 goto invalid_pname_enum
;
3215 else if (att
->Type
== GL_NONE
) {
3216 _mesa_error(ctx
, err
,
3217 "glGetFramebufferAttachmentParameteriv(pname)");
3219 else if (att
->Texture
) {
3220 const struct gl_texture_image
*texImage
=
3221 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
3224 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
3225 texImage
->TexFormat
);
3231 else if (att
->Renderbuffer
) {
3232 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
3233 att
->Renderbuffer
->Format
);
3236 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
3237 " invalid FBO attachment structure");
3240 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
3241 if (!_mesa_has_geometry_shaders(ctx
)) {
3242 goto invalid_pname_enum
;
3243 } else if (att
->Type
== GL_TEXTURE
) {
3244 *params
= att
->Layered
;
3245 } else if (att
->Type
== GL_NONE
) {
3246 _mesa_error(ctx
, err
,
3247 "glGetFramebufferAttachmentParameteriv(pname)");
3249 goto invalid_pname_enum
;
3253 goto invalid_pname_enum
;
3259 _mesa_error(ctx
, GL_INVALID_ENUM
,
3260 "glGetFramebufferAttachmentParameteriv(pname)");
3266 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3267 const GLenum
*attachments
, GLint x
, GLint y
,
3268 GLsizei width
, GLsizei height
, const char *name
)
3271 struct gl_framebuffer
*fb
;
3272 GET_CURRENT_CONTEXT(ctx
);
3274 fb
= get_framebuffer_target(ctx
, target
);
3276 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3280 if (numAttachments
< 0) {
3281 _mesa_error(ctx
, GL_INVALID_VALUE
,
3282 "%s(numAttachments < 0)", name
);
3286 /* The GL_ARB_invalidate_subdata spec says:
3288 * "If an attachment is specified that does not exist in the
3289 * framebuffer bound to <target>, it is ignored."
3293 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3294 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3295 * INVALID_OPERATION is generated."
3297 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3298 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3299 * set of retrictions).
3301 for (i
= 0; i
< numAttachments
; i
++) {
3302 if (_mesa_is_winsys_fbo(fb
)) {
3303 switch (attachments
[i
]) {
3309 /* Accumulation buffers and auxilary buffers were removed in
3310 * OpenGL 3.1, and they never existed in OpenGL ES.
3312 if (ctx
->API
!= API_OPENGL_COMPAT
)
3322 case GL_FRONT_RIGHT
:
3323 if (!_mesa_is_desktop_gl(ctx
))
3330 switch (attachments
[i
]) {
3331 case GL_DEPTH_ATTACHMENT
:
3332 case GL_STENCIL_ATTACHMENT
:
3334 case GL_DEPTH_STENCIL_ATTACHMENT
:
3335 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3336 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3337 * extension does not make this attachment point valid on ES 2.0.
3339 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
3342 case GL_COLOR_ATTACHMENT0
:
3343 case GL_COLOR_ATTACHMENT1
:
3344 case GL_COLOR_ATTACHMENT2
:
3345 case GL_COLOR_ATTACHMENT3
:
3346 case GL_COLOR_ATTACHMENT4
:
3347 case GL_COLOR_ATTACHMENT5
:
3348 case GL_COLOR_ATTACHMENT6
:
3349 case GL_COLOR_ATTACHMENT7
:
3350 case GL_COLOR_ATTACHMENT8
:
3351 case GL_COLOR_ATTACHMENT9
:
3352 case GL_COLOR_ATTACHMENT10
:
3353 case GL_COLOR_ATTACHMENT11
:
3354 case GL_COLOR_ATTACHMENT12
:
3355 case GL_COLOR_ATTACHMENT13
:
3356 case GL_COLOR_ATTACHMENT14
:
3357 case GL_COLOR_ATTACHMENT15
: {
3358 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3359 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3361 "%s(attachment >= max. color attachments)", name
);
3372 /* We don't actually do anything for this yet. Just return after
3373 * validating the parameters and generating the required errors.
3378 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3384 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3385 const GLenum
*attachments
, GLint x
, GLint y
,
3386 GLsizei width
, GLsizei height
)
3388 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3389 x
, y
, width
, height
,
3390 "glInvalidateSubFramebuffer");
3395 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3396 const GLenum
*attachments
)
3398 /* The GL_ARB_invalidate_subdata spec says:
3402 * void InvalidateFramebuffer(enum target,
3403 * sizei numAttachments,
3404 * const enum *attachments);
3406 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3407 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3408 * <MAX_VIEWPORT_DIMS[1]> respectively."
3410 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3412 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3413 "glInvalidateFramebuffer");
3418 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3419 const GLenum
*attachments
)
3421 struct gl_framebuffer
*fb
;
3424 GET_CURRENT_CONTEXT(ctx
);
3426 fb
= get_framebuffer_target(ctx
, target
);
3428 _mesa_error(ctx
, GL_INVALID_ENUM
,
3429 "glDiscardFramebufferEXT(target %s)",
3430 _mesa_lookup_enum_by_nr(target
));
3434 if (numAttachments
< 0) {
3435 _mesa_error(ctx
, GL_INVALID_VALUE
,
3436 "glDiscardFramebufferEXT(numAttachments < 0)");
3440 for (i
= 0; i
< numAttachments
; i
++) {
3441 switch (attachments
[i
]) {
3445 if (_mesa_is_user_fbo(fb
))
3448 case GL_COLOR_ATTACHMENT0
:
3449 case GL_DEPTH_ATTACHMENT
:
3450 case GL_STENCIL_ATTACHMENT
:
3451 if (_mesa_is_winsys_fbo(fb
))
3459 if (ctx
->Driver
.DiscardFramebuffer
)
3460 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3465 _mesa_error(ctx
, GL_INVALID_ENUM
,
3466 "glDiscardFramebufferEXT(attachment %s)",
3467 _mesa_lookup_enum_by_nr(attachments
[i
]));