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
38 #include "debug_output.h"
42 #include "framebuffer.h"
43 #include "glformats.h"
46 #include "multisample.h"
48 #include "renderbuffer.h"
57 * None of the GL_EXT_framebuffer_object functions are compiled into
64 * When glGenRender/FramebuffersEXT() is called we insert pointers to
65 * these placeholder objects into the hash table.
66 * Later, when the object ID is first bound, we replace the placeholder
67 * with the real frame/renderbuffer.
69 static struct gl_framebuffer DummyFramebuffer
;
70 static struct gl_renderbuffer DummyRenderbuffer
;
72 /* We bind this framebuffer when applications pass a NULL
73 * drawable/surface in make current. */
74 static struct gl_framebuffer IncompleteFramebuffer
;
78 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
84 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
91 _mesa_init_fbobjects(struct gl_context
*ctx
)
93 simple_mtx_init(&DummyFramebuffer
.Mutex
, mtx_plain
);
94 simple_mtx_init(&DummyRenderbuffer
.Mutex
, mtx_plain
);
95 simple_mtx_init(&IncompleteFramebuffer
.Mutex
, mtx_plain
);
96 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
97 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
98 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
101 struct gl_framebuffer
*
102 _mesa_get_incomplete_framebuffer(void)
104 return &IncompleteFramebuffer
;
108 * Helper routine for getting a gl_renderbuffer.
110 struct gl_renderbuffer
*
111 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
113 struct gl_renderbuffer
*rb
;
118 rb
= (struct gl_renderbuffer
*)
119 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
125 * A convenience function for direct state access that throws
126 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
128 struct gl_renderbuffer
*
129 _mesa_lookup_renderbuffer_err(struct gl_context
*ctx
, GLuint id
,
132 struct gl_renderbuffer
*rb
;
134 rb
= _mesa_lookup_renderbuffer(ctx
, id
);
135 if (!rb
|| rb
== &DummyRenderbuffer
) {
136 _mesa_error(ctx
, GL_INVALID_OPERATION
,
137 "%s(non-existent renderbuffer %u)", func
, id
);
146 * Helper routine for getting a gl_framebuffer.
148 struct gl_framebuffer
*
149 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
151 struct gl_framebuffer
*fb
;
156 fb
= (struct gl_framebuffer
*)
157 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
163 * A convenience function for direct state access that throws
164 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
166 struct gl_framebuffer
*
167 _mesa_lookup_framebuffer_err(struct gl_context
*ctx
, GLuint id
,
170 struct gl_framebuffer
*fb
;
172 fb
= _mesa_lookup_framebuffer(ctx
, id
);
173 if (!fb
|| fb
== &DummyFramebuffer
) {
174 _mesa_error(ctx
, GL_INVALID_OPERATION
,
175 "%s(non-existent framebuffer %u)", func
, id
);
184 * Mark the given framebuffer as invalid. This will force the
185 * test for framebuffer completeness to be done before the framebuffer
189 invalidate_framebuffer(struct gl_framebuffer
*fb
)
191 fb
->_Status
= 0; /* "indeterminate" */
196 * Return the gl_framebuffer object which corresponds to the given
197 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
198 * Check support for GL_EXT_framebuffer_blit to determine if certain
200 * \return gl_framebuffer pointer or NULL if target is illegal
202 static struct gl_framebuffer
*
203 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
205 bool have_fb_blit
= _mesa_is_gles3(ctx
) || _mesa_is_desktop_gl(ctx
);
207 case GL_DRAW_FRAMEBUFFER
:
208 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
209 case GL_READ_FRAMEBUFFER
:
210 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
211 case GL_FRAMEBUFFER_EXT
:
212 return ctx
->DrawBuffer
;
220 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
221 * gl_renderbuffer_attachment object.
222 * This function is only used for user-created FB objects, not the
223 * default / window-system FB object.
224 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
225 * the depth buffer attachment point.
226 * Returns if the attachment is a GL_COLOR_ATTACHMENTm_EXT on
227 * is_color_attachment, because several callers would return different errors
228 * if they don't find the attachment.
230 static struct gl_renderbuffer_attachment
*
231 get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
232 GLenum attachment
, bool *is_color_attachment
)
236 assert(_mesa_is_user_fbo(fb
));
238 if (is_color_attachment
)
239 *is_color_attachment
= false;
241 switch (attachment
) {
242 case GL_COLOR_ATTACHMENT0_EXT
:
243 case GL_COLOR_ATTACHMENT1_EXT
:
244 case GL_COLOR_ATTACHMENT2_EXT
:
245 case GL_COLOR_ATTACHMENT3_EXT
:
246 case GL_COLOR_ATTACHMENT4_EXT
:
247 case GL_COLOR_ATTACHMENT5_EXT
:
248 case GL_COLOR_ATTACHMENT6_EXT
:
249 case GL_COLOR_ATTACHMENT7_EXT
:
250 case GL_COLOR_ATTACHMENT8_EXT
:
251 case GL_COLOR_ATTACHMENT9_EXT
:
252 case GL_COLOR_ATTACHMENT10_EXT
:
253 case GL_COLOR_ATTACHMENT11_EXT
:
254 case GL_COLOR_ATTACHMENT12_EXT
:
255 case GL_COLOR_ATTACHMENT13_EXT
:
256 case GL_COLOR_ATTACHMENT14_EXT
:
257 case GL_COLOR_ATTACHMENT15_EXT
:
258 if (is_color_attachment
)
259 *is_color_attachment
= true;
260 /* Only OpenGL ES 1.x forbids color attachments other than
261 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
264 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
265 if (i
>= ctx
->Const
.MaxColorAttachments
266 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
269 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
270 case GL_DEPTH_STENCIL_ATTACHMENT
:
271 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
274 case GL_DEPTH_ATTACHMENT_EXT
:
275 return &fb
->Attachment
[BUFFER_DEPTH
];
276 case GL_STENCIL_ATTACHMENT_EXT
:
277 return &fb
->Attachment
[BUFFER_STENCIL
];
285 * As above, but only used for getting attachments of the default /
286 * window-system framebuffer (not user-created framebuffer objects).
288 static struct gl_renderbuffer_attachment
*
289 get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
292 assert(_mesa_is_winsys_fbo(fb
));
294 if (_mesa_is_gles3(ctx
)) {
295 assert(attachment
== GL_BACK
||
296 attachment
== GL_DEPTH
||
297 attachment
== GL_STENCIL
);
298 switch (attachment
) {
300 /* Since there is no stereo rendering in ES 3.0, only return the
303 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
304 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
305 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
307 return &fb
->Attachment
[BUFFER_DEPTH
];
309 return &fb
->Attachment
[BUFFER_STENCIL
];
313 switch (attachment
) {
315 /* Front buffers can be allocated on the first use, but
316 * glGetFramebufferAttachmentParameteriv must work even if that
317 * allocation hasn't happened yet. In such case, use the back buffer,
318 * which should be the same.
320 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Type
== GL_NONE
)
321 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
323 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
326 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Type
== GL_NONE
)
327 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
329 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
331 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
333 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
335 /* The ARB_ES3_1_compatibility spec says:
337 * "Since this command can only query a single framebuffer
338 * attachment, BACK is equivalent to BACK_LEFT."
340 if (ctx
->Extensions
.ARB_ES3_1_compatibility
)
341 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
344 if (fb
->Visual
.numAuxBuffers
== 1) {
345 return &fb
->Attachment
[BUFFER_AUX0
];
349 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
351 * "If the default framebuffer is bound to target, then attachment must
352 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
353 * identifying a color buffer; DEPTH, identifying the depth buffer; or
354 * STENCIL, identifying the stencil buffer."
356 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
357 * language. However, revision #33 of the ARB_framebuffer_object spec
360 * "If the default framebuffer is bound to <target>, then <attachment>
361 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
362 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
363 * depth buffer, or the stencil buffer, and <pname> may be
364 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
365 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
367 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
368 * from glext.h, so shipping apps should not use those values.
370 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
371 * support queries of the window system FBO.
374 return &fb
->Attachment
[BUFFER_DEPTH
];
376 return &fb
->Attachment
[BUFFER_STENCIL
];
385 * Remove any texture or renderbuffer attached to the given attachment
386 * point. Update reference counts, etc.
389 remove_attachment(struct gl_context
*ctx
,
390 struct gl_renderbuffer_attachment
*att
)
392 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
394 /* tell driver that we're done rendering to this texture. */
395 if (rb
&& rb
->NeedsFinishRenderTexture
)
396 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
398 if (att
->Type
== GL_TEXTURE
) {
399 assert(att
->Texture
);
400 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
401 assert(!att
->Texture
);
403 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
404 assert(!att
->Texture
);
405 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
406 assert(!att
->Renderbuffer
);
409 att
->Complete
= GL_TRUE
;
413 * Verify a couple error conditions that will lead to an incomplete FBO and
414 * may cause problems for the driver's RenderTexture path.
417 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment
*att
)
419 const struct gl_texture_image
*const texImage
=
420 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
423 texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
426 if ((texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
427 && att
->Zoffset
>= texImage
->Height
)
428 || (texImage
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
429 && att
->Zoffset
>= texImage
->Depth
))
436 * Create a renderbuffer which will be set up by the driver to wrap the
437 * texture image slice.
439 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
440 * to share most of their framebuffer rendering code between winsys,
441 * renderbuffer, and texture attachments.
443 * The allocated renderbuffer uses a non-zero Name so that drivers can check
444 * it for determining vertical orientation, but we use ~0 to make it fairly
445 * unambiguous with actual user (non-texture) renderbuffers.
448 _mesa_update_texture_renderbuffer(struct gl_context
*ctx
,
449 struct gl_framebuffer
*fb
,
450 struct gl_renderbuffer_attachment
*att
)
452 struct gl_texture_image
*texImage
;
453 struct gl_renderbuffer
*rb
;
455 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
457 rb
= att
->Renderbuffer
;
459 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, ~0);
461 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
464 att
->Renderbuffer
= rb
;
466 /* This can't get called on a texture renderbuffer, so set it to NULL
467 * for clarity compared to user renderbuffers.
469 rb
->AllocStorage
= NULL
;
471 rb
->NeedsFinishRenderTexture
= ctx
->Driver
.FinishRenderTexture
!= NULL
;
477 rb
->_BaseFormat
= texImage
->_BaseFormat
;
478 rb
->Format
= texImage
->TexFormat
;
479 rb
->InternalFormat
= texImage
->InternalFormat
;
480 rb
->Width
= texImage
->Width2
;
481 rb
->Height
= texImage
->Height2
;
482 rb
->Depth
= texImage
->Depth2
;
483 rb
->NumSamples
= texImage
->NumSamples
;
484 rb
->NumStorageSamples
= texImage
->NumSamples
;
485 rb
->TexImage
= texImage
;
487 if (driver_RenderTexture_is_safe(att
))
488 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
492 * Bind a texture object to an attachment point.
493 * The previous binding, if any, will be removed first.
496 set_texture_attachment(struct gl_context
*ctx
,
497 struct gl_framebuffer
*fb
,
498 struct gl_renderbuffer_attachment
*att
,
499 struct gl_texture_object
*texObj
,
500 GLenum texTarget
, GLuint level
, GLuint layer
,
503 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
505 if (rb
&& rb
->NeedsFinishRenderTexture
)
506 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
508 if (att
->Texture
== texObj
) {
509 /* re-attaching same texture */
510 assert(att
->Type
== GL_TEXTURE
);
514 remove_attachment(ctx
, att
);
515 att
->Type
= GL_TEXTURE
;
516 assert(!att
->Texture
);
517 _mesa_reference_texobj(&att
->Texture
, texObj
);
519 invalidate_framebuffer(fb
);
521 /* always update these fields */
522 att
->TextureLevel
= level
;
523 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
524 att
->Zoffset
= layer
;
525 att
->Layered
= layered
;
526 att
->Complete
= GL_FALSE
;
528 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
533 * Bind a renderbuffer to an attachment point.
534 * The previous binding, if any, will be removed first.
537 set_renderbuffer_attachment(struct gl_context
*ctx
,
538 struct gl_renderbuffer_attachment
*att
,
539 struct gl_renderbuffer
*rb
)
541 /* XXX check if re-doing same attachment, exit early */
542 remove_attachment(ctx
, att
);
543 att
->Type
= GL_RENDERBUFFER_EXT
;
544 att
->Texture
= NULL
; /* just to be safe */
545 att
->Layered
= GL_FALSE
;
546 att
->Complete
= GL_FALSE
;
547 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
552 * Fallback for ctx->Driver.FramebufferRenderbuffer()
553 * Attach a renderbuffer object to a framebuffer object.
556 _mesa_FramebufferRenderbuffer_sw(struct gl_context
*ctx
,
557 struct gl_framebuffer
*fb
,
559 struct gl_renderbuffer
*rb
)
561 struct gl_renderbuffer_attachment
*att
;
563 simple_mtx_lock(&fb
->Mutex
);
565 att
= get_attachment(ctx
, fb
, attachment
, NULL
);
568 set_renderbuffer_attachment(ctx
, att
, rb
);
569 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
570 /* do stencil attachment here (depth already done above) */
571 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
, NULL
);
573 set_renderbuffer_attachment(ctx
, att
, rb
);
575 rb
->AttachedAnytime
= GL_TRUE
;
578 remove_attachment(ctx
, att
);
579 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
580 /* detach stencil (depth was detached above) */
581 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
, NULL
);
583 remove_attachment(ctx
, att
);
587 invalidate_framebuffer(fb
);
589 simple_mtx_unlock(&fb
->Mutex
);
594 * Fallback for ctx->Driver.ValidateFramebuffer()
595 * Check if the renderbuffer's formats are supported by the software
597 * Drivers should probably override this.
600 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
603 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
604 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
606 switch (rb
->_BaseFormat
) {
608 case GL_LUMINANCE_ALPHA
:
613 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
617 switch (rb
->Format
) {
618 /* XXX This list is likely incomplete. */
619 case MESA_FORMAT_R9G9B9E5_FLOAT
:
620 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
623 /* render buffer format is supported by software rendering */
632 * Return true if the framebuffer has a combined depth/stencil
633 * renderbuffer attached.
636 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
638 const struct gl_renderbuffer_attachment
*depth
=
639 &fb
->Attachment
[BUFFER_DEPTH
];
640 const struct gl_renderbuffer_attachment
*stencil
=
641 &fb
->Attachment
[BUFFER_STENCIL
];
643 if (depth
->Type
== stencil
->Type
) {
644 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
645 depth
->Renderbuffer
== stencil
->Renderbuffer
)
648 if (depth
->Type
== GL_TEXTURE
&&
649 depth
->Texture
== stencil
->Texture
)
661 att_incomplete(const char *msg
)
663 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
664 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
673 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
675 static GLuint msg_id
;
677 _mesa_gl_debug(ctx
, &msg_id
,
678 MESA_DEBUG_SOURCE_API
,
679 MESA_DEBUG_TYPE_OTHER
,
680 MESA_DEBUG_SEVERITY_MEDIUM
,
681 "FBO incomplete: %s [%d]\n", msg
, index
);
683 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
684 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
690 * Is the given base format a legal format for a color renderbuffer?
693 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
695 switch (baseFormat
) {
700 case GL_LUMINANCE_ALPHA
:
703 return ctx
->API
== API_OPENGL_COMPAT
&&
704 ctx
->Extensions
.ARB_framebuffer_object
;
707 return ctx
->Extensions
.ARB_texture_rg
;
715 * Is the given base format a legal format for a color renderbuffer?
718 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
719 GLenum internalFormat
)
721 const GLenum baseFormat
=
722 _mesa_get_format_base_format(format
);
725 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
726 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
730 /* Reject additional cases for GLES */
731 switch (internalFormat
) {
752 if (internalFormat
!= GL_RGB10_A2
&&
753 (format
== MESA_FORMAT_B10G10R10A2_UNORM
||
754 format
== MESA_FORMAT_B10G10R10X2_UNORM
||
755 format
== MESA_FORMAT_R10G10B10A2_UNORM
||
756 format
== MESA_FORMAT_R10G10B10X2_UNORM
)) {
765 * Is the given base format a legal format for a depth/stencil renderbuffer?
768 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
770 switch (baseFormat
) {
771 case GL_DEPTH_COMPONENT
:
772 case GL_DEPTH_STENCIL_EXT
:
781 * Test if an attachment point is complete and update its Complete field.
782 * \param format if GL_COLOR, this is a color attachment point,
783 * if GL_DEPTH, this is a depth component attachment point,
784 * if GL_STENCIL, this is a stencil component attachment point.
787 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
788 struct gl_renderbuffer_attachment
*att
)
790 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
792 /* assume complete */
793 att
->Complete
= GL_TRUE
;
795 /* Look for reasons why the attachment might be incomplete */
796 if (att
->Type
== GL_TEXTURE
) {
797 const struct gl_texture_object
*texObj
= att
->Texture
;
798 const struct gl_texture_image
*texImage
;
802 att_incomplete("no texobj");
803 att
->Complete
= GL_FALSE
;
807 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
809 att_incomplete("no teximage");
810 att
->Complete
= GL_FALSE
;
813 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
814 att_incomplete("teximage width/height=0");
815 att
->Complete
= GL_FALSE
;
819 switch (texObj
->Target
) {
821 if (att
->Zoffset
>= texImage
->Depth
) {
822 att_incomplete("bad z offset");
823 att
->Complete
= GL_FALSE
;
827 case GL_TEXTURE_1D_ARRAY
:
828 if (att
->Zoffset
>= texImage
->Height
) {
829 att_incomplete("bad 1D-array layer");
830 att
->Complete
= GL_FALSE
;
834 case GL_TEXTURE_2D_ARRAY
:
835 if (att
->Zoffset
>= texImage
->Depth
) {
836 att_incomplete("bad 2D-array layer");
837 att
->Complete
= GL_FALSE
;
841 case GL_TEXTURE_CUBE_MAP_ARRAY
:
842 if (att
->Zoffset
>= texImage
->Depth
) {
843 att_incomplete("bad cube-array layer");
844 att
->Complete
= GL_FALSE
;
850 baseFormat
= texImage
->_BaseFormat
;
852 if (format
== GL_COLOR
) {
853 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
854 att_incomplete("bad format");
855 att
->Complete
= GL_FALSE
;
858 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
859 att_incomplete("compressed internalformat");
860 att
->Complete
= GL_FALSE
;
864 /* OES_texture_float allows creation and use of floating point
865 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
866 * these textures to be used as a render target, this is done via
867 * GL_EXT_color_buffer(_half)_float with set of new sized types.
869 if (_mesa_is_gles(ctx
) && (texObj
->_IsFloat
|| texObj
->_IsHalfFloat
)) {
870 att_incomplete("bad internal format");
871 att
->Complete
= GL_FALSE
;
875 else if (format
== GL_DEPTH
) {
876 if (baseFormat
== GL_DEPTH_COMPONENT
) {
879 else if (ctx
->Extensions
.ARB_depth_texture
&&
880 baseFormat
== GL_DEPTH_STENCIL
) {
884 att
->Complete
= GL_FALSE
;
885 att_incomplete("bad depth format");
890 assert(format
== GL_STENCIL
);
891 if (ctx
->Extensions
.ARB_depth_texture
&&
892 baseFormat
== GL_DEPTH_STENCIL
) {
894 } else if (ctx
->Extensions
.ARB_texture_stencil8
&&
895 baseFormat
== GL_STENCIL_INDEX
) {
898 /* no such thing as stencil-only textures */
899 att_incomplete("illegal stencil texture");
900 att
->Complete
= GL_FALSE
;
905 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
906 const GLenum baseFormat
= att
->Renderbuffer
->_BaseFormat
;
908 assert(att
->Renderbuffer
);
909 if (!att
->Renderbuffer
->InternalFormat
||
910 att
->Renderbuffer
->Width
< 1 ||
911 att
->Renderbuffer
->Height
< 1) {
912 att_incomplete("0x0 renderbuffer");
913 att
->Complete
= GL_FALSE
;
916 if (format
== GL_COLOR
) {
917 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
918 att_incomplete("bad renderbuffer color format");
919 att
->Complete
= GL_FALSE
;
923 else if (format
== GL_DEPTH
) {
924 if (baseFormat
== GL_DEPTH_COMPONENT
) {
927 else if (baseFormat
== GL_DEPTH_STENCIL
) {
931 att_incomplete("bad renderbuffer depth format");
932 att
->Complete
= GL_FALSE
;
937 assert(format
== GL_STENCIL
);
938 if (baseFormat
== GL_STENCIL_INDEX
||
939 baseFormat
== GL_DEPTH_STENCIL
) {
943 att
->Complete
= GL_FALSE
;
944 att_incomplete("bad renderbuffer stencil format");
950 assert(att
->Type
== GL_NONE
);
958 * Test if the given framebuffer object is complete and update its
959 * Status field with the results.
960 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
961 * driver to make hardware-specific validation/completeness checks.
962 * Also update the framebuffer's Width and Height fields if the
963 * framebuffer is complete.
966 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
967 struct gl_framebuffer
*fb
)
970 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
971 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
972 GLint numSamples
= -1;
973 GLint fixedSampleLocations
= -1;
976 /* Covers max_layer_count, is_layered, and layer_tex_target */
977 bool layer_info_valid
= false;
978 GLuint max_layer_count
= 0, att_layer_count
;
979 bool is_layered
= false;
980 GLenum layer_tex_target
= 0;
981 bool has_depth_attachment
= false;
982 bool has_stencil_attachment
= false;
984 assert(_mesa_is_user_fbo(fb
));
986 /* we're changing framebuffer fields here */
987 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
992 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
993 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
994 fb
->_HasAttachments
= true;
995 fb
->_IntegerBuffers
= 0;
997 /* Start at -2 to more easily loop over all attachment points.
1002 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
1003 struct gl_renderbuffer_attachment
*att
;
1005 mesa_format attFormat
;
1006 GLenum att_tex_target
= GL_NONE
;
1009 * XXX for ARB_fbo, only check color buffers that are named by
1010 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
1013 /* check for attachment completeness
1016 att
= &fb
->Attachment
[BUFFER_DEPTH
];
1017 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
1018 if (!att
->Complete
) {
1019 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1020 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
1022 } else if (att
->Type
!= GL_NONE
) {
1023 has_depth_attachment
= true;
1027 att
= &fb
->Attachment
[BUFFER_STENCIL
];
1028 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
1029 if (!att
->Complete
) {
1030 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1031 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
1033 } else if (att
->Type
!= GL_NONE
) {
1034 has_stencil_attachment
= true;
1038 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
1039 test_attachment_completeness(ctx
, GL_COLOR
, att
);
1040 if (!att
->Complete
) {
1041 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1042 fbo_incomplete(ctx
, "color attachment incomplete", i
);
1047 /* get width, height, format of the renderbuffer/texture
1049 if (att
->Type
== GL_TEXTURE
) {
1050 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
1051 att_tex_target
= att
->Texture
->Target
;
1052 minWidth
= MIN2(minWidth
, texImg
->Width
);
1053 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
1054 minHeight
= MIN2(minHeight
, texImg
->Height
);
1055 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
1056 f
= texImg
->_BaseFormat
;
1057 attFormat
= texImg
->TexFormat
;
1060 if (!is_format_color_renderable(ctx
, attFormat
,
1061 texImg
->InternalFormat
) &&
1062 !is_legal_depth_format(ctx
, f
) &&
1063 f
!= GL_STENCIL_INDEX
) {
1064 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
1065 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
1070 numSamples
= texImg
->NumSamples
;
1071 else if (numSamples
!= texImg
->NumSamples
) {
1072 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1073 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1077 if (fixedSampleLocations
< 0)
1078 fixedSampleLocations
= texImg
->FixedSampleLocations
;
1079 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
1080 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1081 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1085 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1086 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1087 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1088 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1089 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1090 f
= att
->Renderbuffer
->InternalFormat
;
1091 attFormat
= att
->Renderbuffer
->Format
;
1095 numSamples
= att
->Renderbuffer
->NumSamples
;
1096 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1097 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1098 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1102 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1103 if (fixedSampleLocations
< 0)
1104 fixedSampleLocations
= GL_TRUE
;
1105 else if (fixedSampleLocations
!= GL_TRUE
) {
1106 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1107 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1112 assert(att
->Type
== GL_NONE
);
1116 /* Update flags describing color buffer datatypes */
1118 GLenum type
= _mesa_get_format_datatype(attFormat
);
1120 /* check if integer color */
1121 if (_mesa_is_format_integer_color(attFormat
))
1122 fb
->_IntegerBuffers
|= (1 << i
);
1124 fb
->_AllColorBuffersFixedPoint
=
1125 fb
->_AllColorBuffersFixedPoint
&&
1126 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1128 fb
->_HasSNormOrFloatColorBuffer
=
1129 fb
->_HasSNormOrFloatColorBuffer
||
1130 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1133 /* Error-check width, height, format */
1134 if (numImages
== 1) {
1141 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1142 /* check that width, height, format are same */
1143 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1144 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1145 fbo_incomplete(ctx
, "width or height mismatch", -1);
1148 /* check that all color buffers are the same format */
1149 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1150 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1151 fbo_incomplete(ctx
, "format mismatch", -1);
1157 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1159 if (att
->Type
== GL_RENDERBUFFER
&&
1160 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1161 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1162 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1166 /* Check that layered rendering is consistent. */
1168 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1169 att_layer_count
= 6;
1170 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1171 att_layer_count
= att
->Renderbuffer
->Height
;
1173 att_layer_count
= att
->Renderbuffer
->Depth
;
1175 att_layer_count
= 0;
1177 if (!layer_info_valid
) {
1178 is_layered
= att
->Layered
;
1179 max_layer_count
= att_layer_count
;
1180 layer_tex_target
= att_tex_target
;
1181 layer_info_valid
= true;
1182 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1183 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1184 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1186 } else if (is_layered
!= att
->Layered
) {
1187 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1189 "framebuffer attachment layer mode is inconsistent",
1192 } else if (att_layer_count
> max_layer_count
) {
1193 max_layer_count
= att_layer_count
;
1197 * The extension GL_ARB_framebuffer_no_attachments places additional
1198 * requirement on each attachment. Those additional requirements are
1199 * tighter that those of previous versions of GL. In interest of better
1200 * compatibility, we will not enforce these restrictions. For the record
1201 * those additional restrictions are quoted below:
1203 * "The width and height of image are greater than zero and less than or
1204 * equal to the values of the implementation-dependent limits
1205 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1207 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1208 * array texture and the attachment is layered, the depth or layer count
1209 * of the texture is less than or equal to the implementation-dependent
1210 * limit MAX_FRAMEBUFFER_LAYERS."
1212 * "If image has multiple samples, its sample count is less than or equal
1213 * to the value of the implementation-dependent limit
1214 * MAX_FRAMEBUFFER_SAMPLES."
1216 * The same requirements are also in place for GL 4.5,
1217 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1221 fb
->MaxNumLayers
= max_layer_count
;
1223 if (numImages
== 0) {
1224 fb
->_HasAttachments
= false;
1226 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
1227 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1228 fbo_incomplete(ctx
, "no attachments", -1);
1232 if (fb
->DefaultGeometry
.Width
== 0 || fb
->DefaultGeometry
.Height
== 0) {
1233 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1234 fbo_incomplete(ctx
, "no attachments and default width or height is 0", -1);
1239 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1240 /* Check that all DrawBuffers are present */
1241 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1242 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1243 const struct gl_renderbuffer_attachment
*att
1244 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
], NULL
);
1246 if (att
->Type
== GL_NONE
) {
1247 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1248 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1254 /* Check that the ReadBuffer is present */
1255 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1256 const struct gl_renderbuffer_attachment
*att
1257 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
, NULL
);
1259 if (att
->Type
== GL_NONE
) {
1260 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1261 fbo_incomplete(ctx
, "missing readbuffer", -1);
1267 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1269 * "Depth and stencil attachments, if present, are the same image."
1271 * This restriction is not present in the OpenGL ES2 spec.
1273 if (_mesa_is_gles3(ctx
) &&
1274 has_stencil_attachment
&& has_depth_attachment
&&
1275 !_mesa_has_depthstencil_combined(fb
)) {
1276 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1277 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1281 /* Provisionally set status = COMPLETE ... */
1282 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1284 /* ... but the driver may say the FB is incomplete.
1285 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1288 if (ctx
->Driver
.ValidateFramebuffer
) {
1289 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1290 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1291 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1297 * Note that if ARB_framebuffer_object is supported and the attached
1298 * renderbuffers/textures are different sizes, the framebuffer
1299 * width/height will be set to the smallest width/height.
1301 if (numImages
!= 0) {
1302 fb
->Width
= minWidth
;
1303 fb
->Height
= minHeight
;
1306 /* finally, update the visual info for the framebuffer */
1307 _mesa_update_framebuffer_visual(ctx
, fb
);
1311 GLboolean GLAPIENTRY
1312 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1314 struct gl_renderbuffer
*rb
;
1316 GET_CURRENT_CONTEXT(ctx
);
1318 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1320 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1321 return rb
!= NULL
&& rb
!= &DummyRenderbuffer
;
1325 static struct gl_renderbuffer
*
1326 allocate_renderbuffer_locked(struct gl_context
*ctx
, GLuint renderbuffer
,
1329 struct gl_renderbuffer
*newRb
;
1331 /* create new renderbuffer object */
1332 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1334 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1337 assert(newRb
->AllocStorage
);
1338 _mesa_HashInsertLocked(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1345 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1347 struct gl_renderbuffer
*newRb
;
1348 GET_CURRENT_CONTEXT(ctx
);
1350 if (target
!= GL_RENDERBUFFER_EXT
) {
1351 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1355 /* No need to flush here since the render buffer binding has no
1356 * effect on rendering state.
1360 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1361 if (newRb
== &DummyRenderbuffer
) {
1362 /* ID was reserved, but no real renderbuffer object made yet */
1365 else if (!newRb
&& !allow_user_names
) {
1366 /* All RB IDs must be Gen'd */
1367 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1372 _mesa_HashLockMutex(ctx
->Shared
->RenderBuffers
);
1373 newRb
= allocate_renderbuffer_locked(ctx
, renderbuffer
,
1374 "glBindRenderbufferEXT");
1375 _mesa_HashUnlockMutex(ctx
->Shared
->RenderBuffers
);
1382 assert(newRb
!= &DummyRenderbuffer
);
1384 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1388 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1390 GET_CURRENT_CONTEXT(ctx
);
1392 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1393 * entry point, but they allow the use of user-generated names.
1395 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1399 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1401 /* This function should not be in the dispatch table for core profile /
1402 * OpenGL 3.1, so execution should never get here in those cases -- no
1403 * need for an explicit test.
1405 bind_renderbuffer(target
, renderbuffer
, true);
1409 * ARB_framebuffer_no_attachment and ARB_sample_locations - Application passes
1410 * requested param's here. NOTE: NumSamples requested need not be _NumSamples
1411 * which is what the hw supports.
1414 framebuffer_parameteri(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1415 GLenum pname
, GLint param
, const char *func
)
1417 bool cannot_be_winsys_fbo
= false;
1420 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1421 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1422 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1423 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1424 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1425 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
)
1426 goto invalid_pname_enum
;
1427 cannot_be_winsys_fbo
= true;
1429 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1430 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1431 if (!ctx
->Extensions
.ARB_sample_locations
)
1432 goto invalid_pname_enum
;
1434 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1435 if (!ctx
->Extensions
.MESA_framebuffer_flip_y
)
1436 goto invalid_pname_enum
;
1437 cannot_be_winsys_fbo
= true;
1439 goto invalid_pname_enum
;
1442 if (cannot_be_winsys_fbo
&& _mesa_is_winsys_fbo(fb
)) {
1443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1444 "%s(invalid pname=0x%x for default framebuffer)", func
, pname
);
1449 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1450 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferWidth
)
1451 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1453 fb
->DefaultGeometry
.Width
= param
;
1455 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1456 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferHeight
)
1457 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1459 fb
->DefaultGeometry
.Height
= param
;
1461 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1463 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1464 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1466 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
) {
1467 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1470 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferLayers
)
1471 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1473 fb
->DefaultGeometry
.Layers
= param
;
1475 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1476 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferSamples
)
1477 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1479 fb
->DefaultGeometry
.NumSamples
= param
;
1481 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1482 fb
->DefaultGeometry
.FixedSampleLocations
= param
;
1484 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1485 fb
->ProgrammableSampleLocations
= !!param
;
1487 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1488 fb
->SampleLocationPixelGrid
= !!param
;
1490 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1496 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1497 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1498 if (fb
== ctx
->DrawBuffer
)
1499 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
1502 invalidate_framebuffer(fb
);
1503 ctx
->NewState
|= _NEW_BUFFERS
;
1510 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1514 _mesa_FramebufferParameteri(GLenum target
, GLenum pname
, GLint param
)
1516 GET_CURRENT_CONTEXT(ctx
);
1517 struct gl_framebuffer
*fb
;
1519 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
1520 !ctx
->Extensions
.ARB_sample_locations
) {
1521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1522 "glFramebufferParameteriv not supported "
1523 "(neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
1528 fb
= get_framebuffer_target(ctx
, target
);
1530 _mesa_error(ctx
, GL_INVALID_ENUM
,
1531 "glFramebufferParameteri(target=0x%x)", target
);
1535 framebuffer_parameteri(ctx
, fb
, pname
, param
, "glFramebufferParameteri");
1539 validate_get_framebuffer_parameteriv_pname(struct gl_context
*ctx
,
1540 struct gl_framebuffer
*fb
,
1541 GLuint pname
, const char *func
)
1543 bool cannot_be_winsys_fbo
= true;
1546 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1548 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1549 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1551 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
) {
1552 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1556 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1557 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1558 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1559 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1561 case GL_DOUBLEBUFFER
:
1562 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
1563 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
1565 case GL_SAMPLE_BUFFERS
:
1567 /* From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries:
1569 * "An INVALID_OPERATION error is generated by GetFramebufferParameteriv
1570 * if the default framebuffer is bound to target and pname is not one
1571 * of the accepted values from table 23.73, other than
1574 * For OpenGL ES, using default framebuffer raises INVALID_OPERATION
1577 cannot_be_winsys_fbo
= !_mesa_is_desktop_gl(ctx
);
1579 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1580 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1581 if (!ctx
->Extensions
.ARB_sample_locations
)
1582 goto invalid_pname_enum
;
1583 cannot_be_winsys_fbo
= false;
1585 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1586 if (!ctx
->Extensions
.MESA_framebuffer_flip_y
) {
1587 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1592 goto invalid_pname_enum
;
1595 if (cannot_be_winsys_fbo
&& _mesa_is_winsys_fbo(fb
)) {
1596 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1597 "%s(invalid pname=0x%x for default framebuffer)", func
, pname
);
1604 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1609 get_framebuffer_parameteriv(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1610 GLenum pname
, GLint
*params
, const char *func
)
1612 if (!validate_get_framebuffer_parameteriv_pname(ctx
, fb
, pname
, func
))
1616 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1617 *params
= fb
->DefaultGeometry
.Width
;
1619 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1620 *params
= fb
->DefaultGeometry
.Height
;
1622 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1623 *params
= fb
->DefaultGeometry
.Layers
;
1625 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1626 *params
= fb
->DefaultGeometry
.NumSamples
;
1628 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1629 *params
= fb
->DefaultGeometry
.FixedSampleLocations
;
1631 case GL_DOUBLEBUFFER
:
1632 *params
= fb
->Visual
.doubleBufferMode
;
1634 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
1635 *params
= _mesa_get_color_read_format(ctx
, fb
, func
);
1637 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
1638 *params
= _mesa_get_color_read_type(ctx
, fb
, func
);
1641 *params
= _mesa_geometric_samples(fb
);
1643 case GL_SAMPLE_BUFFERS
:
1644 *params
= _mesa_geometric_samples(fb
) > 0;
1647 *params
= fb
->Visual
.stereoMode
;
1649 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1650 *params
= fb
->ProgrammableSampleLocations
;
1652 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1653 *params
= fb
->SampleLocationPixelGrid
;
1655 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1656 *params
= fb
->FlipY
;
1662 _mesa_GetFramebufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1664 GET_CURRENT_CONTEXT(ctx
);
1665 struct gl_framebuffer
*fb
;
1667 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
1668 !ctx
->Extensions
.ARB_sample_locations
) {
1669 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1670 "glGetFramebufferParameteriv not supported "
1671 "(neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
1676 fb
= get_framebuffer_target(ctx
, target
);
1678 _mesa_error(ctx
, GL_INVALID_ENUM
,
1679 "glGetFramebufferParameteriv(target=0x%x)", target
);
1683 get_framebuffer_parameteriv(ctx
, fb
, pname
, params
,
1684 "glGetFramebufferParameteriv");
1689 * Remove the specified renderbuffer or texture from any attachment point in
1693 * \c true if the renderbuffer was detached from an attachment point. \c
1697 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1698 struct gl_framebuffer
*fb
,
1702 bool progress
= false;
1704 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1705 if (fb
->Attachment
[i
].Texture
== att
1706 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1707 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1712 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1713 * Completeness," of the OpenGL 3.1 spec says:
1715 * "Performing any of the following actions may change whether the
1716 * framebuffer is considered complete or incomplete:
1720 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1721 * containing an image that is attached to a framebuffer object
1722 * that is bound to the framebuffer."
1725 invalidate_framebuffer(fb
);
1732 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1735 GET_CURRENT_CONTEXT(ctx
);
1738 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1742 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1744 for (i
= 0; i
< n
; i
++) {
1745 if (renderbuffers
[i
] > 0) {
1746 struct gl_renderbuffer
*rb
;
1747 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1749 /* check if deleting currently bound renderbuffer object */
1750 if (rb
== ctx
->CurrentRenderbuffer
) {
1752 assert(rb
->RefCount
>= 2);
1753 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1756 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1757 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1758 * of the OpenGL 3.1 spec says:
1760 * "If a renderbuffer object is deleted while its image is
1761 * attached to one or more attachment points in the currently
1762 * bound framebuffer, then it is as if FramebufferRenderbuffer
1763 * had been called, with a renderbuffer of 0, for each
1764 * attachment point to which this image was attached in the
1765 * currently bound framebuffer. In other words, this
1766 * renderbuffer image is first detached from all attachment
1767 * points in the currently bound framebuffer. Note that the
1768 * renderbuffer image is specifically not detached from any
1769 * non-bound framebuffers. Detaching the image from any
1770 * non-bound framebuffers is the responsibility of the
1773 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1774 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1776 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1777 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1778 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1781 /* Remove from hash table immediately, to free the ID.
1782 * But the object will not be freed until it's no longer
1783 * referenced anywhere else.
1785 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1787 if (rb
!= &DummyRenderbuffer
) {
1788 /* no longer referenced by hash table */
1789 _mesa_reference_renderbuffer(&rb
, NULL
);
1797 create_render_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*renderbuffers
,
1800 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1807 _mesa_HashLockMutex(ctx
->Shared
->RenderBuffers
);
1809 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1811 for (i
= 0; i
< n
; i
++) {
1812 GLuint name
= first
+ i
;
1813 renderbuffers
[i
] = name
;
1816 allocate_renderbuffer_locked(ctx
, name
, func
);
1818 /* insert a dummy renderbuffer into the hash table */
1819 _mesa_HashInsertLocked(ctx
->Shared
->RenderBuffers
, name
,
1820 &DummyRenderbuffer
);
1824 _mesa_HashUnlockMutex(ctx
->Shared
->RenderBuffers
);
1829 create_render_buffers_err(struct gl_context
*ctx
, GLsizei n
,
1830 GLuint
*renderbuffers
, bool dsa
)
1832 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1835 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n<0)", func
);
1839 create_render_buffers(ctx
, n
, renderbuffers
, dsa
);
1844 _mesa_GenRenderbuffers_no_error(GLsizei n
, GLuint
*renderbuffers
)
1846 GET_CURRENT_CONTEXT(ctx
);
1847 create_render_buffers(ctx
, n
, renderbuffers
, false);
1852 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1854 GET_CURRENT_CONTEXT(ctx
);
1855 create_render_buffers_err(ctx
, n
, renderbuffers
, false);
1860 _mesa_CreateRenderbuffers_no_error(GLsizei n
, GLuint
*renderbuffers
)
1862 GET_CURRENT_CONTEXT(ctx
);
1863 create_render_buffers(ctx
, n
, renderbuffers
, true);
1868 _mesa_CreateRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1870 GET_CURRENT_CONTEXT(ctx
);
1871 create_render_buffers_err(ctx
, n
, renderbuffers
, true);
1876 * Given an internal format token for a render buffer, return the
1877 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1878 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1879 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1881 * This is similar to _mesa_base_tex_format() but the set of valid
1882 * internal formats is different.
1884 * Note that even if a format is determined to be legal here, validation
1885 * of the FBO may fail if the format is not supported by the driver/GPU.
1887 * \param internalFormat as passed to glRenderbufferStorage()
1888 * \return the base internal format, or 0 if internalFormat is illegal
1891 _mesa_base_fbo_format(const struct gl_context
*ctx
, GLenum internalFormat
)
1894 * Notes: some formats such as alpha, luminance, etc. were added
1895 * with GL_ARB_framebuffer_object.
1897 switch (internalFormat
) {
1903 return (ctx
->API
== API_OPENGL_COMPAT
&&
1904 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1908 case GL_LUMINANCE12
:
1909 case GL_LUMINANCE16
:
1910 return (ctx
->API
== API_OPENGL_COMPAT
&&
1911 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1912 case GL_LUMINANCE_ALPHA
:
1913 case GL_LUMINANCE4_ALPHA4
:
1914 case GL_LUMINANCE6_ALPHA2
:
1915 case GL_LUMINANCE8_ALPHA8
:
1916 case GL_LUMINANCE12_ALPHA4
:
1917 case GL_LUMINANCE12_ALPHA12
:
1918 case GL_LUMINANCE16_ALPHA16
:
1919 return (ctx
->API
== API_OPENGL_COMPAT
&&
1920 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1924 case GL_INTENSITY12
:
1925 case GL_INTENSITY16
:
1926 return (ctx
->API
== API_OPENGL_COMPAT
&&
1927 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1937 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1939 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1947 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1949 return _mesa_is_desktop_gl(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
1952 case GL_SRGB8_ALPHA8_EXT
:
1953 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1954 case GL_STENCIL_INDEX
:
1955 case GL_STENCIL_INDEX1_EXT
:
1956 case GL_STENCIL_INDEX4_EXT
:
1957 case GL_STENCIL_INDEX16_EXT
:
1958 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1959 * OpenGL ES, but Mesa does not currently support them.
1961 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1962 case GL_STENCIL_INDEX8_EXT
:
1963 return GL_STENCIL_INDEX
;
1964 case GL_DEPTH_COMPONENT
:
1965 case GL_DEPTH_COMPONENT32
:
1966 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1967 case GL_DEPTH_COMPONENT16
:
1968 case GL_DEPTH_COMPONENT24
:
1969 return GL_DEPTH_COMPONENT
;
1970 case GL_DEPTH_STENCIL
:
1971 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1972 case GL_DEPTH24_STENCIL8
:
1973 return GL_DEPTH_STENCIL
;
1974 case GL_DEPTH_COMPONENT32F
:
1975 return ctx
->Version
>= 30
1976 || (ctx
->API
== API_OPENGL_COMPAT
&&
1977 ctx
->Extensions
.ARB_depth_buffer_float
)
1978 ? GL_DEPTH_COMPONENT
: 0;
1979 case GL_DEPTH32F_STENCIL8
:
1980 return ctx
->Version
>= 30
1981 || (ctx
->API
== API_OPENGL_COMPAT
&&
1982 ctx
->Extensions
.ARB_depth_buffer_float
)
1983 ? GL_DEPTH_STENCIL
: 0;
1985 return _mesa_has_ARB_texture_rg(ctx
) ? GL_RED
: 0;
1987 return _mesa_has_ARB_texture_rg(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
1990 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1993 return _mesa_has_ARB_texture_rg(ctx
) ? GL_RG
: 0;
1995 return _mesa_has_ARB_texture_rg(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
1998 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
2000 /* signed normalized texture formats */
2004 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2009 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2013 case GL_RGB16_SNORM
:
2014 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2017 case GL_RGBA8_SNORM
:
2018 case GL_RGBA16_SNORM
:
2019 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2021 case GL_ALPHA_SNORM
:
2022 case GL_ALPHA8_SNORM
:
2023 case GL_ALPHA16_SNORM
:
2024 return ctx
->API
== API_OPENGL_COMPAT
&&
2025 ctx
->Extensions
.EXT_texture_snorm
&&
2026 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2027 case GL_LUMINANCE_SNORM
:
2028 case GL_LUMINANCE8_SNORM
:
2029 case GL_LUMINANCE16_SNORM
:
2030 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2032 case GL_LUMINANCE_ALPHA_SNORM
:
2033 case GL_LUMINANCE8_ALPHA8_SNORM
:
2034 case GL_LUMINANCE16_ALPHA16_SNORM
:
2035 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2036 ? GL_LUMINANCE_ALPHA
: 0;
2037 case GL_INTENSITY_SNORM
:
2038 case GL_INTENSITY8_SNORM
:
2039 case GL_INTENSITY16_SNORM
:
2040 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2045 return ((_mesa_is_desktop_gl(ctx
) &&
2046 ctx
->Extensions
.ARB_texture_rg
&&
2047 ctx
->Extensions
.ARB_texture_float
) ||
2048 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2052 return ((_mesa_is_desktop_gl(ctx
) &&
2053 ctx
->Extensions
.ARB_texture_rg
&&
2054 ctx
->Extensions
.ARB_texture_float
) ||
2055 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2059 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
2063 return ((_mesa_is_desktop_gl(ctx
) &&
2064 ctx
->Extensions
.ARB_texture_float
) ||
2065 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2068 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_shared_exponent
)
2070 case GL_ALPHA16F_ARB
:
2071 case GL_ALPHA32F_ARB
:
2072 return ctx
->API
== API_OPENGL_COMPAT
&&
2073 ctx
->Extensions
.ARB_texture_float
&&
2074 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2075 case GL_LUMINANCE16F_ARB
:
2076 case GL_LUMINANCE32F_ARB
:
2077 return ctx
->API
== API_OPENGL_COMPAT
&&
2078 ctx
->Extensions
.ARB_texture_float
&&
2079 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
2080 case GL_LUMINANCE_ALPHA16F_ARB
:
2081 case GL_LUMINANCE_ALPHA32F_ARB
:
2082 return ctx
->API
== API_OPENGL_COMPAT
&&
2083 ctx
->Extensions
.ARB_texture_float
&&
2084 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
2085 case GL_INTENSITY16F_ARB
:
2086 case GL_INTENSITY32F_ARB
:
2087 return ctx
->API
== API_OPENGL_COMPAT
&&
2088 ctx
->Extensions
.ARB_texture_float
&&
2089 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
2090 case GL_R11F_G11F_B10F
:
2091 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
2092 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2095 case GL_RGBA8UI_EXT
:
2096 case GL_RGBA16UI_EXT
:
2097 case GL_RGBA32UI_EXT
:
2099 case GL_RGBA16I_EXT
:
2100 case GL_RGBA32I_EXT
:
2101 return ctx
->Version
>= 30
2102 || (_mesa_is_desktop_gl(ctx
) &&
2103 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
2106 case GL_RGB16UI_EXT
:
2107 case GL_RGB32UI_EXT
:
2111 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
2119 return ctx
->Version
>= 30
2120 || (_mesa_is_desktop_gl(ctx
) &&
2121 ctx
->Extensions
.ARB_texture_rg
&&
2122 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
2130 return ctx
->Version
>= 30
2131 || (_mesa_is_desktop_gl(ctx
) &&
2132 ctx
->Extensions
.ARB_texture_rg
&&
2133 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
2135 case GL_INTENSITY8I_EXT
:
2136 case GL_INTENSITY8UI_EXT
:
2137 case GL_INTENSITY16I_EXT
:
2138 case GL_INTENSITY16UI_EXT
:
2139 case GL_INTENSITY32I_EXT
:
2140 case GL_INTENSITY32UI_EXT
:
2141 return ctx
->API
== API_OPENGL_COMPAT
&&
2142 ctx
->Extensions
.EXT_texture_integer
&&
2143 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
2145 case GL_LUMINANCE8I_EXT
:
2146 case GL_LUMINANCE8UI_EXT
:
2147 case GL_LUMINANCE16I_EXT
:
2148 case GL_LUMINANCE16UI_EXT
:
2149 case GL_LUMINANCE32I_EXT
:
2150 case GL_LUMINANCE32UI_EXT
:
2151 return ctx
->API
== API_OPENGL_COMPAT
&&
2152 ctx
->Extensions
.EXT_texture_integer
&&
2153 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
2155 case GL_LUMINANCE_ALPHA8I_EXT
:
2156 case GL_LUMINANCE_ALPHA8UI_EXT
:
2157 case GL_LUMINANCE_ALPHA16I_EXT
:
2158 case GL_LUMINANCE_ALPHA16UI_EXT
:
2159 case GL_LUMINANCE_ALPHA32I_EXT
:
2160 case GL_LUMINANCE_ALPHA32UI_EXT
:
2161 return ctx
->API
== API_OPENGL_COMPAT
&&
2162 ctx
->Extensions
.EXT_texture_integer
&&
2163 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
2165 case GL_ALPHA8I_EXT
:
2166 case GL_ALPHA8UI_EXT
:
2167 case GL_ALPHA16I_EXT
:
2168 case GL_ALPHA16UI_EXT
:
2169 case GL_ALPHA32I_EXT
:
2170 case GL_ALPHA32UI_EXT
:
2171 return ctx
->API
== API_OPENGL_COMPAT
&&
2172 ctx
->Extensions
.EXT_texture_integer
&&
2173 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2176 return (_mesa_is_desktop_gl(ctx
) &&
2177 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
2178 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
2181 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
2190 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
2193 invalidate_rb(GLuint key
, void *data
, void *userData
)
2195 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2196 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
2198 /* If this is a user-created FBO */
2199 if (_mesa_is_user_fbo(fb
)) {
2201 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2202 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2203 if (att
->Type
== GL_RENDERBUFFER
&&
2204 att
->Renderbuffer
== rb
) {
2205 /* Mark fb status as indeterminate to force re-validation */
2214 /** sentinal value, see below */
2215 #define NO_SAMPLES 1000
2218 _mesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
2219 GLenum internalFormat
, GLsizei width
,
2220 GLsizei height
, GLsizei samples
,
2221 GLsizei storageSamples
)
2223 const GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
2225 assert(baseFormat
!= 0);
2226 assert(width
>= 0 && width
<= (GLsizei
) ctx
->Const
.MaxRenderbufferSize
);
2227 assert(height
>= 0 && height
<= (GLsizei
) ctx
->Const
.MaxRenderbufferSize
);
2228 assert(samples
!= NO_SAMPLES
);
2230 assert(samples
> 0);
2231 assert(_mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
2232 internalFormat
, samples
,
2233 storageSamples
) == GL_NO_ERROR
);
2236 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2238 if (rb
->InternalFormat
== internalFormat
&&
2239 rb
->Width
== (GLuint
) width
&&
2240 rb
->Height
== (GLuint
) height
&&
2241 rb
->NumSamples
== samples
&&
2242 rb
->NumStorageSamples
== storageSamples
) {
2243 /* no change in allocation needed */
2247 /* These MUST get set by the AllocStorage func */
2248 rb
->Format
= MESA_FORMAT_NONE
;
2249 rb
->NumSamples
= samples
;
2250 rb
->NumStorageSamples
= storageSamples
;
2252 /* Now allocate the storage */
2253 assert(rb
->AllocStorage
);
2254 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
2255 /* No error - check/set fields now */
2256 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2257 assert(rb
->Width
== (GLuint
) width
);
2258 assert(rb
->Height
== (GLuint
) height
);
2259 rb
->InternalFormat
= internalFormat
;
2260 rb
->_BaseFormat
= baseFormat
;
2261 assert(rb
->_BaseFormat
!= 0);
2264 /* Probably ran out of memory - clear the fields */
2267 rb
->Format
= MESA_FORMAT_NONE
;
2268 rb
->InternalFormat
= GL_NONE
;
2269 rb
->_BaseFormat
= GL_NONE
;
2271 rb
->NumStorageSamples
= 0;
2274 /* Invalidate the framebuffers the renderbuffer is attached in. */
2275 if (rb
->AttachedAnytime
) {
2276 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
2281 * Helper function used by renderbuffer_storage_direct() and
2282 * renderbuffer_storage_target().
2283 * samples will be NO_SAMPLES if called by a non-multisample function.
2286 renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
2287 GLenum internalFormat
, GLsizei width
,
2288 GLsizei height
, GLsizei samples
, GLsizei storageSamples
,
2292 GLenum sample_count_error
;
2294 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
2295 if (baseFormat
== 0) {
2296 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
2297 func
, _mesa_enum_to_string(internalFormat
));
2301 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2302 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid width %d)", func
,
2307 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2308 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid height %d)", func
,
2313 if (samples
== NO_SAMPLES
) {
2314 /* NumSamples == 0 indicates non-multisampling */
2319 /* check the sample count;
2320 * note: driver may choose to use more samples than what's requested
2322 sample_count_error
= _mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
2323 internalFormat
, samples
, storageSamples
);
2325 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2327 * "If a negative number is provided where an argument of type sizei or
2328 * sizeiptr is specified, the error INVALID VALUE is generated."
2330 if (samples
< 0 || storageSamples
< 0) {
2331 sample_count_error
= GL_INVALID_VALUE
;
2334 if (sample_count_error
!= GL_NO_ERROR
) {
2335 _mesa_error(ctx
, sample_count_error
,
2336 "%s(samples=%d, storageSamples=%d)", func
, samples
,
2342 _mesa_renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
,
2347 * Helper function used by _mesa_NamedRenderbufferStorage*().
2348 * samples will be NO_SAMPLES if called by a non-multisample function.
2351 renderbuffer_storage_named(GLuint renderbuffer
, GLenum internalFormat
,
2352 GLsizei width
, GLsizei height
, GLsizei samples
,
2353 GLsizei storageSamples
, const char *func
)
2355 GET_CURRENT_CONTEXT(ctx
);
2357 if (MESA_VERBOSE
& VERBOSE_API
) {
2358 if (samples
== NO_SAMPLES
)
2359 _mesa_debug(ctx
, "%s(%u, %s, %d, %d)\n",
2361 _mesa_enum_to_string(internalFormat
),
2364 _mesa_debug(ctx
, "%s(%u, %s, %d, %d, %d)\n",
2366 _mesa_enum_to_string(internalFormat
),
2367 width
, height
, samples
);
2370 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2371 if (!rb
|| rb
== &DummyRenderbuffer
) {
2372 /* ID was reserved, but no real renderbuffer object made yet */
2373 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid renderbuffer %u)",
2374 func
, renderbuffer
);
2378 renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
,
2379 storageSamples
, func
);
2383 * Helper function used by _mesa_RenderbufferStorage() and
2384 * _mesa_RenderbufferStorageMultisample().
2385 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2388 renderbuffer_storage_target(GLenum target
, GLenum internalFormat
,
2389 GLsizei width
, GLsizei height
, GLsizei samples
,
2390 GLsizei storageSamples
, const char *func
)
2392 GET_CURRENT_CONTEXT(ctx
);
2394 if (MESA_VERBOSE
& VERBOSE_API
) {
2395 if (samples
== NO_SAMPLES
)
2396 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
2398 _mesa_enum_to_string(target
),
2399 _mesa_enum_to_string(internalFormat
),
2402 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
2404 _mesa_enum_to_string(target
),
2405 _mesa_enum_to_string(internalFormat
),
2406 width
, height
, samples
);
2409 if (target
!= GL_RENDERBUFFER_EXT
) {
2410 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
2414 if (!ctx
->CurrentRenderbuffer
) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no renderbuffer bound)",
2420 renderbuffer_storage(ctx
, ctx
->CurrentRenderbuffer
, internalFormat
, width
,
2421 height
, samples
, storageSamples
, func
);
2426 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
2428 struct gl_renderbuffer
*rb
;
2429 GET_CURRENT_CONTEXT(ctx
);
2431 if (!ctx
->Extensions
.OES_EGL_image
) {
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2433 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2437 if (target
!= GL_RENDERBUFFER
) {
2438 _mesa_error(ctx
, GL_INVALID_ENUM
,
2439 "EGLImageTargetRenderbufferStorageOES");
2443 rb
= ctx
->CurrentRenderbuffer
;
2445 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2446 "EGLImageTargetRenderbufferStorageOES");
2450 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2452 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
2457 * Helper function for _mesa_GetRenderbufferParameteriv() and
2458 * _mesa_GetFramebufferAttachmentParameteriv()
2459 * We have to be careful to respect the base format. For example, if a
2460 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2461 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2462 * we need to return zero.
2465 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
2467 if (_mesa_base_format_has_channel(baseFormat
, pname
))
2468 return _mesa_get_format_bits(format
, pname
);
2476 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
2477 GLsizei width
, GLsizei height
)
2479 /* GL_ARB_fbo says calling this function is equivalent to calling
2480 * glRenderbufferStorageMultisample() with samples=0. We pass in
2481 * a token value here just for error reporting purposes.
2483 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2484 NO_SAMPLES
, 0, "glRenderbufferStorage");
2489 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
2490 GLenum internalFormat
,
2491 GLsizei width
, GLsizei height
)
2493 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2495 "glRenderbufferStorageMultisample");
2500 _mesa_RenderbufferStorageMultisampleAdvancedAMD(
2501 GLenum target
, GLsizei samples
, GLsizei storageSamples
,
2502 GLenum internalFormat
, GLsizei width
, GLsizei height
)
2504 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2505 samples
, storageSamples
,
2506 "glRenderbufferStorageMultisampleAdvancedAMD");
2511 * OpenGL ES version of glRenderBufferStorage.
2514 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
2515 GLsizei width
, GLsizei height
)
2517 switch (internalFormat
) {
2519 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2520 /* choose a closest format */
2521 internalFormat
= GL_RGB5
;
2527 renderbuffer_storage_target(target
, internalFormat
, width
, height
, 0, 0,
2528 "glRenderbufferStorageEXT");
2532 _mesa_NamedRenderbufferStorage(GLuint renderbuffer
, GLenum internalformat
,
2533 GLsizei width
, GLsizei height
)
2535 /* GL_ARB_fbo says calling this function is equivalent to calling
2536 * glRenderbufferStorageMultisample() with samples=0. We pass in
2537 * a token value here just for error reporting purposes.
2539 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2540 NO_SAMPLES
, 0, "glNamedRenderbufferStorage");
2544 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer
, GLsizei samples
,
2545 GLenum internalformat
,
2546 GLsizei width
, GLsizei height
)
2548 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2550 "glNamedRenderbufferStorageMultisample");
2555 _mesa_NamedRenderbufferStorageMultisampleAdvancedAMD(
2556 GLuint renderbuffer
, GLsizei samples
, GLsizei storageSamples
,
2557 GLenum internalformat
, GLsizei width
, GLsizei height
)
2559 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2560 samples
, storageSamples
,
2561 "glNamedRenderbufferStorageMultisampleAdvancedAMD");
2566 get_render_buffer_parameteriv(struct gl_context
*ctx
,
2567 struct gl_renderbuffer
*rb
, GLenum pname
,
2568 GLint
*params
, const char *func
)
2570 /* No need to flush here since we're just quering state which is
2571 * not effected by rendering.
2575 case GL_RENDERBUFFER_WIDTH_EXT
:
2576 *params
= rb
->Width
;
2578 case GL_RENDERBUFFER_HEIGHT_EXT
:
2579 *params
= rb
->Height
;
2581 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
2582 *params
= rb
->InternalFormat
;
2584 case GL_RENDERBUFFER_RED_SIZE_EXT
:
2585 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
2586 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
2587 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
2588 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
2589 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
2590 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2592 case GL_RENDERBUFFER_SAMPLES
:
2593 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2594 || _mesa_is_gles3(ctx
)) {
2595 *params
= rb
->NumSamples
;
2599 case GL_RENDERBUFFER_STORAGE_SAMPLES_AMD
:
2600 if (ctx
->Extensions
.AMD_framebuffer_multisample_advanced
) {
2601 *params
= rb
->NumStorageSamples
;
2607 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname=%s)", func
,
2608 _mesa_enum_to_string(pname
));
2613 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2615 GET_CURRENT_CONTEXT(ctx
);
2617 if (target
!= GL_RENDERBUFFER_EXT
) {
2618 _mesa_error(ctx
, GL_INVALID_ENUM
,
2619 "glGetRenderbufferParameterivEXT(target)");
2623 if (!ctx
->CurrentRenderbuffer
) {
2624 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetRenderbufferParameterivEXT"
2625 "(no renderbuffer bound)");
2629 get_render_buffer_parameteriv(ctx
, ctx
->CurrentRenderbuffer
, pname
,
2630 params
, "glGetRenderbufferParameteriv");
2635 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer
, GLenum pname
,
2638 GET_CURRENT_CONTEXT(ctx
);
2640 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2641 if (!rb
|| rb
== &DummyRenderbuffer
) {
2642 /* ID was reserved, but no real renderbuffer object made yet */
2643 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetNamedRenderbufferParameteriv"
2644 "(invalid renderbuffer %i)", renderbuffer
);
2648 get_render_buffer_parameteriv(ctx
, rb
, pname
, params
,
2649 "glGetNamedRenderbufferParameteriv");
2653 GLboolean GLAPIENTRY
2654 _mesa_IsFramebuffer(GLuint framebuffer
)
2656 GET_CURRENT_CONTEXT(ctx
);
2657 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2659 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2660 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2668 * Check if any of the attachments of the given framebuffer are textures
2669 * (render to texture). Call ctx->Driver.RenderTexture() for such
2673 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2676 assert(ctx
->Driver
.RenderTexture
);
2678 if (_mesa_is_winsys_fbo(fb
))
2679 return; /* can't render to texture with winsys framebuffers */
2681 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2682 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2683 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2684 && driver_RenderTexture_is_safe(att
)) {
2685 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2692 * Examine all the framebuffer's attachments to see if any are textures.
2693 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2694 * notify the device driver that the texture image may have changed.
2697 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2699 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2700 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2703 if (ctx
->Driver
.FinishRenderTexture
) {
2705 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2706 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2707 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2708 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2709 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2717 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2719 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2720 GLboolean bindReadBuf
, bindDrawBuf
;
2721 GET_CURRENT_CONTEXT(ctx
);
2724 case GL_DRAW_FRAMEBUFFER_EXT
:
2725 bindDrawBuf
= GL_TRUE
;
2726 bindReadBuf
= GL_FALSE
;
2728 case GL_READ_FRAMEBUFFER_EXT
:
2729 bindDrawBuf
= GL_FALSE
;
2730 bindReadBuf
= GL_TRUE
;
2732 case GL_FRAMEBUFFER_EXT
:
2733 bindDrawBuf
= GL_TRUE
;
2734 bindReadBuf
= GL_TRUE
;
2737 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2742 /* Binding a user-created framebuffer object */
2743 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2744 if (newDrawFb
== &DummyFramebuffer
) {
2745 /* ID was reserved, but no real framebuffer object made yet */
2748 else if (!newDrawFb
&& !allow_user_names
) {
2749 /* All FBO IDs must be Gen'd */
2750 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2755 /* create new framebuffer object */
2756 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2758 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2761 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2763 newReadFb
= newDrawFb
;
2766 /* Binding the window system framebuffer (which was originally set
2767 * with MakeCurrent).
2769 newDrawFb
= ctx
->WinSysDrawBuffer
;
2770 newReadFb
= ctx
->WinSysReadBuffer
;
2773 _mesa_bind_framebuffers(ctx
,
2774 bindDrawBuf
? newDrawFb
: ctx
->DrawBuffer
,
2775 bindReadBuf
? newReadFb
: ctx
->ReadBuffer
);
2779 _mesa_bind_framebuffers(struct gl_context
*ctx
,
2780 struct gl_framebuffer
*newDrawFb
,
2781 struct gl_framebuffer
*newReadFb
)
2783 struct gl_framebuffer
*const oldDrawFb
= ctx
->DrawBuffer
;
2784 struct gl_framebuffer
*const oldReadFb
= ctx
->ReadBuffer
;
2785 const bool bindDrawBuf
= oldDrawFb
!= newDrawFb
;
2786 const bool bindReadBuf
= oldReadFb
!= newReadFb
;
2789 assert(newDrawFb
!= &DummyFramebuffer
);
2792 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2794 * We also check if we're beginning and/or ending render-to-texture.
2795 * When a framebuffer with texture attachments is unbound, call
2796 * ctx->Driver.FinishRenderTexture().
2797 * When a framebuffer with texture attachments is bound, call
2798 * ctx->Driver.RenderTexture().
2800 * Note that if the ReadBuffer has texture attachments we don't consider
2801 * that a render-to-texture case.
2804 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2806 /* check if old readbuffer was render-to-texture */
2807 check_end_texture_render(ctx
, oldReadFb
);
2809 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2813 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2814 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
2816 /* check if old framebuffer had any texture attachments */
2818 check_end_texture_render(ctx
, oldDrawFb
);
2820 /* check if newly bound framebuffer has any texture attachments */
2821 check_begin_texture_render(ctx
, newDrawFb
);
2823 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2826 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2827 /* The few classic drivers that actually hook this function really only
2828 * want to know if the draw framebuffer changed.
2830 ctx
->Driver
.BindFramebuffer(ctx
,
2831 bindDrawBuf
? GL_FRAMEBUFFER
: GL_READ_FRAMEBUFFER
,
2832 newDrawFb
, newReadFb
);
2837 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2839 GET_CURRENT_CONTEXT(ctx
);
2841 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2842 * point, but they allow the use of user-generated names.
2844 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2849 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2851 /* This function should not be in the dispatch table for core profile /
2852 * OpenGL 3.1, so execution should never get here in those cases -- no
2853 * need for an explicit test.
2855 bind_framebuffer(target
, framebuffer
, true);
2860 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2863 GET_CURRENT_CONTEXT(ctx
);
2866 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2870 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2872 for (i
= 0; i
< n
; i
++) {
2873 if (framebuffers
[i
] > 0) {
2874 struct gl_framebuffer
*fb
;
2875 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2877 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2879 /* check if deleting currently bound framebuffer object */
2880 if (fb
== ctx
->DrawBuffer
) {
2882 assert(fb
->RefCount
>= 2);
2883 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2885 if (fb
== ctx
->ReadBuffer
) {
2887 assert(fb
->RefCount
>= 2);
2888 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2891 /* remove from hash table immediately, to free the ID */
2892 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2894 if (fb
!= &DummyFramebuffer
) {
2895 /* But the object will not be freed until it's no longer
2896 * bound in any context.
2898 _mesa_reference_framebuffer(&fb
, NULL
);
2907 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2908 * It is not exposed to the rest of Mesa to encourage the use of
2909 * nameless buffers in driver internals.
2912 create_framebuffers(GLsizei n
, GLuint
*framebuffers
, bool dsa
)
2914 GET_CURRENT_CONTEXT(ctx
);
2917 struct gl_framebuffer
*fb
;
2919 const char *func
= dsa
? "glCreateFramebuffers" : "glGenFramebuffers";
2922 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
2929 _mesa_HashLockMutex(ctx
->Shared
->FrameBuffers
);
2931 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2933 for (i
= 0; i
< n
; i
++) {
2934 GLuint name
= first
+ i
;
2935 framebuffers
[i
] = name
;
2938 fb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffers
[i
]);
2940 _mesa_HashUnlockMutex(ctx
->Shared
->FrameBuffers
);
2941 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2946 fb
= &DummyFramebuffer
;
2948 _mesa_HashInsertLocked(ctx
->Shared
->FrameBuffers
, name
, fb
);
2951 _mesa_HashUnlockMutex(ctx
->Shared
->FrameBuffers
);
2956 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2958 create_framebuffers(n
, framebuffers
, false);
2963 _mesa_CreateFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2965 create_framebuffers(n
, framebuffers
, true);
2970 _mesa_check_framebuffer_status(struct gl_context
*ctx
,
2971 struct gl_framebuffer
*buffer
)
2973 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2975 if (_mesa_is_winsys_fbo(buffer
)) {
2976 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2977 if (buffer
!= &IncompleteFramebuffer
) {
2978 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2980 return GL_FRAMEBUFFER_UNDEFINED
;
2984 /* No need to flush here */
2986 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2987 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2990 return buffer
->_Status
;
2995 _mesa_CheckFramebufferStatus_no_error(GLenum target
)
2997 GET_CURRENT_CONTEXT(ctx
);
2999 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
3000 return _mesa_check_framebuffer_status(ctx
, fb
);
3005 _mesa_CheckFramebufferStatus(GLenum target
)
3007 struct gl_framebuffer
*fb
;
3008 GET_CURRENT_CONTEXT(ctx
);
3010 if (MESA_VERBOSE
& VERBOSE_API
)
3011 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
3012 _mesa_enum_to_string(target
));
3014 fb
= get_framebuffer_target(ctx
, target
);
3016 _mesa_error(ctx
, GL_INVALID_ENUM
,
3017 "glCheckFramebufferStatus(invalid target %s)",
3018 _mesa_enum_to_string(target
));
3022 return _mesa_check_framebuffer_status(ctx
, fb
);
3027 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer
, GLenum target
)
3029 struct gl_framebuffer
*fb
;
3030 GET_CURRENT_CONTEXT(ctx
);
3032 /* Validate the target (for conformance's sake) and grab a reference to the
3033 * default framebuffer in case framebuffer = 0.
3034 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
3035 * (30.10.2014, PDF page 336) says:
3036 * "If framebuffer is zero, then the status of the default read or
3037 * draw framebuffer (as determined by target) is returned."
3040 case GL_DRAW_FRAMEBUFFER
:
3041 case GL_FRAMEBUFFER
:
3042 fb
= ctx
->WinSysDrawBuffer
;
3044 case GL_READ_FRAMEBUFFER
:
3045 fb
= ctx
->WinSysReadBuffer
;
3048 _mesa_error(ctx
, GL_INVALID_ENUM
,
3049 "glCheckNamedFramebufferStatus(invalid target %s)",
3050 _mesa_enum_to_string(target
));
3055 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3056 "glCheckNamedFramebufferStatus");
3061 return _mesa_check_framebuffer_status(ctx
, fb
);
3066 * Replicate the src attachment point. Used by framebuffer_texture() when
3067 * the same texture is attached at GL_DEPTH_ATTACHMENT and
3068 * GL_STENCIL_ATTACHMENT.
3071 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
3072 gl_buffer_index dst
,
3073 gl_buffer_index src
)
3075 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
3076 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
3078 assert(src_att
->Texture
!= NULL
);
3079 assert(src_att
->Renderbuffer
!= NULL
);
3081 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
3082 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
3083 dst_att
->Type
= src_att
->Type
;
3084 dst_att
->Complete
= src_att
->Complete
;
3085 dst_att
->TextureLevel
= src_att
->TextureLevel
;
3086 dst_att
->CubeMapFace
= src_att
->CubeMapFace
;
3087 dst_att
->Zoffset
= src_att
->Zoffset
;
3088 dst_att
->Layered
= src_att
->Layered
;
3092 static struct gl_texture_object
*
3093 get_texture_for_framebuffer(struct gl_context
*ctx
, GLuint texture
)
3098 return _mesa_lookup_texture(ctx
, texture
);
3103 * Common code called by gl*FramebufferTexture*() to retrieve the correct
3104 * texture object pointer.
3106 * \param texObj where the pointer to the texture object is returned. Note
3107 * that a successful call may return texObj = NULL.
3109 * \return true if no errors, false if errors
3112 get_texture_for_framebuffer_err(struct gl_context
*ctx
, GLuint texture
,
3113 bool layered
, const char *caller
,
3114 struct gl_texture_object
**texObj
)
3116 *texObj
= NULL
; /* This will get returned if texture = 0. */
3121 *texObj
= _mesa_lookup_texture(ctx
, texture
);
3122 if (*texObj
== NULL
|| (*texObj
)->Target
== 0) {
3123 /* Can't render to a non-existent texture object.
3125 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
3126 * Managing Framebuffer Objects specifies a different error
3127 * depending upon the calling function (PDF pages 325-328).
3128 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
3129 * value, while the other commands throw invalid operation (where
3130 * layered = GL_FALSE).
3132 const GLenum error
= layered
? GL_INVALID_VALUE
:
3133 GL_INVALID_OPERATION
;
3134 _mesa_error(ctx
, error
,
3135 "%s(non-existent texture %u)", caller
, texture
);
3144 * Common code called by gl*FramebufferTexture() to verify the texture target
3145 * and decide whether or not the attachment should truly be considered
3148 * \param layered true if attachment should be considered layered, false if
3151 * \return true if no errors, false if errors
3154 check_layered_texture_target(struct gl_context
*ctx
, GLenum target
,
3155 const char *caller
, GLboolean
*layered
)
3161 case GL_TEXTURE_1D_ARRAY_EXT
:
3162 case GL_TEXTURE_2D_ARRAY_EXT
:
3163 case GL_TEXTURE_CUBE_MAP
:
3164 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3165 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3169 case GL_TEXTURE_RECTANGLE
:
3170 case GL_TEXTURE_2D_MULTISAMPLE
:
3171 /* These texture types are valid to pass to
3172 * glFramebufferTexture(), but since they aren't layered, it
3173 * is equivalent to calling glFramebufferTexture{1D,2D}().
3175 *layered
= GL_FALSE
;
3179 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3180 "%s(invalid texture target %s)", caller
,
3181 _mesa_enum_to_string(target
));
3187 * Common code called by gl*FramebufferTextureLayer() to verify the texture
3190 * \return true if no errors, false if errors
3193 check_texture_target(struct gl_context
*ctx
, GLenum target
,
3196 /* We're being called by glFramebufferTextureLayer().
3197 * The only legal texture types for that function are 3D,
3198 * cube-map, and 1D/2D/cube-map array textures.
3200 * We don't need to check for GL_ARB_texture_cube_map_array because the
3201 * application wouldn't have been able to create a texture with a
3202 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
3206 case GL_TEXTURE_1D_ARRAY
:
3207 case GL_TEXTURE_2D_ARRAY
:
3208 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3209 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3211 case GL_TEXTURE_CUBE_MAP
:
3212 /* We don't need to check the extension (GL_ARB_direct_state_access) or
3213 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
3214 * enabled in core profile. This can be called from
3215 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
3216 * so we do have to check the profile.
3218 return ctx
->API
== API_OPENGL_CORE
;
3221 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3222 "%s(invalid texture target %s)", caller
,
3223 _mesa_enum_to_string(target
));
3229 * Common code called by glFramebufferTexture*D() to verify the texture
3232 * \return true if no errors, false if errors
3235 check_textarget(struct gl_context
*ctx
, int dims
, GLenum target
,
3236 GLenum textarget
, const char *caller
)
3240 switch (textarget
) {
3244 case GL_TEXTURE_1D_ARRAY
:
3245 err
= dims
!= 1 || !ctx
->Extensions
.EXT_texture_array
;
3250 case GL_TEXTURE_2D_ARRAY
:
3251 err
= dims
!= 2 || !ctx
->Extensions
.EXT_texture_array
||
3252 (_mesa_is_gles(ctx
) && ctx
->Version
< 30);
3254 case GL_TEXTURE_2D_MULTISAMPLE
:
3255 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3257 !ctx
->Extensions
.ARB_texture_multisample
||
3258 (_mesa_is_gles(ctx
) && ctx
->Version
< 31);
3260 case GL_TEXTURE_RECTANGLE
:
3261 err
= dims
!= 2 || _mesa_is_gles(ctx
) ||
3262 !ctx
->Extensions
.NV_texture_rectangle
;
3264 case GL_TEXTURE_CUBE_MAP
:
3265 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3268 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3269 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3270 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3271 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3272 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3273 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3274 err
= dims
!= 2 || !ctx
->Extensions
.ARB_texture_cube_map
;
3280 _mesa_error(ctx
, GL_INVALID_ENUM
,
3281 "%s(unknown textarget 0x%x)", caller
, textarget
);
3286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3287 "%s(invalid textarget %s)",
3288 caller
, _mesa_enum_to_string(textarget
));
3292 /* Make sure textarget is consistent with the texture's type */
3293 err
= (target
== GL_TEXTURE_CUBE_MAP
) ?
3294 !_mesa_is_cube_face(textarget
): (target
!= textarget
);
3297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3298 "%s(mismatched texture target)", caller
);
3307 * Common code called by gl*FramebufferTextureLayer() and
3308 * glFramebufferTexture3D() to validate the layer.
3310 * \return true if no errors, false if errors
3313 check_layer(struct gl_context
*ctx
, GLenum target
, GLint layer
,
3316 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3319 * "An INVALID_VALUE error is generated if texture is non-zero
3320 * and layer is negative."
3323 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(layer %d < 0)", caller
, layer
);
3327 if (target
== GL_TEXTURE_3D
) {
3328 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
3329 if (layer
>= maxSize
) {
3330 _mesa_error(ctx
, GL_INVALID_VALUE
,
3331 "%s(invalid layer %u)", caller
, layer
);
3335 else if ((target
== GL_TEXTURE_1D_ARRAY
) ||
3336 (target
== GL_TEXTURE_2D_ARRAY
) ||
3337 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
3338 (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
3339 if (layer
>= ctx
->Const
.MaxArrayTextureLayers
) {
3340 _mesa_error(ctx
, GL_INVALID_VALUE
,
3341 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3346 else if (target
== GL_TEXTURE_CUBE_MAP
) {
3348 _mesa_error(ctx
, GL_INVALID_VALUE
,
3349 "%s(layer %u >= 6)", caller
, layer
);
3359 * Common code called by all gl*FramebufferTexture*() entry points to verify
3362 * \return true if no errors, false if errors
3365 check_level(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3366 GLenum target
, GLint level
, const char *caller
)
3368 /* Section 9.2.8 of the OpenGL 4.6 specification says:
3370 * "If texture refers to an immutable-format texture, level must be
3371 * greater than or equal to zero and smaller than the value of
3372 * TEXTURE_VIEW_NUM_LEVELS for texture."
3374 const int max_levels
= texObj
->Immutable
? texObj
->ImmutableLevels
:
3375 _mesa_max_texture_levels(ctx
, target
);
3377 if (level
< 0 || level
>= max_levels
) {
3378 _mesa_error(ctx
, GL_INVALID_VALUE
,
3379 "%s(invalid level %d)", caller
, level
);
3387 struct gl_renderbuffer_attachment
*
3388 _mesa_get_and_validate_attachment(struct gl_context
*ctx
,
3389 struct gl_framebuffer
*fb
,
3390 GLenum attachment
, const char *caller
)
3392 /* The window-system framebuffer object is immutable */
3393 if (_mesa_is_winsys_fbo(fb
)) {
3394 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(window-system framebuffer)",
3399 /* Not a hash lookup, so we can afford to get the attachment here. */
3400 bool is_color_attachment
;
3401 struct gl_renderbuffer_attachment
*att
=
3402 get_attachment(ctx
, fb
, attachment
, &is_color_attachment
);
3404 if (is_color_attachment
) {
3405 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3406 "%s(invalid color attachment %s)", caller
,
3407 _mesa_enum_to_string(attachment
));
3409 _mesa_error(ctx
, GL_INVALID_ENUM
,
3410 "%s(invalid attachment %s)", caller
,
3411 _mesa_enum_to_string(attachment
));
3421 _mesa_framebuffer_texture(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
3423 struct gl_renderbuffer_attachment
*att
,
3424 struct gl_texture_object
*texObj
, GLenum textarget
,
3425 GLint level
, GLuint layer
, GLboolean layered
)
3427 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3429 simple_mtx_lock(&fb
->Mutex
);
3431 if (attachment
== GL_DEPTH_ATTACHMENT
&&
3432 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
3433 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
3434 _mesa_tex_target_to_face(textarget
) ==
3435 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
3436 layer
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
3437 /* The texture object is already attached to the stencil attachment
3438 * point. Don't create a new renderbuffer; just reuse the stencil
3439 * attachment's. This is required to prevent a GL error in
3440 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3442 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
3444 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
3445 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
3446 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
3447 _mesa_tex_target_to_face(textarget
) ==
3448 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
3449 layer
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
3450 /* As above, but with depth and stencil transposed. */
3451 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
3454 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
3455 level
, layer
, layered
);
3457 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3458 /* Above we created a new renderbuffer and attached it to the
3459 * depth attachment point. Now attach it to the stencil attachment
3462 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3463 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
3468 /* Set the render-to-texture flag. We'll check this flag in
3469 * glTexImage() and friends to determine if we need to revalidate
3470 * any FBOs that might be rendering into this texture.
3471 * This flag never gets cleared since it's non-trivial to determine
3472 * when all FBOs might be done rendering to this texture. That's OK
3473 * though since it's uncommon to render to a texture then repeatedly
3474 * call glTexImage() to change images in the texture.
3476 texObj
->_RenderToTexture
= GL_TRUE
;
3479 remove_attachment(ctx
, att
);
3480 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3481 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3482 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
3486 invalidate_framebuffer(fb
);
3488 simple_mtx_unlock(&fb
->Mutex
);
3493 framebuffer_texture_with_dims_no_error(GLenum target
, GLenum attachment
,
3494 GLenum textarget
, GLuint texture
,
3495 GLint level
, GLint layer
)
3497 GET_CURRENT_CONTEXT(ctx
);
3499 /* Get the framebuffer object */
3500 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
3502 /* Get the texture object */
3503 struct gl_texture_object
*texObj
=
3504 get_texture_for_framebuffer(ctx
, texture
);
3506 struct gl_renderbuffer_attachment
*att
=
3507 get_attachment(ctx
, fb
, attachment
, NULL
);
3509 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3510 level
, layer
, GL_FALSE
);
3515 framebuffer_texture_with_dims(int dims
, GLenum target
,
3516 GLenum attachment
, GLenum textarget
,
3517 GLuint texture
, GLint level
, GLint layer
,
3520 GET_CURRENT_CONTEXT(ctx
);
3521 struct gl_framebuffer
*fb
;
3522 struct gl_texture_object
*texObj
;
3524 /* Get the framebuffer object */
3525 fb
= get_framebuffer_target(ctx
, target
);
3527 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
3528 _mesa_enum_to_string(target
));
3532 /* Get the texture object */
3533 if (!get_texture_for_framebuffer_err(ctx
, texture
, false, caller
, &texObj
))
3537 if (!check_textarget(ctx
, dims
, texObj
->Target
, textarget
, caller
))
3540 if ((dims
== 3) && !check_layer(ctx
, texObj
->Target
, layer
, caller
))
3543 if (!check_level(ctx
, texObj
, textarget
, level
, caller
))
3547 struct gl_renderbuffer_attachment
*att
=
3548 _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, caller
);
3552 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3553 level
, layer
, GL_FALSE
);
3558 _mesa_FramebufferTexture1D_no_error(GLenum target
, GLenum attachment
,
3559 GLenum textarget
, GLuint texture
,
3562 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3568 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
3569 GLenum textarget
, GLuint texture
, GLint level
)
3571 framebuffer_texture_with_dims(1, target
, attachment
, textarget
, texture
,
3572 level
, 0, "glFramebufferTexture1D");
3577 _mesa_FramebufferTexture2D_no_error(GLenum target
, GLenum attachment
,
3578 GLenum textarget
, GLuint texture
,
3581 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3587 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
3588 GLenum textarget
, GLuint texture
, GLint level
)
3590 framebuffer_texture_with_dims(2, target
, attachment
, textarget
, texture
,
3591 level
, 0, "glFramebufferTexture2D");
3596 _mesa_FramebufferTexture3D_no_error(GLenum target
, GLenum attachment
,
3597 GLenum textarget
, GLuint texture
,
3598 GLint level
, GLint layer
)
3600 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3601 texture
, level
, layer
);
3606 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
3607 GLenum textarget
, GLuint texture
,
3608 GLint level
, GLint layer
)
3610 framebuffer_texture_with_dims(3, target
, attachment
, textarget
, texture
,
3611 level
, layer
, "glFramebufferTexture3D");
3615 static ALWAYS_INLINE
void
3616 frame_buffer_texture(GLuint framebuffer
, GLenum target
,
3617 GLenum attachment
, GLuint texture
,
3618 GLint level
, GLint layer
, const char *func
,
3619 bool dsa
, bool no_error
, bool check_layered
)
3621 GET_CURRENT_CONTEXT(ctx
);
3622 GLboolean layered
= GL_FALSE
;
3624 if (!no_error
&& check_layered
) {
3625 if (!_mesa_has_geometry_shaders(ctx
)) {
3626 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3627 "unsupported function (%s) called", func
);
3632 /* Get the framebuffer object */
3633 struct gl_framebuffer
*fb
;
3636 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
3638 fb
= get_framebuffer_target(ctx
, target
);
3642 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
, func
);
3646 fb
= get_framebuffer_target(ctx
, target
);
3648 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)",
3649 func
, _mesa_enum_to_string(target
));
3655 /* Get the texture object and framebuffer attachment*/
3656 struct gl_renderbuffer_attachment
*att
;
3657 struct gl_texture_object
*texObj
;
3659 texObj
= get_texture_for_framebuffer(ctx
, texture
);
3660 att
= get_attachment(ctx
, fb
, attachment
, NULL
);
3662 if (!get_texture_for_framebuffer_err(ctx
, texture
, check_layered
, func
,
3666 att
= _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, func
);
3671 GLenum textarget
= 0;
3673 if (check_layered
) {
3674 /* We do this regardless of no_error because this sets layered */
3675 if (!check_layered_texture_target(ctx
, texObj
->Target
, func
,
3681 if (!check_layered
) {
3682 if (!check_texture_target(ctx
, texObj
->Target
, func
))
3685 if (!check_layer(ctx
, texObj
->Target
, layer
, func
))
3689 if (!check_level(ctx
, texObj
, texObj
->Target
, level
, func
))
3693 if (!check_layered
&& texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3694 assert(layer
>= 0 && layer
< 6);
3695 textarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ layer
;
3700 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3701 level
, layer
, layered
);
3705 _mesa_FramebufferTextureLayer_no_error(GLenum target
, GLenum attachment
,
3706 GLuint texture
, GLint level
,
3709 frame_buffer_texture(0, target
, attachment
, texture
, level
, layer
,
3710 "glFramebufferTextureLayer", false, true, false);
3715 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
3716 GLuint texture
, GLint level
, GLint layer
)
3718 frame_buffer_texture(0, target
, attachment
, texture
, level
, layer
,
3719 "glFramebufferTextureLayer", false, false, false);
3724 _mesa_NamedFramebufferTextureLayer_no_error(GLuint framebuffer
,
3726 GLuint texture
, GLint level
,
3729 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, layer
,
3730 "glNamedFramebufferTextureLayer", true, true, false);
3735 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer
, GLenum attachment
,
3736 GLuint texture
, GLint level
, GLint layer
)
3738 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, layer
,
3739 "glNamedFramebufferTextureLayer", true, false, false);
3744 _mesa_FramebufferTexture_no_error(GLenum target
, GLenum attachment
,
3745 GLuint texture
, GLint level
)
3747 frame_buffer_texture(0, target
, attachment
, texture
, level
, 0,
3748 "glFramebufferTexture", false, true, true);
3753 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
3754 GLuint texture
, GLint level
)
3756 frame_buffer_texture(0, target
, attachment
, texture
, level
, 0,
3757 "glFramebufferTexture", false, false, true);
3761 _mesa_NamedFramebufferTexture_no_error(GLuint framebuffer
, GLenum attachment
,
3762 GLuint texture
, GLint level
)
3764 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, 0,
3765 "glNamedFramebufferTexture", true, true, true);
3770 _mesa_NamedFramebufferTexture(GLuint framebuffer
, GLenum attachment
,
3771 GLuint texture
, GLint level
)
3773 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, 0,
3774 "glNamedFramebufferTexture", true, false, true);
3779 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
3780 struct gl_framebuffer
*fb
,
3782 struct gl_renderbuffer
*rb
)
3784 assert(!_mesa_is_winsys_fbo(fb
));
3786 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3788 assert(ctx
->Driver
.FramebufferRenderbuffer
);
3789 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
3791 /* Some subsequent GL commands may depend on the framebuffer's visual
3792 * after the binding is updated. Update visual info now.
3794 _mesa_update_framebuffer_visual(ctx
, fb
);
3797 static ALWAYS_INLINE
void
3798 framebuffer_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
3799 GLenum attachment
, GLenum renderbuffertarget
,
3800 GLuint renderbuffer
, const char *func
, bool no_error
)
3802 struct gl_renderbuffer_attachment
*att
;
3803 struct gl_renderbuffer
*rb
;
3804 bool is_color_attachment
;
3806 if (!no_error
&& renderbuffertarget
!= GL_RENDERBUFFER
) {
3807 _mesa_error(ctx
, GL_INVALID_ENUM
,
3808 "%s(renderbuffertarget is not GL_RENDERBUFFER)", func
);
3814 rb
= _mesa_lookup_renderbuffer_err(ctx
, renderbuffer
, func
);
3818 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
3821 /* remove renderbuffer attachment */
3826 if (_mesa_is_winsys_fbo(fb
)) {
3827 /* Can't attach new renderbuffers to a window system framebuffer */
3828 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3829 "%s(window-system framebuffer)", func
);
3833 att
= get_attachment(ctx
, fb
, attachment
, &is_color_attachment
);
3836 * From OpenGL 4.5 spec, section 9.2.7 "Attaching Renderbuffer Images
3837 * to a Framebuffer":
3839 * "An INVALID_OPERATION error is generated if attachment is
3840 * COLOR_- ATTACHMENTm where m is greater than or equal to the
3841 * value of MAX_COLOR_- ATTACHMENTS ."
3843 * If we are at this point, is because the attachment is not valid, so
3844 * if is_color_attachment is true, is because of the previous reason.
3846 if (is_color_attachment
) {
3847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3848 "%s(invalid color attachment %s)", func
,
3849 _mesa_enum_to_string(attachment
));
3851 _mesa_error(ctx
, GL_INVALID_ENUM
,
3852 "%s(invalid attachment %s)", func
,
3853 _mesa_enum_to_string(attachment
));
3859 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
3860 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
3861 /* make sure the renderbuffer is a depth/stencil format */
3862 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
3863 if (baseFormat
!= GL_DEPTH_STENCIL
) {
3864 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3865 "%s(renderbuffer is not DEPTH_STENCIL format)", func
);
3871 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
3875 framebuffer_renderbuffer_error(struct gl_context
*ctx
,
3876 struct gl_framebuffer
*fb
, GLenum attachment
,
3877 GLenum renderbuffertarget
,
3878 GLuint renderbuffer
, const char *func
)
3880 framebuffer_renderbuffer(ctx
, fb
, attachment
, renderbuffertarget
,
3881 renderbuffer
, func
, false);
3885 framebuffer_renderbuffer_no_error(struct gl_context
*ctx
,
3886 struct gl_framebuffer
*fb
, GLenum attachment
,
3887 GLenum renderbuffertarget
,
3888 GLuint renderbuffer
, const char *func
)
3890 framebuffer_renderbuffer(ctx
, fb
, attachment
, renderbuffertarget
,
3891 renderbuffer
, func
, true);
3895 _mesa_FramebufferRenderbuffer_no_error(GLenum target
, GLenum attachment
,
3896 GLenum renderbuffertarget
,
3897 GLuint renderbuffer
)
3899 GET_CURRENT_CONTEXT(ctx
);
3901 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
3902 framebuffer_renderbuffer_no_error(ctx
, fb
, attachment
, renderbuffertarget
,
3903 renderbuffer
, "glFramebufferRenderbuffer");
3907 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
3908 GLenum renderbuffertarget
,
3909 GLuint renderbuffer
)
3911 struct gl_framebuffer
*fb
;
3912 GET_CURRENT_CONTEXT(ctx
);
3914 fb
= get_framebuffer_target(ctx
, target
);
3916 _mesa_error(ctx
, GL_INVALID_ENUM
,
3917 "glFramebufferRenderbuffer(invalid target %s)",
3918 _mesa_enum_to_string(target
));
3922 framebuffer_renderbuffer_error(ctx
, fb
, attachment
, renderbuffertarget
,
3923 renderbuffer
, "glFramebufferRenderbuffer");
3927 _mesa_NamedFramebufferRenderbuffer_no_error(GLuint framebuffer
,
3929 GLenum renderbuffertarget
,
3930 GLuint renderbuffer
)
3932 GET_CURRENT_CONTEXT(ctx
);
3934 struct gl_framebuffer
*fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
3935 framebuffer_renderbuffer_no_error(ctx
, fb
, attachment
, renderbuffertarget
,
3937 "glNamedFramebufferRenderbuffer");
3941 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer
, GLenum attachment
,
3942 GLenum renderbuffertarget
,
3943 GLuint renderbuffer
)
3945 struct gl_framebuffer
*fb
;
3946 GET_CURRENT_CONTEXT(ctx
);
3948 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3949 "glNamedFramebufferRenderbuffer");
3953 framebuffer_renderbuffer_error(ctx
, fb
, attachment
, renderbuffertarget
,
3955 "glNamedFramebufferRenderbuffer");
3960 get_framebuffer_attachment_parameter(struct gl_context
*ctx
,
3961 struct gl_framebuffer
*buffer
,
3962 GLenum attachment
, GLenum pname
,
3963 GLint
*params
, const char *caller
)
3965 const struct gl_renderbuffer_attachment
*att
;
3966 bool is_color_attachment
= false;
3969 /* The error code for an attachment type of GL_NONE differs between APIs.
3971 * From the ES 2.0.25 specification, page 127:
3972 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3973 * querying any other pname will generate INVALID_ENUM."
3975 * From the OpenGL 3.0 specification, page 337, or identically,
3976 * the OpenGL ES 3.0.4 specification, page 240:
3978 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3979 * framebuffer is bound to target. In this case querying pname
3980 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3981 * queries will generate an INVALID_OPERATION error."
3983 err
= ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30 ?
3984 GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
3986 if (_mesa_is_winsys_fbo(buffer
)) {
3987 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3990 * "If the framebuffer currently bound to target is zero, then
3991 * INVALID_OPERATION is generated."
3993 * The EXT_framebuffer_object spec has the same wording, and the
3994 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3997 if ((!_mesa_is_desktop_gl(ctx
) ||
3998 !ctx
->Extensions
.ARB_framebuffer_object
)
3999 && !_mesa_is_gles3(ctx
)) {
4000 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4001 "%s(window-system framebuffer)", caller
);
4005 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
4006 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
4007 _mesa_error(ctx
, GL_INVALID_ENUM
,
4008 "%s(invalid attachment %s)", caller
,
4009 _mesa_enum_to_string(attachment
));
4013 /* The specs are not clear about how to handle
4014 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
4015 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
4018 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
4019 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
4021 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
) {
4022 _mesa_error(ctx
, GL_INVALID_ENUM
,
4023 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
4024 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
4025 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller
);
4029 /* the default / window-system FBO */
4030 att
= get_fb0_attachment(ctx
, buffer
, attachment
);
4033 /* user-created framebuffer FBO */
4034 att
= get_attachment(ctx
, buffer
, attachment
, &is_color_attachment
);
4039 * From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries":
4041 * "An INVALID_OPERATION error is generated if a framebuffer object
4042 * is bound to target and attachment is COLOR_ATTACHMENTm where m is
4043 * greater than or equal to the value of MAX_COLOR_ATTACHMENTS."
4045 * If we are at this point, is because the attachment is not valid, so
4046 * if is_color_attachment is true, is because of the previous reason.
4048 if (is_color_attachment
) {
4049 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid color attachment %s)",
4050 caller
, _mesa_enum_to_string(attachment
));
4052 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", caller
,
4053 _mesa_enum_to_string(attachment
));
4058 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
4059 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
4060 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
4061 /* This behavior is first specified in OpenGL 4.4 specification.
4063 * From the OpenGL 4.4 spec page 275:
4064 * "This query cannot be performed for a combined depth+stencil
4065 * attachment, since it does not have a single format."
4067 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4068 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
4069 " is invalid for depth+stencil attachment)", caller
);
4072 /* the depth and stencil attachments must point to the same buffer */
4073 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
, NULL
);
4074 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
, NULL
);
4075 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
4076 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4077 "%s(DEPTH/STENCIL attachments differ)", caller
);
4082 /* No need to flush here */
4085 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
4086 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
4088 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
4089 * either no framebuffer is bound to target; or the default framebuffer
4090 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
4091 * stencil bits, respectively, is zero."
4093 * Note that we don't need explicit checks on DEPTH and STENCIL, because
4094 * on the case the spec is pointing, att->Type is already NONE, so we
4095 * just need to check att->Type.
4097 *params
= (_mesa_is_winsys_fbo(buffer
) && att
->Type
!= GL_NONE
) ?
4098 GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
4100 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
4101 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
4102 *params
= att
->Renderbuffer
->Name
;
4104 else if (att
->Type
== GL_TEXTURE
) {
4105 *params
= att
->Texture
->Name
;
4108 assert(att
->Type
== GL_NONE
);
4109 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
4112 goto invalid_pname_enum
;
4116 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
4117 if (att
->Type
== GL_TEXTURE
) {
4118 *params
= att
->TextureLevel
;
4120 else if (att
->Type
== GL_NONE
) {
4121 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4122 _mesa_enum_to_string(pname
));
4125 goto invalid_pname_enum
;
4128 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
4129 if (att
->Type
== GL_TEXTURE
) {
4130 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
4131 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
4137 else if (att
->Type
== GL_NONE
) {
4138 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4139 _mesa_enum_to_string(pname
));
4142 goto invalid_pname_enum
;
4145 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
4146 if (ctx
->API
== API_OPENGLES
) {
4147 goto invalid_pname_enum
;
4148 } else if (att
->Type
== GL_NONE
) {
4149 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4150 _mesa_enum_to_string(pname
));
4151 } else if (att
->Type
== GL_TEXTURE
) {
4152 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
4153 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
4154 *params
= att
->Zoffset
;
4161 goto invalid_pname_enum
;
4164 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
4165 if ((!_mesa_is_desktop_gl(ctx
) ||
4166 !ctx
->Extensions
.ARB_framebuffer_object
)
4167 && !_mesa_is_gles3(ctx
)) {
4168 goto invalid_pname_enum
;
4170 else if (att
->Type
== GL_NONE
) {
4171 if (_mesa_is_winsys_fbo(buffer
) &&
4172 (attachment
== GL_DEPTH
|| attachment
== GL_STENCIL
)) {
4173 *params
= GL_LINEAR
;
4175 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4176 _mesa_enum_to_string(pname
));
4180 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
4182 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
4185 /* According to ARB_framebuffer_sRGB, we should return LINEAR
4186 * if the sRGB conversion is unsupported. */
4187 *params
= GL_LINEAR
;
4191 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
4192 if ((ctx
->API
!= API_OPENGL_COMPAT
||
4193 !ctx
->Extensions
.ARB_framebuffer_object
)
4194 && ctx
->API
!= API_OPENGL_CORE
4195 && !_mesa_is_gles3(ctx
)) {
4196 goto invalid_pname_enum
;
4198 else if (att
->Type
== GL_NONE
) {
4199 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4200 _mesa_enum_to_string(pname
));
4203 mesa_format format
= att
->Renderbuffer
->Format
;
4205 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
4208 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
4209 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
4210 * generate an INVALID_OPERATION error.
4212 if (_mesa_is_gles3(ctx
) &&
4213 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
4214 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4216 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
4217 "GL_DEPTH_STENCIL_ATTACHMENT)", caller
);
4221 if (format
== MESA_FORMAT_S_UINT8
) {
4225 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
4226 /* depends on the attachment parameter */
4227 if (attachment
== GL_STENCIL_ATTACHMENT
) {
4235 *params
= _mesa_get_format_datatype(format
);
4239 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
4240 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
4241 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
4242 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
4243 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
4244 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
4245 if ((!_mesa_is_desktop_gl(ctx
) ||
4246 !ctx
->Extensions
.ARB_framebuffer_object
)
4247 && !_mesa_is_gles3(ctx
)) {
4248 goto invalid_pname_enum
;
4250 else if (att
->Texture
) {
4251 const struct gl_texture_image
*texImage
=
4252 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
4255 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
4256 texImage
->TexFormat
);
4262 else if (att
->Renderbuffer
) {
4263 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
4264 att
->Renderbuffer
->Format
);
4267 assert(att
->Type
== GL_NONE
);
4268 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4269 _mesa_enum_to_string(pname
));
4272 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
4273 if (!_mesa_has_geometry_shaders(ctx
)) {
4274 goto invalid_pname_enum
;
4275 } else if (att
->Type
== GL_TEXTURE
) {
4276 *params
= att
->Layered
;
4277 } else if (att
->Type
== GL_NONE
) {
4278 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4279 _mesa_enum_to_string(pname
));
4281 goto invalid_pname_enum
;
4285 goto invalid_pname_enum
;
4291 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname %s)", caller
,
4292 _mesa_enum_to_string(pname
));
4298 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
4299 GLenum pname
, GLint
*params
)
4301 GET_CURRENT_CONTEXT(ctx
);
4302 struct gl_framebuffer
*buffer
;
4304 buffer
= get_framebuffer_target(ctx
, target
);
4306 _mesa_error(ctx
, GL_INVALID_ENUM
,
4307 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
4308 _mesa_enum_to_string(target
));
4312 get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
4314 "glGetFramebufferAttachmentParameteriv");
4319 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer
,
4321 GLenum pname
, GLint
*params
)
4323 GET_CURRENT_CONTEXT(ctx
);
4324 struct gl_framebuffer
*buffer
;
4327 buffer
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4328 "glGetNamedFramebufferAttachmentParameteriv");
4334 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4335 * 4.5 core spec (30.10.2014, PDF page 314):
4336 * "If framebuffer is zero, then the default draw framebuffer is
4339 buffer
= ctx
->WinSysDrawBuffer
;
4342 get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
4344 "glGetNamedFramebufferAttachmentParameteriv");
4349 _mesa_NamedFramebufferParameteri(GLuint framebuffer
, GLenum pname
,
4352 GET_CURRENT_CONTEXT(ctx
);
4353 struct gl_framebuffer
*fb
= NULL
;
4355 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
4356 !ctx
->Extensions
.ARB_sample_locations
) {
4357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4358 "glNamedFramebufferParameteri("
4359 "neither ARB_framebuffer_no_attachments nor "
4360 "ARB_sample_locations is available)");
4365 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4366 "glNamedFramebufferParameteri");
4368 fb
= ctx
->WinSysDrawBuffer
;
4372 framebuffer_parameteri(ctx
, fb
, pname
, param
,
4373 "glNamedFramebufferParameteriv");
4379 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer
, GLenum pname
,
4382 GET_CURRENT_CONTEXT(ctx
);
4383 struct gl_framebuffer
*fb
;
4385 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
4386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4387 "glNamedFramebufferParameteriv("
4388 "neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
4394 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4395 "glGetNamedFramebufferParameteriv");
4397 fb
= ctx
->WinSysDrawBuffer
;
4400 get_framebuffer_parameteriv(ctx
, fb
, pname
, param
,
4401 "glGetNamedFramebufferParameteriv");
4407 invalidate_framebuffer_storage(struct gl_context
*ctx
,
4408 struct gl_framebuffer
*fb
,
4409 GLsizei numAttachments
,
4410 const GLenum
*attachments
, GLint x
, GLint y
,
4411 GLsizei width
, GLsizei height
, const char *name
)
4415 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4416 * Spec (2.2.2015, PDF page 522) says:
4417 * "An INVALID_VALUE error is generated if numAttachments, width, or
4418 * height is negative."
4420 if (numAttachments
< 0) {
4421 _mesa_error(ctx
, GL_INVALID_VALUE
,
4422 "%s(numAttachments < 0)", name
);
4427 _mesa_error(ctx
, GL_INVALID_VALUE
,
4428 "%s(width < 0)", name
);
4433 _mesa_error(ctx
, GL_INVALID_VALUE
,
4434 "%s(height < 0)", name
);
4438 /* The GL_ARB_invalidate_subdata spec says:
4440 * "If an attachment is specified that does not exist in the
4441 * framebuffer bound to <target>, it is ignored."
4445 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4446 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4447 * INVALID_OPERATION is generated."
4449 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4450 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4451 * set of retrictions).
4453 for (i
= 0; i
< numAttachments
; i
++) {
4454 if (_mesa_is_winsys_fbo(fb
)) {
4455 switch (attachments
[i
]) {
4461 /* Accumulation buffers and auxilary buffers were removed in
4462 * OpenGL 3.1, and they never existed in OpenGL ES.
4464 if (ctx
->API
!= API_OPENGL_COMPAT
)
4474 case GL_FRONT_RIGHT
:
4475 if (!_mesa_is_desktop_gl(ctx
))
4482 switch (attachments
[i
]) {
4483 case GL_DEPTH_ATTACHMENT
:
4484 case GL_STENCIL_ATTACHMENT
:
4486 case GL_DEPTH_STENCIL_ATTACHMENT
:
4487 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4488 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4489 * extension does not make this attachment point valid on ES 2.0.
4491 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
4494 case GL_COLOR_ATTACHMENT0
:
4495 case GL_COLOR_ATTACHMENT1
:
4496 case GL_COLOR_ATTACHMENT2
:
4497 case GL_COLOR_ATTACHMENT3
:
4498 case GL_COLOR_ATTACHMENT4
:
4499 case GL_COLOR_ATTACHMENT5
:
4500 case GL_COLOR_ATTACHMENT6
:
4501 case GL_COLOR_ATTACHMENT7
:
4502 case GL_COLOR_ATTACHMENT8
:
4503 case GL_COLOR_ATTACHMENT9
:
4504 case GL_COLOR_ATTACHMENT10
:
4505 case GL_COLOR_ATTACHMENT11
:
4506 case GL_COLOR_ATTACHMENT12
:
4507 case GL_COLOR_ATTACHMENT13
:
4508 case GL_COLOR_ATTACHMENT14
:
4509 case GL_COLOR_ATTACHMENT15
: {
4510 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
4511 if (k
>= ctx
->Const
.MaxColorAttachments
) {
4512 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4513 "%s(attachment >= max. color attachments)", name
);
4524 /* We don't actually do anything for this yet. Just return after
4525 * validating the parameters and generating the required errors.
4530 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", name
,
4531 _mesa_enum_to_string(attachments
[i
]));
4537 _mesa_InvalidateSubFramebuffer_no_error(GLenum target
, GLsizei numAttachments
,
4538 const GLenum
*attachments
, GLint x
,
4539 GLint y
, GLsizei width
, GLsizei height
)
4546 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
4547 const GLenum
*attachments
, GLint x
, GLint y
,
4548 GLsizei width
, GLsizei height
)
4550 struct gl_framebuffer
*fb
;
4551 GET_CURRENT_CONTEXT(ctx
);
4553 fb
= get_framebuffer_target(ctx
, target
);
4555 _mesa_error(ctx
, GL_INVALID_ENUM
,
4556 "glInvalidateSubFramebuffer(invalid target %s)",
4557 _mesa_enum_to_string(target
));
4561 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4562 x
, y
, width
, height
,
4563 "glInvalidateSubFramebuffer");
4568 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer
,
4569 GLsizei numAttachments
,
4570 const GLenum
*attachments
,
4572 GLsizei width
, GLsizei height
)
4574 struct gl_framebuffer
*fb
;
4575 GET_CURRENT_CONTEXT(ctx
);
4577 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4578 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4579 * default draw framebuffer is affected."
4582 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4583 "glInvalidateNamedFramebufferSubData");
4588 fb
= ctx
->WinSysDrawBuffer
;
4590 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4591 x
, y
, width
, height
,
4592 "glInvalidateNamedFramebufferSubData");
4597 _mesa_InvalidateFramebuffer_no_error(GLenum target
, GLsizei numAttachments
,
4598 const GLenum
*attachments
)
4605 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
4606 const GLenum
*attachments
)
4608 struct gl_framebuffer
*fb
;
4609 GET_CURRENT_CONTEXT(ctx
);
4611 fb
= get_framebuffer_target(ctx
, target
);
4613 _mesa_error(ctx
, GL_INVALID_ENUM
,
4614 "glInvalidateFramebuffer(invalid target %s)",
4615 _mesa_enum_to_string(target
));
4619 /* The GL_ARB_invalidate_subdata spec says:
4623 * void InvalidateFramebuffer(enum target,
4624 * sizei numAttachments,
4625 * const enum *attachments);
4627 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4628 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4629 * <MAX_VIEWPORT_DIMS[1]> respectively."
4631 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4633 ctx
->Const
.MaxViewportWidth
,
4634 ctx
->Const
.MaxViewportHeight
,
4635 "glInvalidateFramebuffer");
4640 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer
,
4641 GLsizei numAttachments
,
4642 const GLenum
*attachments
)
4644 struct gl_framebuffer
*fb
;
4645 GET_CURRENT_CONTEXT(ctx
);
4647 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4648 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4649 * default draw framebuffer is affected."
4652 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4653 "glInvalidateNamedFramebufferData");
4658 fb
= ctx
->WinSysDrawBuffer
;
4660 /* The GL_ARB_invalidate_subdata spec says:
4664 * void InvalidateFramebuffer(enum target,
4665 * sizei numAttachments,
4666 * const enum *attachments);
4668 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4669 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4670 * <MAX_VIEWPORT_DIMS[1]> respectively."
4672 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4674 ctx
->Const
.MaxViewportWidth
,
4675 ctx
->Const
.MaxViewportHeight
,
4676 "glInvalidateNamedFramebufferData");
4681 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
4682 const GLenum
*attachments
)
4684 struct gl_framebuffer
*fb
;
4687 GET_CURRENT_CONTEXT(ctx
);
4689 fb
= get_framebuffer_target(ctx
, target
);
4691 _mesa_error(ctx
, GL_INVALID_ENUM
,
4692 "glDiscardFramebufferEXT(target %s)",
4693 _mesa_enum_to_string(target
));
4697 if (numAttachments
< 0) {
4698 _mesa_error(ctx
, GL_INVALID_VALUE
,
4699 "glDiscardFramebufferEXT(numAttachments < 0)");
4703 for (i
= 0; i
< numAttachments
; i
++) {
4704 switch (attachments
[i
]) {
4708 if (_mesa_is_user_fbo(fb
))
4711 case GL_COLOR_ATTACHMENT0
:
4712 case GL_DEPTH_ATTACHMENT
:
4713 case GL_STENCIL_ATTACHMENT
:
4714 if (_mesa_is_winsys_fbo(fb
))
4722 if (ctx
->Driver
.DiscardFramebuffer
)
4723 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
4728 _mesa_error(ctx
, GL_INVALID_ENUM
,
4729 "glDiscardFramebufferEXT(attachment %s)",
4730 _mesa_enum_to_string(attachments
[i
]));
4734 sample_locations(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
4735 GLuint start
, GLsizei count
, const GLfloat
*v
, bool no_error
,
4741 if (!ctx
->Extensions
.ARB_sample_locations
) {
4742 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4744 "(ARB_sample_locations not available)", name
);
4748 if (start
+ count
> MAX_SAMPLE_LOCATION_TABLE_SIZE
) {
4749 _mesa_error(ctx
, GL_INVALID_VALUE
,
4750 "%s(start+size > sample location table size)", name
);
4755 if (!fb
->SampleLocationTable
) {
4756 size_t size
= MAX_SAMPLE_LOCATION_TABLE_SIZE
* 2 * sizeof(GLfloat
);
4757 fb
->SampleLocationTable
= malloc(size
);
4758 if (!fb
->SampleLocationTable
) {
4759 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
4760 "Cannot allocate sample location table");
4763 for (i
= 0; i
< MAX_SAMPLE_LOCATION_TABLE_SIZE
* 2; i
++)
4764 fb
->SampleLocationTable
[i
] = 0.5f
;
4767 for (i
= 0; i
< count
* 2; i
++) {
4768 /* The ARB_sample_locations spec says:
4770 * Sample locations outside of [0,1] result in undefined
4773 * To simplify driver implementations, we choose to clamp to
4774 * [0,1] and change NaN into 0.5.
4776 if (isnan(v
[i
]) || v
[i
] < 0.0f
|| v
[i
] > 1.0f
) {
4777 static GLuint msg_id
= 0;
4778 static const char* msg
= "Invalid sample location specified";
4779 _mesa_debug_get_id(&msg_id
);
4781 _mesa_log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_UNDEFINED
,
4782 msg_id
, MESA_DEBUG_SEVERITY_HIGH
, strlen(msg
), msg
);
4786 fb
->SampleLocationTable
[start
* 2 + i
] = 0.5f
;
4788 fb
->SampleLocationTable
[start
* 2 + i
] = CLAMP(v
[i
], 0.0f
, 1.0f
);
4791 if (fb
== ctx
->DrawBuffer
)
4792 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
4796 _mesa_FramebufferSampleLocationsfvARB(GLenum target
, GLuint start
,
4797 GLsizei count
, const GLfloat
*v
)
4799 struct gl_framebuffer
*fb
;
4801 GET_CURRENT_CONTEXT(ctx
);
4803 fb
= get_framebuffer_target(ctx
, target
);
4805 _mesa_error(ctx
, GL_INVALID_ENUM
,
4806 "glFramebufferSampleLocationsfvARB(target %s)",
4807 _mesa_enum_to_string(target
));
4811 sample_locations(ctx
, fb
, start
, count
, v
, false,
4812 "glFramebufferSampleLocationsfvARB");
4816 _mesa_NamedFramebufferSampleLocationsfvARB(GLuint framebuffer
, GLuint start
,
4817 GLsizei count
, const GLfloat
*v
)
4819 struct gl_framebuffer
*fb
;
4821 GET_CURRENT_CONTEXT(ctx
);
4824 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4825 "glNamedFramebufferSampleLocationsfvARB");
4830 fb
= ctx
->WinSysDrawBuffer
;
4832 sample_locations(ctx
, fb
, start
, count
, v
, false,
4833 "glNamedFramebufferSampleLocationsfvARB");
4837 _mesa_FramebufferSampleLocationsfvARB_no_error(GLenum target
, GLuint start
,
4838 GLsizei count
, const GLfloat
*v
)
4840 GET_CURRENT_CONTEXT(ctx
);
4841 sample_locations(ctx
, get_framebuffer_target(ctx
, target
), start
,
4842 count
, v
, true, "glFramebufferSampleLocationsfvARB");
4846 _mesa_NamedFramebufferSampleLocationsfvARB_no_error(GLuint framebuffer
,
4847 GLuint start
, GLsizei count
,
4850 GET_CURRENT_CONTEXT(ctx
);
4851 sample_locations(ctx
, _mesa_lookup_framebuffer(ctx
, framebuffer
), start
,
4852 count
, v
, true, "glNamedFramebufferSampleLocationsfvARB");
4856 _mesa_EvaluateDepthValuesARB(void)
4858 GET_CURRENT_CONTEXT(ctx
);
4860 if (!ctx
->Extensions
.ARB_sample_locations
) {
4861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4862 "EvaluateDepthValuesARB not supported (neither "
4863 "ARB_sample_locations nor NV_sample_locations is available)");
4867 if (ctx
->Driver
.EvaluateDepthValues
)
4868 ctx
->Driver
.EvaluateDepthValues(ctx
);