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 * Helper routine for getting a gl_framebuffer.
126 struct gl_framebuffer
*
127 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
129 struct gl_framebuffer
*fb
;
134 fb
= (struct gl_framebuffer
*)
135 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
141 * A convenience function for direct state access that throws
142 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
144 struct gl_framebuffer
*
145 _mesa_lookup_framebuffer_err(struct gl_context
*ctx
, GLuint id
,
148 struct gl_framebuffer
*fb
;
150 fb
= _mesa_lookup_framebuffer(ctx
, id
);
151 if (!fb
|| fb
== &DummyFramebuffer
) {
152 _mesa_error(ctx
, GL_INVALID_OPERATION
,
153 "%s(non-existent framebuffer %u)", func
, id
);
162 * Mark the given framebuffer as invalid. This will force the
163 * test for framebuffer completeness to be done before the framebuffer
167 invalidate_framebuffer(struct gl_framebuffer
*fb
)
169 fb
->_Status
= 0; /* "indeterminate" */
174 * Return the gl_framebuffer object which corresponds to the given
175 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
176 * Check support for GL_EXT_framebuffer_blit to determine if certain
178 * \return gl_framebuffer pointer or NULL if target is illegal
180 static struct gl_framebuffer
*
181 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
183 bool have_fb_blit
= _mesa_is_gles3(ctx
) || _mesa_is_desktop_gl(ctx
);
185 case GL_DRAW_FRAMEBUFFER
:
186 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
187 case GL_READ_FRAMEBUFFER
:
188 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
189 case GL_FRAMEBUFFER_EXT
:
190 return ctx
->DrawBuffer
;
198 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
199 * gl_renderbuffer_attachment object.
200 * This function is only used for user-created FB objects, not the
201 * default / window-system FB object.
202 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
203 * the depth buffer attachment point.
205 static struct gl_renderbuffer_attachment
*
206 get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
211 assert(_mesa_is_user_fbo(fb
));
213 switch (attachment
) {
214 case GL_COLOR_ATTACHMENT0_EXT
:
215 case GL_COLOR_ATTACHMENT1_EXT
:
216 case GL_COLOR_ATTACHMENT2_EXT
:
217 case GL_COLOR_ATTACHMENT3_EXT
:
218 case GL_COLOR_ATTACHMENT4_EXT
:
219 case GL_COLOR_ATTACHMENT5_EXT
:
220 case GL_COLOR_ATTACHMENT6_EXT
:
221 case GL_COLOR_ATTACHMENT7_EXT
:
222 case GL_COLOR_ATTACHMENT8_EXT
:
223 case GL_COLOR_ATTACHMENT9_EXT
:
224 case GL_COLOR_ATTACHMENT10_EXT
:
225 case GL_COLOR_ATTACHMENT11_EXT
:
226 case GL_COLOR_ATTACHMENT12_EXT
:
227 case GL_COLOR_ATTACHMENT13_EXT
:
228 case GL_COLOR_ATTACHMENT14_EXT
:
229 case GL_COLOR_ATTACHMENT15_EXT
:
230 /* Only OpenGL ES 1.x forbids color attachments other than
231 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
234 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
235 if (i
>= ctx
->Const
.MaxColorAttachments
236 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
239 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
240 case GL_DEPTH_STENCIL_ATTACHMENT
:
241 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
244 case GL_DEPTH_ATTACHMENT_EXT
:
245 return &fb
->Attachment
[BUFFER_DEPTH
];
246 case GL_STENCIL_ATTACHMENT_EXT
:
247 return &fb
->Attachment
[BUFFER_STENCIL
];
255 * As above, but only used for getting attachments of the default /
256 * window-system framebuffer (not user-created framebuffer objects).
258 static struct gl_renderbuffer_attachment
*
259 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
262 assert(_mesa_is_winsys_fbo(fb
));
264 if (_mesa_is_gles3(ctx
)) {
265 assert(attachment
== GL_BACK
||
266 attachment
== GL_DEPTH
||
267 attachment
== GL_STENCIL
);
268 switch (attachment
) {
270 /* Since there is no stereo rendering in ES 3.0, only return the
273 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
274 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
275 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
277 return &fb
->Attachment
[BUFFER_DEPTH
];
279 return &fb
->Attachment
[BUFFER_STENCIL
];
283 switch (attachment
) {
285 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
287 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
289 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
291 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
293 if (fb
->Visual
.numAuxBuffers
== 1) {
294 return &fb
->Attachment
[BUFFER_AUX0
];
298 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
300 * "If the default framebuffer is bound to target, then attachment must
301 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
302 * identifying a color buffer; DEPTH, identifying the depth buffer; or
303 * STENCIL, identifying the stencil buffer."
305 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
306 * language. However, revision #33 of the ARB_framebuffer_object spec
309 * "If the default framebuffer is bound to <target>, then <attachment>
310 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
311 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
312 * depth buffer, or the stencil buffer, and <pname> may be
313 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
314 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
316 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
317 * from glext.h, so shipping apps should not use those values.
319 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
320 * support queries of the window system FBO.
323 return &fb
->Attachment
[BUFFER_DEPTH
];
325 return &fb
->Attachment
[BUFFER_STENCIL
];
334 * Remove any texture or renderbuffer attached to the given attachment
335 * point. Update reference counts, etc.
338 remove_attachment(struct gl_context
*ctx
,
339 struct gl_renderbuffer_attachment
*att
)
341 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
343 /* tell driver that we're done rendering to this texture. */
344 if (rb
&& rb
->NeedsFinishRenderTexture
)
345 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
347 if (att
->Type
== GL_TEXTURE
) {
348 assert(att
->Texture
);
349 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
350 assert(!att
->Texture
);
352 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
353 assert(!att
->Texture
);
354 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
355 assert(!att
->Renderbuffer
);
358 att
->Complete
= GL_TRUE
;
362 * Verify a couple error conditions that will lead to an incomplete FBO and
363 * may cause problems for the driver's RenderTexture path.
366 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment
*att
)
368 const struct gl_texture_image
*const texImage
=
369 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
371 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
374 if ((texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
375 && att
->Zoffset
>= texImage
->Height
)
376 || (texImage
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
377 && att
->Zoffset
>= texImage
->Depth
))
384 * Create a renderbuffer which will be set up by the driver to wrap the
385 * texture image slice.
387 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
388 * to share most of their framebuffer rendering code between winsys,
389 * renderbuffer, and texture attachments.
391 * The allocated renderbuffer uses a non-zero Name so that drivers can check
392 * it for determining vertical orientation, but we use ~0 to make it fairly
393 * unambiguous with actual user (non-texture) renderbuffers.
396 _mesa_update_texture_renderbuffer(struct gl_context
*ctx
,
397 struct gl_framebuffer
*fb
,
398 struct gl_renderbuffer_attachment
*att
)
400 struct gl_texture_image
*texImage
;
401 struct gl_renderbuffer
*rb
;
403 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
405 rb
= att
->Renderbuffer
;
407 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, ~0);
409 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
412 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
414 /* This can't get called on a texture renderbuffer, so set it to NULL
415 * for clarity compared to user renderbuffers.
417 rb
->AllocStorage
= NULL
;
419 rb
->NeedsFinishRenderTexture
= ctx
->Driver
.FinishRenderTexture
!= NULL
;
425 rb
->_BaseFormat
= texImage
->_BaseFormat
;
426 rb
->Format
= texImage
->TexFormat
;
427 rb
->InternalFormat
= texImage
->InternalFormat
;
428 rb
->Width
= texImage
->Width2
;
429 rb
->Height
= texImage
->Height2
;
430 rb
->Depth
= texImage
->Depth2
;
431 rb
->NumSamples
= texImage
->NumSamples
;
432 rb
->TexImage
= texImage
;
434 if (driver_RenderTexture_is_safe(att
))
435 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
439 * Bind a texture object to an attachment point.
440 * The previous binding, if any, will be removed first.
443 set_texture_attachment(struct gl_context
*ctx
,
444 struct gl_framebuffer
*fb
,
445 struct gl_renderbuffer_attachment
*att
,
446 struct gl_texture_object
*texObj
,
447 GLenum texTarget
, GLuint level
, GLuint zoffset
,
450 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
452 if (rb
&& rb
->NeedsFinishRenderTexture
)
453 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
455 if (att
->Texture
== texObj
) {
456 /* re-attaching same texture */
457 assert(att
->Type
== GL_TEXTURE
);
461 remove_attachment(ctx
, att
);
462 att
->Type
= GL_TEXTURE
;
463 assert(!att
->Texture
);
464 _mesa_reference_texobj(&att
->Texture
, texObj
);
466 invalidate_framebuffer(fb
);
468 /* always update these fields */
469 att
->TextureLevel
= level
;
470 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
471 att
->Zoffset
= zoffset
;
472 att
->Layered
= layered
;
473 att
->Complete
= GL_FALSE
;
475 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
480 * Bind a renderbuffer to an attachment point.
481 * The previous binding, if any, will be removed first.
484 set_renderbuffer_attachment(struct gl_context
*ctx
,
485 struct gl_renderbuffer_attachment
*att
,
486 struct gl_renderbuffer
*rb
)
488 /* XXX check if re-doing same attachment, exit early */
489 remove_attachment(ctx
, att
);
490 att
->Type
= GL_RENDERBUFFER_EXT
;
491 att
->Texture
= NULL
; /* just to be safe */
492 att
->Layered
= GL_FALSE
;
493 att
->Complete
= GL_FALSE
;
494 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
499 * Fallback for ctx->Driver.FramebufferRenderbuffer()
500 * Attach a renderbuffer object to a framebuffer object.
503 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
504 struct gl_framebuffer
*fb
,
505 GLenum attachment
, struct gl_renderbuffer
*rb
)
507 struct gl_renderbuffer_attachment
*att
;
509 mtx_lock(&fb
->Mutex
);
511 att
= get_attachment(ctx
, fb
, attachment
);
514 set_renderbuffer_attachment(ctx
, att
, rb
);
515 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
516 /* do stencil attachment here (depth already done above) */
517 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
519 set_renderbuffer_attachment(ctx
, att
, rb
);
521 rb
->AttachedAnytime
= GL_TRUE
;
524 remove_attachment(ctx
, att
);
525 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
526 /* detach stencil (depth was detached above) */
527 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
529 remove_attachment(ctx
, att
);
533 invalidate_framebuffer(fb
);
535 mtx_unlock(&fb
->Mutex
);
540 * Fallback for ctx->Driver.ValidateFramebuffer()
541 * Check if the renderbuffer's formats are supported by the software
543 * Drivers should probably override this.
546 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
549 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
550 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
552 switch (rb
->_BaseFormat
) {
554 case GL_LUMINANCE_ALPHA
:
559 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
563 switch (rb
->Format
) {
564 /* XXX This list is likely incomplete. */
565 case MESA_FORMAT_R9G9B9E5_FLOAT
:
566 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
569 /* render buffer format is supported by software rendering */
578 * Return true if the framebuffer has a combined depth/stencil
579 * renderbuffer attached.
582 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
584 const struct gl_renderbuffer_attachment
*depth
=
585 &fb
->Attachment
[BUFFER_DEPTH
];
586 const struct gl_renderbuffer_attachment
*stencil
=
587 &fb
->Attachment
[BUFFER_STENCIL
];
589 if (depth
->Type
== stencil
->Type
) {
590 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
591 depth
->Renderbuffer
== stencil
->Renderbuffer
)
594 if (depth
->Type
== GL_TEXTURE
&&
595 depth
->Texture
== stencil
->Texture
)
607 att_incomplete(const char *msg
)
609 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
610 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
619 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
621 static GLuint msg_id
;
623 _mesa_gl_debug(ctx
, &msg_id
,
624 MESA_DEBUG_SOURCE_API
,
625 MESA_DEBUG_TYPE_OTHER
,
626 MESA_DEBUG_SEVERITY_MEDIUM
,
627 "FBO incomplete: %s [%d]\n", msg
, index
);
629 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
630 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
636 * Is the given base format a legal format for a color renderbuffer?
639 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
641 switch (baseFormat
) {
646 case GL_LUMINANCE_ALPHA
:
649 return ctx
->API
== API_OPENGL_COMPAT
&&
650 ctx
->Extensions
.ARB_framebuffer_object
;
653 return ctx
->Extensions
.ARB_texture_rg
;
661 * Is the given base format a legal format for a color renderbuffer?
664 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
665 GLenum internalFormat
)
667 const GLenum baseFormat
=
668 _mesa_get_format_base_format(format
);
671 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
672 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
676 /* Reject additional cases for GLES */
677 switch (internalFormat
) {
697 if (format
== MESA_FORMAT_B10G10R10A2_UNORM
&&
698 internalFormat
!= GL_RGB10_A2
) {
707 * Is the given base format a legal format for a depth/stencil renderbuffer?
710 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
712 switch (baseFormat
) {
713 case GL_DEPTH_COMPONENT
:
714 case GL_DEPTH_STENCIL_EXT
:
723 * Test if an attachment point is complete and update its Complete field.
724 * \param format if GL_COLOR, this is a color attachment point,
725 * if GL_DEPTH, this is a depth component attachment point,
726 * if GL_STENCIL, this is a stencil component attachment point.
729 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
730 struct gl_renderbuffer_attachment
*att
)
732 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
734 /* assume complete */
735 att
->Complete
= GL_TRUE
;
737 /* Look for reasons why the attachment might be incomplete */
738 if (att
->Type
== GL_TEXTURE
) {
739 const struct gl_texture_object
*texObj
= att
->Texture
;
740 struct gl_texture_image
*texImage
;
744 att_incomplete("no texobj");
745 att
->Complete
= GL_FALSE
;
749 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
751 att_incomplete("no teximage");
752 att
->Complete
= GL_FALSE
;
755 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
756 att_incomplete("teximage width/height=0");
757 att
->Complete
= GL_FALSE
;
761 switch (texObj
->Target
) {
763 if (att
->Zoffset
>= texImage
->Depth
) {
764 att_incomplete("bad z offset");
765 att
->Complete
= GL_FALSE
;
769 case GL_TEXTURE_1D_ARRAY
:
770 if (att
->Zoffset
>= texImage
->Height
) {
771 att_incomplete("bad 1D-array layer");
772 att
->Complete
= GL_FALSE
;
776 case GL_TEXTURE_2D_ARRAY
:
777 if (att
->Zoffset
>= texImage
->Depth
) {
778 att_incomplete("bad 2D-array layer");
779 att
->Complete
= GL_FALSE
;
783 case GL_TEXTURE_CUBE_MAP_ARRAY
:
784 if (att
->Zoffset
>= texImage
->Depth
) {
785 att_incomplete("bad cube-array layer");
786 att
->Complete
= GL_FALSE
;
792 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
794 if (format
== GL_COLOR
) {
795 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
796 att_incomplete("bad format");
797 att
->Complete
= GL_FALSE
;
800 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
801 att_incomplete("compressed internalformat");
802 att
->Complete
= GL_FALSE
;
806 /* OES_texture_float allows creation and use of floating point
807 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
808 * these textures to be used as a render target, this is done via
809 * GL_EXT_color_buffer(_half)_float with set of new sized types.
811 if (_mesa_is_gles(ctx
) && (texImage
->TexObject
->_IsFloat
||
812 texImage
->TexObject
->_IsHalfFloat
)) {
813 att_incomplete("bad internal format");
814 att
->Complete
= GL_FALSE
;
818 else if (format
== GL_DEPTH
) {
819 if (baseFormat
== GL_DEPTH_COMPONENT
) {
822 else if (ctx
->Extensions
.ARB_depth_texture
&&
823 baseFormat
== GL_DEPTH_STENCIL
) {
827 att
->Complete
= GL_FALSE
;
828 att_incomplete("bad depth format");
833 assert(format
== GL_STENCIL
);
834 if (ctx
->Extensions
.ARB_depth_texture
&&
835 baseFormat
== GL_DEPTH_STENCIL
) {
837 } else if (ctx
->Extensions
.ARB_texture_stencil8
&&
838 baseFormat
== GL_STENCIL_INDEX
) {
841 /* no such thing as stencil-only textures */
842 att_incomplete("illegal stencil texture");
843 att
->Complete
= GL_FALSE
;
848 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
849 const GLenum baseFormat
=
850 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
852 assert(att
->Renderbuffer
);
853 if (!att
->Renderbuffer
->InternalFormat
||
854 att
->Renderbuffer
->Width
< 1 ||
855 att
->Renderbuffer
->Height
< 1) {
856 att_incomplete("0x0 renderbuffer");
857 att
->Complete
= GL_FALSE
;
860 if (format
== GL_COLOR
) {
861 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
862 att_incomplete("bad renderbuffer color format");
863 att
->Complete
= GL_FALSE
;
867 else if (format
== GL_DEPTH
) {
868 if (baseFormat
== GL_DEPTH_COMPONENT
) {
871 else if (baseFormat
== GL_DEPTH_STENCIL
) {
875 att_incomplete("bad renderbuffer depth format");
876 att
->Complete
= GL_FALSE
;
881 assert(format
== GL_STENCIL
);
882 if (baseFormat
== GL_STENCIL_INDEX
||
883 baseFormat
== GL_DEPTH_STENCIL
) {
887 att
->Complete
= GL_FALSE
;
888 att_incomplete("bad renderbuffer stencil format");
894 assert(att
->Type
== GL_NONE
);
902 * Test if the given framebuffer object is complete and update its
903 * Status field with the results.
904 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
905 * driver to make hardware-specific validation/completeness checks.
906 * Also update the framebuffer's Width and Height fields if the
907 * framebuffer is complete.
910 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
911 struct gl_framebuffer
*fb
)
914 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
915 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
916 GLint numSamples
= -1;
917 GLint fixedSampleLocations
= -1;
920 /* Covers max_layer_count, is_layered, and layer_tex_target */
921 bool layer_info_valid
= false;
922 GLuint max_layer_count
= 0, att_layer_count
;
923 bool is_layered
= false;
924 GLenum layer_tex_target
= 0;
925 bool has_depth_attachment
= false;
926 bool has_stencil_attachment
= false;
928 assert(_mesa_is_user_fbo(fb
));
930 /* we're changing framebuffer fields here */
931 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
936 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
937 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
939 /* Start at -2 to more easily loop over all attachment points.
944 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
945 struct gl_renderbuffer_attachment
*att
;
947 mesa_format attFormat
;
948 GLenum att_tex_target
= GL_NONE
;
951 * XXX for ARB_fbo, only check color buffers that are named by
952 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
955 /* check for attachment completeness
958 att
= &fb
->Attachment
[BUFFER_DEPTH
];
959 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
960 if (!att
->Complete
) {
961 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
962 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
964 } else if (att
->Type
!= GL_NONE
) {
965 has_depth_attachment
= true;
969 att
= &fb
->Attachment
[BUFFER_STENCIL
];
970 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
971 if (!att
->Complete
) {
972 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
973 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
975 } else if (att
->Type
!= GL_NONE
) {
976 has_stencil_attachment
= true;
980 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
981 test_attachment_completeness(ctx
, GL_COLOR
, att
);
982 if (!att
->Complete
) {
983 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
984 fbo_incomplete(ctx
, "color attachment incomplete", i
);
989 /* get width, height, format of the renderbuffer/texture
991 if (att
->Type
== GL_TEXTURE
) {
992 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
993 att_tex_target
= att
->Texture
->Target
;
994 minWidth
= MIN2(minWidth
, texImg
->Width
);
995 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
996 minHeight
= MIN2(minHeight
, texImg
->Height
);
997 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
998 f
= texImg
->_BaseFormat
;
999 attFormat
= texImg
->TexFormat
;
1002 if (!is_format_color_renderable(ctx
, attFormat
,
1003 texImg
->InternalFormat
) &&
1004 !is_legal_depth_format(ctx
, f
) &&
1005 f
!= GL_STENCIL_INDEX
) {
1006 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
1007 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
1012 numSamples
= texImg
->NumSamples
;
1013 else if (numSamples
!= texImg
->NumSamples
) {
1014 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1015 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1019 if (fixedSampleLocations
< 0)
1020 fixedSampleLocations
= texImg
->FixedSampleLocations
;
1021 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
1022 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1023 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1027 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1028 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1029 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1030 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1031 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1032 f
= att
->Renderbuffer
->InternalFormat
;
1033 attFormat
= att
->Renderbuffer
->Format
;
1037 numSamples
= att
->Renderbuffer
->NumSamples
;
1038 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1039 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1040 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1044 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1045 if (fixedSampleLocations
< 0)
1046 fixedSampleLocations
= GL_TRUE
;
1047 else if (fixedSampleLocations
!= GL_TRUE
) {
1048 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1049 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1054 assert(att
->Type
== GL_NONE
);
1058 /* check if integer color */
1059 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
1061 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1063 GLenum type
= _mesa_get_format_datatype(attFormat
);
1065 fb
->_AllColorBuffersFixedPoint
=
1066 fb
->_AllColorBuffersFixedPoint
&&
1067 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1069 fb
->_HasSNormOrFloatColorBuffer
=
1070 fb
->_HasSNormOrFloatColorBuffer
||
1071 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1074 /* Error-check width, height, format */
1075 if (numImages
== 1) {
1082 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1083 /* check that width, height, format are same */
1084 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1085 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1086 fbo_incomplete(ctx
, "width or height mismatch", -1);
1089 /* check that all color buffers are the same format */
1090 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1091 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1092 fbo_incomplete(ctx
, "format mismatch", -1);
1098 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1100 if (att
->Type
== GL_RENDERBUFFER
&&
1101 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1102 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1103 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1107 /* Check that layered rendering is consistent. */
1109 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1110 att_layer_count
= 6;
1111 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1112 att_layer_count
= att
->Renderbuffer
->Height
;
1114 att_layer_count
= att
->Renderbuffer
->Depth
;
1116 att_layer_count
= 0;
1118 if (!layer_info_valid
) {
1119 is_layered
= att
->Layered
;
1120 max_layer_count
= att_layer_count
;
1121 layer_tex_target
= att_tex_target
;
1122 layer_info_valid
= true;
1123 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1124 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1125 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1127 } else if (is_layered
!= att
->Layered
) {
1128 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1130 "framebuffer attachment layer mode is inconsistent",
1133 } else if (att_layer_count
> max_layer_count
) {
1134 max_layer_count
= att_layer_count
;
1138 fb
->MaxNumLayers
= max_layer_count
;
1140 if (numImages
== 0) {
1141 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1142 fbo_incomplete(ctx
, "no attachments", -1);
1146 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1147 /* Check that all DrawBuffers are present */
1148 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1149 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1150 const struct gl_renderbuffer_attachment
*att
1151 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
1153 if (att
->Type
== GL_NONE
) {
1154 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1155 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1161 /* Check that the ReadBuffer is present */
1162 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1163 const struct gl_renderbuffer_attachment
*att
1164 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
1166 if (att
->Type
== GL_NONE
) {
1167 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1168 fbo_incomplete(ctx
, "missing readbuffer", -1);
1174 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1176 * "Depth and stencil attachments, if present, are the same image."
1178 * This restriction is not present in the OpenGL ES2 spec.
1180 if (_mesa_is_gles3(ctx
) &&
1181 has_stencil_attachment
&& has_depth_attachment
&&
1182 !_mesa_has_depthstencil_combined(fb
)) {
1183 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1184 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1188 /* Provisionally set status = COMPLETE ... */
1189 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1191 /* ... but the driver may say the FB is incomplete.
1192 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1195 if (ctx
->Driver
.ValidateFramebuffer
) {
1196 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1197 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1198 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1202 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
1204 * Note that if ARB_framebuffer_object is supported and the attached
1205 * renderbuffers/textures are different sizes, the framebuffer
1206 * width/height will be set to the smallest width/height.
1208 fb
->Width
= minWidth
;
1209 fb
->Height
= minHeight
;
1211 /* finally, update the visual info for the framebuffer */
1212 _mesa_update_framebuffer_visual(ctx
, fb
);
1217 GLboolean GLAPIENTRY
1218 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1220 GET_CURRENT_CONTEXT(ctx
);
1221 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1223 struct gl_renderbuffer
*rb
=
1224 _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1225 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1232 static struct gl_renderbuffer
*
1233 allocate_renderbuffer(struct gl_context
*ctx
, GLuint renderbuffer
,
1236 struct gl_renderbuffer
*newRb
;
1238 /* create new renderbuffer object */
1239 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1241 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1244 assert(newRb
->AllocStorage
);
1245 mtx_lock(&ctx
->Shared
->Mutex
);
1246 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1247 newRb
->RefCount
= 1; /* referenced by hash table */
1248 mtx_unlock(&ctx
->Shared
->Mutex
);
1255 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1257 struct gl_renderbuffer
*newRb
;
1258 GET_CURRENT_CONTEXT(ctx
);
1260 if (target
!= GL_RENDERBUFFER_EXT
) {
1261 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1265 /* No need to flush here since the render buffer binding has no
1266 * effect on rendering state.
1270 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1271 if (newRb
== &DummyRenderbuffer
) {
1272 /* ID was reserved, but no real renderbuffer object made yet */
1275 else if (!newRb
&& !allow_user_names
) {
1276 /* All RB IDs must be Gen'd */
1277 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1282 newRb
= allocate_renderbuffer(ctx
, renderbuffer
, "glBindRenderbufferEXT");
1289 assert(newRb
!= &DummyRenderbuffer
);
1291 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1295 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1297 GET_CURRENT_CONTEXT(ctx
);
1299 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1300 * entry point, but they allow the use of user-generated names.
1302 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1306 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1308 /* This function should not be in the dispatch table for core profile /
1309 * OpenGL 3.1, so execution should never get here in those cases -- no
1310 * need for an explicit test.
1312 bind_renderbuffer(target
, renderbuffer
, true);
1317 * Remove the specified renderbuffer or texture from any attachment point in
1321 * \c true if the renderbuffer was detached from an attachment point. \c
1325 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1326 struct gl_framebuffer
*fb
,
1330 bool progress
= false;
1332 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1333 if (fb
->Attachment
[i
].Texture
== att
1334 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1335 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1340 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1341 * Completeness," of the OpenGL 3.1 spec says:
1343 * "Performing any of the following actions may change whether the
1344 * framebuffer is considered complete or incomplete:
1348 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1349 * containing an image that is attached to a framebuffer object
1350 * that is bound to the framebuffer."
1353 invalidate_framebuffer(fb
);
1360 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1363 GET_CURRENT_CONTEXT(ctx
);
1366 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1370 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1372 for (i
= 0; i
< n
; i
++) {
1373 if (renderbuffers
[i
] > 0) {
1374 struct gl_renderbuffer
*rb
;
1375 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1377 /* check if deleting currently bound renderbuffer object */
1378 if (rb
== ctx
->CurrentRenderbuffer
) {
1380 assert(rb
->RefCount
>= 2);
1381 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1384 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1385 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1386 * of the OpenGL 3.1 spec says:
1388 * "If a renderbuffer object is deleted while its image is
1389 * attached to one or more attachment points in the currently
1390 * bound framebuffer, then it is as if FramebufferRenderbuffer
1391 * had been called, with a renderbuffer of 0, for each
1392 * attachment point to which this image was attached in the
1393 * currently bound framebuffer. In other words, this
1394 * renderbuffer image is first detached from all attachment
1395 * points in the currently bound framebuffer. Note that the
1396 * renderbuffer image is specifically not detached from any
1397 * non-bound framebuffers. Detaching the image from any
1398 * non-bound framebuffers is the responsibility of the
1401 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1402 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1404 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1405 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1406 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1409 /* Remove from hash table immediately, to free the ID.
1410 * But the object will not be freed until it's no longer
1411 * referenced anywhere else.
1413 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1415 if (rb
!= &DummyRenderbuffer
) {
1416 /* no longer referenced by hash table */
1417 _mesa_reference_renderbuffer(&rb
, NULL
);
1425 create_render_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*renderbuffers
,
1428 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1433 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n<0)", func
);
1440 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1442 for (i
= 0; i
< n
; i
++) {
1443 GLuint name
= first
+ i
;
1444 renderbuffers
[i
] = name
;
1447 allocate_renderbuffer(ctx
, name
, func
);
1449 /* insert a dummy renderbuffer into the hash table */
1450 mtx_lock(&ctx
->Shared
->Mutex
);
1451 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1452 mtx_unlock(&ctx
->Shared
->Mutex
);
1459 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1461 GET_CURRENT_CONTEXT(ctx
);
1462 create_render_buffers(ctx
, n
, renderbuffers
, false);
1467 _mesa_CreateRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1469 GET_CURRENT_CONTEXT(ctx
);
1470 create_render_buffers(ctx
, n
, renderbuffers
, true);
1475 * Given an internal format token for a render buffer, return the
1476 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1477 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1478 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1480 * This is similar to _mesa_base_tex_format() but the set of valid
1481 * internal formats is different.
1483 * Note that even if a format is determined to be legal here, validation
1484 * of the FBO may fail if the format is not supported by the driver/GPU.
1486 * \param internalFormat as passed to glRenderbufferStorage()
1487 * \return the base internal format, or 0 if internalFormat is illegal
1490 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1493 * Notes: some formats such as alpha, luminance, etc. were added
1494 * with GL_ARB_framebuffer_object.
1496 switch (internalFormat
) {
1502 return (ctx
->API
== API_OPENGL_COMPAT
&&
1503 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1507 case GL_LUMINANCE12
:
1508 case GL_LUMINANCE16
:
1509 return (ctx
->API
== API_OPENGL_COMPAT
&&
1510 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1511 case GL_LUMINANCE_ALPHA
:
1512 case GL_LUMINANCE4_ALPHA4
:
1513 case GL_LUMINANCE6_ALPHA2
:
1514 case GL_LUMINANCE8_ALPHA8
:
1515 case GL_LUMINANCE12_ALPHA4
:
1516 case GL_LUMINANCE12_ALPHA12
:
1517 case GL_LUMINANCE16_ALPHA16
:
1518 return (ctx
->API
== API_OPENGL_COMPAT
&&
1519 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1523 case GL_INTENSITY12
:
1524 case GL_INTENSITY16
:
1525 return (ctx
->API
== API_OPENGL_COMPAT
&&
1526 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1536 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1538 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1547 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1549 case GL_SRGB8_ALPHA8_EXT
:
1550 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1551 case GL_STENCIL_INDEX
:
1552 case GL_STENCIL_INDEX1_EXT
:
1553 case GL_STENCIL_INDEX4_EXT
:
1554 case GL_STENCIL_INDEX16_EXT
:
1555 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1556 * OpenGL ES, but Mesa does not currently support them.
1558 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1559 case GL_STENCIL_INDEX8_EXT
:
1560 return GL_STENCIL_INDEX
;
1561 case GL_DEPTH_COMPONENT
:
1562 case GL_DEPTH_COMPONENT32
:
1563 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1564 case GL_DEPTH_COMPONENT16
:
1565 case GL_DEPTH_COMPONENT24
:
1566 return GL_DEPTH_COMPONENT
;
1567 case GL_DEPTH_STENCIL
:
1568 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1569 case GL_DEPTH24_STENCIL8
:
1570 return GL_DEPTH_STENCIL
;
1571 case GL_DEPTH_COMPONENT32F
:
1572 return ctx
->Version
>= 30
1573 || (ctx
->API
== API_OPENGL_COMPAT
&&
1574 ctx
->Extensions
.ARB_depth_buffer_float
)
1575 ? GL_DEPTH_COMPONENT
: 0;
1576 case GL_DEPTH32F_STENCIL8
:
1577 return ctx
->Version
>= 30
1578 || (ctx
->API
== API_OPENGL_COMPAT
&&
1579 ctx
->Extensions
.ARB_depth_buffer_float
)
1580 ? GL_DEPTH_STENCIL
: 0;
1583 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1586 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1590 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1593 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1595 /* signed normalized texture formats */
1599 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1604 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1608 case GL_RGB16_SNORM
:
1609 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1612 case GL_RGBA8_SNORM
:
1613 case GL_RGBA16_SNORM
:
1614 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1616 case GL_ALPHA_SNORM
:
1617 case GL_ALPHA8_SNORM
:
1618 case GL_ALPHA16_SNORM
:
1619 return ctx
->API
== API_OPENGL_COMPAT
&&
1620 ctx
->Extensions
.EXT_texture_snorm
&&
1621 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1622 case GL_LUMINANCE_SNORM
:
1623 case GL_LUMINANCE8_SNORM
:
1624 case GL_LUMINANCE16_SNORM
:
1625 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1627 case GL_LUMINANCE_ALPHA_SNORM
:
1628 case GL_LUMINANCE8_ALPHA8_SNORM
:
1629 case GL_LUMINANCE16_ALPHA16_SNORM
:
1630 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1631 ? GL_LUMINANCE_ALPHA
: 0;
1632 case GL_INTENSITY_SNORM
:
1633 case GL_INTENSITY8_SNORM
:
1634 case GL_INTENSITY16_SNORM
:
1635 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1640 return ((_mesa_is_desktop_gl(ctx
) &&
1641 ctx
->Extensions
.ARB_texture_rg
&&
1642 ctx
->Extensions
.ARB_texture_float
) ||
1643 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1647 return ((_mesa_is_desktop_gl(ctx
) &&
1648 ctx
->Extensions
.ARB_texture_rg
&&
1649 ctx
->Extensions
.ARB_texture_float
) ||
1650 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1654 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1658 return ((_mesa_is_desktop_gl(ctx
) &&
1659 ctx
->Extensions
.ARB_texture_float
) ||
1660 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1662 case GL_ALPHA16F_ARB
:
1663 case GL_ALPHA32F_ARB
:
1664 return ctx
->API
== API_OPENGL_COMPAT
&&
1665 ctx
->Extensions
.ARB_texture_float
&&
1666 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1667 case GL_LUMINANCE16F_ARB
:
1668 case GL_LUMINANCE32F_ARB
:
1669 return ctx
->API
== API_OPENGL_COMPAT
&&
1670 ctx
->Extensions
.ARB_texture_float
&&
1671 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1672 case GL_LUMINANCE_ALPHA16F_ARB
:
1673 case GL_LUMINANCE_ALPHA32F_ARB
:
1674 return ctx
->API
== API_OPENGL_COMPAT
&&
1675 ctx
->Extensions
.ARB_texture_float
&&
1676 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1677 case GL_INTENSITY16F_ARB
:
1678 case GL_INTENSITY32F_ARB
:
1679 return ctx
->API
== API_OPENGL_COMPAT
&&
1680 ctx
->Extensions
.ARB_texture_float
&&
1681 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1682 case GL_R11F_G11F_B10F
:
1683 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1684 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1687 case GL_RGBA8UI_EXT
:
1688 case GL_RGBA16UI_EXT
:
1689 case GL_RGBA32UI_EXT
:
1691 case GL_RGBA16I_EXT
:
1692 case GL_RGBA32I_EXT
:
1693 return ctx
->Version
>= 30
1694 || (_mesa_is_desktop_gl(ctx
) &&
1695 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1698 case GL_RGB16UI_EXT
:
1699 case GL_RGB32UI_EXT
:
1703 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1711 return ctx
->Version
>= 30
1712 || (_mesa_is_desktop_gl(ctx
) &&
1713 ctx
->Extensions
.ARB_texture_rg
&&
1714 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1722 return ctx
->Version
>= 30
1723 || (_mesa_is_desktop_gl(ctx
) &&
1724 ctx
->Extensions
.ARB_texture_rg
&&
1725 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1727 case GL_INTENSITY8I_EXT
:
1728 case GL_INTENSITY8UI_EXT
:
1729 case GL_INTENSITY16I_EXT
:
1730 case GL_INTENSITY16UI_EXT
:
1731 case GL_INTENSITY32I_EXT
:
1732 case GL_INTENSITY32UI_EXT
:
1733 return ctx
->API
== API_OPENGL_COMPAT
&&
1734 ctx
->Extensions
.EXT_texture_integer
&&
1735 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1737 case GL_LUMINANCE8I_EXT
:
1738 case GL_LUMINANCE8UI_EXT
:
1739 case GL_LUMINANCE16I_EXT
:
1740 case GL_LUMINANCE16UI_EXT
:
1741 case GL_LUMINANCE32I_EXT
:
1742 case GL_LUMINANCE32UI_EXT
:
1743 return ctx
->API
== API_OPENGL_COMPAT
&&
1744 ctx
->Extensions
.EXT_texture_integer
&&
1745 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1747 case GL_LUMINANCE_ALPHA8I_EXT
:
1748 case GL_LUMINANCE_ALPHA8UI_EXT
:
1749 case GL_LUMINANCE_ALPHA16I_EXT
:
1750 case GL_LUMINANCE_ALPHA16UI_EXT
:
1751 case GL_LUMINANCE_ALPHA32I_EXT
:
1752 case GL_LUMINANCE_ALPHA32UI_EXT
:
1753 return ctx
->API
== API_OPENGL_COMPAT
&&
1754 ctx
->Extensions
.EXT_texture_integer
&&
1755 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1757 case GL_ALPHA8I_EXT
:
1758 case GL_ALPHA8UI_EXT
:
1759 case GL_ALPHA16I_EXT
:
1760 case GL_ALPHA16UI_EXT
:
1761 case GL_ALPHA32I_EXT
:
1762 case GL_ALPHA32UI_EXT
:
1763 return ctx
->API
== API_OPENGL_COMPAT
&&
1764 ctx
->Extensions
.EXT_texture_integer
&&
1765 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1768 return (_mesa_is_desktop_gl(ctx
) &&
1769 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1770 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1773 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1782 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1785 invalidate_rb(GLuint key
, void *data
, void *userData
)
1787 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1788 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1790 /* If this is a user-created FBO */
1791 if (_mesa_is_user_fbo(fb
)) {
1793 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1794 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1795 if (att
->Type
== GL_RENDERBUFFER
&&
1796 att
->Renderbuffer
== rb
) {
1797 /* Mark fb status as indeterminate to force re-validation */
1806 /** sentinal value, see below */
1807 #define NO_SAMPLES 1000
1811 * Helper function used by renderbuffer_storage_direct() and
1812 * renderbuffer_storage_target().
1813 * samples will be NO_SAMPLES if called by a non-multisample function.
1816 renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1817 GLenum internalFormat
, GLsizei width
,
1818 GLsizei height
, GLsizei samples
, const char *func
)
1821 GLenum sample_count_error
;
1823 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1824 if (baseFormat
== 0) {
1825 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1826 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1830 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1831 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid width %d)", func
,
1836 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1837 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid height %d)", func
,
1842 if (samples
== NO_SAMPLES
) {
1843 /* NumSamples == 0 indicates non-multisampling */
1847 /* check the sample count;
1848 * note: driver may choose to use more samples than what's requested
1850 sample_count_error
= _mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
1851 internalFormat
, samples
);
1852 if (sample_count_error
!= GL_NO_ERROR
) {
1853 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
1858 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1860 if (rb
->InternalFormat
== internalFormat
&&
1861 rb
->Width
== (GLuint
) width
&&
1862 rb
->Height
== (GLuint
) height
&&
1863 rb
->NumSamples
== samples
) {
1864 /* no change in allocation needed */
1868 /* These MUST get set by the AllocStorage func */
1869 rb
->Format
= MESA_FORMAT_NONE
;
1870 rb
->NumSamples
= samples
;
1872 /* Now allocate the storage */
1873 assert(rb
->AllocStorage
);
1874 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1875 /* No error - check/set fields now */
1876 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1877 assert(rb
->Width
== (GLuint
) width
);
1878 assert(rb
->Height
== (GLuint
) height
);
1879 rb
->InternalFormat
= internalFormat
;
1880 rb
->_BaseFormat
= baseFormat
;
1881 assert(rb
->_BaseFormat
!= 0);
1884 /* Probably ran out of memory - clear the fields */
1887 rb
->Format
= MESA_FORMAT_NONE
;
1888 rb
->InternalFormat
= GL_NONE
;
1889 rb
->_BaseFormat
= GL_NONE
;
1893 /* Invalidate the framebuffers the renderbuffer is attached in. */
1894 if (rb
->AttachedAnytime
) {
1895 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1900 * Helper function used by _mesa_NamedRenderbufferStorage*().
1901 * samples will be NO_SAMPLES if called by a non-multisample function.
1904 renderbuffer_storage_named(GLuint renderbuffer
, GLenum internalFormat
,
1905 GLsizei width
, GLsizei height
, GLsizei samples
,
1908 GET_CURRENT_CONTEXT(ctx
);
1910 if (MESA_VERBOSE
& VERBOSE_API
) {
1911 if (samples
== NO_SAMPLES
)
1912 _mesa_debug(ctx
, "%s(%u, %s, %d, %d)\n",
1914 _mesa_lookup_enum_by_nr(internalFormat
),
1917 _mesa_debug(ctx
, "%s(%u, %s, %d, %d, %d)\n",
1919 _mesa_lookup_enum_by_nr(internalFormat
),
1920 width
, height
, samples
);
1923 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1924 if (!rb
|| rb
== &DummyRenderbuffer
) {
1925 /* ID was reserved, but no real renderbuffer object made yet */
1926 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid renderbuffer %u)",
1927 func
, renderbuffer
);
1931 renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
, func
);
1935 * Helper function used by _mesa_RenderbufferStorage() and
1936 * _mesa_RenderbufferStorageMultisample().
1937 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1940 renderbuffer_storage_target(GLenum target
, GLenum internalFormat
,
1941 GLsizei width
, GLsizei height
, GLsizei samples
,
1944 GET_CURRENT_CONTEXT(ctx
);
1946 if (MESA_VERBOSE
& VERBOSE_API
) {
1947 if (samples
== NO_SAMPLES
)
1948 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1950 _mesa_lookup_enum_by_nr(target
),
1951 _mesa_lookup_enum_by_nr(internalFormat
),
1954 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1956 _mesa_lookup_enum_by_nr(target
),
1957 _mesa_lookup_enum_by_nr(internalFormat
),
1958 width
, height
, samples
);
1961 if (target
!= GL_RENDERBUFFER_EXT
) {
1962 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1966 if (!ctx
->CurrentRenderbuffer
) {
1967 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no renderbuffer bound)",
1972 renderbuffer_storage(ctx
, ctx
->CurrentRenderbuffer
, internalFormat
, width
,
1973 height
, samples
, func
);
1978 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1980 struct gl_renderbuffer
*rb
;
1981 GET_CURRENT_CONTEXT(ctx
);
1983 if (!ctx
->Extensions
.OES_EGL_image
) {
1984 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1985 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1989 if (target
!= GL_RENDERBUFFER
) {
1990 _mesa_error(ctx
, GL_INVALID_ENUM
,
1991 "EGLImageTargetRenderbufferStorageOES");
1995 rb
= ctx
->CurrentRenderbuffer
;
1997 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1998 "EGLImageTargetRenderbufferStorageOES");
2002 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2004 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
2009 * Helper function for _mesa_GetRenderbufferParameteriv() and
2010 * _mesa_GetFramebufferAttachmentParameteriv()
2011 * We have to be careful to respect the base format. For example, if a
2012 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2013 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2014 * we need to return zero.
2017 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
2019 if (_mesa_base_format_has_channel(baseFormat
, pname
))
2020 return _mesa_get_format_bits(format
, pname
);
2028 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
2029 GLsizei width
, GLsizei height
)
2031 /* GL_ARB_fbo says calling this function is equivalent to calling
2032 * glRenderbufferStorageMultisample() with samples=0. We pass in
2033 * a token value here just for error reporting purposes.
2035 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2036 NO_SAMPLES
, "glRenderbufferStorage");
2041 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
2042 GLenum internalFormat
,
2043 GLsizei width
, GLsizei height
)
2045 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2046 samples
, "glRenderbufferStorageMultisample");
2051 * OpenGL ES version of glRenderBufferStorage.
2054 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
2055 GLsizei width
, GLsizei height
)
2057 switch (internalFormat
) {
2059 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2060 /* choose a closest format */
2061 internalFormat
= GL_RGB5
;
2067 renderbuffer_storage_target(target
, internalFormat
, width
, height
, 0,
2068 "glRenderbufferStorageEXT");
2072 _mesa_NamedRenderbufferStorage(GLuint renderbuffer
, GLenum internalformat
,
2073 GLsizei width
, GLsizei height
)
2075 /* GL_ARB_fbo says calling this function is equivalent to calling
2076 * glRenderbufferStorageMultisample() with samples=0. We pass in
2077 * a token value here just for error reporting purposes.
2079 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2080 NO_SAMPLES
, "glNamedRenderbufferStorage");
2084 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer
, GLsizei samples
,
2085 GLenum internalformat
,
2086 GLsizei width
, GLsizei height
)
2088 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2090 "glNamedRenderbufferStorageMultisample");
2095 get_render_buffer_parameteriv(struct gl_context
*ctx
,
2096 struct gl_renderbuffer
*rb
, GLenum pname
,
2097 GLint
*params
, const char *func
)
2099 /* No need to flush here since we're just quering state which is
2100 * not effected by rendering.
2104 case GL_RENDERBUFFER_WIDTH_EXT
:
2105 *params
= rb
->Width
;
2107 case GL_RENDERBUFFER_HEIGHT_EXT
:
2108 *params
= rb
->Height
;
2110 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
2111 *params
= rb
->InternalFormat
;
2113 case GL_RENDERBUFFER_RED_SIZE_EXT
:
2114 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
2115 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
2116 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
2117 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
2118 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
2119 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2121 case GL_RENDERBUFFER_SAMPLES
:
2122 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2123 || _mesa_is_gles3(ctx
)) {
2124 *params
= rb
->NumSamples
;
2129 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname=%s)", func
,
2130 _mesa_lookup_enum_by_nr(pname
));
2137 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2139 GET_CURRENT_CONTEXT(ctx
);
2141 if (target
!= GL_RENDERBUFFER_EXT
) {
2142 _mesa_error(ctx
, GL_INVALID_ENUM
,
2143 "glGetRenderbufferParameterivEXT(target)");
2147 if (!ctx
->CurrentRenderbuffer
) {
2148 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetRenderbufferParameterivEXT"
2149 "(no renderbuffer bound)");
2153 get_render_buffer_parameteriv(ctx
, ctx
->CurrentRenderbuffer
, pname
,
2154 params
, "glGetRenderbufferParameteriv");
2159 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer
, GLenum pname
,
2162 GET_CURRENT_CONTEXT(ctx
);
2164 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2165 if (!rb
|| rb
== &DummyRenderbuffer
) {
2166 /* ID was reserved, but no real renderbuffer object made yet */
2167 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetNamedRenderbufferParameteriv"
2168 "(invalid renderbuffer %i)", renderbuffer
);
2172 get_render_buffer_parameteriv(ctx
, rb
, pname
, params
,
2173 "glGetNamedRenderbufferParameteriv");
2177 GLboolean GLAPIENTRY
2178 _mesa_IsFramebuffer(GLuint framebuffer
)
2180 GET_CURRENT_CONTEXT(ctx
);
2181 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2183 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2184 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2192 * Check if any of the attachments of the given framebuffer are textures
2193 * (render to texture). Call ctx->Driver.RenderTexture() for such
2197 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2200 assert(ctx
->Driver
.RenderTexture
);
2202 if (_mesa_is_winsys_fbo(fb
))
2203 return; /* can't render to texture with winsys framebuffers */
2205 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2206 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2207 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2208 && driver_RenderTexture_is_safe(att
)) {
2209 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2216 * Examine all the framebuffer's attachments to see if any are textures.
2217 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2218 * notify the device driver that the texture image may have changed.
2221 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2223 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2224 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2227 if (ctx
->Driver
.FinishRenderTexture
) {
2229 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2230 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2231 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2232 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2233 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2241 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2243 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2244 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
2245 GLboolean bindReadBuf
, bindDrawBuf
;
2246 GET_CURRENT_CONTEXT(ctx
);
2249 case GL_DRAW_FRAMEBUFFER_EXT
:
2250 bindDrawBuf
= GL_TRUE
;
2251 bindReadBuf
= GL_FALSE
;
2253 case GL_READ_FRAMEBUFFER_EXT
:
2254 bindDrawBuf
= GL_FALSE
;
2255 bindReadBuf
= GL_TRUE
;
2257 case GL_FRAMEBUFFER_EXT
:
2258 bindDrawBuf
= GL_TRUE
;
2259 bindReadBuf
= GL_TRUE
;
2262 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2267 /* Binding a user-created framebuffer object */
2268 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2269 if (newDrawFb
== &DummyFramebuffer
) {
2270 /* ID was reserved, but no real framebuffer object made yet */
2273 else if (!newDrawFb
&& !allow_user_names
) {
2274 /* All FBO IDs must be Gen'd */
2275 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2280 /* create new framebuffer object */
2281 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2283 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2286 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2288 newReadFb
= newDrawFb
;
2291 /* Binding the window system framebuffer (which was originally set
2292 * with MakeCurrent).
2294 newDrawFb
= ctx
->WinSysDrawBuffer
;
2295 newReadFb
= ctx
->WinSysReadBuffer
;
2299 assert(newDrawFb
!= &DummyFramebuffer
);
2301 /* save pointers to current/old framebuffers */
2302 oldDrawFb
= ctx
->DrawBuffer
;
2303 oldReadFb
= ctx
->ReadBuffer
;
2305 /* check if really changing bindings */
2306 if (oldDrawFb
== newDrawFb
)
2307 bindDrawBuf
= GL_FALSE
;
2308 if (oldReadFb
== newReadFb
)
2309 bindReadBuf
= GL_FALSE
;
2312 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2314 * We also check if we're beginning and/or ending render-to-texture.
2315 * When a framebuffer with texture attachments is unbound, call
2316 * ctx->Driver.FinishRenderTexture().
2317 * When a framebuffer with texture attachments is bound, call
2318 * ctx->Driver.RenderTexture().
2320 * Note that if the ReadBuffer has texture attachments we don't consider
2321 * that a render-to-texture case.
2324 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2326 /* check if old readbuffer was render-to-texture */
2327 check_end_texture_render(ctx
, oldReadFb
);
2329 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2333 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2335 /* check if old framebuffer had any texture attachments */
2337 check_end_texture_render(ctx
, oldDrawFb
);
2339 /* check if newly bound framebuffer has any texture attachments */
2340 check_begin_texture_render(ctx
, newDrawFb
);
2342 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2345 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2346 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
2351 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2353 GET_CURRENT_CONTEXT(ctx
);
2355 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2356 * point, but they allow the use of user-generated names.
2358 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2363 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2365 /* This function should not be in the dispatch table for core profile /
2366 * OpenGL 3.1, so execution should never get here in those cases -- no
2367 * need for an explicit test.
2369 bind_framebuffer(target
, framebuffer
, true);
2374 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2377 GET_CURRENT_CONTEXT(ctx
);
2380 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2384 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2386 for (i
= 0; i
< n
; i
++) {
2387 if (framebuffers
[i
] > 0) {
2388 struct gl_framebuffer
*fb
;
2389 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2391 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2393 /* check if deleting currently bound framebuffer object */
2394 if (fb
== ctx
->DrawBuffer
) {
2396 assert(fb
->RefCount
>= 2);
2397 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2399 if (fb
== ctx
->ReadBuffer
) {
2401 assert(fb
->RefCount
>= 2);
2402 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2405 /* remove from hash table immediately, to free the ID */
2406 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2408 if (fb
!= &DummyFramebuffer
) {
2409 /* But the object will not be freed until it's no longer
2410 * bound in any context.
2412 _mesa_reference_framebuffer(&fb
, NULL
);
2421 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2423 GET_CURRENT_CONTEXT(ctx
);
2428 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
2435 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2437 for (i
= 0; i
< n
; i
++) {
2438 GLuint name
= first
+ i
;
2439 framebuffers
[i
] = name
;
2440 /* insert dummy placeholder into hash table */
2441 mtx_lock(&ctx
->Shared
->Mutex
);
2442 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
2443 mtx_unlock(&ctx
->Shared
->Mutex
);
2449 _mesa_CheckFramebufferStatus(GLenum target
)
2451 struct gl_framebuffer
*buffer
;
2452 GET_CURRENT_CONTEXT(ctx
);
2454 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2456 if (MESA_VERBOSE
& VERBOSE_API
)
2457 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2458 _mesa_lookup_enum_by_nr(target
));
2460 buffer
= get_framebuffer_target(ctx
, target
);
2462 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2466 if (_mesa_is_winsys_fbo(buffer
)) {
2467 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2468 if (buffer
!= &IncompleteFramebuffer
) {
2469 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2471 return GL_FRAMEBUFFER_UNDEFINED
;
2475 /* No need to flush here */
2477 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2478 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2481 return buffer
->_Status
;
2486 * Replicate the src attachment point. Used by framebuffer_texture() when
2487 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2488 * GL_STENCIL_ATTACHMENT.
2491 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2492 gl_buffer_index dst
,
2493 gl_buffer_index src
)
2495 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2496 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2498 assert(src_att
->Texture
!= NULL
);
2499 assert(src_att
->Renderbuffer
!= NULL
);
2501 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2502 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2503 dst_att
->Type
= src_att
->Type
;
2504 dst_att
->Complete
= src_att
->Complete
;
2505 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2506 dst_att
->Zoffset
= src_att
->Zoffset
;
2511 * Common code called by glFramebufferTexture1D/2D/3D() and
2512 * glFramebufferTextureLayer().
2514 * \param textarget is the textarget that was passed to the
2515 * glFramebufferTexture...() function, or 0 if the corresponding function
2516 * doesn't have a textarget parameter.
2518 * \param layered is true if this function was called from
2519 * glFramebufferTexture(), false otherwise.
2522 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2523 GLenum attachment
, GLenum textarget
, GLuint texture
,
2524 GLint level
, GLuint zoffset
, GLboolean layered
)
2526 struct gl_renderbuffer_attachment
*att
;
2527 struct gl_texture_object
*texObj
= NULL
;
2528 struct gl_framebuffer
*fb
;
2529 GLenum maxLevelsTarget
;
2531 fb
= get_framebuffer_target(ctx
, target
);
2533 _mesa_error(ctx
, GL_INVALID_ENUM
,
2534 "glFramebufferTexture%s(target=0x%x)", caller
, target
);
2538 /* check framebuffer binding */
2539 if (_mesa_is_winsys_fbo(fb
)) {
2540 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2541 "glFramebufferTexture%s", caller
);
2545 /* The textarget, level, and zoffset parameters are only validated if
2546 * texture is non-zero.
2549 GLboolean err
= GL_TRUE
;
2551 texObj
= _mesa_lookup_texture(ctx
, texture
);
2552 if (texObj
!= NULL
) {
2553 if (textarget
== 0) {
2555 /* We're being called by glFramebufferTexture() and textarget
2558 switch (texObj
->Target
) {
2560 case GL_TEXTURE_1D_ARRAY_EXT
:
2561 case GL_TEXTURE_2D_ARRAY_EXT
:
2562 case GL_TEXTURE_CUBE_MAP
:
2563 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2564 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2569 case GL_TEXTURE_RECTANGLE
:
2570 case GL_TEXTURE_2D_MULTISAMPLE
:
2571 /* These texture types are valid to pass to
2572 * glFramebufferTexture(), but since they aren't layered, it
2573 * is equivalent to calling glFramebufferTexture{1D,2D}().
2577 textarget
= texObj
->Target
;
2584 /* We're being called by glFramebufferTextureLayer() and
2585 * textarget is not used. The only legal texture types for
2586 * that function are 3D and 1D/2D arrays textures.
2588 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2589 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2590 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2591 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2592 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2596 /* Make sure textarget is consistent with the texture's type */
2597 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2598 ? !_mesa_is_cube_face(textarget
)
2599 : (texObj
->Target
!= textarget
);
2603 /* can't render to a non-existant texture */
2604 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2605 "glFramebufferTexture%s(non existant texture)",
2611 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2612 "glFramebufferTexture%s(texture target mismatch)",
2617 if (texObj
->Target
== GL_TEXTURE_3D
) {
2618 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2619 if (zoffset
>= maxSize
) {
2620 _mesa_error(ctx
, GL_INVALID_VALUE
,
2621 "glFramebufferTexture%s(zoffset)", caller
);
2625 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2626 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2627 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2628 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2629 if (zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
2630 _mesa_error(ctx
, GL_INVALID_VALUE
,
2631 "glFramebufferTexture%s(layer)", caller
);
2636 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2638 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2639 _mesa_error(ctx
, GL_INVALID_VALUE
,
2640 "glFramebufferTexture%s(level)", caller
);
2645 att
= get_attachment(ctx
, fb
, attachment
);
2647 _mesa_error(ctx
, GL_INVALID_ENUM
,
2648 "glFramebufferTexture%s(attachment)", caller
);
2652 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2654 mtx_lock(&fb
->Mutex
);
2656 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2657 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2658 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2659 _mesa_tex_target_to_face(textarget
) ==
2660 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2661 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2662 /* The texture object is already attached to the stencil attachment
2663 * point. Don't create a new renderbuffer; just reuse the stencil
2664 * attachment's. This is required to prevent a GL error in
2665 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2667 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2669 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2670 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2671 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2672 _mesa_tex_target_to_face(textarget
) ==
2673 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2674 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2675 /* As above, but with depth and stencil transposed. */
2676 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2679 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2680 level
, zoffset
, layered
);
2681 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2682 /* Above we created a new renderbuffer and attached it to the
2683 * depth attachment point. Now attach it to the stencil attachment
2686 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2687 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2692 /* Set the render-to-texture flag. We'll check this flag in
2693 * glTexImage() and friends to determine if we need to revalidate
2694 * any FBOs that might be rendering into this texture.
2695 * This flag never gets cleared since it's non-trivial to determine
2696 * when all FBOs might be done rendering to this texture. That's OK
2697 * though since it's uncommon to render to a texture then repeatedly
2698 * call glTexImage() to change images in the texture.
2700 texObj
->_RenderToTexture
= GL_TRUE
;
2703 remove_attachment(ctx
, att
);
2704 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2705 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2706 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2710 invalidate_framebuffer(fb
);
2712 mtx_unlock(&fb
->Mutex
);
2717 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2718 GLenum textarget
, GLuint texture
, GLint level
)
2720 GET_CURRENT_CONTEXT(ctx
);
2725 switch (textarget
) {
2729 case GL_TEXTURE_1D_ARRAY
:
2730 error
= !ctx
->Extensions
.EXT_texture_array
;
2737 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2738 "glFramebufferTexture1D(textarget=%s)",
2739 _mesa_lookup_enum_by_nr(textarget
));
2744 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2745 level
, 0, GL_FALSE
);
2750 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2751 GLenum textarget
, GLuint texture
, GLint level
)
2753 GET_CURRENT_CONTEXT(ctx
);
2758 switch (textarget
) {
2762 case GL_TEXTURE_RECTANGLE
:
2763 error
= _mesa_is_gles(ctx
)
2764 || !ctx
->Extensions
.NV_texture_rectangle
;
2766 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2767 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2768 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2769 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2770 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2771 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2772 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2774 case GL_TEXTURE_2D_ARRAY
:
2775 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2776 || !ctx
->Extensions
.EXT_texture_array
;
2778 case GL_TEXTURE_2D_MULTISAMPLE
:
2779 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2780 error
= _mesa_is_gles(ctx
)
2781 || !ctx
->Extensions
.ARB_texture_multisample
;
2788 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2789 "glFramebufferTexture2D(textarget=%s)",
2790 _mesa_lookup_enum_by_nr(textarget
));
2795 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2796 level
, 0, GL_FALSE
);
2801 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2802 GLenum textarget
, GLuint texture
,
2803 GLint level
, GLint zoffset
)
2805 GET_CURRENT_CONTEXT(ctx
);
2807 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2808 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2809 "glFramebufferTexture3D(textarget)");
2813 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2814 level
, zoffset
, GL_FALSE
);
2819 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2820 GLuint texture
, GLint level
, GLint layer
)
2822 GET_CURRENT_CONTEXT(ctx
);
2824 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2825 level
, layer
, GL_FALSE
);
2830 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
2831 GLuint texture
, GLint level
)
2833 GET_CURRENT_CONTEXT(ctx
);
2835 if (_mesa_has_geometry_shaders(ctx
)) {
2836 framebuffer_texture(ctx
, "", target
, attachment
, 0, texture
,
2839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2840 "unsupported function (glFramebufferTexture) called");
2846 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2847 GLenum renderbufferTarget
,
2848 GLuint renderbuffer
)
2850 struct gl_renderbuffer_attachment
*att
;
2851 struct gl_framebuffer
*fb
;
2852 struct gl_renderbuffer
*rb
;
2853 GET_CURRENT_CONTEXT(ctx
);
2855 fb
= get_framebuffer_target(ctx
, target
);
2857 _mesa_error(ctx
, GL_INVALID_ENUM
,
2858 "glFramebufferRenderbuffer(target)");
2862 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2863 _mesa_error(ctx
, GL_INVALID_ENUM
,
2864 "glFramebufferRenderbuffer(renderbufferTarget)");
2868 if (_mesa_is_winsys_fbo(fb
)) {
2869 /* Can't attach new renderbuffers to a window system framebuffer */
2870 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbuffer");
2874 att
= get_attachment(ctx
, fb
, attachment
);
2876 _mesa_error(ctx
, GL_INVALID_ENUM
,
2877 "glFramebufferRenderbuffer(invalid attachment %s)",
2878 _mesa_lookup_enum_by_nr(attachment
));
2883 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2885 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2886 "glFramebufferRenderbuffer(non-existant"
2887 " renderbuffer %u)", renderbuffer
);
2890 else if (rb
== &DummyRenderbuffer
) {
2891 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2892 "glFramebufferRenderbuffer(renderbuffer %u)",
2898 /* remove renderbuffer attachment */
2902 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2903 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2904 /* make sure the renderbuffer is a depth/stencil format */
2905 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2906 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2908 "glFramebufferRenderbuffer(renderbuffer"
2909 " is not DEPTH_STENCIL format)");
2914 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2916 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2917 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2919 /* Some subsequent GL commands may depend on the framebuffer's visual
2920 * after the binding is updated. Update visual info now.
2922 _mesa_update_framebuffer_visual(ctx
, fb
);
2927 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2928 GLenum pname
, GLint
*params
)
2930 const struct gl_renderbuffer_attachment
*att
;
2931 struct gl_framebuffer
*buffer
;
2933 GET_CURRENT_CONTEXT(ctx
);
2935 /* The error differs in GL and GLES. */
2936 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2938 buffer
= get_framebuffer_target(ctx
, target
);
2940 _mesa_error(ctx
, GL_INVALID_ENUM
,
2941 "glGetFramebufferAttachmentParameteriv(target)");
2945 if (_mesa_is_winsys_fbo(buffer
)) {
2946 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2949 * "If the framebuffer currently bound to target is zero, then
2950 * INVALID_OPERATION is generated."
2952 * The EXT_framebuffer_object spec has the same wording, and the
2953 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2956 if ((!_mesa_is_desktop_gl(ctx
) ||
2957 !ctx
->Extensions
.ARB_framebuffer_object
)
2958 && !_mesa_is_gles3(ctx
)) {
2959 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2960 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2964 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2965 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2966 _mesa_error(ctx
, GL_INVALID_ENUM
,
2967 "glGetFramebufferAttachmentParameteriv(attachment)");
2970 /* the default / window-system FBO */
2971 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2974 /* user-created framebuffer FBO */
2975 att
= get_attachment(ctx
, buffer
, attachment
);
2979 _mesa_error(ctx
, GL_INVALID_ENUM
,
2980 "glGetFramebufferAttachmentParameteriv(attachment)");
2984 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2985 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2986 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
2987 /* This behavior is first specified in OpenGL 4.4 specification.
2989 * From the OpenGL 4.4 spec page 275:
2990 * "This query cannot be performed for a combined depth+stencil
2991 * attachment, since it does not have a single format."
2993 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2994 "glGetFramebufferAttachmentParameteriv("
2995 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
2996 " is invalid for depth+stencil attachment)");
2999 /* the depth and stencil attachments must point to the same buffer */
3000 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
3001 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
3002 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
3003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3004 "glGetFramebufferAttachmentParameteriv(DEPTH/STENCIL"
3005 " attachments differ)");
3010 /* No need to flush here */
3013 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
3014 *params
= _mesa_is_winsys_fbo(buffer
)
3015 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
3017 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
3018 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
3019 *params
= att
->Renderbuffer
->Name
;
3021 else if (att
->Type
== GL_TEXTURE
) {
3022 *params
= att
->Texture
->Name
;
3025 assert(att
->Type
== GL_NONE
);
3026 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
3029 goto invalid_pname_enum
;
3033 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
3034 if (att
->Type
== GL_TEXTURE
) {
3035 *params
= att
->TextureLevel
;
3037 else if (att
->Type
== GL_NONE
) {
3038 _mesa_error(ctx
, err
,
3039 "glGetFramebufferAttachmentParameteriv(pname)");
3042 goto invalid_pname_enum
;
3045 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
3046 if (att
->Type
== GL_TEXTURE
) {
3047 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
3048 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
3054 else if (att
->Type
== GL_NONE
) {
3055 _mesa_error(ctx
, err
,
3056 "glGetFramebufferAttachmentParameteriv(pname)");
3059 goto invalid_pname_enum
;
3062 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
3063 if (ctx
->API
== API_OPENGLES
) {
3064 goto invalid_pname_enum
;
3065 } else if (att
->Type
== GL_NONE
) {
3066 _mesa_error(ctx
, err
,
3067 "glGetFramebufferAttachmentParameteriv(pname)");
3068 } else if (att
->Type
== GL_TEXTURE
) {
3069 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
3070 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
3071 *params
= att
->Zoffset
;
3078 goto invalid_pname_enum
;
3081 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
3082 if ((!_mesa_is_desktop_gl(ctx
) ||
3083 !ctx
->Extensions
.ARB_framebuffer_object
)
3084 && !_mesa_is_gles3(ctx
)) {
3085 goto invalid_pname_enum
;
3087 else if (att
->Type
== GL_NONE
) {
3088 _mesa_error(ctx
, err
,
3089 "glGetFramebufferAttachmentParameteriv(pname)");
3092 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3094 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
3097 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3098 * if the sRGB conversion is unsupported. */
3099 *params
= GL_LINEAR
;
3103 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
3104 if ((ctx
->API
!= API_OPENGL_COMPAT
||
3105 !ctx
->Extensions
.ARB_framebuffer_object
)
3106 && ctx
->API
!= API_OPENGL_CORE
3107 && !_mesa_is_gles3(ctx
)) {
3108 goto invalid_pname_enum
;
3110 else if (att
->Type
== GL_NONE
) {
3111 _mesa_error(ctx
, err
,
3112 "glGetFramebufferAttachmentParameteriv(pname)");
3115 mesa_format format
= att
->Renderbuffer
->Format
;
3117 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3120 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3121 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3122 * generate an INVALID_OPERATION error.
3124 if (_mesa_is_gles3(ctx
) &&
3125 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3127 "glGetFramebufferAttachmentParameteriv(cannot query "
3128 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3129 "GL_DEPTH_STENCIL_ATTACHMENT");
3133 if (format
== MESA_FORMAT_S_UINT8
) {
3137 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
3138 /* depends on the attachment parameter */
3139 if (attachment
== GL_STENCIL_ATTACHMENT
) {
3147 *params
= _mesa_get_format_datatype(format
);
3151 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
3152 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
3153 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
3154 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
3155 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
3156 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
3157 if ((!_mesa_is_desktop_gl(ctx
) ||
3158 !ctx
->Extensions
.ARB_framebuffer_object
)
3159 && !_mesa_is_gles3(ctx
)) {
3160 goto invalid_pname_enum
;
3162 else if (att
->Type
== GL_NONE
) {
3163 _mesa_error(ctx
, err
,
3164 "glGetFramebufferAttachmentParameteriv(pname)");
3166 else if (att
->Texture
) {
3167 const struct gl_texture_image
*texImage
=
3168 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
3171 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
3172 texImage
->TexFormat
);
3178 else if (att
->Renderbuffer
) {
3179 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
3180 att
->Renderbuffer
->Format
);
3183 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
3184 " invalid FBO attachment structure");
3187 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
3188 if (!_mesa_has_geometry_shaders(ctx
)) {
3189 goto invalid_pname_enum
;
3190 } else if (att
->Type
== GL_TEXTURE
) {
3191 *params
= att
->Layered
;
3192 } else if (att
->Type
== GL_NONE
) {
3193 _mesa_error(ctx
, err
,
3194 "glGetFramebufferAttachmentParameteriv(pname)");
3196 goto invalid_pname_enum
;
3200 goto invalid_pname_enum
;
3206 _mesa_error(ctx
, GL_INVALID_ENUM
,
3207 "glGetFramebufferAttachmentParameteriv(pname)");
3213 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3214 const GLenum
*attachments
, GLint x
, GLint y
,
3215 GLsizei width
, GLsizei height
, const char *name
)
3218 struct gl_framebuffer
*fb
;
3219 GET_CURRENT_CONTEXT(ctx
);
3221 fb
= get_framebuffer_target(ctx
, target
);
3223 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3227 if (numAttachments
< 0) {
3228 _mesa_error(ctx
, GL_INVALID_VALUE
,
3229 "%s(numAttachments < 0)", name
);
3233 /* The GL_ARB_invalidate_subdata spec says:
3235 * "If an attachment is specified that does not exist in the
3236 * framebuffer bound to <target>, it is ignored."
3240 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3241 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3242 * INVALID_OPERATION is generated."
3244 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3245 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3246 * set of retrictions).
3248 for (i
= 0; i
< numAttachments
; i
++) {
3249 if (_mesa_is_winsys_fbo(fb
)) {
3250 switch (attachments
[i
]) {
3256 /* Accumulation buffers and auxilary buffers were removed in
3257 * OpenGL 3.1, and they never existed in OpenGL ES.
3259 if (ctx
->API
!= API_OPENGL_COMPAT
)
3269 case GL_FRONT_RIGHT
:
3270 if (!_mesa_is_desktop_gl(ctx
))
3277 switch (attachments
[i
]) {
3278 case GL_DEPTH_ATTACHMENT
:
3279 case GL_STENCIL_ATTACHMENT
:
3281 case GL_DEPTH_STENCIL_ATTACHMENT
:
3282 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3283 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3284 * extension does not make this attachment point valid on ES 2.0.
3286 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
3289 case GL_COLOR_ATTACHMENT0
:
3290 case GL_COLOR_ATTACHMENT1
:
3291 case GL_COLOR_ATTACHMENT2
:
3292 case GL_COLOR_ATTACHMENT3
:
3293 case GL_COLOR_ATTACHMENT4
:
3294 case GL_COLOR_ATTACHMENT5
:
3295 case GL_COLOR_ATTACHMENT6
:
3296 case GL_COLOR_ATTACHMENT7
:
3297 case GL_COLOR_ATTACHMENT8
:
3298 case GL_COLOR_ATTACHMENT9
:
3299 case GL_COLOR_ATTACHMENT10
:
3300 case GL_COLOR_ATTACHMENT11
:
3301 case GL_COLOR_ATTACHMENT12
:
3302 case GL_COLOR_ATTACHMENT13
:
3303 case GL_COLOR_ATTACHMENT14
:
3304 case GL_COLOR_ATTACHMENT15
: {
3305 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3306 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3307 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3308 "%s(attachment >= max. color attachments)", name
);
3319 /* We don't actually do anything for this yet. Just return after
3320 * validating the parameters and generating the required errors.
3325 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3331 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3332 const GLenum
*attachments
, GLint x
, GLint y
,
3333 GLsizei width
, GLsizei height
)
3335 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3336 x
, y
, width
, height
,
3337 "glInvalidateSubFramebuffer");
3342 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3343 const GLenum
*attachments
)
3345 /* The GL_ARB_invalidate_subdata spec says:
3349 * void InvalidateFramebuffer(enum target,
3350 * sizei numAttachments,
3351 * const enum *attachments);
3353 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3354 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3355 * <MAX_VIEWPORT_DIMS[1]> respectively."
3357 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3359 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3360 "glInvalidateFramebuffer");
3365 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3366 const GLenum
*attachments
)
3368 struct gl_framebuffer
*fb
;
3371 GET_CURRENT_CONTEXT(ctx
);
3373 fb
= get_framebuffer_target(ctx
, target
);
3375 _mesa_error(ctx
, GL_INVALID_ENUM
,
3376 "glDiscardFramebufferEXT(target %s)",
3377 _mesa_lookup_enum_by_nr(target
));
3381 if (numAttachments
< 0) {
3382 _mesa_error(ctx
, GL_INVALID_VALUE
,
3383 "glDiscardFramebufferEXT(numAttachments < 0)");
3387 for (i
= 0; i
< numAttachments
; i
++) {
3388 switch (attachments
[i
]) {
3392 if (_mesa_is_user_fbo(fb
))
3395 case GL_COLOR_ATTACHMENT0
:
3396 case GL_DEPTH_ATTACHMENT
:
3397 case GL_STENCIL_ATTACHMENT
:
3398 if (_mesa_is_winsys_fbo(fb
))
3406 if (ctx
->Driver
.DiscardFramebuffer
)
3407 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3412 _mesa_error(ctx
, GL_INVALID_ENUM
,
3413 "glDiscardFramebufferEXT(attachment %s)",
3414 _mesa_lookup_enum_by_nr(attachments
[i
]));