2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * GL_EXT/ARB_framebuffer_object extensions
41 #include "framebuffer.h"
42 #include "glformats.h"
45 #include "multisample.h"
47 #include "renderbuffer.h"
56 * None of the GL_EXT_framebuffer_object functions are compiled into
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
68 static struct gl_framebuffer DummyFramebuffer
;
69 static struct gl_renderbuffer DummyRenderbuffer
;
71 /* We bind this framebuffer when applications pass a NULL
72 * drawable/surface in make current. */
73 static struct gl_framebuffer IncompleteFramebuffer
;
77 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
83 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
90 _mesa_init_fbobjects(struct gl_context
*ctx
)
92 mtx_init(&DummyFramebuffer
.Mutex
, mtx_plain
);
93 mtx_init(&DummyRenderbuffer
.Mutex
, mtx_plain
);
94 mtx_init(&IncompleteFramebuffer
.Mutex
, mtx_plain
);
95 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
96 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
97 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 struct gl_framebuffer
*
101 _mesa_get_incomplete_framebuffer(void)
103 return &IncompleteFramebuffer
;
107 * Helper routine for getting a gl_renderbuffer.
109 struct gl_renderbuffer
*
110 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
112 struct gl_renderbuffer
*rb
;
117 rb
= (struct gl_renderbuffer
*)
118 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
124 * Helper routine for getting a gl_framebuffer.
126 struct gl_framebuffer
*
127 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
129 struct gl_framebuffer
*fb
;
134 fb
= (struct gl_framebuffer
*)
135 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
141 * Mark the given framebuffer as invalid. This will force the
142 * test for framebuffer completeness to be done before the framebuffer
146 invalidate_framebuffer(struct gl_framebuffer
*fb
)
148 fb
->_Status
= 0; /* "indeterminate" */
153 * Return the gl_framebuffer object which corresponds to the given
154 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
155 * Check support for GL_EXT_framebuffer_blit to determine if certain
157 * \return gl_framebuffer pointer or NULL if target is illegal
159 static struct gl_framebuffer
*
160 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
162 bool have_fb_blit
= _mesa_is_gles3(ctx
) || _mesa_is_desktop_gl(ctx
);
164 case GL_DRAW_FRAMEBUFFER
:
165 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
166 case GL_READ_FRAMEBUFFER
:
167 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
168 case GL_FRAMEBUFFER_EXT
:
169 return ctx
->DrawBuffer
;
177 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
178 * gl_renderbuffer_attachment object.
179 * This function is only used for user-created FB objects, not the
180 * default / window-system FB object.
181 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
182 * the depth buffer attachment point.
184 static struct gl_renderbuffer_attachment
*
185 get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
190 assert(_mesa_is_user_fbo(fb
));
192 switch (attachment
) {
193 case GL_COLOR_ATTACHMENT0_EXT
:
194 case GL_COLOR_ATTACHMENT1_EXT
:
195 case GL_COLOR_ATTACHMENT2_EXT
:
196 case GL_COLOR_ATTACHMENT3_EXT
:
197 case GL_COLOR_ATTACHMENT4_EXT
:
198 case GL_COLOR_ATTACHMENT5_EXT
:
199 case GL_COLOR_ATTACHMENT6_EXT
:
200 case GL_COLOR_ATTACHMENT7_EXT
:
201 case GL_COLOR_ATTACHMENT8_EXT
:
202 case GL_COLOR_ATTACHMENT9_EXT
:
203 case GL_COLOR_ATTACHMENT10_EXT
:
204 case GL_COLOR_ATTACHMENT11_EXT
:
205 case GL_COLOR_ATTACHMENT12_EXT
:
206 case GL_COLOR_ATTACHMENT13_EXT
:
207 case GL_COLOR_ATTACHMENT14_EXT
:
208 case GL_COLOR_ATTACHMENT15_EXT
:
209 /* Only OpenGL ES 1.x forbids color attachments other than
210 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
213 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
214 if (i
>= ctx
->Const
.MaxColorAttachments
215 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
218 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
219 case GL_DEPTH_STENCIL_ATTACHMENT
:
220 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
223 case GL_DEPTH_ATTACHMENT_EXT
:
224 return &fb
->Attachment
[BUFFER_DEPTH
];
225 case GL_STENCIL_ATTACHMENT_EXT
:
226 return &fb
->Attachment
[BUFFER_STENCIL
];
234 * As above, but only used for getting attachments of the default /
235 * window-system framebuffer (not user-created framebuffer objects).
237 static struct gl_renderbuffer_attachment
*
238 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
241 assert(_mesa_is_winsys_fbo(fb
));
243 if (_mesa_is_gles3(ctx
)) {
244 assert(attachment
== GL_BACK
||
245 attachment
== GL_DEPTH
||
246 attachment
== GL_STENCIL
);
247 switch (attachment
) {
249 /* Since there is no stereo rendering in ES 3.0, only return the
252 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
253 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
254 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
256 return &fb
->Attachment
[BUFFER_DEPTH
];
258 return &fb
->Attachment
[BUFFER_STENCIL
];
262 switch (attachment
) {
264 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
266 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
268 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
270 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
272 if (fb
->Visual
.numAuxBuffers
== 1) {
273 return &fb
->Attachment
[BUFFER_AUX0
];
277 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
279 * "If the default framebuffer is bound to target, then attachment must
280 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
281 * identifying a color buffer; DEPTH, identifying the depth buffer; or
282 * STENCIL, identifying the stencil buffer."
284 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
285 * language. However, revision #33 of the ARB_framebuffer_object spec
288 * "If the default framebuffer is bound to <target>, then <attachment>
289 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
290 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
291 * depth buffer, or the stencil buffer, and <pname> may be
292 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
293 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
295 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
296 * from glext.h, so shipping apps should not use those values.
298 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
299 * support queries of the window system FBO.
302 return &fb
->Attachment
[BUFFER_DEPTH
];
304 return &fb
->Attachment
[BUFFER_STENCIL
];
313 * Remove any texture or renderbuffer attached to the given attachment
314 * point. Update reference counts, etc.
317 remove_attachment(struct gl_context
*ctx
,
318 struct gl_renderbuffer_attachment
*att
)
320 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
322 /* tell driver that we're done rendering to this texture. */
323 if (rb
&& rb
->NeedsFinishRenderTexture
)
324 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
326 if (att
->Type
== GL_TEXTURE
) {
327 ASSERT(att
->Texture
);
328 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
329 ASSERT(!att
->Texture
);
331 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
332 ASSERT(!att
->Texture
);
333 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
334 ASSERT(!att
->Renderbuffer
);
337 att
->Complete
= GL_TRUE
;
341 * Verify a couple error conditions that will lead to an incomplete FBO and
342 * may cause problems for the driver's RenderTexture path.
345 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment
*att
)
347 const struct gl_texture_image
*const texImage
=
348 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
350 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
353 if ((texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
354 && att
->Zoffset
>= texImage
->Height
)
355 || (texImage
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
356 && att
->Zoffset
>= texImage
->Depth
))
363 * Create a renderbuffer which will be set up by the driver to wrap the
364 * texture image slice.
366 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
367 * to share most of their framebuffer rendering code between winsys,
368 * renderbuffer, and texture attachments.
370 * The allocated renderbuffer uses a non-zero Name so that drivers can check
371 * it for determining vertical orientation, but we use ~0 to make it fairly
372 * unambiguous with actual user (non-texture) renderbuffers.
375 _mesa_update_texture_renderbuffer(struct gl_context
*ctx
,
376 struct gl_framebuffer
*fb
,
377 struct gl_renderbuffer_attachment
*att
)
379 struct gl_texture_image
*texImage
;
380 struct gl_renderbuffer
*rb
;
382 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
384 rb
= att
->Renderbuffer
;
386 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, ~0);
388 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
391 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
393 /* This can't get called on a texture renderbuffer, so set it to NULL
394 * for clarity compared to user renderbuffers.
396 rb
->AllocStorage
= NULL
;
398 rb
->NeedsFinishRenderTexture
= ctx
->Driver
.FinishRenderTexture
!= NULL
;
404 rb
->_BaseFormat
= texImage
->_BaseFormat
;
405 rb
->Format
= texImage
->TexFormat
;
406 rb
->InternalFormat
= texImage
->InternalFormat
;
407 rb
->Width
= texImage
->Width2
;
408 rb
->Height
= texImage
->Height2
;
409 rb
->Depth
= texImage
->Depth2
;
410 rb
->NumSamples
= texImage
->NumSamples
;
411 rb
->TexImage
= texImage
;
413 if (driver_RenderTexture_is_safe(att
))
414 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
418 * Bind a texture object to an attachment point.
419 * The previous binding, if any, will be removed first.
422 set_texture_attachment(struct gl_context
*ctx
,
423 struct gl_framebuffer
*fb
,
424 struct gl_renderbuffer_attachment
*att
,
425 struct gl_texture_object
*texObj
,
426 GLenum texTarget
, GLuint level
, GLuint zoffset
,
429 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
431 if (rb
&& rb
->NeedsFinishRenderTexture
)
432 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
434 if (att
->Texture
== texObj
) {
435 /* re-attaching same texture */
436 ASSERT(att
->Type
== GL_TEXTURE
);
440 remove_attachment(ctx
, att
);
441 att
->Type
= GL_TEXTURE
;
442 assert(!att
->Texture
);
443 _mesa_reference_texobj(&att
->Texture
, texObj
);
445 invalidate_framebuffer(fb
);
447 /* always update these fields */
448 att
->TextureLevel
= level
;
449 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
450 att
->Zoffset
= zoffset
;
451 att
->Layered
= layered
;
452 att
->Complete
= GL_FALSE
;
454 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
459 * Bind a renderbuffer to an attachment point.
460 * The previous binding, if any, will be removed first.
463 set_renderbuffer_attachment(struct gl_context
*ctx
,
464 struct gl_renderbuffer_attachment
*att
,
465 struct gl_renderbuffer
*rb
)
467 /* XXX check if re-doing same attachment, exit early */
468 remove_attachment(ctx
, att
);
469 att
->Type
= GL_RENDERBUFFER_EXT
;
470 att
->Texture
= NULL
; /* just to be safe */
471 att
->Complete
= GL_FALSE
;
472 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
477 * Fallback for ctx->Driver.FramebufferRenderbuffer()
478 * Attach a renderbuffer object to a framebuffer object.
481 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
482 struct gl_framebuffer
*fb
,
483 GLenum attachment
, struct gl_renderbuffer
*rb
)
485 struct gl_renderbuffer_attachment
*att
;
487 mtx_lock(&fb
->Mutex
);
489 att
= get_attachment(ctx
, fb
, attachment
);
492 set_renderbuffer_attachment(ctx
, att
, rb
);
493 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
494 /* do stencil attachment here (depth already done above) */
495 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
497 set_renderbuffer_attachment(ctx
, att
, rb
);
499 rb
->AttachedAnytime
= GL_TRUE
;
502 remove_attachment(ctx
, att
);
503 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
504 /* detach stencil (depth was detached above) */
505 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
507 remove_attachment(ctx
, att
);
511 invalidate_framebuffer(fb
);
513 mtx_unlock(&fb
->Mutex
);
518 * Fallback for ctx->Driver.ValidateFramebuffer()
519 * Check if the renderbuffer's formats are supported by the software
521 * Drivers should probably override this.
524 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
527 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
528 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
530 switch (rb
->_BaseFormat
) {
532 case GL_LUMINANCE_ALPHA
:
537 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
541 switch (rb
->Format
) {
542 /* XXX This list is likely incomplete. */
543 case MESA_FORMAT_R9G9B9E5_FLOAT
:
544 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
547 /* render buffer format is supported by software rendering */
556 * Return true if the framebuffer has a combined depth/stencil
557 * renderbuffer attached.
560 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
562 const struct gl_renderbuffer_attachment
*depth
=
563 &fb
->Attachment
[BUFFER_DEPTH
];
564 const struct gl_renderbuffer_attachment
*stencil
=
565 &fb
->Attachment
[BUFFER_STENCIL
];
567 if (depth
->Type
== stencil
->Type
) {
568 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
569 depth
->Renderbuffer
== stencil
->Renderbuffer
)
572 if (depth
->Type
== GL_TEXTURE
&&
573 depth
->Texture
== stencil
->Texture
)
585 att_incomplete(const char *msg
)
587 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
588 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
597 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
599 static GLuint msg_id
;
601 _mesa_gl_debug(ctx
, &msg_id
,
602 MESA_DEBUG_SOURCE_API
,
603 MESA_DEBUG_TYPE_OTHER
,
604 MESA_DEBUG_SEVERITY_MEDIUM
,
605 "FBO incomplete: %s [%d]\n", msg
, index
);
607 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
608 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
614 * Is the given base format a legal format for a color renderbuffer?
617 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
619 switch (baseFormat
) {
624 case GL_LUMINANCE_ALPHA
:
627 return ctx
->API
== API_OPENGL_COMPAT
&&
628 ctx
->Extensions
.ARB_framebuffer_object
;
631 return ctx
->Extensions
.ARB_texture_rg
;
639 * Is the given base format a legal format for a color renderbuffer?
642 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
643 GLenum internalFormat
)
645 const GLenum baseFormat
=
646 _mesa_get_format_base_format(format
);
649 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
650 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
654 /* Reject additional cases for GLES */
655 switch (internalFormat
) {
675 if (format
== MESA_FORMAT_B10G10R10A2_UNORM
&&
676 internalFormat
!= GL_RGB10_A2
) {
685 * Is the given base format a legal format for a depth/stencil renderbuffer?
688 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
690 switch (baseFormat
) {
691 case GL_DEPTH_COMPONENT
:
692 case GL_DEPTH_STENCIL_EXT
:
701 * Test if an attachment point is complete and update its Complete field.
702 * \param format if GL_COLOR, this is a color attachment point,
703 * if GL_DEPTH, this is a depth component attachment point,
704 * if GL_STENCIL, this is a stencil component attachment point.
707 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
708 struct gl_renderbuffer_attachment
*att
)
710 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
712 /* assume complete */
713 att
->Complete
= GL_TRUE
;
715 /* Look for reasons why the attachment might be incomplete */
716 if (att
->Type
== GL_TEXTURE
) {
717 const struct gl_texture_object
*texObj
= att
->Texture
;
718 struct gl_texture_image
*texImage
;
722 att_incomplete("no texobj");
723 att
->Complete
= GL_FALSE
;
727 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
729 att_incomplete("no teximage");
730 att
->Complete
= GL_FALSE
;
733 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
734 att_incomplete("teximage width/height=0");
735 att
->Complete
= GL_FALSE
;
739 switch (texObj
->Target
) {
741 if (att
->Zoffset
>= texImage
->Depth
) {
742 att_incomplete("bad z offset");
743 att
->Complete
= GL_FALSE
;
747 case GL_TEXTURE_1D_ARRAY
:
748 if (att
->Zoffset
>= texImage
->Height
) {
749 att_incomplete("bad 1D-array layer");
750 att
->Complete
= GL_FALSE
;
754 case GL_TEXTURE_2D_ARRAY
:
755 if (att
->Zoffset
>= texImage
->Depth
) {
756 att_incomplete("bad 2D-array layer");
757 att
->Complete
= GL_FALSE
;
761 case GL_TEXTURE_CUBE_MAP_ARRAY
:
762 if (att
->Zoffset
>= texImage
->Depth
) {
763 att_incomplete("bad cube-array layer");
764 att
->Complete
= GL_FALSE
;
770 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
772 if (format
== GL_COLOR
) {
773 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
774 att_incomplete("bad format");
775 att
->Complete
= GL_FALSE
;
778 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
779 att_incomplete("compressed internalformat");
780 att
->Complete
= GL_FALSE
;
784 else if (format
== GL_DEPTH
) {
785 if (baseFormat
== GL_DEPTH_COMPONENT
) {
788 else if (ctx
->Extensions
.ARB_depth_texture
&&
789 baseFormat
== GL_DEPTH_STENCIL
) {
793 att
->Complete
= GL_FALSE
;
794 att_incomplete("bad depth format");
799 ASSERT(format
== GL_STENCIL
);
800 if (ctx
->Extensions
.ARB_depth_texture
&&
801 baseFormat
== GL_DEPTH_STENCIL
) {
805 /* no such thing as stencil-only textures */
806 att_incomplete("illegal stencil texture");
807 att
->Complete
= GL_FALSE
;
812 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
813 const GLenum baseFormat
=
814 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
816 ASSERT(att
->Renderbuffer
);
817 if (!att
->Renderbuffer
->InternalFormat
||
818 att
->Renderbuffer
->Width
< 1 ||
819 att
->Renderbuffer
->Height
< 1) {
820 att_incomplete("0x0 renderbuffer");
821 att
->Complete
= GL_FALSE
;
824 if (format
== GL_COLOR
) {
825 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
826 att_incomplete("bad renderbuffer color format");
827 att
->Complete
= GL_FALSE
;
831 else if (format
== GL_DEPTH
) {
832 if (baseFormat
== GL_DEPTH_COMPONENT
) {
835 else if (baseFormat
== GL_DEPTH_STENCIL
) {
839 att_incomplete("bad renderbuffer depth format");
840 att
->Complete
= GL_FALSE
;
845 assert(format
== GL_STENCIL
);
846 if (baseFormat
== GL_STENCIL_INDEX
||
847 baseFormat
== GL_DEPTH_STENCIL
) {
851 att
->Complete
= GL_FALSE
;
852 att_incomplete("bad renderbuffer stencil format");
858 ASSERT(att
->Type
== GL_NONE
);
866 * Test if the given framebuffer object is complete and update its
867 * Status field with the results.
868 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
869 * driver to make hardware-specific validation/completeness checks.
870 * Also update the framebuffer's Width and Height fields if the
871 * framebuffer is complete.
874 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
875 struct gl_framebuffer
*fb
)
878 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
879 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
880 GLint numSamples
= -1;
881 GLint fixedSampleLocations
= -1;
884 /* Covers max_layer_count, is_layered, and layer_tex_target */
885 bool layer_info_valid
= false;
886 GLuint max_layer_count
= 0, att_layer_count
;
887 bool is_layered
= false;
888 GLenum layer_tex_target
= 0;
889 bool has_depth_attachment
= false;
890 bool has_stencil_attachment
= false;
892 assert(_mesa_is_user_fbo(fb
));
894 /* we're changing framebuffer fields here */
895 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
900 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
901 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
903 /* Start at -2 to more easily loop over all attachment points.
908 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
909 struct gl_renderbuffer_attachment
*att
;
911 mesa_format attFormat
;
912 GLenum att_tex_target
= GL_NONE
;
915 * XXX for ARB_fbo, only check color buffers that are named by
916 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
919 /* check for attachment completeness
922 att
= &fb
->Attachment
[BUFFER_DEPTH
];
923 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
924 if (!att
->Complete
) {
925 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
926 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
928 } else if (att
->Type
!= GL_NONE
) {
929 has_depth_attachment
= true;
933 att
= &fb
->Attachment
[BUFFER_STENCIL
];
934 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
935 if (!att
->Complete
) {
936 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
937 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
939 } else if (att
->Type
!= GL_NONE
) {
940 has_stencil_attachment
= true;
944 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
945 test_attachment_completeness(ctx
, GL_COLOR
, att
);
946 if (!att
->Complete
) {
947 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
948 fbo_incomplete(ctx
, "color attachment incomplete", i
);
953 /* get width, height, format of the renderbuffer/texture
955 if (att
->Type
== GL_TEXTURE
) {
956 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
957 att_tex_target
= att
->Texture
->Target
;
958 minWidth
= MIN2(minWidth
, texImg
->Width
);
959 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
960 minHeight
= MIN2(minHeight
, texImg
->Height
);
961 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
962 f
= texImg
->_BaseFormat
;
963 attFormat
= texImg
->TexFormat
;
966 if (!is_format_color_renderable(ctx
, attFormat
,
967 texImg
->InternalFormat
) &&
968 !is_legal_depth_format(ctx
, f
)) {
969 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
970 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
975 numSamples
= texImg
->NumSamples
;
976 else if (numSamples
!= texImg
->NumSamples
) {
977 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
978 fbo_incomplete(ctx
, "inconsistent sample count", -1);
982 if (fixedSampleLocations
< 0)
983 fixedSampleLocations
= texImg
->FixedSampleLocations
;
984 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
985 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
986 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
990 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
991 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
992 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
993 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
994 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
995 f
= att
->Renderbuffer
->InternalFormat
;
996 attFormat
= att
->Renderbuffer
->Format
;
1000 numSamples
= att
->Renderbuffer
->NumSamples
;
1001 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1002 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1003 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1007 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1008 if (fixedSampleLocations
< 0)
1009 fixedSampleLocations
= GL_TRUE
;
1010 else if (fixedSampleLocations
!= GL_TRUE
) {
1011 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1012 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1017 assert(att
->Type
== GL_NONE
);
1021 /* check if integer color */
1022 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
1024 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1026 GLenum type
= _mesa_get_format_datatype(attFormat
);
1028 fb
->_AllColorBuffersFixedPoint
=
1029 fb
->_AllColorBuffersFixedPoint
&&
1030 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1032 fb
->_HasSNormOrFloatColorBuffer
=
1033 fb
->_HasSNormOrFloatColorBuffer
||
1034 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1037 /* Error-check width, height, format */
1038 if (numImages
== 1) {
1045 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1046 /* check that width, height, format are same */
1047 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1048 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1049 fbo_incomplete(ctx
, "width or height mismatch", -1);
1052 /* check that all color buffers are the same format */
1053 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1054 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1055 fbo_incomplete(ctx
, "format mismatch", -1);
1061 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1063 if (att
->Type
== GL_RENDERBUFFER
&&
1064 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1065 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1066 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1070 /* Check that layered rendering is consistent. */
1072 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1073 att_layer_count
= 6;
1074 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1075 att_layer_count
= att
->Renderbuffer
->Height
;
1077 att_layer_count
= att
->Renderbuffer
->Depth
;
1079 att_layer_count
= 0;
1081 if (!layer_info_valid
) {
1082 is_layered
= att
->Layered
;
1083 max_layer_count
= att_layer_count
;
1084 layer_tex_target
= att_tex_target
;
1085 layer_info_valid
= true;
1086 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1087 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1088 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1090 } else if (is_layered
!= att
->Layered
) {
1091 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1093 "framebuffer attachment layer mode is inconsistent",
1096 } else if (att_layer_count
> max_layer_count
) {
1097 max_layer_count
= att_layer_count
;
1101 fb
->MaxNumLayers
= max_layer_count
;
1103 if (numImages
== 0) {
1104 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1105 fbo_incomplete(ctx
, "no attachments", -1);
1109 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1110 /* Check that all DrawBuffers are present */
1111 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1112 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1113 const struct gl_renderbuffer_attachment
*att
1114 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
1116 if (att
->Type
== GL_NONE
) {
1117 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1118 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1124 /* Check that the ReadBuffer is present */
1125 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1126 const struct gl_renderbuffer_attachment
*att
1127 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
1129 if (att
->Type
== GL_NONE
) {
1130 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1131 fbo_incomplete(ctx
, "missing readbuffer", -1);
1137 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1139 * "Depth and stencil attachments, if present, are the same image."
1141 * This restriction is not present in the OpenGL ES2 spec.
1143 if (_mesa_is_gles3(ctx
) &&
1144 has_stencil_attachment
&& has_depth_attachment
&&
1145 !_mesa_has_depthstencil_combined(fb
)) {
1146 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1147 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1151 /* Provisionally set status = COMPLETE ... */
1152 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1154 /* ... but the driver may say the FB is incomplete.
1155 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1158 if (ctx
->Driver
.ValidateFramebuffer
) {
1159 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1160 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1161 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1165 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
1167 * Note that if ARB_framebuffer_object is supported and the attached
1168 * renderbuffers/textures are different sizes, the framebuffer
1169 * width/height will be set to the smallest width/height.
1171 fb
->Width
= minWidth
;
1172 fb
->Height
= minHeight
;
1174 /* finally, update the visual info for the framebuffer */
1175 _mesa_update_framebuffer_visual(ctx
, fb
);
1180 GLboolean GLAPIENTRY
1181 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1183 GET_CURRENT_CONTEXT(ctx
);
1184 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1186 struct gl_renderbuffer
*rb
=
1187 _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1188 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1196 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1198 struct gl_renderbuffer
*newRb
;
1199 GET_CURRENT_CONTEXT(ctx
);
1201 if (target
!= GL_RENDERBUFFER_EXT
) {
1202 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1206 /* No need to flush here since the render buffer binding has no
1207 * effect on rendering state.
1211 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1212 if (newRb
== &DummyRenderbuffer
) {
1213 /* ID was reserved, but no real renderbuffer object made yet */
1216 else if (!newRb
&& !allow_user_names
) {
1217 /* All RB IDs must be Gen'd */
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1223 /* create new renderbuffer object */
1224 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
1229 ASSERT(newRb
->AllocStorage
);
1230 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1231 newRb
->RefCount
= 1; /* referenced by hash table */
1238 ASSERT(newRb
!= &DummyRenderbuffer
);
1240 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1244 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1246 GET_CURRENT_CONTEXT(ctx
);
1248 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1249 * entry point, but they allow the use of user-generated names.
1251 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1255 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1257 /* This function should not be in the dispatch table for core profile /
1258 * OpenGL 3.1, so execution should never get here in those cases -- no
1259 * need for an explicit test.
1261 bind_renderbuffer(target
, renderbuffer
, true);
1266 * Remove the specified renderbuffer or texture from any attachment point in
1270 * \c true if the renderbuffer was detached from an attachment point. \c
1274 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1275 struct gl_framebuffer
*fb
,
1279 bool progress
= false;
1281 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1282 if (fb
->Attachment
[i
].Texture
== att
1283 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1284 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1289 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1290 * Completeness," of the OpenGL 3.1 spec says:
1292 * "Performing any of the following actions may change whether the
1293 * framebuffer is considered complete or incomplete:
1297 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1298 * containing an image that is attached to a framebuffer object
1299 * that is bound to the framebuffer."
1302 invalidate_framebuffer(fb
);
1309 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1312 GET_CURRENT_CONTEXT(ctx
);
1314 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1316 for (i
= 0; i
< n
; i
++) {
1317 if (renderbuffers
[i
] > 0) {
1318 struct gl_renderbuffer
*rb
;
1319 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1321 /* check if deleting currently bound renderbuffer object */
1322 if (rb
== ctx
->CurrentRenderbuffer
) {
1324 ASSERT(rb
->RefCount
>= 2);
1325 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1328 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1329 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1330 * of the OpenGL 3.1 spec says:
1332 * "If a renderbuffer object is deleted while its image is
1333 * attached to one or more attachment points in the currently
1334 * bound framebuffer, then it is as if FramebufferRenderbuffer
1335 * had been called, with a renderbuffer of 0, for each
1336 * attachment point to which this image was attached in the
1337 * currently bound framebuffer. In other words, this
1338 * renderbuffer image is first detached from all attachment
1339 * points in the currently bound framebuffer. Note that the
1340 * renderbuffer image is specifically not detached from any
1341 * non-bound framebuffers. Detaching the image from any
1342 * non-bound framebuffers is the responsibility of the
1345 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1346 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1348 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1349 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1350 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1353 /* Remove from hash table immediately, to free the ID.
1354 * But the object will not be freed until it's no longer
1355 * referenced anywhere else.
1357 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1359 if (rb
!= &DummyRenderbuffer
) {
1360 /* no longer referenced by hash table */
1361 _mesa_reference_renderbuffer(&rb
, NULL
);
1370 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1372 GET_CURRENT_CONTEXT(ctx
);
1377 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1384 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1386 for (i
= 0; i
< n
; i
++) {
1387 GLuint name
= first
+ i
;
1388 renderbuffers
[i
] = name
;
1389 /* insert dummy placeholder into hash table */
1390 mtx_lock(&ctx
->Shared
->Mutex
);
1391 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1392 mtx_unlock(&ctx
->Shared
->Mutex
);
1398 * Given an internal format token for a render buffer, return the
1399 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1400 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1401 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1403 * This is similar to _mesa_base_tex_format() but the set of valid
1404 * internal formats is different.
1406 * Note that even if a format is determined to be legal here, validation
1407 * of the FBO may fail if the format is not supported by the driver/GPU.
1409 * \param internalFormat as passed to glRenderbufferStorage()
1410 * \return the base internal format, or 0 if internalFormat is illegal
1413 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1416 * Notes: some formats such as alpha, luminance, etc. were added
1417 * with GL_ARB_framebuffer_object.
1419 switch (internalFormat
) {
1425 return (ctx
->API
== API_OPENGL_COMPAT
&&
1426 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1430 case GL_LUMINANCE12
:
1431 case GL_LUMINANCE16
:
1432 return (ctx
->API
== API_OPENGL_COMPAT
&&
1433 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1434 case GL_LUMINANCE_ALPHA
:
1435 case GL_LUMINANCE4_ALPHA4
:
1436 case GL_LUMINANCE6_ALPHA2
:
1437 case GL_LUMINANCE8_ALPHA8
:
1438 case GL_LUMINANCE12_ALPHA4
:
1439 case GL_LUMINANCE12_ALPHA12
:
1440 case GL_LUMINANCE16_ALPHA16
:
1441 return (ctx
->API
== API_OPENGL_COMPAT
&&
1442 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1446 case GL_INTENSITY12
:
1447 case GL_INTENSITY16
:
1448 return (ctx
->API
== API_OPENGL_COMPAT
&&
1449 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1453 if (_mesa_is_gles3(ctx
))
1462 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1464 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1470 if (_mesa_is_gles3(ctx
))
1476 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1478 case GL_SRGB8_ALPHA8_EXT
:
1479 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1480 case GL_STENCIL_INDEX
:
1481 case GL_STENCIL_INDEX1_EXT
:
1482 case GL_STENCIL_INDEX4_EXT
:
1483 case GL_STENCIL_INDEX16_EXT
:
1484 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1485 * OpenGL ES, but Mesa does not currently support them.
1487 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1488 case GL_STENCIL_INDEX8_EXT
:
1489 return GL_STENCIL_INDEX
;
1490 case GL_DEPTH_COMPONENT
:
1491 case GL_DEPTH_COMPONENT32
:
1492 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1493 case GL_DEPTH_COMPONENT16
:
1494 case GL_DEPTH_COMPONENT24
:
1495 return GL_DEPTH_COMPONENT
;
1496 case GL_DEPTH_STENCIL
:
1497 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1498 case GL_DEPTH24_STENCIL8
:
1499 return GL_DEPTH_STENCIL
;
1500 case GL_DEPTH_COMPONENT32F
:
1501 return ctx
->Version
>= 30
1502 || (ctx
->API
== API_OPENGL_COMPAT
&&
1503 ctx
->Extensions
.ARB_depth_buffer_float
)
1504 ? GL_DEPTH_COMPONENT
: 0;
1505 case GL_DEPTH32F_STENCIL8
:
1506 return ctx
->Version
>= 30
1507 || (ctx
->API
== API_OPENGL_COMPAT
&&
1508 ctx
->Extensions
.ARB_depth_buffer_float
)
1509 ? GL_DEPTH_STENCIL
: 0;
1512 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1515 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1519 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1522 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1524 /* signed normalized texture formats */
1528 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1533 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1537 case GL_RGB16_SNORM
:
1538 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1541 case GL_RGBA8_SNORM
:
1542 case GL_RGBA16_SNORM
:
1543 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1545 case GL_ALPHA_SNORM
:
1546 case GL_ALPHA8_SNORM
:
1547 case GL_ALPHA16_SNORM
:
1548 return ctx
->API
== API_OPENGL_COMPAT
&&
1549 ctx
->Extensions
.EXT_texture_snorm
&&
1550 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1551 case GL_LUMINANCE_SNORM
:
1552 case GL_LUMINANCE8_SNORM
:
1553 case GL_LUMINANCE16_SNORM
:
1554 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1556 case GL_LUMINANCE_ALPHA_SNORM
:
1557 case GL_LUMINANCE8_ALPHA8_SNORM
:
1558 case GL_LUMINANCE16_ALPHA16_SNORM
:
1559 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1560 ? GL_LUMINANCE_ALPHA
: 0;
1561 case GL_INTENSITY_SNORM
:
1562 case GL_INTENSITY8_SNORM
:
1563 case GL_INTENSITY16_SNORM
:
1564 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1569 return ((_mesa_is_desktop_gl(ctx
) &&
1570 ctx
->Extensions
.ARB_texture_rg
&&
1571 ctx
->Extensions
.ARB_texture_float
) ||
1572 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1576 return ((_mesa_is_desktop_gl(ctx
) &&
1577 ctx
->Extensions
.ARB_texture_rg
&&
1578 ctx
->Extensions
.ARB_texture_float
) ||
1579 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1583 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1587 return ((_mesa_is_desktop_gl(ctx
) &&
1588 ctx
->Extensions
.ARB_texture_float
) ||
1589 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1591 case GL_ALPHA16F_ARB
:
1592 case GL_ALPHA32F_ARB
:
1593 return ctx
->API
== API_OPENGL_COMPAT
&&
1594 ctx
->Extensions
.ARB_texture_float
&&
1595 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1596 case GL_LUMINANCE16F_ARB
:
1597 case GL_LUMINANCE32F_ARB
:
1598 return ctx
->API
== API_OPENGL_COMPAT
&&
1599 ctx
->Extensions
.ARB_texture_float
&&
1600 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1601 case GL_LUMINANCE_ALPHA16F_ARB
:
1602 case GL_LUMINANCE_ALPHA32F_ARB
:
1603 return ctx
->API
== API_OPENGL_COMPAT
&&
1604 ctx
->Extensions
.ARB_texture_float
&&
1605 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1606 case GL_INTENSITY16F_ARB
:
1607 case GL_INTENSITY32F_ARB
:
1608 return ctx
->API
== API_OPENGL_COMPAT
&&
1609 ctx
->Extensions
.ARB_texture_float
&&
1610 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1611 case GL_R11F_G11F_B10F
:
1612 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1613 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1616 case GL_RGBA8UI_EXT
:
1617 case GL_RGBA16UI_EXT
:
1618 case GL_RGBA32UI_EXT
:
1620 case GL_RGBA16I_EXT
:
1621 case GL_RGBA32I_EXT
:
1622 return ctx
->Version
>= 30
1623 || (_mesa_is_desktop_gl(ctx
) &&
1624 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1627 case GL_RGB16UI_EXT
:
1628 case GL_RGB32UI_EXT
:
1632 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1640 return ctx
->Version
>= 30
1641 || (_mesa_is_desktop_gl(ctx
) &&
1642 ctx
->Extensions
.ARB_texture_rg
&&
1643 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1651 return ctx
->Version
>= 30
1652 || (_mesa_is_desktop_gl(ctx
) &&
1653 ctx
->Extensions
.ARB_texture_rg
&&
1654 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1656 case GL_INTENSITY8I_EXT
:
1657 case GL_INTENSITY8UI_EXT
:
1658 case GL_INTENSITY16I_EXT
:
1659 case GL_INTENSITY16UI_EXT
:
1660 case GL_INTENSITY32I_EXT
:
1661 case GL_INTENSITY32UI_EXT
:
1662 return ctx
->API
== API_OPENGL_COMPAT
&&
1663 ctx
->Extensions
.EXT_texture_integer
&&
1664 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1666 case GL_LUMINANCE8I_EXT
:
1667 case GL_LUMINANCE8UI_EXT
:
1668 case GL_LUMINANCE16I_EXT
:
1669 case GL_LUMINANCE16UI_EXT
:
1670 case GL_LUMINANCE32I_EXT
:
1671 case GL_LUMINANCE32UI_EXT
:
1672 return ctx
->API
== API_OPENGL_COMPAT
&&
1673 ctx
->Extensions
.EXT_texture_integer
&&
1674 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1676 case GL_LUMINANCE_ALPHA8I_EXT
:
1677 case GL_LUMINANCE_ALPHA8UI_EXT
:
1678 case GL_LUMINANCE_ALPHA16I_EXT
:
1679 case GL_LUMINANCE_ALPHA16UI_EXT
:
1680 case GL_LUMINANCE_ALPHA32I_EXT
:
1681 case GL_LUMINANCE_ALPHA32UI_EXT
:
1682 return ctx
->API
== API_OPENGL_COMPAT
&&
1683 ctx
->Extensions
.EXT_texture_integer
&&
1684 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1686 case GL_ALPHA8I_EXT
:
1687 case GL_ALPHA8UI_EXT
:
1688 case GL_ALPHA16I_EXT
:
1689 case GL_ALPHA16UI_EXT
:
1690 case GL_ALPHA32I_EXT
:
1691 case GL_ALPHA32UI_EXT
:
1692 return ctx
->API
== API_OPENGL_COMPAT
&&
1693 ctx
->Extensions
.EXT_texture_integer
&&
1694 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1697 return (_mesa_is_desktop_gl(ctx
) &&
1698 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1699 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1702 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1711 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1714 invalidate_rb(GLuint key
, void *data
, void *userData
)
1716 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1717 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1719 /* If this is a user-created FBO */
1720 if (_mesa_is_user_fbo(fb
)) {
1722 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1723 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1724 if (att
->Type
== GL_RENDERBUFFER
&&
1725 att
->Renderbuffer
== rb
) {
1726 /* Mark fb status as indeterminate to force re-validation */
1735 /** sentinal value, see below */
1736 #define NO_SAMPLES 1000
1740 * Helper function used by _mesa_RenderbufferStorage() and
1741 * _mesa_RenderbufferStorageMultisample().
1742 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1745 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1746 GLsizei width
, GLsizei height
, GLsizei samples
)
1748 const char *func
= samples
== NO_SAMPLES
?
1749 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1750 struct gl_renderbuffer
*rb
;
1752 GLenum sample_count_error
;
1753 GET_CURRENT_CONTEXT(ctx
);
1755 if (MESA_VERBOSE
& VERBOSE_API
) {
1756 if (samples
== NO_SAMPLES
)
1757 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1759 _mesa_lookup_enum_by_nr(target
),
1760 _mesa_lookup_enum_by_nr(internalFormat
),
1763 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1765 _mesa_lookup_enum_by_nr(target
),
1766 _mesa_lookup_enum_by_nr(internalFormat
),
1767 width
, height
, samples
);
1770 if (target
!= GL_RENDERBUFFER_EXT
) {
1771 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1775 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1776 if (baseFormat
== 0) {
1777 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1778 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1782 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1783 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1787 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1788 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1792 if (samples
== NO_SAMPLES
) {
1793 /* NumSamples == 0 indicates non-multisampling */
1797 /* check the sample count;
1798 * note: driver may choose to use more samples than what's requested
1800 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
1801 internalFormat
, samples
);
1802 if (sample_count_error
!= GL_NO_ERROR
) {
1803 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
1808 rb
= ctx
->CurrentRenderbuffer
;
1810 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1814 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1816 if (rb
->InternalFormat
== internalFormat
&&
1817 rb
->Width
== (GLuint
) width
&&
1818 rb
->Height
== (GLuint
) height
&&
1819 rb
->NumSamples
== samples
) {
1820 /* no change in allocation needed */
1824 /* These MUST get set by the AllocStorage func */
1825 rb
->Format
= MESA_FORMAT_NONE
;
1826 rb
->NumSamples
= samples
;
1828 /* Now allocate the storage */
1829 ASSERT(rb
->AllocStorage
);
1830 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1831 /* No error - check/set fields now */
1832 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1833 assert(rb
->Width
== (GLuint
) width
);
1834 assert(rb
->Height
== (GLuint
) height
);
1835 rb
->InternalFormat
= internalFormat
;
1836 rb
->_BaseFormat
= baseFormat
;
1837 assert(rb
->_BaseFormat
!= 0);
1840 /* Probably ran out of memory - clear the fields */
1843 rb
->Format
= MESA_FORMAT_NONE
;
1844 rb
->InternalFormat
= GL_NONE
;
1845 rb
->_BaseFormat
= GL_NONE
;
1849 /* Invalidate the framebuffers the renderbuffer is attached in. */
1850 if (rb
->AttachedAnytime
) {
1851 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1857 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1859 struct gl_renderbuffer
*rb
;
1860 GET_CURRENT_CONTEXT(ctx
);
1862 if (!ctx
->Extensions
.OES_EGL_image
) {
1863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1864 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1868 if (target
!= GL_RENDERBUFFER
) {
1869 _mesa_error(ctx
, GL_INVALID_ENUM
,
1870 "EGLImageTargetRenderbufferStorageOES");
1874 rb
= ctx
->CurrentRenderbuffer
;
1876 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1877 "EGLImageTargetRenderbufferStorageOES");
1881 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1883 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1888 * Helper function for _mesa_GetRenderbufferParameteriv() and
1889 * _mesa_GetFramebufferAttachmentParameteriv()
1890 * We have to be careful to respect the base format. For example, if a
1891 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1892 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1893 * we need to return zero.
1896 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
1898 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1899 return _mesa_get_format_bits(format
, pname
);
1907 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1908 GLsizei width
, GLsizei height
)
1910 /* GL_ARB_fbo says calling this function is equivalent to calling
1911 * glRenderbufferStorageMultisample() with samples=0. We pass in
1912 * a token value here just for error reporting purposes.
1914 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1919 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1920 GLenum internalFormat
,
1921 GLsizei width
, GLsizei height
)
1923 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1928 * OpenGL ES version of glRenderBufferStorage.
1931 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1932 GLsizei width
, GLsizei height
)
1934 switch (internalFormat
) {
1936 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1937 /* choose a closest format */
1938 internalFormat
= GL_RGB5
;
1944 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1949 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1951 struct gl_renderbuffer
*rb
;
1952 GET_CURRENT_CONTEXT(ctx
);
1954 if (target
!= GL_RENDERBUFFER_EXT
) {
1955 _mesa_error(ctx
, GL_INVALID_ENUM
,
1956 "glGetRenderbufferParameterivEXT(target)");
1960 rb
= ctx
->CurrentRenderbuffer
;
1962 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1963 "glGetRenderbufferParameterivEXT");
1967 /* No need to flush here since we're just quering state which is
1968 * not effected by rendering.
1972 case GL_RENDERBUFFER_WIDTH_EXT
:
1973 *params
= rb
->Width
;
1975 case GL_RENDERBUFFER_HEIGHT_EXT
:
1976 *params
= rb
->Height
;
1978 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1979 *params
= rb
->InternalFormat
;
1981 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1982 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1983 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1984 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1985 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1986 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1987 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1989 case GL_RENDERBUFFER_SAMPLES
:
1990 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1991 || _mesa_is_gles3(ctx
)) {
1992 *params
= rb
->NumSamples
;
1997 _mesa_error(ctx
, GL_INVALID_ENUM
,
1998 "glGetRenderbufferParameterivEXT(target)");
2004 GLboolean GLAPIENTRY
2005 _mesa_IsFramebuffer(GLuint framebuffer
)
2007 GET_CURRENT_CONTEXT(ctx
);
2008 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2010 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2011 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2019 * Check if any of the attachments of the given framebuffer are textures
2020 * (render to texture). Call ctx->Driver.RenderTexture() for such
2024 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2027 ASSERT(ctx
->Driver
.RenderTexture
);
2029 if (_mesa_is_winsys_fbo(fb
))
2030 return; /* can't render to texture with winsys framebuffers */
2032 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2033 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2034 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2035 && driver_RenderTexture_is_safe(att
)) {
2036 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2043 * Examine all the framebuffer's attachments to see if any are textures.
2044 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2045 * notify the device driver that the texture image may have changed.
2048 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2050 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2051 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2054 if (ctx
->Driver
.FinishRenderTexture
) {
2056 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2057 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2058 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2059 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2060 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2068 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2070 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2071 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
2072 GLboolean bindReadBuf
, bindDrawBuf
;
2073 GET_CURRENT_CONTEXT(ctx
);
2076 case GL_DRAW_FRAMEBUFFER_EXT
:
2077 bindDrawBuf
= GL_TRUE
;
2078 bindReadBuf
= GL_FALSE
;
2080 case GL_READ_FRAMEBUFFER_EXT
:
2081 bindDrawBuf
= GL_FALSE
;
2082 bindReadBuf
= GL_TRUE
;
2084 case GL_FRAMEBUFFER_EXT
:
2085 bindDrawBuf
= GL_TRUE
;
2086 bindReadBuf
= GL_TRUE
;
2089 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2094 /* Binding a user-created framebuffer object */
2095 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2096 if (newDrawFb
== &DummyFramebuffer
) {
2097 /* ID was reserved, but no real framebuffer object made yet */
2100 else if (!newDrawFb
&& !allow_user_names
) {
2101 /* All FBO IDs must be Gen'd */
2102 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2107 /* create new framebuffer object */
2108 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2110 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2113 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2115 newReadFb
= newDrawFb
;
2118 /* Binding the window system framebuffer (which was originally set
2119 * with MakeCurrent).
2121 newDrawFb
= ctx
->WinSysDrawBuffer
;
2122 newReadFb
= ctx
->WinSysReadBuffer
;
2126 ASSERT(newDrawFb
!= &DummyFramebuffer
);
2128 /* save pointers to current/old framebuffers */
2129 oldDrawFb
= ctx
->DrawBuffer
;
2130 oldReadFb
= ctx
->ReadBuffer
;
2132 /* check if really changing bindings */
2133 if (oldDrawFb
== newDrawFb
)
2134 bindDrawBuf
= GL_FALSE
;
2135 if (oldReadFb
== newReadFb
)
2136 bindReadBuf
= GL_FALSE
;
2139 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2141 * We also check if we're beginning and/or ending render-to-texture.
2142 * When a framebuffer with texture attachments is unbound, call
2143 * ctx->Driver.FinishRenderTexture().
2144 * When a framebuffer with texture attachments is bound, call
2145 * ctx->Driver.RenderTexture().
2147 * Note that if the ReadBuffer has texture attachments we don't consider
2148 * that a render-to-texture case.
2151 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2153 /* check if old readbuffer was render-to-texture */
2154 check_end_texture_render(ctx
, oldReadFb
);
2156 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2160 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2162 /* check if old framebuffer had any texture attachments */
2164 check_end_texture_render(ctx
, oldDrawFb
);
2166 /* check if newly bound framebuffer has any texture attachments */
2167 check_begin_texture_render(ctx
, newDrawFb
);
2169 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2172 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2173 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
2178 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2180 GET_CURRENT_CONTEXT(ctx
);
2182 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2183 * point, but they allow the use of user-generated names.
2185 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2190 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2192 /* This function should not be in the dispatch table for core profile /
2193 * OpenGL 3.1, so execution should never get here in those cases -- no
2194 * need for an explicit test.
2196 bind_framebuffer(target
, framebuffer
, true);
2201 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2204 GET_CURRENT_CONTEXT(ctx
);
2207 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2211 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2213 for (i
= 0; i
< n
; i
++) {
2214 if (framebuffers
[i
] > 0) {
2215 struct gl_framebuffer
*fb
;
2216 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2218 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2220 /* check if deleting currently bound framebuffer object */
2221 if (fb
== ctx
->DrawBuffer
) {
2223 ASSERT(fb
->RefCount
>= 2);
2224 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2226 if (fb
== ctx
->ReadBuffer
) {
2228 ASSERT(fb
->RefCount
>= 2);
2229 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2232 /* remove from hash table immediately, to free the ID */
2233 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2235 if (fb
!= &DummyFramebuffer
) {
2236 /* But the object will not be freed until it's no longer
2237 * bound in any context.
2239 _mesa_reference_framebuffer(&fb
, NULL
);
2248 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2250 GET_CURRENT_CONTEXT(ctx
);
2255 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
2262 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2264 for (i
= 0; i
< n
; i
++) {
2265 GLuint name
= first
+ i
;
2266 framebuffers
[i
] = name
;
2267 /* insert dummy placeholder into hash table */
2268 mtx_lock(&ctx
->Shared
->Mutex
);
2269 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
2270 mtx_unlock(&ctx
->Shared
->Mutex
);
2276 _mesa_CheckFramebufferStatus(GLenum target
)
2278 struct gl_framebuffer
*buffer
;
2279 GET_CURRENT_CONTEXT(ctx
);
2281 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2283 if (MESA_VERBOSE
& VERBOSE_API
)
2284 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2285 _mesa_lookup_enum_by_nr(target
));
2287 buffer
= get_framebuffer_target(ctx
, target
);
2289 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2293 if (_mesa_is_winsys_fbo(buffer
)) {
2294 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2295 if (buffer
!= &IncompleteFramebuffer
) {
2296 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2298 return GL_FRAMEBUFFER_UNDEFINED
;
2302 /* No need to flush here */
2304 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2305 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2308 return buffer
->_Status
;
2313 * Replicate the src attachment point. Used by framebuffer_texture() when
2314 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2315 * GL_STENCIL_ATTACHMENT.
2318 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2319 gl_buffer_index dst
,
2320 gl_buffer_index src
)
2322 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2323 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2325 assert(src_att
->Texture
!= NULL
);
2326 assert(src_att
->Renderbuffer
!= NULL
);
2328 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2329 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2330 dst_att
->Type
= src_att
->Type
;
2331 dst_att
->Complete
= src_att
->Complete
;
2332 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2333 dst_att
->Zoffset
= src_att
->Zoffset
;
2338 * Common code called by glFramebufferTexture1D/2D/3D() and
2339 * glFramebufferTextureLayer().
2341 * \param textarget is the textarget that was passed to the
2342 * glFramebufferTexture...() function, or 0 if the corresponding function
2343 * doesn't have a textarget parameter.
2345 * \param layered is true if this function was called from
2346 * glFramebufferTexture(), false otherwise.
2349 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2350 GLenum attachment
, GLenum textarget
, GLuint texture
,
2351 GLint level
, GLint zoffset
, GLboolean layered
)
2353 struct gl_renderbuffer_attachment
*att
;
2354 struct gl_texture_object
*texObj
= NULL
;
2355 struct gl_framebuffer
*fb
;
2356 GLenum maxLevelsTarget
;
2358 fb
= get_framebuffer_target(ctx
, target
);
2360 _mesa_error(ctx
, GL_INVALID_ENUM
,
2361 "glFramebufferTexture%s(target=0x%x)", caller
, target
);
2365 /* check framebuffer binding */
2366 if (_mesa_is_winsys_fbo(fb
)) {
2367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2368 "glFramebufferTexture%s", caller
);
2372 /* The textarget, level, and zoffset parameters are only validated if
2373 * texture is non-zero.
2376 GLboolean err
= GL_TRUE
;
2378 texObj
= _mesa_lookup_texture(ctx
, texture
);
2379 if (texObj
!= NULL
) {
2380 if (textarget
== 0) {
2382 /* We're being called by glFramebufferTexture() and textarget
2385 switch (texObj
->Target
) {
2387 case GL_TEXTURE_1D_ARRAY_EXT
:
2388 case GL_TEXTURE_2D_ARRAY_EXT
:
2389 case GL_TEXTURE_CUBE_MAP
:
2390 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2391 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2396 case GL_TEXTURE_RECTANGLE
:
2397 case GL_TEXTURE_2D_MULTISAMPLE
:
2398 /* These texture types are valid to pass to
2399 * glFramebufferTexture(), but since they aren't layered, it
2400 * is equivalent to calling glFramebufferTexture{1D,2D}().
2404 textarget
= texObj
->Target
;
2411 /* We're being called by glFramebufferTextureLayer() and
2412 * textarget is not used. The only legal texture types for
2413 * that function are 3D and 1D/2D arrays textures.
2415 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2416 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2417 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2418 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2419 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2423 /* Make sure textarget is consistent with the texture's type */
2424 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2425 ? !_mesa_is_cube_face(textarget
)
2426 : (texObj
->Target
!= textarget
);
2430 /* can't render to a non-existant texture */
2431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2432 "glFramebufferTexture%s(non existant texture)",
2438 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2439 "glFramebufferTexture%s(texture target mismatch)",
2444 if (texObj
->Target
== GL_TEXTURE_3D
) {
2445 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2446 if (zoffset
< 0 || zoffset
>= maxSize
) {
2447 _mesa_error(ctx
, GL_INVALID_VALUE
,
2448 "glFramebufferTexture%s(zoffset)", caller
);
2452 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2453 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2454 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2455 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2457 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2458 _mesa_error(ctx
, GL_INVALID_VALUE
,
2459 "glFramebufferTexture%s(layer)", caller
);
2464 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2466 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2467 _mesa_error(ctx
, GL_INVALID_VALUE
,
2468 "glFramebufferTexture%s(level)", caller
);
2473 att
= get_attachment(ctx
, fb
, attachment
);
2475 _mesa_error(ctx
, GL_INVALID_ENUM
,
2476 "glFramebufferTexture%s(attachment)", caller
);
2480 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2482 mtx_lock(&fb
->Mutex
);
2484 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2485 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2486 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2487 _mesa_tex_target_to_face(textarget
) ==
2488 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2489 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2490 /* The texture object is already attached to the stencil attachment
2491 * point. Don't create a new renderbuffer; just reuse the stencil
2492 * attachment's. This is required to prevent a GL error in
2493 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2495 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2497 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2498 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2499 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2500 _mesa_tex_target_to_face(textarget
) ==
2501 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2502 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2503 /* As above, but with depth and stencil transposed. */
2504 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2507 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2508 level
, zoffset
, layered
);
2509 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2510 /* Above we created a new renderbuffer and attached it to the
2511 * depth attachment point. Now attach it to the stencil attachment
2514 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2515 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2520 /* Set the render-to-texture flag. We'll check this flag in
2521 * glTexImage() and friends to determine if we need to revalidate
2522 * any FBOs that might be rendering into this texture.
2523 * This flag never gets cleared since it's non-trivial to determine
2524 * when all FBOs might be done rendering to this texture. That's OK
2525 * though since it's uncommon to render to a texture then repeatedly
2526 * call glTexImage() to change images in the texture.
2528 texObj
->_RenderToTexture
= GL_TRUE
;
2531 remove_attachment(ctx
, att
);
2532 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2533 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2534 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2538 invalidate_framebuffer(fb
);
2540 mtx_unlock(&fb
->Mutex
);
2545 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2546 GLenum textarget
, GLuint texture
, GLint level
)
2548 GET_CURRENT_CONTEXT(ctx
);
2553 switch (textarget
) {
2557 case GL_TEXTURE_1D_ARRAY
:
2558 error
= !ctx
->Extensions
.EXT_texture_array
;
2565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2566 "glFramebufferTexture1D(textarget=%s)",
2567 _mesa_lookup_enum_by_nr(textarget
));
2572 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2573 level
, 0, GL_FALSE
);
2578 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2579 GLenum textarget
, GLuint texture
, GLint level
)
2581 GET_CURRENT_CONTEXT(ctx
);
2586 switch (textarget
) {
2590 case GL_TEXTURE_RECTANGLE
:
2591 error
= _mesa_is_gles(ctx
)
2592 || !ctx
->Extensions
.NV_texture_rectangle
;
2594 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2595 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2596 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2597 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2598 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2599 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2600 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2602 case GL_TEXTURE_2D_ARRAY
:
2603 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2604 || !ctx
->Extensions
.EXT_texture_array
;
2606 case GL_TEXTURE_2D_MULTISAMPLE
:
2607 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2608 error
= _mesa_is_gles(ctx
)
2609 || !ctx
->Extensions
.ARB_texture_multisample
;
2616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2617 "glFramebufferTexture2D(textarget=%s)",
2618 _mesa_lookup_enum_by_nr(textarget
));
2623 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2624 level
, 0, GL_FALSE
);
2629 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2630 GLenum textarget
, GLuint texture
,
2631 GLint level
, GLint zoffset
)
2633 GET_CURRENT_CONTEXT(ctx
);
2635 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2636 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2637 "glFramebufferTexture3D(textarget)");
2641 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2642 level
, zoffset
, GL_FALSE
);
2647 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2648 GLuint texture
, GLint level
, GLint layer
)
2650 GET_CURRENT_CONTEXT(ctx
);
2652 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2653 level
, layer
, GL_FALSE
);
2658 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
2659 GLuint texture
, GLint level
)
2661 GET_CURRENT_CONTEXT(ctx
);
2663 if (_mesa_has_geometry_shaders(ctx
)) {
2664 framebuffer_texture(ctx
, "", target
, attachment
, 0, texture
,
2667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2668 "unsupported function (glFramebufferTexture) called");
2674 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2675 GLenum renderbufferTarget
,
2676 GLuint renderbuffer
)
2678 struct gl_renderbuffer_attachment
*att
;
2679 struct gl_framebuffer
*fb
;
2680 struct gl_renderbuffer
*rb
;
2681 GET_CURRENT_CONTEXT(ctx
);
2683 fb
= get_framebuffer_target(ctx
, target
);
2685 _mesa_error(ctx
, GL_INVALID_ENUM
,
2686 "glFramebufferRenderbuffer(target)");
2690 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2691 _mesa_error(ctx
, GL_INVALID_ENUM
,
2692 "glFramebufferRenderbuffer(renderbufferTarget)");
2696 if (_mesa_is_winsys_fbo(fb
)) {
2697 /* Can't attach new renderbuffers to a window system framebuffer */
2698 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbuffer");
2702 att
= get_attachment(ctx
, fb
, attachment
);
2704 _mesa_error(ctx
, GL_INVALID_ENUM
,
2705 "glFramebufferRenderbuffer(invalid attachment %s)",
2706 _mesa_lookup_enum_by_nr(attachment
));
2711 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2713 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2714 "glFramebufferRenderbuffer(non-existant"
2715 " renderbuffer %u)", renderbuffer
);
2718 else if (rb
== &DummyRenderbuffer
) {
2719 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2720 "glFramebufferRenderbuffer(renderbuffer %u)",
2726 /* remove renderbuffer attachment */
2730 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2731 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2732 /* make sure the renderbuffer is a depth/stencil format */
2733 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2734 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2736 "glFramebufferRenderbuffer(renderbuffer"
2737 " is not DEPTH_STENCIL format)");
2742 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2744 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2745 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2747 /* Some subsequent GL commands may depend on the framebuffer's visual
2748 * after the binding is updated. Update visual info now.
2750 _mesa_update_framebuffer_visual(ctx
, fb
);
2755 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2756 GLenum pname
, GLint
*params
)
2758 const struct gl_renderbuffer_attachment
*att
;
2759 struct gl_framebuffer
*buffer
;
2761 GET_CURRENT_CONTEXT(ctx
);
2763 /* The error differs in GL and GLES. */
2764 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2766 buffer
= get_framebuffer_target(ctx
, target
);
2768 _mesa_error(ctx
, GL_INVALID_ENUM
,
2769 "glGetFramebufferAttachmentParameteriv(target)");
2773 if (_mesa_is_winsys_fbo(buffer
)) {
2774 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2777 * "If the framebuffer currently bound to target is zero, then
2778 * INVALID_OPERATION is generated."
2780 * The EXT_framebuffer_object spec has the same wording, and the
2781 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2784 if ((!_mesa_is_desktop_gl(ctx
) ||
2785 !ctx
->Extensions
.ARB_framebuffer_object
)
2786 && !_mesa_is_gles3(ctx
)) {
2787 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2788 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2792 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2793 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2794 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2795 "glGetFramebufferAttachmentParameteriv(attachment)");
2798 /* the default / window-system FBO */
2799 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2802 /* user-created framebuffer FBO */
2803 att
= get_attachment(ctx
, buffer
, attachment
);
2807 _mesa_error(ctx
, GL_INVALID_ENUM
,
2808 "glGetFramebufferAttachmentParameteriv(attachment)");
2812 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2813 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2814 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
2815 /* This behavior is first specified in OpenGL 4.4 specification.
2817 * From the OpenGL 4.4 spec page 275:
2818 * "This query cannot be performed for a combined depth+stencil
2819 * attachment, since it does not have a single format."
2821 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2822 "glGetFramebufferAttachmentParameteriv("
2823 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
2824 " is invalid for depth+stencil attachment)");
2827 /* the depth and stencil attachments must point to the same buffer */
2828 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2829 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2830 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2831 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2832 "glGetFramebufferAttachmentParameteriv(DEPTH/STENCIL"
2833 " attachments differ)");
2838 /* No need to flush here */
2841 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2842 *params
= _mesa_is_winsys_fbo(buffer
)
2843 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2845 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2846 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2847 *params
= att
->Renderbuffer
->Name
;
2849 else if (att
->Type
== GL_TEXTURE
) {
2850 *params
= att
->Texture
->Name
;
2853 assert(att
->Type
== GL_NONE
);
2854 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2857 goto invalid_pname_enum
;
2861 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2862 if (att
->Type
== GL_TEXTURE
) {
2863 *params
= att
->TextureLevel
;
2865 else if (att
->Type
== GL_NONE
) {
2866 _mesa_error(ctx
, err
,
2867 "glGetFramebufferAttachmentParameteriv(pname)");
2870 goto invalid_pname_enum
;
2873 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2874 if (att
->Type
== GL_TEXTURE
) {
2875 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2876 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2882 else if (att
->Type
== GL_NONE
) {
2883 _mesa_error(ctx
, err
,
2884 "glGetFramebufferAttachmentParameteriv(pname)");
2887 goto invalid_pname_enum
;
2890 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2891 if (ctx
->API
== API_OPENGLES
) {
2892 goto invalid_pname_enum
;
2893 } else if (att
->Type
== GL_NONE
) {
2894 _mesa_error(ctx
, err
,
2895 "glGetFramebufferAttachmentParameteriv(pname)");
2896 } else if (att
->Type
== GL_TEXTURE
) {
2897 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
2898 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
2899 *params
= att
->Zoffset
;
2906 goto invalid_pname_enum
;
2909 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2910 if ((!_mesa_is_desktop_gl(ctx
) ||
2911 !ctx
->Extensions
.ARB_framebuffer_object
)
2912 && !_mesa_is_gles3(ctx
)) {
2913 goto invalid_pname_enum
;
2915 else if (att
->Type
== GL_NONE
) {
2916 _mesa_error(ctx
, err
,
2917 "glGetFramebufferAttachmentParameteriv(pname)");
2920 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2922 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2925 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2926 * if the sRGB conversion is unsupported. */
2927 *params
= GL_LINEAR
;
2931 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2932 if ((ctx
->API
!= API_OPENGL_COMPAT
||
2933 !ctx
->Extensions
.ARB_framebuffer_object
)
2934 && ctx
->API
!= API_OPENGL_CORE
2935 && !_mesa_is_gles3(ctx
)) {
2936 goto invalid_pname_enum
;
2938 else if (att
->Type
== GL_NONE
) {
2939 _mesa_error(ctx
, err
,
2940 "glGetFramebufferAttachmentParameteriv(pname)");
2943 mesa_format format
= att
->Renderbuffer
->Format
;
2945 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2948 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2949 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2950 * generate an INVALID_OPERATION error.
2952 if (_mesa_is_gles3(ctx
) &&
2953 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2954 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2955 "glGetFramebufferAttachmentParameteriv(cannot query "
2956 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2957 "GL_DEPTH_STENCIL_ATTACHMENT");
2961 if (format
== MESA_FORMAT_S_UINT8
) {
2965 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
2966 /* depends on the attachment parameter */
2967 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2975 *params
= _mesa_get_format_datatype(format
);
2979 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2980 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2981 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2982 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2983 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2984 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2985 if ((!_mesa_is_desktop_gl(ctx
) ||
2986 !ctx
->Extensions
.ARB_framebuffer_object
)
2987 && !_mesa_is_gles3(ctx
)) {
2988 goto invalid_pname_enum
;
2990 else if (att
->Type
== GL_NONE
) {
2991 _mesa_error(ctx
, err
,
2992 "glGetFramebufferAttachmentParameteriv(pname)");
2994 else if (att
->Texture
) {
2995 const struct gl_texture_image
*texImage
=
2996 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
2999 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
3000 texImage
->TexFormat
);
3006 else if (att
->Renderbuffer
) {
3007 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
3008 att
->Renderbuffer
->Format
);
3011 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
3012 " invalid FBO attachment structure");
3015 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
3016 if (!_mesa_has_geometry_shaders(ctx
)) {
3017 goto invalid_pname_enum
;
3018 } else if (att
->Type
== GL_TEXTURE
) {
3019 *params
= att
->Layered
;
3020 } else if (att
->Type
== GL_NONE
) {
3021 _mesa_error(ctx
, err
,
3022 "glGetFramebufferAttachmentParameteriv(pname)");
3024 goto invalid_pname_enum
;
3028 goto invalid_pname_enum
;
3034 _mesa_error(ctx
, GL_INVALID_ENUM
,
3035 "glGetFramebufferAttachmentParameteriv(pname)");
3041 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3042 const GLenum
*attachments
, GLint x
, GLint y
,
3043 GLsizei width
, GLsizei height
, const char *name
)
3046 struct gl_framebuffer
*fb
;
3047 GET_CURRENT_CONTEXT(ctx
);
3049 fb
= get_framebuffer_target(ctx
, target
);
3051 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3055 if (numAttachments
< 0) {
3056 _mesa_error(ctx
, GL_INVALID_VALUE
,
3057 "%s(numAttachments < 0)", name
);
3061 /* The GL_ARB_invalidate_subdata spec says:
3063 * "If an attachment is specified that does not exist in the
3064 * framebuffer bound to <target>, it is ignored."
3068 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3069 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3070 * INVALID_OPERATION is generated."
3072 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3073 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3074 * set of retrictions).
3076 for (i
= 0; i
< numAttachments
; i
++) {
3077 if (_mesa_is_winsys_fbo(fb
)) {
3078 switch (attachments
[i
]) {
3084 /* Accumulation buffers and auxilary buffers were removed in
3085 * OpenGL 3.1, and they never existed in OpenGL ES.
3087 if (ctx
->API
!= API_OPENGL_COMPAT
)
3097 case GL_FRONT_RIGHT
:
3098 if (!_mesa_is_desktop_gl(ctx
))
3105 switch (attachments
[i
]) {
3106 case GL_DEPTH_ATTACHMENT
:
3107 case GL_STENCIL_ATTACHMENT
:
3109 case GL_DEPTH_STENCIL_ATTACHMENT
:
3110 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3111 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3112 * extension does not make this attachment point valid on ES 2.0.
3114 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
3117 case GL_COLOR_ATTACHMENT0
:
3118 case GL_COLOR_ATTACHMENT1
:
3119 case GL_COLOR_ATTACHMENT2
:
3120 case GL_COLOR_ATTACHMENT3
:
3121 case GL_COLOR_ATTACHMENT4
:
3122 case GL_COLOR_ATTACHMENT5
:
3123 case GL_COLOR_ATTACHMENT6
:
3124 case GL_COLOR_ATTACHMENT7
:
3125 case GL_COLOR_ATTACHMENT8
:
3126 case GL_COLOR_ATTACHMENT9
:
3127 case GL_COLOR_ATTACHMENT10
:
3128 case GL_COLOR_ATTACHMENT11
:
3129 case GL_COLOR_ATTACHMENT12
:
3130 case GL_COLOR_ATTACHMENT13
:
3131 case GL_COLOR_ATTACHMENT14
:
3132 case GL_COLOR_ATTACHMENT15
: {
3133 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3134 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3136 "%s(attachment >= max. color attachments)", name
);
3147 /* We don't actually do anything for this yet. Just return after
3148 * validating the parameters and generating the required errors.
3153 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3159 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3160 const GLenum
*attachments
, GLint x
, GLint y
,
3161 GLsizei width
, GLsizei height
)
3163 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3164 x
, y
, width
, height
,
3165 "glInvalidateSubFramebuffer");
3170 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3171 const GLenum
*attachments
)
3173 /* The GL_ARB_invalidate_subdata spec says:
3177 * void InvalidateFramebuffer(enum target,
3178 * sizei numAttachments,
3179 * const enum *attachments);
3181 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3182 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3183 * <MAX_VIEWPORT_DIMS[1]> respectively."
3185 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3187 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3188 "glInvalidateFramebuffer");
3193 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3194 const GLenum
*attachments
)
3196 struct gl_framebuffer
*fb
;
3199 GET_CURRENT_CONTEXT(ctx
);
3201 fb
= get_framebuffer_target(ctx
, target
);
3203 _mesa_error(ctx
, GL_INVALID_ENUM
,
3204 "glDiscardFramebufferEXT(target %s)",
3205 _mesa_lookup_enum_by_nr(target
));
3209 if (numAttachments
< 0) {
3210 _mesa_error(ctx
, GL_INVALID_VALUE
,
3211 "glDiscardFramebufferEXT(numAttachments < 0)");
3215 for (i
= 0; i
< numAttachments
; i
++) {
3216 switch (attachments
[i
]) {
3220 if (_mesa_is_user_fbo(fb
))
3223 case GL_COLOR_ATTACHMENT0
:
3224 case GL_DEPTH_ATTACHMENT
:
3225 case GL_STENCIL_ATTACHMENT
:
3226 if (_mesa_is_winsys_fbo(fb
))
3234 if (ctx
->Driver
.DiscardFramebuffer
)
3235 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3240 _mesa_error(ctx
, GL_INVALID_ENUM
,
3241 "glDiscardFramebufferEXT(attachment %s)",
3242 _mesa_lookup_enum_by_nr(attachments
[i
]));