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 layer
,
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
= layer
;
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_FramebufferRenderbuffer_sw(struct gl_context
*ctx
,
525 struct gl_framebuffer
*fb
,
527 struct gl_renderbuffer
*rb
)
529 struct gl_renderbuffer_attachment
*att
;
531 mtx_lock(&fb
->Mutex
);
533 att
= get_attachment(ctx
, fb
, attachment
);
536 set_renderbuffer_attachment(ctx
, att
, rb
);
537 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
538 /* do stencil attachment here (depth already done above) */
539 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
541 set_renderbuffer_attachment(ctx
, att
, rb
);
543 rb
->AttachedAnytime
= GL_TRUE
;
546 remove_attachment(ctx
, att
);
547 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
548 /* detach stencil (depth was detached above) */
549 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
551 remove_attachment(ctx
, att
);
555 invalidate_framebuffer(fb
);
557 mtx_unlock(&fb
->Mutex
);
562 * Fallback for ctx->Driver.ValidateFramebuffer()
563 * Check if the renderbuffer's formats are supported by the software
565 * Drivers should probably override this.
568 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
571 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
572 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
574 switch (rb
->_BaseFormat
) {
576 case GL_LUMINANCE_ALPHA
:
581 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
585 switch (rb
->Format
) {
586 /* XXX This list is likely incomplete. */
587 case MESA_FORMAT_R9G9B9E5_FLOAT
:
588 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
591 /* render buffer format is supported by software rendering */
600 * Return true if the framebuffer has a combined depth/stencil
601 * renderbuffer attached.
604 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
606 const struct gl_renderbuffer_attachment
*depth
=
607 &fb
->Attachment
[BUFFER_DEPTH
];
608 const struct gl_renderbuffer_attachment
*stencil
=
609 &fb
->Attachment
[BUFFER_STENCIL
];
611 if (depth
->Type
== stencil
->Type
) {
612 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
613 depth
->Renderbuffer
== stencil
->Renderbuffer
)
616 if (depth
->Type
== GL_TEXTURE
&&
617 depth
->Texture
== stencil
->Texture
)
629 att_incomplete(const char *msg
)
631 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
632 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
641 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
643 static GLuint msg_id
;
645 _mesa_gl_debug(ctx
, &msg_id
,
646 MESA_DEBUG_SOURCE_API
,
647 MESA_DEBUG_TYPE_OTHER
,
648 MESA_DEBUG_SEVERITY_MEDIUM
,
649 "FBO incomplete: %s [%d]\n", msg
, index
);
651 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
652 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
658 * Is the given base format a legal format for a color renderbuffer?
661 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
663 switch (baseFormat
) {
668 case GL_LUMINANCE_ALPHA
:
671 return ctx
->API
== API_OPENGL_COMPAT
&&
672 ctx
->Extensions
.ARB_framebuffer_object
;
675 return ctx
->Extensions
.ARB_texture_rg
;
683 * Is the given base format a legal format for a color renderbuffer?
686 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
687 GLenum internalFormat
)
689 const GLenum baseFormat
=
690 _mesa_get_format_base_format(format
);
693 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
694 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
698 /* Reject additional cases for GLES */
699 switch (internalFormat
) {
719 if (format
== MESA_FORMAT_B10G10R10A2_UNORM
&&
720 internalFormat
!= GL_RGB10_A2
) {
729 * Is the given base format a legal format for a depth/stencil renderbuffer?
732 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
734 switch (baseFormat
) {
735 case GL_DEPTH_COMPONENT
:
736 case GL_DEPTH_STENCIL_EXT
:
745 * Test if an attachment point is complete and update its Complete field.
746 * \param format if GL_COLOR, this is a color attachment point,
747 * if GL_DEPTH, this is a depth component attachment point,
748 * if GL_STENCIL, this is a stencil component attachment point.
751 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
752 struct gl_renderbuffer_attachment
*att
)
754 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
756 /* assume complete */
757 att
->Complete
= GL_TRUE
;
759 /* Look for reasons why the attachment might be incomplete */
760 if (att
->Type
== GL_TEXTURE
) {
761 const struct gl_texture_object
*texObj
= att
->Texture
;
762 struct gl_texture_image
*texImage
;
766 att_incomplete("no texobj");
767 att
->Complete
= GL_FALSE
;
771 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
773 att_incomplete("no teximage");
774 att
->Complete
= GL_FALSE
;
777 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
778 att_incomplete("teximage width/height=0");
779 att
->Complete
= GL_FALSE
;
783 switch (texObj
->Target
) {
785 if (att
->Zoffset
>= texImage
->Depth
) {
786 att_incomplete("bad z offset");
787 att
->Complete
= GL_FALSE
;
791 case GL_TEXTURE_1D_ARRAY
:
792 if (att
->Zoffset
>= texImage
->Height
) {
793 att_incomplete("bad 1D-array layer");
794 att
->Complete
= GL_FALSE
;
798 case GL_TEXTURE_2D_ARRAY
:
799 if (att
->Zoffset
>= texImage
->Depth
) {
800 att_incomplete("bad 2D-array layer");
801 att
->Complete
= GL_FALSE
;
805 case GL_TEXTURE_CUBE_MAP_ARRAY
:
806 if (att
->Zoffset
>= texImage
->Depth
) {
807 att_incomplete("bad cube-array layer");
808 att
->Complete
= GL_FALSE
;
814 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
816 if (format
== GL_COLOR
) {
817 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
818 att_incomplete("bad format");
819 att
->Complete
= GL_FALSE
;
822 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
823 att_incomplete("compressed internalformat");
824 att
->Complete
= GL_FALSE
;
828 /* OES_texture_float allows creation and use of floating point
829 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
830 * these textures to be used as a render target, this is done via
831 * GL_EXT_color_buffer(_half)_float with set of new sized types.
833 if (_mesa_is_gles(ctx
) && (texImage
->TexObject
->_IsFloat
||
834 texImage
->TexObject
->_IsHalfFloat
)) {
835 att_incomplete("bad internal format");
836 att
->Complete
= GL_FALSE
;
840 else if (format
== GL_DEPTH
) {
841 if (baseFormat
== GL_DEPTH_COMPONENT
) {
844 else if (ctx
->Extensions
.ARB_depth_texture
&&
845 baseFormat
== GL_DEPTH_STENCIL
) {
849 att
->Complete
= GL_FALSE
;
850 att_incomplete("bad depth format");
855 assert(format
== GL_STENCIL
);
856 if (ctx
->Extensions
.ARB_depth_texture
&&
857 baseFormat
== GL_DEPTH_STENCIL
) {
859 } else if (ctx
->Extensions
.ARB_texture_stencil8
&&
860 baseFormat
== GL_STENCIL_INDEX
) {
863 /* no such thing as stencil-only textures */
864 att_incomplete("illegal stencil texture");
865 att
->Complete
= GL_FALSE
;
870 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
871 const GLenum baseFormat
=
872 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
874 assert(att
->Renderbuffer
);
875 if (!att
->Renderbuffer
->InternalFormat
||
876 att
->Renderbuffer
->Width
< 1 ||
877 att
->Renderbuffer
->Height
< 1) {
878 att_incomplete("0x0 renderbuffer");
879 att
->Complete
= GL_FALSE
;
882 if (format
== GL_COLOR
) {
883 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
884 att_incomplete("bad renderbuffer color format");
885 att
->Complete
= GL_FALSE
;
889 else if (format
== GL_DEPTH
) {
890 if (baseFormat
== GL_DEPTH_COMPONENT
) {
893 else if (baseFormat
== GL_DEPTH_STENCIL
) {
897 att_incomplete("bad renderbuffer depth format");
898 att
->Complete
= GL_FALSE
;
903 assert(format
== GL_STENCIL
);
904 if (baseFormat
== GL_STENCIL_INDEX
||
905 baseFormat
== GL_DEPTH_STENCIL
) {
909 att
->Complete
= GL_FALSE
;
910 att_incomplete("bad renderbuffer stencil format");
916 assert(att
->Type
== GL_NONE
);
924 * Test if the given framebuffer object is complete and update its
925 * Status field with the results.
926 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
927 * driver to make hardware-specific validation/completeness checks.
928 * Also update the framebuffer's Width and Height fields if the
929 * framebuffer is complete.
932 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
933 struct gl_framebuffer
*fb
)
936 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
937 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
938 GLint numSamples
= -1;
939 GLint fixedSampleLocations
= -1;
942 /* Covers max_layer_count, is_layered, and layer_tex_target */
943 bool layer_info_valid
= false;
944 GLuint max_layer_count
= 0, att_layer_count
;
945 bool is_layered
= false;
946 GLenum layer_tex_target
= 0;
947 bool has_depth_attachment
= false;
948 bool has_stencil_attachment
= false;
950 assert(_mesa_is_user_fbo(fb
));
952 /* we're changing framebuffer fields here */
953 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
958 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
959 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
960 fb
->_HasAttachments
= true;
962 /* Start at -2 to more easily loop over all attachment points.
967 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
968 struct gl_renderbuffer_attachment
*att
;
970 mesa_format attFormat
;
971 GLenum att_tex_target
= GL_NONE
;
974 * XXX for ARB_fbo, only check color buffers that are named by
975 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
978 /* check for attachment completeness
981 att
= &fb
->Attachment
[BUFFER_DEPTH
];
982 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
983 if (!att
->Complete
) {
984 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
985 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
987 } else if (att
->Type
!= GL_NONE
) {
988 has_depth_attachment
= true;
992 att
= &fb
->Attachment
[BUFFER_STENCIL
];
993 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
994 if (!att
->Complete
) {
995 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
996 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
998 } else if (att
->Type
!= GL_NONE
) {
999 has_stencil_attachment
= true;
1003 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
1004 test_attachment_completeness(ctx
, GL_COLOR
, att
);
1005 if (!att
->Complete
) {
1006 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1007 fbo_incomplete(ctx
, "color attachment incomplete", i
);
1012 /* get width, height, format of the renderbuffer/texture
1014 if (att
->Type
== GL_TEXTURE
) {
1015 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
1016 att_tex_target
= att
->Texture
->Target
;
1017 minWidth
= MIN2(minWidth
, texImg
->Width
);
1018 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
1019 minHeight
= MIN2(minHeight
, texImg
->Height
);
1020 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
1021 f
= texImg
->_BaseFormat
;
1022 attFormat
= texImg
->TexFormat
;
1025 if (!is_format_color_renderable(ctx
, attFormat
,
1026 texImg
->InternalFormat
) &&
1027 !is_legal_depth_format(ctx
, f
) &&
1028 f
!= GL_STENCIL_INDEX
) {
1029 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
1030 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
1035 numSamples
= texImg
->NumSamples
;
1036 else if (numSamples
!= texImg
->NumSamples
) {
1037 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1038 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1042 if (fixedSampleLocations
< 0)
1043 fixedSampleLocations
= texImg
->FixedSampleLocations
;
1044 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
1045 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1046 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1050 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1051 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1052 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1053 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1054 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1055 f
= att
->Renderbuffer
->InternalFormat
;
1056 attFormat
= att
->Renderbuffer
->Format
;
1060 numSamples
= att
->Renderbuffer
->NumSamples
;
1061 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1062 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1063 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1067 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1068 if (fixedSampleLocations
< 0)
1069 fixedSampleLocations
= GL_TRUE
;
1070 else if (fixedSampleLocations
!= GL_TRUE
) {
1071 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1072 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1077 assert(att
->Type
== GL_NONE
);
1081 /* check if integer color */
1082 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
1084 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1086 GLenum type
= _mesa_get_format_datatype(attFormat
);
1088 fb
->_AllColorBuffersFixedPoint
=
1089 fb
->_AllColorBuffersFixedPoint
&&
1090 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1092 fb
->_HasSNormOrFloatColorBuffer
=
1093 fb
->_HasSNormOrFloatColorBuffer
||
1094 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1097 /* Error-check width, height, format */
1098 if (numImages
== 1) {
1105 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1106 /* check that width, height, format are same */
1107 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1108 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1109 fbo_incomplete(ctx
, "width or height mismatch", -1);
1112 /* check that all color buffers are the same format */
1113 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1114 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1115 fbo_incomplete(ctx
, "format mismatch", -1);
1121 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1123 if (att
->Type
== GL_RENDERBUFFER
&&
1124 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1125 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1126 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1130 /* Check that layered rendering is consistent. */
1132 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1133 att_layer_count
= 6;
1134 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1135 att_layer_count
= att
->Renderbuffer
->Height
;
1137 att_layer_count
= att
->Renderbuffer
->Depth
;
1139 att_layer_count
= 0;
1141 if (!layer_info_valid
) {
1142 is_layered
= att
->Layered
;
1143 max_layer_count
= att_layer_count
;
1144 layer_tex_target
= att_tex_target
;
1145 layer_info_valid
= true;
1146 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1147 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1148 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1150 } else if (is_layered
!= att
->Layered
) {
1151 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1153 "framebuffer attachment layer mode is inconsistent",
1156 } else if (att_layer_count
> max_layer_count
) {
1157 max_layer_count
= att_layer_count
;
1161 * The extension GL_ARB_framebuffer_no_attachments places additional
1162 * requirement on each attachment. Those additional requirements are
1163 * tighter that those of previous versions of GL. In interest of better
1164 * compatibility, we will not enforce these restrictions. For the record
1165 * those additional restrictions are quoted below:
1167 * "The width and height of image are greater than zero and less than or
1168 * equal to the values of the implementation-dependent limits
1169 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1171 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1172 * array texture and the attachment is layered, the depth or layer count
1173 * of the texture is less than or equal to the implementation-dependent
1174 * limit MAX_FRAMEBUFFER_LAYERS."
1176 * "If image has multiple samples, its sample count is less than or equal
1177 * to the value of the implementation-dependent limit
1178 * MAX_FRAMEBUFFER_SAMPLES."
1180 * The same requirements are also in place for GL 4.5,
1181 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1185 fb
->MaxNumLayers
= max_layer_count
;
1187 if (numImages
== 0) {
1188 fb
->_HasAttachments
= false;
1190 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
1191 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1192 fbo_incomplete(ctx
, "no attachments", -1);
1196 if (fb
->DefaultGeometry
.Width
== 0 || fb
->DefaultGeometry
.Height
== 0) {
1197 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1198 fbo_incomplete(ctx
, "no attachments and default width or height is 0", -1);
1203 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1204 /* Check that all DrawBuffers are present */
1205 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1206 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1207 const struct gl_renderbuffer_attachment
*att
1208 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
1210 if (att
->Type
== GL_NONE
) {
1211 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1212 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1218 /* Check that the ReadBuffer is present */
1219 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1220 const struct gl_renderbuffer_attachment
*att
1221 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
1223 if (att
->Type
== GL_NONE
) {
1224 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1225 fbo_incomplete(ctx
, "missing readbuffer", -1);
1231 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1233 * "Depth and stencil attachments, if present, are the same image."
1235 * This restriction is not present in the OpenGL ES2 spec.
1237 if (_mesa_is_gles3(ctx
) &&
1238 has_stencil_attachment
&& has_depth_attachment
&&
1239 !_mesa_has_depthstencil_combined(fb
)) {
1240 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1241 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1245 /* Provisionally set status = COMPLETE ... */
1246 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1248 /* ... but the driver may say the FB is incomplete.
1249 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1252 if (ctx
->Driver
.ValidateFramebuffer
) {
1253 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1254 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1255 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1259 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
1261 * Note that if ARB_framebuffer_object is supported and the attached
1262 * renderbuffers/textures are different sizes, the framebuffer
1263 * width/height will be set to the smallest width/height.
1265 if (numImages
!= 0) {
1266 fb
->Width
= minWidth
;
1267 fb
->Height
= minHeight
;
1270 /* finally, update the visual info for the framebuffer */
1271 _mesa_update_framebuffer_visual(ctx
, fb
);
1276 GLboolean GLAPIENTRY
1277 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1279 GET_CURRENT_CONTEXT(ctx
);
1280 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1282 struct gl_renderbuffer
*rb
=
1283 _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1284 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1291 static struct gl_renderbuffer
*
1292 allocate_renderbuffer(struct gl_context
*ctx
, GLuint renderbuffer
,
1295 struct gl_renderbuffer
*newRb
;
1297 /* create new renderbuffer object */
1298 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1300 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1303 assert(newRb
->AllocStorage
);
1304 mtx_lock(&ctx
->Shared
->Mutex
);
1305 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1306 newRb
->RefCount
= 1; /* referenced by hash table */
1307 mtx_unlock(&ctx
->Shared
->Mutex
);
1314 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1316 struct gl_renderbuffer
*newRb
;
1317 GET_CURRENT_CONTEXT(ctx
);
1319 if (target
!= GL_RENDERBUFFER_EXT
) {
1320 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1324 /* No need to flush here since the render buffer binding has no
1325 * effect on rendering state.
1329 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1330 if (newRb
== &DummyRenderbuffer
) {
1331 /* ID was reserved, but no real renderbuffer object made yet */
1334 else if (!newRb
&& !allow_user_names
) {
1335 /* All RB IDs must be Gen'd */
1336 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1341 newRb
= allocate_renderbuffer(ctx
, renderbuffer
, "glBindRenderbufferEXT");
1348 assert(newRb
!= &DummyRenderbuffer
);
1350 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1354 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1356 GET_CURRENT_CONTEXT(ctx
);
1358 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1359 * entry point, but they allow the use of user-generated names.
1361 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1365 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1367 /* This function should not be in the dispatch table for core profile /
1368 * OpenGL 3.1, so execution should never get here in those cases -- no
1369 * need for an explicit test.
1371 bind_renderbuffer(target
, renderbuffer
, true);
1375 framebuffer_parameteri(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1376 GLenum pname
, GLint param
, const char *func
)
1379 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1380 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferWidth
)
1381 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1383 fb
->DefaultGeometry
.Width
= param
;
1385 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1386 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferHeight
)
1387 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1389 fb
->DefaultGeometry
.Height
= param
;
1391 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1393 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1394 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1396 if (_mesa_is_gles31(ctx
)) {
1397 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1400 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferLayers
)
1401 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1403 fb
->DefaultGeometry
.Layers
= param
;
1405 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1406 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferSamples
)
1407 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1409 fb
->DefaultGeometry
.NumSamples
= param
;
1411 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1412 fb
->DefaultGeometry
.FixedSampleLocations
= param
;
1415 _mesa_error(ctx
, GL_INVALID_ENUM
,
1416 "%s(pname=0x%x)", func
, pname
);
1421 _mesa_FramebufferParameteri(GLenum target
, GLenum pname
, GLint param
)
1423 GET_CURRENT_CONTEXT(ctx
);
1424 struct gl_framebuffer
*fb
;
1426 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
1427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1428 "glFramebufferParameteriv not supported "
1429 "(ARB_framebuffer_no_attachments not implemented)");
1433 fb
= get_framebuffer_target(ctx
, target
);
1435 _mesa_error(ctx
, GL_INVALID_ENUM
,
1436 "glFramebufferParameteri(target=0x%x)", target
);
1440 /* check framebuffer binding */
1441 if (_mesa_is_winsys_fbo(fb
)) {
1442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1443 "glFramebufferParameteri");
1447 framebuffer_parameteri(ctx
, fb
, pname
, param
, "glFramebufferParameteri");
1451 get_framebuffer_parameteriv(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1452 GLenum pname
, GLint
*params
, const char *func
)
1455 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1456 *params
= fb
->DefaultGeometry
.Width
;
1458 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1459 *params
= fb
->DefaultGeometry
.Height
;
1461 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1463 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1464 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1466 if (_mesa_is_gles31(ctx
)) {
1467 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1470 *params
= fb
->DefaultGeometry
.Layers
;
1472 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1473 *params
= fb
->DefaultGeometry
.NumSamples
;
1475 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1476 *params
= fb
->DefaultGeometry
.FixedSampleLocations
;
1479 _mesa_error(ctx
, GL_INVALID_ENUM
,
1480 "%s(pname=0x%x)", func
, pname
);
1485 _mesa_GetFramebufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1487 GET_CURRENT_CONTEXT(ctx
);
1488 struct gl_framebuffer
*fb
;
1490 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
1491 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1492 "glGetFramebufferParameteriv not supported "
1493 "(ARB_framebuffer_no_attachments not implemented)");
1497 fb
= get_framebuffer_target(ctx
, target
);
1499 _mesa_error(ctx
, GL_INVALID_ENUM
,
1500 "glGetFramebufferParameteriv(target=0x%x)", target
);
1504 /* check framebuffer binding */
1505 if (_mesa_is_winsys_fbo(fb
)) {
1506 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1507 "glGetFramebufferParameteriv");
1511 get_framebuffer_parameteriv(ctx
, fb
, pname
, params
,
1512 "glGetFramebufferParameteriv");
1517 * Remove the specified renderbuffer or texture from any attachment point in
1521 * \c true if the renderbuffer was detached from an attachment point. \c
1525 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1526 struct gl_framebuffer
*fb
,
1530 bool progress
= false;
1532 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1533 if (fb
->Attachment
[i
].Texture
== att
1534 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1535 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1540 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1541 * Completeness," of the OpenGL 3.1 spec says:
1543 * "Performing any of the following actions may change whether the
1544 * framebuffer is considered complete or incomplete:
1548 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1549 * containing an image that is attached to a framebuffer object
1550 * that is bound to the framebuffer."
1553 invalidate_framebuffer(fb
);
1560 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1563 GET_CURRENT_CONTEXT(ctx
);
1566 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1570 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1572 for (i
= 0; i
< n
; i
++) {
1573 if (renderbuffers
[i
] > 0) {
1574 struct gl_renderbuffer
*rb
;
1575 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1577 /* check if deleting currently bound renderbuffer object */
1578 if (rb
== ctx
->CurrentRenderbuffer
) {
1580 assert(rb
->RefCount
>= 2);
1581 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1584 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1585 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1586 * of the OpenGL 3.1 spec says:
1588 * "If a renderbuffer object is deleted while its image is
1589 * attached to one or more attachment points in the currently
1590 * bound framebuffer, then it is as if FramebufferRenderbuffer
1591 * had been called, with a renderbuffer of 0, for each
1592 * attachment point to which this image was attached in the
1593 * currently bound framebuffer. In other words, this
1594 * renderbuffer image is first detached from all attachment
1595 * points in the currently bound framebuffer. Note that the
1596 * renderbuffer image is specifically not detached from any
1597 * non-bound framebuffers. Detaching the image from any
1598 * non-bound framebuffers is the responsibility of the
1601 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1602 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1604 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1605 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1606 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1609 /* Remove from hash table immediately, to free the ID.
1610 * But the object will not be freed until it's no longer
1611 * referenced anywhere else.
1613 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1615 if (rb
!= &DummyRenderbuffer
) {
1616 /* no longer referenced by hash table */
1617 _mesa_reference_renderbuffer(&rb
, NULL
);
1625 create_render_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*renderbuffers
,
1628 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1633 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n<0)", func
);
1640 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1642 for (i
= 0; i
< n
; i
++) {
1643 GLuint name
= first
+ i
;
1644 renderbuffers
[i
] = name
;
1647 allocate_renderbuffer(ctx
, name
, func
);
1649 /* insert a dummy renderbuffer into the hash table */
1650 mtx_lock(&ctx
->Shared
->Mutex
);
1651 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1652 mtx_unlock(&ctx
->Shared
->Mutex
);
1659 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1661 GET_CURRENT_CONTEXT(ctx
);
1662 create_render_buffers(ctx
, n
, renderbuffers
, false);
1667 _mesa_CreateRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1669 GET_CURRENT_CONTEXT(ctx
);
1670 create_render_buffers(ctx
, n
, renderbuffers
, true);
1675 * Given an internal format token for a render buffer, return the
1676 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1677 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1678 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1680 * This is similar to _mesa_base_tex_format() but the set of valid
1681 * internal formats is different.
1683 * Note that even if a format is determined to be legal here, validation
1684 * of the FBO may fail if the format is not supported by the driver/GPU.
1686 * \param internalFormat as passed to glRenderbufferStorage()
1687 * \return the base internal format, or 0 if internalFormat is illegal
1690 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1693 * Notes: some formats such as alpha, luminance, etc. were added
1694 * with GL_ARB_framebuffer_object.
1696 switch (internalFormat
) {
1702 return (ctx
->API
== API_OPENGL_COMPAT
&&
1703 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1707 case GL_LUMINANCE12
:
1708 case GL_LUMINANCE16
:
1709 return (ctx
->API
== API_OPENGL_COMPAT
&&
1710 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1711 case GL_LUMINANCE_ALPHA
:
1712 case GL_LUMINANCE4_ALPHA4
:
1713 case GL_LUMINANCE6_ALPHA2
:
1714 case GL_LUMINANCE8_ALPHA8
:
1715 case GL_LUMINANCE12_ALPHA4
:
1716 case GL_LUMINANCE12_ALPHA12
:
1717 case GL_LUMINANCE16_ALPHA16
:
1718 return (ctx
->API
== API_OPENGL_COMPAT
&&
1719 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1723 case GL_INTENSITY12
:
1724 case GL_INTENSITY16
:
1725 return (ctx
->API
== API_OPENGL_COMPAT
&&
1726 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1736 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1738 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1747 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1749 case GL_SRGB8_ALPHA8_EXT
:
1750 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1751 case GL_STENCIL_INDEX
:
1752 case GL_STENCIL_INDEX1_EXT
:
1753 case GL_STENCIL_INDEX4_EXT
:
1754 case GL_STENCIL_INDEX16_EXT
:
1755 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1756 * OpenGL ES, but Mesa does not currently support them.
1758 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1759 case GL_STENCIL_INDEX8_EXT
:
1760 return GL_STENCIL_INDEX
;
1761 case GL_DEPTH_COMPONENT
:
1762 case GL_DEPTH_COMPONENT32
:
1763 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1764 case GL_DEPTH_COMPONENT16
:
1765 case GL_DEPTH_COMPONENT24
:
1766 return GL_DEPTH_COMPONENT
;
1767 case GL_DEPTH_STENCIL
:
1768 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1769 case GL_DEPTH24_STENCIL8
:
1770 return GL_DEPTH_STENCIL
;
1771 case GL_DEPTH_COMPONENT32F
:
1772 return ctx
->Version
>= 30
1773 || (ctx
->API
== API_OPENGL_COMPAT
&&
1774 ctx
->Extensions
.ARB_depth_buffer_float
)
1775 ? GL_DEPTH_COMPONENT
: 0;
1776 case GL_DEPTH32F_STENCIL8
:
1777 return ctx
->Version
>= 30
1778 || (ctx
->API
== API_OPENGL_COMPAT
&&
1779 ctx
->Extensions
.ARB_depth_buffer_float
)
1780 ? GL_DEPTH_STENCIL
: 0;
1783 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1786 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1790 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1793 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1795 /* signed normalized texture formats */
1799 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1804 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1808 case GL_RGB16_SNORM
:
1809 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1812 case GL_RGBA8_SNORM
:
1813 case GL_RGBA16_SNORM
:
1814 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1816 case GL_ALPHA_SNORM
:
1817 case GL_ALPHA8_SNORM
:
1818 case GL_ALPHA16_SNORM
:
1819 return ctx
->API
== API_OPENGL_COMPAT
&&
1820 ctx
->Extensions
.EXT_texture_snorm
&&
1821 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1822 case GL_LUMINANCE_SNORM
:
1823 case GL_LUMINANCE8_SNORM
:
1824 case GL_LUMINANCE16_SNORM
:
1825 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1827 case GL_LUMINANCE_ALPHA_SNORM
:
1828 case GL_LUMINANCE8_ALPHA8_SNORM
:
1829 case GL_LUMINANCE16_ALPHA16_SNORM
:
1830 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1831 ? GL_LUMINANCE_ALPHA
: 0;
1832 case GL_INTENSITY_SNORM
:
1833 case GL_INTENSITY8_SNORM
:
1834 case GL_INTENSITY16_SNORM
:
1835 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1840 return ((_mesa_is_desktop_gl(ctx
) &&
1841 ctx
->Extensions
.ARB_texture_rg
&&
1842 ctx
->Extensions
.ARB_texture_float
) ||
1843 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1847 return ((_mesa_is_desktop_gl(ctx
) &&
1848 ctx
->Extensions
.ARB_texture_rg
&&
1849 ctx
->Extensions
.ARB_texture_float
) ||
1850 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1854 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1858 return ((_mesa_is_desktop_gl(ctx
) &&
1859 ctx
->Extensions
.ARB_texture_float
) ||
1860 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1862 case GL_ALPHA16F_ARB
:
1863 case GL_ALPHA32F_ARB
:
1864 return ctx
->API
== API_OPENGL_COMPAT
&&
1865 ctx
->Extensions
.ARB_texture_float
&&
1866 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1867 case GL_LUMINANCE16F_ARB
:
1868 case GL_LUMINANCE32F_ARB
:
1869 return ctx
->API
== API_OPENGL_COMPAT
&&
1870 ctx
->Extensions
.ARB_texture_float
&&
1871 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1872 case GL_LUMINANCE_ALPHA16F_ARB
:
1873 case GL_LUMINANCE_ALPHA32F_ARB
:
1874 return ctx
->API
== API_OPENGL_COMPAT
&&
1875 ctx
->Extensions
.ARB_texture_float
&&
1876 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1877 case GL_INTENSITY16F_ARB
:
1878 case GL_INTENSITY32F_ARB
:
1879 return ctx
->API
== API_OPENGL_COMPAT
&&
1880 ctx
->Extensions
.ARB_texture_float
&&
1881 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1882 case GL_R11F_G11F_B10F
:
1883 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1884 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1887 case GL_RGBA8UI_EXT
:
1888 case GL_RGBA16UI_EXT
:
1889 case GL_RGBA32UI_EXT
:
1891 case GL_RGBA16I_EXT
:
1892 case GL_RGBA32I_EXT
:
1893 return ctx
->Version
>= 30
1894 || (_mesa_is_desktop_gl(ctx
) &&
1895 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1898 case GL_RGB16UI_EXT
:
1899 case GL_RGB32UI_EXT
:
1903 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1911 return ctx
->Version
>= 30
1912 || (_mesa_is_desktop_gl(ctx
) &&
1913 ctx
->Extensions
.ARB_texture_rg
&&
1914 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1922 return ctx
->Version
>= 30
1923 || (_mesa_is_desktop_gl(ctx
) &&
1924 ctx
->Extensions
.ARB_texture_rg
&&
1925 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1927 case GL_INTENSITY8I_EXT
:
1928 case GL_INTENSITY8UI_EXT
:
1929 case GL_INTENSITY16I_EXT
:
1930 case GL_INTENSITY16UI_EXT
:
1931 case GL_INTENSITY32I_EXT
:
1932 case GL_INTENSITY32UI_EXT
:
1933 return ctx
->API
== API_OPENGL_COMPAT
&&
1934 ctx
->Extensions
.EXT_texture_integer
&&
1935 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1937 case GL_LUMINANCE8I_EXT
:
1938 case GL_LUMINANCE8UI_EXT
:
1939 case GL_LUMINANCE16I_EXT
:
1940 case GL_LUMINANCE16UI_EXT
:
1941 case GL_LUMINANCE32I_EXT
:
1942 case GL_LUMINANCE32UI_EXT
:
1943 return ctx
->API
== API_OPENGL_COMPAT
&&
1944 ctx
->Extensions
.EXT_texture_integer
&&
1945 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1947 case GL_LUMINANCE_ALPHA8I_EXT
:
1948 case GL_LUMINANCE_ALPHA8UI_EXT
:
1949 case GL_LUMINANCE_ALPHA16I_EXT
:
1950 case GL_LUMINANCE_ALPHA16UI_EXT
:
1951 case GL_LUMINANCE_ALPHA32I_EXT
:
1952 case GL_LUMINANCE_ALPHA32UI_EXT
:
1953 return ctx
->API
== API_OPENGL_COMPAT
&&
1954 ctx
->Extensions
.EXT_texture_integer
&&
1955 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1957 case GL_ALPHA8I_EXT
:
1958 case GL_ALPHA8UI_EXT
:
1959 case GL_ALPHA16I_EXT
:
1960 case GL_ALPHA16UI_EXT
:
1961 case GL_ALPHA32I_EXT
:
1962 case GL_ALPHA32UI_EXT
:
1963 return ctx
->API
== API_OPENGL_COMPAT
&&
1964 ctx
->Extensions
.EXT_texture_integer
&&
1965 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1968 return (_mesa_is_desktop_gl(ctx
) &&
1969 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1970 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1973 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1982 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1985 invalidate_rb(GLuint key
, void *data
, void *userData
)
1987 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1988 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1990 /* If this is a user-created FBO */
1991 if (_mesa_is_user_fbo(fb
)) {
1993 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1994 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1995 if (att
->Type
== GL_RENDERBUFFER
&&
1996 att
->Renderbuffer
== rb
) {
1997 /* Mark fb status as indeterminate to force re-validation */
2006 /** sentinal value, see below */
2007 #define NO_SAMPLES 1000
2011 * Helper function used by renderbuffer_storage_direct() and
2012 * renderbuffer_storage_target().
2013 * samples will be NO_SAMPLES if called by a non-multisample function.
2016 renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
2017 GLenum internalFormat
, GLsizei width
,
2018 GLsizei height
, GLsizei samples
, const char *func
)
2021 GLenum sample_count_error
;
2023 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
2024 if (baseFormat
== 0) {
2025 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
2026 func
, _mesa_enum_to_string(internalFormat
));
2030 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2031 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid width %d)", func
,
2036 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2037 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid height %d)", func
,
2042 if (samples
== NO_SAMPLES
) {
2043 /* NumSamples == 0 indicates non-multisampling */
2047 /* check the sample count;
2048 * note: driver may choose to use more samples than what's requested
2050 sample_count_error
= _mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
2051 internalFormat
, samples
);
2053 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2055 * "If a negative number is provided where an argument of type sizei or
2056 * sizeiptr is specified, the error INVALID VALUE is generated."
2059 sample_count_error
= GL_INVALID_VALUE
;
2062 if (sample_count_error
!= GL_NO_ERROR
) {
2063 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
2068 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2070 if (rb
->InternalFormat
== internalFormat
&&
2071 rb
->Width
== (GLuint
) width
&&
2072 rb
->Height
== (GLuint
) height
&&
2073 rb
->NumSamples
== samples
) {
2074 /* no change in allocation needed */
2078 /* These MUST get set by the AllocStorage func */
2079 rb
->Format
= MESA_FORMAT_NONE
;
2080 rb
->NumSamples
= samples
;
2082 /* Now allocate the storage */
2083 assert(rb
->AllocStorage
);
2084 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
2085 /* No error - check/set fields now */
2086 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2087 assert(rb
->Width
== (GLuint
) width
);
2088 assert(rb
->Height
== (GLuint
) height
);
2089 rb
->InternalFormat
= internalFormat
;
2090 rb
->_BaseFormat
= baseFormat
;
2091 assert(rb
->_BaseFormat
!= 0);
2094 /* Probably ran out of memory - clear the fields */
2097 rb
->Format
= MESA_FORMAT_NONE
;
2098 rb
->InternalFormat
= GL_NONE
;
2099 rb
->_BaseFormat
= GL_NONE
;
2103 /* Invalidate the framebuffers the renderbuffer is attached in. */
2104 if (rb
->AttachedAnytime
) {
2105 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
2110 * Helper function used by _mesa_NamedRenderbufferStorage*().
2111 * samples will be NO_SAMPLES if called by a non-multisample function.
2114 renderbuffer_storage_named(GLuint renderbuffer
, GLenum internalFormat
,
2115 GLsizei width
, GLsizei height
, GLsizei samples
,
2118 GET_CURRENT_CONTEXT(ctx
);
2120 if (MESA_VERBOSE
& VERBOSE_API
) {
2121 if (samples
== NO_SAMPLES
)
2122 _mesa_debug(ctx
, "%s(%u, %s, %d, %d)\n",
2124 _mesa_enum_to_string(internalFormat
),
2127 _mesa_debug(ctx
, "%s(%u, %s, %d, %d, %d)\n",
2129 _mesa_enum_to_string(internalFormat
),
2130 width
, height
, samples
);
2133 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2134 if (!rb
|| rb
== &DummyRenderbuffer
) {
2135 /* ID was reserved, but no real renderbuffer object made yet */
2136 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid renderbuffer %u)",
2137 func
, renderbuffer
);
2141 renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
, func
);
2145 * Helper function used by _mesa_RenderbufferStorage() and
2146 * _mesa_RenderbufferStorageMultisample().
2147 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2150 renderbuffer_storage_target(GLenum target
, GLenum internalFormat
,
2151 GLsizei width
, GLsizei height
, GLsizei samples
,
2154 GET_CURRENT_CONTEXT(ctx
);
2156 if (MESA_VERBOSE
& VERBOSE_API
) {
2157 if (samples
== NO_SAMPLES
)
2158 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
2160 _mesa_enum_to_string(target
),
2161 _mesa_enum_to_string(internalFormat
),
2164 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
2166 _mesa_enum_to_string(target
),
2167 _mesa_enum_to_string(internalFormat
),
2168 width
, height
, samples
);
2171 if (target
!= GL_RENDERBUFFER_EXT
) {
2172 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
2176 if (!ctx
->CurrentRenderbuffer
) {
2177 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no renderbuffer bound)",
2182 renderbuffer_storage(ctx
, ctx
->CurrentRenderbuffer
, internalFormat
, width
,
2183 height
, samples
, func
);
2188 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
2190 struct gl_renderbuffer
*rb
;
2191 GET_CURRENT_CONTEXT(ctx
);
2193 if (!ctx
->Extensions
.OES_EGL_image
) {
2194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2195 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2199 if (target
!= GL_RENDERBUFFER
) {
2200 _mesa_error(ctx
, GL_INVALID_ENUM
,
2201 "EGLImageTargetRenderbufferStorageOES");
2205 rb
= ctx
->CurrentRenderbuffer
;
2207 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2208 "EGLImageTargetRenderbufferStorageOES");
2212 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2214 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
2219 * Helper function for _mesa_GetRenderbufferParameteriv() and
2220 * _mesa_GetFramebufferAttachmentParameteriv()
2221 * We have to be careful to respect the base format. For example, if a
2222 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2223 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2224 * we need to return zero.
2227 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
2229 if (_mesa_base_format_has_channel(baseFormat
, pname
))
2230 return _mesa_get_format_bits(format
, pname
);
2238 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
2239 GLsizei width
, GLsizei height
)
2241 /* GL_ARB_fbo says calling this function is equivalent to calling
2242 * glRenderbufferStorageMultisample() with samples=0. We pass in
2243 * a token value here just for error reporting purposes.
2245 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2246 NO_SAMPLES
, "glRenderbufferStorage");
2251 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
2252 GLenum internalFormat
,
2253 GLsizei width
, GLsizei height
)
2255 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2256 samples
, "glRenderbufferStorageMultisample");
2261 * OpenGL ES version of glRenderBufferStorage.
2264 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
2265 GLsizei width
, GLsizei height
)
2267 switch (internalFormat
) {
2269 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2270 /* choose a closest format */
2271 internalFormat
= GL_RGB5
;
2277 renderbuffer_storage_target(target
, internalFormat
, width
, height
, 0,
2278 "glRenderbufferStorageEXT");
2282 _mesa_NamedRenderbufferStorage(GLuint renderbuffer
, GLenum internalformat
,
2283 GLsizei width
, GLsizei height
)
2285 /* GL_ARB_fbo says calling this function is equivalent to calling
2286 * glRenderbufferStorageMultisample() with samples=0. We pass in
2287 * a token value here just for error reporting purposes.
2289 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2290 NO_SAMPLES
, "glNamedRenderbufferStorage");
2294 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer
, GLsizei samples
,
2295 GLenum internalformat
,
2296 GLsizei width
, GLsizei height
)
2298 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2300 "glNamedRenderbufferStorageMultisample");
2305 get_render_buffer_parameteriv(struct gl_context
*ctx
,
2306 struct gl_renderbuffer
*rb
, GLenum pname
,
2307 GLint
*params
, const char *func
)
2309 /* No need to flush here since we're just quering state which is
2310 * not effected by rendering.
2314 case GL_RENDERBUFFER_WIDTH_EXT
:
2315 *params
= rb
->Width
;
2317 case GL_RENDERBUFFER_HEIGHT_EXT
:
2318 *params
= rb
->Height
;
2320 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
2321 *params
= rb
->InternalFormat
;
2323 case GL_RENDERBUFFER_RED_SIZE_EXT
:
2324 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
2325 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
2326 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
2327 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
2328 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
2329 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2331 case GL_RENDERBUFFER_SAMPLES
:
2332 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2333 || _mesa_is_gles3(ctx
)) {
2334 *params
= rb
->NumSamples
;
2339 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname=%s)", func
,
2340 _mesa_enum_to_string(pname
));
2347 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2349 GET_CURRENT_CONTEXT(ctx
);
2351 if (target
!= GL_RENDERBUFFER_EXT
) {
2352 _mesa_error(ctx
, GL_INVALID_ENUM
,
2353 "glGetRenderbufferParameterivEXT(target)");
2357 if (!ctx
->CurrentRenderbuffer
) {
2358 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetRenderbufferParameterivEXT"
2359 "(no renderbuffer bound)");
2363 get_render_buffer_parameteriv(ctx
, ctx
->CurrentRenderbuffer
, pname
,
2364 params
, "glGetRenderbufferParameteriv");
2369 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer
, GLenum pname
,
2372 GET_CURRENT_CONTEXT(ctx
);
2374 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2375 if (!rb
|| rb
== &DummyRenderbuffer
) {
2376 /* ID was reserved, but no real renderbuffer object made yet */
2377 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetNamedRenderbufferParameteriv"
2378 "(invalid renderbuffer %i)", renderbuffer
);
2382 get_render_buffer_parameteriv(ctx
, rb
, pname
, params
,
2383 "glGetNamedRenderbufferParameteriv");
2387 GLboolean GLAPIENTRY
2388 _mesa_IsFramebuffer(GLuint framebuffer
)
2390 GET_CURRENT_CONTEXT(ctx
);
2391 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2393 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2394 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2402 * Check if any of the attachments of the given framebuffer are textures
2403 * (render to texture). Call ctx->Driver.RenderTexture() for such
2407 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2410 assert(ctx
->Driver
.RenderTexture
);
2412 if (_mesa_is_winsys_fbo(fb
))
2413 return; /* can't render to texture with winsys framebuffers */
2415 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2416 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2417 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2418 && driver_RenderTexture_is_safe(att
)) {
2419 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2426 * Examine all the framebuffer's attachments to see if any are textures.
2427 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2428 * notify the device driver that the texture image may have changed.
2431 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2433 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2434 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2437 if (ctx
->Driver
.FinishRenderTexture
) {
2439 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2440 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2441 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2442 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2443 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2451 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2453 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2454 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
2455 GLboolean bindReadBuf
, bindDrawBuf
;
2456 GET_CURRENT_CONTEXT(ctx
);
2459 case GL_DRAW_FRAMEBUFFER_EXT
:
2460 bindDrawBuf
= GL_TRUE
;
2461 bindReadBuf
= GL_FALSE
;
2463 case GL_READ_FRAMEBUFFER_EXT
:
2464 bindDrawBuf
= GL_FALSE
;
2465 bindReadBuf
= GL_TRUE
;
2467 case GL_FRAMEBUFFER_EXT
:
2468 bindDrawBuf
= GL_TRUE
;
2469 bindReadBuf
= GL_TRUE
;
2472 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2477 /* Binding a user-created framebuffer object */
2478 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2479 if (newDrawFb
== &DummyFramebuffer
) {
2480 /* ID was reserved, but no real framebuffer object made yet */
2483 else if (!newDrawFb
&& !allow_user_names
) {
2484 /* All FBO IDs must be Gen'd */
2485 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2490 /* create new framebuffer object */
2491 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2493 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2496 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2498 newReadFb
= newDrawFb
;
2501 /* Binding the window system framebuffer (which was originally set
2502 * with MakeCurrent).
2504 newDrawFb
= ctx
->WinSysDrawBuffer
;
2505 newReadFb
= ctx
->WinSysReadBuffer
;
2509 assert(newDrawFb
!= &DummyFramebuffer
);
2511 /* save pointers to current/old framebuffers */
2512 oldDrawFb
= ctx
->DrawBuffer
;
2513 oldReadFb
= ctx
->ReadBuffer
;
2515 /* check if really changing bindings */
2516 if (oldDrawFb
== newDrawFb
)
2517 bindDrawBuf
= GL_FALSE
;
2518 if (oldReadFb
== newReadFb
)
2519 bindReadBuf
= GL_FALSE
;
2522 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2524 * We also check if we're beginning and/or ending render-to-texture.
2525 * When a framebuffer with texture attachments is unbound, call
2526 * ctx->Driver.FinishRenderTexture().
2527 * When a framebuffer with texture attachments is bound, call
2528 * ctx->Driver.RenderTexture().
2530 * Note that if the ReadBuffer has texture attachments we don't consider
2531 * that a render-to-texture case.
2534 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2536 /* check if old readbuffer was render-to-texture */
2537 check_end_texture_render(ctx
, oldReadFb
);
2539 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2543 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2545 /* check if old framebuffer had any texture attachments */
2547 check_end_texture_render(ctx
, oldDrawFb
);
2549 /* check if newly bound framebuffer has any texture attachments */
2550 check_begin_texture_render(ctx
, newDrawFb
);
2552 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2555 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2556 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
2561 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2563 GET_CURRENT_CONTEXT(ctx
);
2565 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2566 * point, but they allow the use of user-generated names.
2568 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2573 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2575 /* This function should not be in the dispatch table for core profile /
2576 * OpenGL 3.1, so execution should never get here in those cases -- no
2577 * need for an explicit test.
2579 bind_framebuffer(target
, framebuffer
, true);
2584 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2587 GET_CURRENT_CONTEXT(ctx
);
2590 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2594 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2596 for (i
= 0; i
< n
; i
++) {
2597 if (framebuffers
[i
] > 0) {
2598 struct gl_framebuffer
*fb
;
2599 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2601 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2603 /* check if deleting currently bound framebuffer object */
2604 if (fb
== ctx
->DrawBuffer
) {
2606 assert(fb
->RefCount
>= 2);
2607 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2609 if (fb
== ctx
->ReadBuffer
) {
2611 assert(fb
->RefCount
>= 2);
2612 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2615 /* remove from hash table immediately, to free the ID */
2616 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2618 if (fb
!= &DummyFramebuffer
) {
2619 /* But the object will not be freed until it's no longer
2620 * bound in any context.
2622 _mesa_reference_framebuffer(&fb
, NULL
);
2631 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2632 * It is not exposed to the rest of Mesa to encourage the use of
2633 * nameless buffers in driver internals.
2636 create_framebuffers(GLsizei n
, GLuint
*framebuffers
, bool dsa
)
2638 GET_CURRENT_CONTEXT(ctx
);
2641 struct gl_framebuffer
*fb
;
2643 const char *func
= dsa
? "glCreateFramebuffers" : "glGenFramebuffers";
2646 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
2653 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2655 for (i
= 0; i
< n
; i
++) {
2656 GLuint name
= first
+ i
;
2657 framebuffers
[i
] = name
;
2660 fb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffers
[i
]);
2662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2667 fb
= &DummyFramebuffer
;
2669 mtx_lock(&ctx
->Shared
->Mutex
);
2670 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, fb
);
2671 mtx_unlock(&ctx
->Shared
->Mutex
);
2677 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2679 create_framebuffers(n
, framebuffers
, false);
2684 _mesa_CreateFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2686 create_framebuffers(n
, framebuffers
, true);
2691 _mesa_check_framebuffer_status(struct gl_context
*ctx
,
2692 struct gl_framebuffer
*buffer
)
2694 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2696 if (_mesa_is_winsys_fbo(buffer
)) {
2697 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2698 if (buffer
!= &IncompleteFramebuffer
) {
2699 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2701 return GL_FRAMEBUFFER_UNDEFINED
;
2705 /* No need to flush here */
2707 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2708 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2711 return buffer
->_Status
;
2716 _mesa_CheckFramebufferStatus(GLenum target
)
2718 struct gl_framebuffer
*fb
;
2719 GET_CURRENT_CONTEXT(ctx
);
2721 if (MESA_VERBOSE
& VERBOSE_API
)
2722 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2723 _mesa_enum_to_string(target
));
2725 fb
= get_framebuffer_target(ctx
, target
);
2727 _mesa_error(ctx
, GL_INVALID_ENUM
,
2728 "glCheckFramebufferStatus(invalid target %s)",
2729 _mesa_enum_to_string(target
));
2733 return _mesa_check_framebuffer_status(ctx
, fb
);
2738 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer
, GLenum target
)
2740 struct gl_framebuffer
*fb
;
2741 GET_CURRENT_CONTEXT(ctx
);
2743 /* Validate the target (for conformance's sake) and grab a reference to the
2744 * default framebuffer in case framebuffer = 0.
2745 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2746 * (30.10.2014, PDF page 336) says:
2747 * "If framebuffer is zero, then the status of the default read or
2748 * draw framebuffer (as determined by target) is returned."
2751 case GL_DRAW_FRAMEBUFFER
:
2752 case GL_FRAMEBUFFER
:
2753 fb
= ctx
->WinSysDrawBuffer
;
2755 case GL_READ_FRAMEBUFFER
:
2756 fb
= ctx
->WinSysReadBuffer
;
2759 _mesa_error(ctx
, GL_INVALID_ENUM
,
2760 "glCheckNamedFramebufferStatus(invalid target %s)",
2761 _mesa_enum_to_string(target
));
2766 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
2767 "glCheckNamedFramebufferStatus");
2772 return _mesa_check_framebuffer_status(ctx
, fb
);
2777 * Replicate the src attachment point. Used by framebuffer_texture() when
2778 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2779 * GL_STENCIL_ATTACHMENT.
2782 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2783 gl_buffer_index dst
,
2784 gl_buffer_index src
)
2786 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2787 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2789 assert(src_att
->Texture
!= NULL
);
2790 assert(src_att
->Renderbuffer
!= NULL
);
2792 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2793 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2794 dst_att
->Type
= src_att
->Type
;
2795 dst_att
->Complete
= src_att
->Complete
;
2796 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2797 dst_att
->Zoffset
= src_att
->Zoffset
;
2802 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2803 * texture object pointer.
2805 * \param texObj where the pointer to the texture object is returned. Note
2806 * that a successful call may return texObj = NULL.
2808 * \return true if no errors, false if errors
2811 get_texture_for_framebuffer(struct gl_context
*ctx
, GLuint texture
,
2812 bool layered
, const char *caller
,
2813 struct gl_texture_object
**texObj
)
2815 *texObj
= NULL
; /* This will get returned if texture = 0. */
2820 *texObj
= _mesa_lookup_texture(ctx
, texture
);
2821 if (*texObj
== NULL
|| (*texObj
)->Target
== 0) {
2822 /* Can't render to a non-existent texture object.
2824 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2825 * Managing Framebuffer Objects specifies a different error
2826 * depending upon the calling function (PDF pages 325-328).
2827 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
2828 * value, while the other commands throw invalid operation (where
2829 * layered = GL_FALSE).
2831 const GLenum error
= layered
? GL_INVALID_VALUE
:
2832 GL_INVALID_OPERATION
;
2833 _mesa_error(ctx
, error
,
2834 "%s(non-existent texture %u)", caller
, texture
);
2843 * Common code called by gl*FramebufferTexture() to verify the texture target
2844 * and decide whether or not the attachment should truly be considered
2847 * \param layered true if attachment should be considered layered, false if
2850 * \return true if no errors, false if errors
2853 check_layered_texture_target(struct gl_context
*ctx
, GLenum target
,
2854 const char *caller
, GLboolean
*layered
)
2860 case GL_TEXTURE_1D_ARRAY_EXT
:
2861 case GL_TEXTURE_2D_ARRAY_EXT
:
2862 case GL_TEXTURE_CUBE_MAP
:
2863 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2864 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2868 case GL_TEXTURE_RECTANGLE
:
2869 case GL_TEXTURE_2D_MULTISAMPLE
:
2870 /* These texture types are valid to pass to
2871 * glFramebufferTexture(), but since they aren't layered, it
2872 * is equivalent to calling glFramebufferTexture{1D,2D}().
2874 *layered
= GL_FALSE
;
2878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2879 "%s(invalid texture target %s)", caller
,
2880 _mesa_enum_to_string(target
));
2886 * Common code called by gl*FramebufferTextureLayer() to verify the texture
2889 * \return true if no errors, false if errors
2892 check_texture_target(struct gl_context
*ctx
, GLenum target
,
2895 /* We're being called by glFramebufferTextureLayer().
2896 * The only legal texture types for that function are 3D,
2897 * cube-map, and 1D/2D/cube-map array textures.
2899 * We don't need to check for GL_ARB_texture_cube_map_array because the
2900 * application wouldn't have been able to create a texture with a
2901 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
2905 case GL_TEXTURE_1D_ARRAY
:
2906 case GL_TEXTURE_2D_ARRAY
:
2907 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2908 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2910 case GL_TEXTURE_CUBE_MAP
:
2911 /* We don't need to check the extension (GL_ARB_direct_state_access) or
2912 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
2913 * enabled in core profile. This can be called from
2914 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
2915 * so we do have to check the profile.
2917 return ctx
->API
== API_OPENGL_CORE
;
2920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2921 "%s(invalid texture target %s)", caller
,
2922 _mesa_enum_to_string(target
));
2928 * Common code called by glFramebufferTexture*D() to verify the texture
2931 * \return true if no errors, false if errors
2934 check_textarget(struct gl_context
*ctx
, int dims
, GLenum target
,
2935 GLenum textarget
, const char *caller
)
2941 switch (textarget
) {
2944 case GL_TEXTURE_1D_ARRAY
:
2945 err
= !ctx
->Extensions
.EXT_texture_array
;
2952 switch (textarget
) {
2955 case GL_TEXTURE_RECTANGLE
:
2956 err
= _mesa_is_gles(ctx
)
2957 || !ctx
->Extensions
.NV_texture_rectangle
;
2959 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2960 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2961 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2962 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2963 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2964 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2965 err
= !ctx
->Extensions
.ARB_texture_cube_map
;
2967 case GL_TEXTURE_2D_ARRAY
:
2968 err
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2969 || !ctx
->Extensions
.EXT_texture_array
;
2971 case GL_TEXTURE_2D_MULTISAMPLE
:
2972 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2973 err
= (_mesa_is_gles(ctx
) ||
2974 !ctx
->Extensions
.ARB_texture_multisample
) &&
2975 !_mesa_is_gles31(ctx
);
2982 if (textarget
!= GL_TEXTURE_3D
)
2990 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2991 "%s(invalid textarget %s)",
2992 caller
, _mesa_enum_to_string(textarget
));
2996 /* Make sure textarget is consistent with the texture's type */
2997 err
= (target
== GL_TEXTURE_CUBE_MAP
) ?
2998 !_mesa_is_cube_face(textarget
): (target
!= textarget
);
3001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3002 "%s(mismatched texture target)", caller
);
3011 * Common code called by gl*FramebufferTextureLayer() and
3012 * glFramebufferTexture3D() to validate the layer.
3014 * \return true if no errors, false if errors
3017 check_layer(struct gl_context
*ctx
, GLenum target
, GLint layer
,
3020 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3023 * "An INVALID_VALUE error is generated if texture is non-zero
3024 * and layer is negative."
3027 _mesa_error(ctx
, GL_INVALID_VALUE
,
3028 "%s(layer %u < 0)", caller
, layer
);
3032 if (target
== GL_TEXTURE_3D
) {
3033 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
3034 if (layer
>= maxSize
) {
3035 _mesa_error(ctx
, GL_INVALID_VALUE
,
3036 "%s(invalid layer %u)", caller
, layer
);
3040 else if ((target
== GL_TEXTURE_1D_ARRAY
) ||
3041 (target
== GL_TEXTURE_2D_ARRAY
) ||
3042 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
3043 (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
3044 if (layer
>= ctx
->Const
.MaxArrayTextureLayers
) {
3045 _mesa_error(ctx
, GL_INVALID_VALUE
,
3046 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3051 else if (target
== GL_TEXTURE_CUBE_MAP
) {
3053 _mesa_error(ctx
, GL_INVALID_VALUE
,
3054 "%s(layer %u >= 6)", caller
, layer
);
3064 * Common code called by all gl*FramebufferTexture*() entry points to verify
3067 * \return true if no errors, false if errors
3070 check_level(struct gl_context
*ctx
, GLenum target
, GLint level
,
3074 (level
>= _mesa_max_texture_levels(ctx
, target
))) {
3075 _mesa_error(ctx
, GL_INVALID_VALUE
,
3076 "%s(invalid level %d)", caller
, level
);
3085 _mesa_framebuffer_texture(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
3087 struct gl_texture_object
*texObj
, GLenum textarget
,
3088 GLint level
, GLuint layer
, GLboolean layered
,
3091 struct gl_renderbuffer_attachment
*att
;
3093 /* The window-system framebuffer object is immutable */
3094 if (_mesa_is_winsys_fbo(fb
)) {
3095 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(window-system framebuffer)",
3100 /* Not a hash lookup, so we can afford to get the attachment here. */
3101 att
= get_attachment(ctx
, fb
, attachment
);
3103 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", caller
,
3104 _mesa_enum_to_string(attachment
));
3108 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3110 mtx_lock(&fb
->Mutex
);
3112 if (attachment
== GL_DEPTH_ATTACHMENT
&&
3113 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
3114 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
3115 _mesa_tex_target_to_face(textarget
) ==
3116 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
3117 layer
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
3118 /* The texture object is already attached to the stencil attachment
3119 * point. Don't create a new renderbuffer; just reuse the stencil
3120 * attachment's. This is required to prevent a GL error in
3121 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3123 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
3125 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
3126 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
3127 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
3128 _mesa_tex_target_to_face(textarget
) ==
3129 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
3130 layer
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
3131 /* As above, but with depth and stencil transposed. */
3132 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
3135 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
3136 level
, layer
, layered
);
3138 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3139 /* Above we created a new renderbuffer and attached it to the
3140 * depth attachment point. Now attach it to the stencil attachment
3143 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3144 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
3149 /* Set the render-to-texture flag. We'll check this flag in
3150 * glTexImage() and friends to determine if we need to revalidate
3151 * any FBOs that might be rendering into this texture.
3152 * This flag never gets cleared since it's non-trivial to determine
3153 * when all FBOs might be done rendering to this texture. That's OK
3154 * though since it's uncommon to render to a texture then repeatedly
3155 * call glTexImage() to change images in the texture.
3157 texObj
->_RenderToTexture
= GL_TRUE
;
3160 remove_attachment(ctx
, att
);
3161 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3162 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3163 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
3167 invalidate_framebuffer(fb
);
3169 mtx_unlock(&fb
->Mutex
);
3174 framebuffer_texture_with_dims(int dims
, GLenum target
,
3175 GLenum attachment
, GLenum textarget
,
3176 GLuint texture
, GLint level
, GLint layer
,
3179 GET_CURRENT_CONTEXT(ctx
);
3180 struct gl_framebuffer
*fb
;
3181 struct gl_texture_object
*texObj
;
3183 /* Get the framebuffer object */
3184 fb
= get_framebuffer_target(ctx
, target
);
3186 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
3187 _mesa_enum_to_string(target
));
3191 /* Get the texture object */
3192 if (!get_texture_for_framebuffer(ctx
, texture
, false, caller
, &texObj
))
3196 if (!check_textarget(ctx
, dims
, texObj
->Target
, textarget
, caller
))
3199 if ((dims
== 3) && !check_layer(ctx
, texObj
->Target
, layer
, caller
))
3203 if (!check_level(ctx
, textarget
, level
, caller
))
3206 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, textarget
, level
,
3207 layer
, GL_FALSE
, caller
);
3212 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
3213 GLenum textarget
, GLuint texture
, GLint level
)
3215 framebuffer_texture_with_dims(1, target
, attachment
, textarget
, texture
,
3216 level
, 0, "glFramebufferTexture1D");
3221 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
3222 GLenum textarget
, GLuint texture
, GLint level
)
3224 framebuffer_texture_with_dims(2, target
, attachment
, textarget
, texture
,
3225 level
, 0, "glFramebufferTexture2D");
3230 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
3231 GLenum textarget
, GLuint texture
,
3232 GLint level
, GLint layer
)
3234 framebuffer_texture_with_dims(3, target
, attachment
, textarget
, texture
,
3235 level
, layer
, "glFramebufferTexture3D");
3240 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
3241 GLuint texture
, GLint level
, GLint layer
)
3243 GET_CURRENT_CONTEXT(ctx
);
3244 struct gl_framebuffer
*fb
;
3245 struct gl_texture_object
*texObj
;
3246 GLenum textarget
= 0;
3248 const char *func
= "glFramebufferTextureLayer";
3250 /* Get the framebuffer object */
3251 fb
= get_framebuffer_target(ctx
, target
);
3253 _mesa_error(ctx
, GL_INVALID_ENUM
,
3254 "glFramebufferTextureLayer(invalid target %s)",
3255 _mesa_enum_to_string(target
));
3259 /* Get the texture object */
3260 if (!get_texture_for_framebuffer(ctx
, texture
, false, func
, &texObj
))
3264 if (!check_texture_target(ctx
, texObj
->Target
, func
))
3267 if (!check_layer(ctx
, texObj
->Target
, layer
, func
))
3270 if (!check_level(ctx
, texObj
->Target
, level
, func
))
3273 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3274 assert(layer
>= 0 && layer
< 6);
3275 textarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ layer
;
3280 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, textarget
, level
,
3281 layer
, GL_FALSE
, func
);
3286 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer
, GLenum attachment
,
3287 GLuint texture
, GLint level
, GLint layer
)
3289 GET_CURRENT_CONTEXT(ctx
);
3290 struct gl_framebuffer
*fb
;
3291 struct gl_texture_object
*texObj
;
3292 GLenum textarget
= 0;
3294 const char *func
= "glNamedFramebufferTextureLayer";
3296 /* Get the framebuffer object */
3297 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
, func
);
3301 /* Get the texture object */
3302 if (!get_texture_for_framebuffer(ctx
, texture
, false, func
, &texObj
))
3306 if (!check_texture_target(ctx
, texObj
->Target
, func
))
3309 if (!check_layer(ctx
, texObj
->Target
, layer
, func
))
3312 if (!check_level(ctx
, texObj
->Target
, level
, func
))
3315 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3316 assert(layer
>= 0 && layer
< 6);
3317 textarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ layer
;
3322 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, textarget
, level
,
3323 layer
, GL_FALSE
, func
);
3328 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
3329 GLuint texture
, GLint level
)
3331 GET_CURRENT_CONTEXT(ctx
);
3332 struct gl_framebuffer
*fb
;
3333 struct gl_texture_object
*texObj
;
3334 GLboolean layered
= GL_FALSE
;
3336 const char *func
= "FramebufferTexture";
3338 if (!_mesa_has_geometry_shaders(ctx
)) {
3339 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3340 "unsupported function (glFramebufferTexture) called");
3344 /* Get the framebuffer object */
3345 fb
= get_framebuffer_target(ctx
, target
);
3347 _mesa_error(ctx
, GL_INVALID_ENUM
,
3348 "glFramebufferTexture(invalid target %s)",
3349 _mesa_enum_to_string(target
));
3353 /* Get the texture object */
3354 if (!get_texture_for_framebuffer(ctx
, texture
, true, func
, &texObj
))
3358 if (!check_layered_texture_target(ctx
, texObj
->Target
, func
, &layered
))
3361 if (!check_level(ctx
, texObj
->Target
, level
, func
))
3365 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, 0, level
,
3371 _mesa_NamedFramebufferTexture(GLuint framebuffer
, GLenum attachment
,
3372 GLuint texture
, GLint level
)
3374 GET_CURRENT_CONTEXT(ctx
);
3375 struct gl_framebuffer
*fb
;
3376 struct gl_texture_object
*texObj
;
3377 GLboolean layered
= GL_FALSE
;
3379 const char *func
= "glNamedFramebufferTexture";
3381 if (!_mesa_has_geometry_shaders(ctx
)) {
3382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3383 "unsupported function (glNamedFramebufferTexture) called");
3387 /* Get the framebuffer object */
3388 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
, func
);
3392 /* Get the texture object */
3393 if (!get_texture_for_framebuffer(ctx
, texture
, true, func
, &texObj
))
3397 if (!check_layered_texture_target(ctx
, texObj
->Target
, func
,
3401 if (!check_level(ctx
, texObj
->Target
, level
, func
))
3405 _mesa_framebuffer_texture(ctx
, fb
, attachment
, texObj
, 0, level
,
3411 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
3412 struct gl_framebuffer
*fb
,
3414 struct gl_renderbuffer
*rb
,
3417 struct gl_renderbuffer_attachment
*att
;
3419 if (_mesa_is_winsys_fbo(fb
)) {
3420 /* Can't attach new renderbuffers to a window system framebuffer */
3421 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3422 "%s(window-system framebuffer)", func
);
3426 att
= get_attachment(ctx
, fb
, attachment
);
3428 _mesa_error(ctx
, GL_INVALID_ENUM
,
3429 "%s(invalid attachment %s)", func
,
3430 _mesa_enum_to_string(attachment
));
3434 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
3435 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
3436 /* make sure the renderbuffer is a depth/stencil format */
3437 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
3438 if (baseFormat
!= GL_DEPTH_STENCIL
) {
3439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3440 "%s(renderbuffer is not DEPTH_STENCIL format)", func
);
3445 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3447 assert(ctx
->Driver
.FramebufferRenderbuffer
);
3448 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
3450 /* Some subsequent GL commands may depend on the framebuffer's visual
3451 * after the binding is updated. Update visual info now.
3453 _mesa_update_framebuffer_visual(ctx
, fb
);
3458 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
3459 GLenum renderbuffertarget
,
3460 GLuint renderbuffer
)
3462 struct gl_framebuffer
*fb
;
3463 struct gl_renderbuffer
*rb
;
3464 GET_CURRENT_CONTEXT(ctx
);
3466 fb
= get_framebuffer_target(ctx
, target
);
3468 _mesa_error(ctx
, GL_INVALID_ENUM
,
3469 "glFramebufferRenderbuffer(invalid target %s)",
3470 _mesa_enum_to_string(target
));
3474 if (renderbuffertarget
!= GL_RENDERBUFFER
) {
3475 _mesa_error(ctx
, GL_INVALID_ENUM
,
3476 "glFramebufferRenderbuffer(renderbuffertarget is not "
3477 "GL_RENDERBUFFER)");
3482 rb
= _mesa_lookup_renderbuffer_err(ctx
, renderbuffer
,
3483 "glFramebufferRenderbuffer");
3488 /* remove renderbuffer attachment */
3492 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
,
3493 "glFramebufferRenderbuffer");
3498 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer
, GLenum attachment
,
3499 GLenum renderbuffertarget
,
3500 GLuint renderbuffer
)
3502 struct gl_framebuffer
*fb
;
3503 struct gl_renderbuffer
*rb
;
3504 GET_CURRENT_CONTEXT(ctx
);
3506 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3507 "glNamedFramebufferRenderbuffer");
3511 if (renderbuffertarget
!= GL_RENDERBUFFER
) {
3512 _mesa_error(ctx
, GL_INVALID_ENUM
,
3513 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3514 "GL_RENDERBUFFER)");
3519 rb
= _mesa_lookup_renderbuffer_err(ctx
, renderbuffer
,
3520 "glNamedFramebufferRenderbuffer");
3525 /* remove renderbuffer attachment */
3529 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
,
3530 "glNamedFramebufferRenderbuffer");
3535 _mesa_get_framebuffer_attachment_parameter(struct gl_context
*ctx
,
3536 struct gl_framebuffer
*buffer
,
3537 GLenum attachment
, GLenum pname
,
3538 GLint
*params
, const char *caller
)
3540 const struct gl_renderbuffer_attachment
*att
;
3543 /* The error differs in GL and GLES. */
3544 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
3546 if (_mesa_is_winsys_fbo(buffer
)) {
3547 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3550 * "If the framebuffer currently bound to target is zero, then
3551 * INVALID_OPERATION is generated."
3553 * The EXT_framebuffer_object spec has the same wording, and the
3554 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3557 if ((!_mesa_is_desktop_gl(ctx
) ||
3558 !ctx
->Extensions
.ARB_framebuffer_object
)
3559 && !_mesa_is_gles3(ctx
)) {
3560 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3561 "%s(window-system framebuffer)", caller
);
3565 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
3566 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
3567 _mesa_error(ctx
, GL_INVALID_ENUM
,
3568 "%s(invalid attachment %s)", caller
,
3569 _mesa_enum_to_string(attachment
));
3572 /* the default / window-system FBO */
3573 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
3576 /* user-created framebuffer FBO */
3577 att
= get_attachment(ctx
, buffer
, attachment
);
3581 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", caller
,
3582 _mesa_enum_to_string(attachment
));
3586 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3587 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
3588 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
3589 /* This behavior is first specified in OpenGL 4.4 specification.
3591 * From the OpenGL 4.4 spec page 275:
3592 * "This query cannot be performed for a combined depth+stencil
3593 * attachment, since it does not have a single format."
3595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3596 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3597 " is invalid for depth+stencil attachment)", caller
);
3600 /* the depth and stencil attachments must point to the same buffer */
3601 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
3602 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
3603 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
3604 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3605 "%s(DEPTH/STENCIL attachments differ)", caller
);
3610 /* No need to flush here */
3613 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
3614 *params
= _mesa_is_winsys_fbo(buffer
)
3615 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
3617 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
3618 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
3619 *params
= att
->Renderbuffer
->Name
;
3621 else if (att
->Type
== GL_TEXTURE
) {
3622 *params
= att
->Texture
->Name
;
3625 assert(att
->Type
== GL_NONE
);
3626 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
3629 goto invalid_pname_enum
;
3633 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
3634 if (att
->Type
== GL_TEXTURE
) {
3635 *params
= att
->TextureLevel
;
3637 else if (att
->Type
== GL_NONE
) {
3638 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3639 _mesa_enum_to_string(pname
));
3642 goto invalid_pname_enum
;
3645 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
3646 if (att
->Type
== GL_TEXTURE
) {
3647 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
3648 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
3654 else if (att
->Type
== GL_NONE
) {
3655 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3656 _mesa_enum_to_string(pname
));
3659 goto invalid_pname_enum
;
3662 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
3663 if (ctx
->API
== API_OPENGLES
) {
3664 goto invalid_pname_enum
;
3665 } else if (att
->Type
== GL_NONE
) {
3666 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3667 _mesa_enum_to_string(pname
));
3668 } else if (att
->Type
== GL_TEXTURE
) {
3669 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
3670 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
3671 *params
= att
->Zoffset
;
3678 goto invalid_pname_enum
;
3681 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
3682 if ((!_mesa_is_desktop_gl(ctx
) ||
3683 !ctx
->Extensions
.ARB_framebuffer_object
)
3684 && !_mesa_is_gles3(ctx
)) {
3685 goto invalid_pname_enum
;
3687 else if (att
->Type
== GL_NONE
) {
3688 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3689 _mesa_enum_to_string(pname
));
3692 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3694 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
3697 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3698 * if the sRGB conversion is unsupported. */
3699 *params
= GL_LINEAR
;
3703 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
3704 if ((ctx
->API
!= API_OPENGL_COMPAT
||
3705 !ctx
->Extensions
.ARB_framebuffer_object
)
3706 && ctx
->API
!= API_OPENGL_CORE
3707 && !_mesa_is_gles3(ctx
)) {
3708 goto invalid_pname_enum
;
3710 else if (att
->Type
== GL_NONE
) {
3711 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3712 _mesa_enum_to_string(pname
));
3715 mesa_format format
= att
->Renderbuffer
->Format
;
3717 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3720 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3721 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3722 * generate an INVALID_OPERATION error.
3724 if (_mesa_is_gles3(ctx
) &&
3725 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3726 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3728 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3729 "GL_DEPTH_STENCIL_ATTACHMENT)", caller
);
3733 if (format
== MESA_FORMAT_S_UINT8
) {
3737 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
3738 /* depends on the attachment parameter */
3739 if (attachment
== GL_STENCIL_ATTACHMENT
) {
3747 *params
= _mesa_get_format_datatype(format
);
3751 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
3752 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
3753 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
3754 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
3755 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
3756 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
3757 if ((!_mesa_is_desktop_gl(ctx
) ||
3758 !ctx
->Extensions
.ARB_framebuffer_object
)
3759 && !_mesa_is_gles3(ctx
)) {
3760 goto invalid_pname_enum
;
3762 else if (att
->Type
== GL_NONE
) {
3763 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3764 _mesa_enum_to_string(pname
));
3766 else if (att
->Texture
) {
3767 const struct gl_texture_image
*texImage
=
3768 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
3771 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
3772 texImage
->TexFormat
);
3778 else if (att
->Renderbuffer
) {
3779 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
3780 att
->Renderbuffer
->Format
);
3783 _mesa_problem(ctx
, "%s: invalid FBO attachment structure", caller
);
3786 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
3787 if (!_mesa_has_geometry_shaders(ctx
)) {
3788 goto invalid_pname_enum
;
3789 } else if (att
->Type
== GL_TEXTURE
) {
3790 *params
= att
->Layered
;
3791 } else if (att
->Type
== GL_NONE
) {
3792 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
3793 _mesa_enum_to_string(pname
));
3795 goto invalid_pname_enum
;
3799 goto invalid_pname_enum
;
3805 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname %s)", caller
,
3806 _mesa_enum_to_string(pname
));
3812 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
3813 GLenum pname
, GLint
*params
)
3815 GET_CURRENT_CONTEXT(ctx
);
3816 struct gl_framebuffer
*buffer
;
3818 buffer
= get_framebuffer_target(ctx
, target
);
3820 _mesa_error(ctx
, GL_INVALID_ENUM
,
3821 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
3822 _mesa_enum_to_string(target
));
3826 _mesa_get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
3828 "glGetFramebufferAttachmentParameteriv");
3833 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer
,
3835 GLenum pname
, GLint
*params
)
3837 GET_CURRENT_CONTEXT(ctx
);
3838 struct gl_framebuffer
*buffer
;
3841 buffer
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3842 "glGetNamedFramebufferAttachmentParameteriv");
3848 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
3849 * 4.5 core spec (30.10.2014, PDF page 314):
3850 * "If framebuffer is zero, then the default draw framebuffer is
3853 buffer
= ctx
->WinSysDrawBuffer
;
3856 _mesa_get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
3858 "glGetNamedFramebufferAttachmentParameteriv");
3863 _mesa_NamedFramebufferParameteri(GLuint framebuffer
, GLenum pname
,
3866 GET_CURRENT_CONTEXT(ctx
);
3867 struct gl_framebuffer
*fb
= NULL
;
3869 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
3870 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3871 "glNamedFramebufferParameteri("
3872 "ARB_framebuffer_no_attachments not implemented)");
3876 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3877 "glNamedFramebufferParameteri");
3880 framebuffer_parameteri(ctx
, fb
, pname
, param
,
3881 "glNamedFramebufferParameteriv");
3887 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer
, GLenum pname
,
3890 GET_CURRENT_CONTEXT(ctx
);
3891 struct gl_framebuffer
*fb
;
3893 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
3894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3895 "glNamedFramebufferParameteriv("
3896 "ARB_framebuffer_no_attachments not implemented)");
3901 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3902 "glGetNamedFramebufferParameteriv");
3904 fb
= ctx
->WinSysDrawBuffer
;
3908 get_framebuffer_parameteriv(ctx
, fb
, pname
, param
,
3909 "glGetNamedFramebufferParameteriv");
3915 invalidate_framebuffer_storage(struct gl_context
*ctx
,
3916 struct gl_framebuffer
*fb
,
3917 GLsizei numAttachments
,
3918 const GLenum
*attachments
, GLint x
, GLint y
,
3919 GLsizei width
, GLsizei height
, const char *name
)
3923 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
3924 * Spec (2.2.2015, PDF page 522) says:
3925 * "An INVALID_VALUE error is generated if numAttachments, width, or
3926 * height is negative."
3928 if (numAttachments
< 0) {
3929 _mesa_error(ctx
, GL_INVALID_VALUE
,
3930 "%s(numAttachments < 0)", name
);
3935 _mesa_error(ctx
, GL_INVALID_VALUE
,
3936 "%s(width < 0)", name
);
3941 _mesa_error(ctx
, GL_INVALID_VALUE
,
3942 "%s(height < 0)", name
);
3946 /* The GL_ARB_invalidate_subdata spec says:
3948 * "If an attachment is specified that does not exist in the
3949 * framebuffer bound to <target>, it is ignored."
3953 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3954 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3955 * INVALID_OPERATION is generated."
3957 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3958 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3959 * set of retrictions).
3961 for (i
= 0; i
< numAttachments
; i
++) {
3962 if (_mesa_is_winsys_fbo(fb
)) {
3963 switch (attachments
[i
]) {
3969 /* Accumulation buffers and auxilary buffers were removed in
3970 * OpenGL 3.1, and they never existed in OpenGL ES.
3972 if (ctx
->API
!= API_OPENGL_COMPAT
)
3982 case GL_FRONT_RIGHT
:
3983 if (!_mesa_is_desktop_gl(ctx
))
3990 switch (attachments
[i
]) {
3991 case GL_DEPTH_ATTACHMENT
:
3992 case GL_STENCIL_ATTACHMENT
:
3994 case GL_DEPTH_STENCIL_ATTACHMENT
:
3995 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3996 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3997 * extension does not make this attachment point valid on ES 2.0.
3999 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
4002 case GL_COLOR_ATTACHMENT0
:
4003 case GL_COLOR_ATTACHMENT1
:
4004 case GL_COLOR_ATTACHMENT2
:
4005 case GL_COLOR_ATTACHMENT3
:
4006 case GL_COLOR_ATTACHMENT4
:
4007 case GL_COLOR_ATTACHMENT5
:
4008 case GL_COLOR_ATTACHMENT6
:
4009 case GL_COLOR_ATTACHMENT7
:
4010 case GL_COLOR_ATTACHMENT8
:
4011 case GL_COLOR_ATTACHMENT9
:
4012 case GL_COLOR_ATTACHMENT10
:
4013 case GL_COLOR_ATTACHMENT11
:
4014 case GL_COLOR_ATTACHMENT12
:
4015 case GL_COLOR_ATTACHMENT13
:
4016 case GL_COLOR_ATTACHMENT14
:
4017 case GL_COLOR_ATTACHMENT15
: {
4018 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
4019 if (k
>= ctx
->Const
.MaxColorAttachments
) {
4020 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4021 "%s(attachment >= max. color attachments)", name
);
4032 /* We don't actually do anything for this yet. Just return after
4033 * validating the parameters and generating the required errors.
4038 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", name
,
4039 _mesa_enum_to_string(attachments
[i
]));
4045 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
4046 const GLenum
*attachments
, GLint x
, GLint y
,
4047 GLsizei width
, GLsizei height
)
4049 struct gl_framebuffer
*fb
;
4050 GET_CURRENT_CONTEXT(ctx
);
4052 fb
= get_framebuffer_target(ctx
, target
);
4054 _mesa_error(ctx
, GL_INVALID_ENUM
,
4055 "glInvalidateSubFramebuffer(invalid target %s)",
4056 _mesa_enum_to_string(target
));
4060 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4061 x
, y
, width
, height
,
4062 "glInvalidateSubFramebuffer");
4067 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer
,
4068 GLsizei numAttachments
,
4069 const GLenum
*attachments
,
4071 GLsizei width
, GLsizei height
)
4073 struct gl_framebuffer
*fb
;
4074 GET_CURRENT_CONTEXT(ctx
);
4076 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4077 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4078 * default draw framebuffer is affected."
4081 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4082 "glInvalidateNamedFramebufferSubData");
4087 fb
= ctx
->WinSysDrawBuffer
;
4089 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4090 x
, y
, width
, height
,
4091 "glInvalidateNamedFramebufferSubData");
4096 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
4097 const GLenum
*attachments
)
4099 struct gl_framebuffer
*fb
;
4100 GET_CURRENT_CONTEXT(ctx
);
4102 fb
= get_framebuffer_target(ctx
, target
);
4104 _mesa_error(ctx
, GL_INVALID_ENUM
,
4105 "glInvalidateFramebuffer(invalid target %s)",
4106 _mesa_enum_to_string(target
));
4110 /* The GL_ARB_invalidate_subdata spec says:
4114 * void InvalidateFramebuffer(enum target,
4115 * sizei numAttachments,
4116 * const enum *attachments);
4118 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4119 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4120 * <MAX_VIEWPORT_DIMS[1]> respectively."
4122 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4124 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
4125 "glInvalidateFramebuffer");
4130 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer
,
4131 GLsizei numAttachments
,
4132 const GLenum
*attachments
)
4134 struct gl_framebuffer
*fb
;
4135 GET_CURRENT_CONTEXT(ctx
);
4137 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4138 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4139 * default draw framebuffer is affected."
4142 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4143 "glInvalidateNamedFramebufferData");
4148 fb
= ctx
->WinSysDrawBuffer
;
4150 /* The GL_ARB_invalidate_subdata spec says:
4154 * void InvalidateFramebuffer(enum target,
4155 * sizei numAttachments,
4156 * const enum *attachments);
4158 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4159 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4160 * <MAX_VIEWPORT_DIMS[1]> respectively."
4162 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4164 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
4165 "glInvalidateNamedFramebufferData");
4170 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
4171 const GLenum
*attachments
)
4173 struct gl_framebuffer
*fb
;
4176 GET_CURRENT_CONTEXT(ctx
);
4178 fb
= get_framebuffer_target(ctx
, target
);
4180 _mesa_error(ctx
, GL_INVALID_ENUM
,
4181 "glDiscardFramebufferEXT(target %s)",
4182 _mesa_enum_to_string(target
));
4186 if (numAttachments
< 0) {
4187 _mesa_error(ctx
, GL_INVALID_VALUE
,
4188 "glDiscardFramebufferEXT(numAttachments < 0)");
4192 for (i
= 0; i
< numAttachments
; i
++) {
4193 switch (attachments
[i
]) {
4197 if (_mesa_is_user_fbo(fb
))
4200 case GL_COLOR_ATTACHMENT0
:
4201 case GL_DEPTH_ATTACHMENT
:
4202 case GL_STENCIL_ATTACHMENT
:
4203 if (_mesa_is_winsys_fbo(fb
))
4211 if (ctx
->Driver
.DiscardFramebuffer
)
4212 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
4217 _mesa_error(ctx
, GL_INVALID_ENUM
,
4218 "glDiscardFramebufferEXT(attachment %s)",
4219 _mesa_enum_to_string(attachments
[i
]));