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
->Layered
= GL_FALSE
;
472 att
->Complete
= GL_FALSE
;
473 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
478 * Fallback for ctx->Driver.FramebufferRenderbuffer()
479 * Attach a renderbuffer object to a framebuffer object.
482 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
483 struct gl_framebuffer
*fb
,
484 GLenum attachment
, struct gl_renderbuffer
*rb
)
486 struct gl_renderbuffer_attachment
*att
;
488 mtx_lock(&fb
->Mutex
);
490 att
= get_attachment(ctx
, fb
, attachment
);
493 set_renderbuffer_attachment(ctx
, att
, rb
);
494 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
495 /* do stencil attachment here (depth already done above) */
496 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
498 set_renderbuffer_attachment(ctx
, att
, rb
);
500 rb
->AttachedAnytime
= GL_TRUE
;
503 remove_attachment(ctx
, att
);
504 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
505 /* detach stencil (depth was detached above) */
506 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
508 remove_attachment(ctx
, att
);
512 invalidate_framebuffer(fb
);
514 mtx_unlock(&fb
->Mutex
);
519 * Fallback for ctx->Driver.ValidateFramebuffer()
520 * Check if the renderbuffer's formats are supported by the software
522 * Drivers should probably override this.
525 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
528 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
529 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
531 switch (rb
->_BaseFormat
) {
533 case GL_LUMINANCE_ALPHA
:
538 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
542 switch (rb
->Format
) {
543 /* XXX This list is likely incomplete. */
544 case MESA_FORMAT_R9G9B9E5_FLOAT
:
545 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
548 /* render buffer format is supported by software rendering */
557 * Return true if the framebuffer has a combined depth/stencil
558 * renderbuffer attached.
561 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
563 const struct gl_renderbuffer_attachment
*depth
=
564 &fb
->Attachment
[BUFFER_DEPTH
];
565 const struct gl_renderbuffer_attachment
*stencil
=
566 &fb
->Attachment
[BUFFER_STENCIL
];
568 if (depth
->Type
== stencil
->Type
) {
569 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
570 depth
->Renderbuffer
== stencil
->Renderbuffer
)
573 if (depth
->Type
== GL_TEXTURE
&&
574 depth
->Texture
== stencil
->Texture
)
586 att_incomplete(const char *msg
)
588 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
589 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
598 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
600 static GLuint msg_id
;
602 _mesa_gl_debug(ctx
, &msg_id
,
603 MESA_DEBUG_SOURCE_API
,
604 MESA_DEBUG_TYPE_OTHER
,
605 MESA_DEBUG_SEVERITY_MEDIUM
,
606 "FBO incomplete: %s [%d]\n", msg
, index
);
608 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
609 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
615 * Is the given base format a legal format for a color renderbuffer?
618 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
620 switch (baseFormat
) {
625 case GL_LUMINANCE_ALPHA
:
628 return ctx
->API
== API_OPENGL_COMPAT
&&
629 ctx
->Extensions
.ARB_framebuffer_object
;
632 return ctx
->Extensions
.ARB_texture_rg
;
640 * Is the given base format a legal format for a color renderbuffer?
643 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
644 GLenum internalFormat
)
646 const GLenum baseFormat
=
647 _mesa_get_format_base_format(format
);
650 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
651 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
655 /* Reject additional cases for GLES */
656 switch (internalFormat
) {
676 if (format
== MESA_FORMAT_B10G10R10A2_UNORM
&&
677 internalFormat
!= GL_RGB10_A2
) {
686 * Is the given base format a legal format for a depth/stencil renderbuffer?
689 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
691 switch (baseFormat
) {
692 case GL_DEPTH_COMPONENT
:
693 case GL_DEPTH_STENCIL_EXT
:
702 * Test if an attachment point is complete and update its Complete field.
703 * \param format if GL_COLOR, this is a color attachment point,
704 * if GL_DEPTH, this is a depth component attachment point,
705 * if GL_STENCIL, this is a stencil component attachment point.
708 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
709 struct gl_renderbuffer_attachment
*att
)
711 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
713 /* assume complete */
714 att
->Complete
= GL_TRUE
;
716 /* Look for reasons why the attachment might be incomplete */
717 if (att
->Type
== GL_TEXTURE
) {
718 const struct gl_texture_object
*texObj
= att
->Texture
;
719 struct gl_texture_image
*texImage
;
723 att_incomplete("no texobj");
724 att
->Complete
= GL_FALSE
;
728 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
730 att_incomplete("no teximage");
731 att
->Complete
= GL_FALSE
;
734 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
735 att_incomplete("teximage width/height=0");
736 att
->Complete
= GL_FALSE
;
740 switch (texObj
->Target
) {
742 if (att
->Zoffset
>= texImage
->Depth
) {
743 att_incomplete("bad z offset");
744 att
->Complete
= GL_FALSE
;
748 case GL_TEXTURE_1D_ARRAY
:
749 if (att
->Zoffset
>= texImage
->Height
) {
750 att_incomplete("bad 1D-array layer");
751 att
->Complete
= GL_FALSE
;
755 case GL_TEXTURE_2D_ARRAY
:
756 if (att
->Zoffset
>= texImage
->Depth
) {
757 att_incomplete("bad 2D-array layer");
758 att
->Complete
= GL_FALSE
;
762 case GL_TEXTURE_CUBE_MAP_ARRAY
:
763 if (att
->Zoffset
>= texImage
->Depth
) {
764 att_incomplete("bad cube-array layer");
765 att
->Complete
= GL_FALSE
;
771 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
773 if (format
== GL_COLOR
) {
774 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
775 att_incomplete("bad format");
776 att
->Complete
= GL_FALSE
;
779 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
780 att_incomplete("compressed internalformat");
781 att
->Complete
= GL_FALSE
;
785 /* OES_texture_float allows creation and use of floating point
786 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
787 * these textures to be used as a render target, this is done via
788 * GL_EXT_color_buffer(_half)_float with set of new sized types.
790 if (_mesa_is_gles(ctx
) && (texImage
->TexObject
->_IsFloat
||
791 texImage
->TexObject
->_IsHalfFloat
)) {
792 att_incomplete("bad internal format");
793 att
->Complete
= GL_FALSE
;
797 else if (format
== GL_DEPTH
) {
798 if (baseFormat
== GL_DEPTH_COMPONENT
) {
801 else if (ctx
->Extensions
.ARB_depth_texture
&&
802 baseFormat
== GL_DEPTH_STENCIL
) {
806 att
->Complete
= GL_FALSE
;
807 att_incomplete("bad depth format");
812 assert(format
== GL_STENCIL
);
813 if (ctx
->Extensions
.ARB_depth_texture
&&
814 baseFormat
== GL_DEPTH_STENCIL
) {
818 /* no such thing as stencil-only textures */
819 att_incomplete("illegal stencil texture");
820 att
->Complete
= GL_FALSE
;
825 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
826 const GLenum baseFormat
=
827 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
829 assert(att
->Renderbuffer
);
830 if (!att
->Renderbuffer
->InternalFormat
||
831 att
->Renderbuffer
->Width
< 1 ||
832 att
->Renderbuffer
->Height
< 1) {
833 att_incomplete("0x0 renderbuffer");
834 att
->Complete
= GL_FALSE
;
837 if (format
== GL_COLOR
) {
838 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
839 att_incomplete("bad renderbuffer color format");
840 att
->Complete
= GL_FALSE
;
844 else if (format
== GL_DEPTH
) {
845 if (baseFormat
== GL_DEPTH_COMPONENT
) {
848 else if (baseFormat
== GL_DEPTH_STENCIL
) {
852 att_incomplete("bad renderbuffer depth format");
853 att
->Complete
= GL_FALSE
;
858 assert(format
== GL_STENCIL
);
859 if (baseFormat
== GL_STENCIL_INDEX
||
860 baseFormat
== GL_DEPTH_STENCIL
) {
864 att
->Complete
= GL_FALSE
;
865 att_incomplete("bad renderbuffer stencil format");
871 assert(att
->Type
== GL_NONE
);
879 * Test if the given framebuffer object is complete and update its
880 * Status field with the results.
881 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
882 * driver to make hardware-specific validation/completeness checks.
883 * Also update the framebuffer's Width and Height fields if the
884 * framebuffer is complete.
887 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
888 struct gl_framebuffer
*fb
)
891 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
892 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
893 GLint numSamples
= -1;
894 GLint fixedSampleLocations
= -1;
897 /* Covers max_layer_count, is_layered, and layer_tex_target */
898 bool layer_info_valid
= false;
899 GLuint max_layer_count
= 0, att_layer_count
;
900 bool is_layered
= false;
901 GLenum layer_tex_target
= 0;
902 bool has_depth_attachment
= false;
903 bool has_stencil_attachment
= false;
905 assert(_mesa_is_user_fbo(fb
));
907 /* we're changing framebuffer fields here */
908 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
913 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
914 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
916 /* Start at -2 to more easily loop over all attachment points.
921 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
922 struct gl_renderbuffer_attachment
*att
;
924 mesa_format attFormat
;
925 GLenum att_tex_target
= GL_NONE
;
928 * XXX for ARB_fbo, only check color buffers that are named by
929 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
932 /* check for attachment completeness
935 att
= &fb
->Attachment
[BUFFER_DEPTH
];
936 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
937 if (!att
->Complete
) {
938 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
939 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
941 } else if (att
->Type
!= GL_NONE
) {
942 has_depth_attachment
= true;
946 att
= &fb
->Attachment
[BUFFER_STENCIL
];
947 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
948 if (!att
->Complete
) {
949 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
950 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
952 } else if (att
->Type
!= GL_NONE
) {
953 has_stencil_attachment
= true;
957 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
958 test_attachment_completeness(ctx
, GL_COLOR
, att
);
959 if (!att
->Complete
) {
960 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
961 fbo_incomplete(ctx
, "color attachment incomplete", i
);
966 /* get width, height, format of the renderbuffer/texture
968 if (att
->Type
== GL_TEXTURE
) {
969 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
970 att_tex_target
= att
->Texture
->Target
;
971 minWidth
= MIN2(minWidth
, texImg
->Width
);
972 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
973 minHeight
= MIN2(minHeight
, texImg
->Height
);
974 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
975 f
= texImg
->_BaseFormat
;
976 attFormat
= texImg
->TexFormat
;
979 if (!is_format_color_renderable(ctx
, attFormat
,
980 texImg
->InternalFormat
) &&
981 !is_legal_depth_format(ctx
, f
)) {
982 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
983 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
988 numSamples
= texImg
->NumSamples
;
989 else if (numSamples
!= texImg
->NumSamples
) {
990 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
991 fbo_incomplete(ctx
, "inconsistent sample count", -1);
995 if (fixedSampleLocations
< 0)
996 fixedSampleLocations
= texImg
->FixedSampleLocations
;
997 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
998 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
999 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1003 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1004 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1005 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1006 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1007 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1008 f
= att
->Renderbuffer
->InternalFormat
;
1009 attFormat
= att
->Renderbuffer
->Format
;
1013 numSamples
= att
->Renderbuffer
->NumSamples
;
1014 else if (numSamples
!= att
->Renderbuffer
->NumSamples
) {
1015 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1016 fbo_incomplete(ctx
, "inconsistent sample count", -1);
1020 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1021 if (fixedSampleLocations
< 0)
1022 fixedSampleLocations
= GL_TRUE
;
1023 else if (fixedSampleLocations
!= GL_TRUE
) {
1024 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1025 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1030 assert(att
->Type
== GL_NONE
);
1034 /* check if integer color */
1035 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
1037 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1039 GLenum type
= _mesa_get_format_datatype(attFormat
);
1041 fb
->_AllColorBuffersFixedPoint
=
1042 fb
->_AllColorBuffersFixedPoint
&&
1043 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1045 fb
->_HasSNormOrFloatColorBuffer
=
1046 fb
->_HasSNormOrFloatColorBuffer
||
1047 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1050 /* Error-check width, height, format */
1051 if (numImages
== 1) {
1058 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1059 /* check that width, height, format are same */
1060 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1061 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1062 fbo_incomplete(ctx
, "width or height mismatch", -1);
1065 /* check that all color buffers are the same format */
1066 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1067 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1068 fbo_incomplete(ctx
, "format mismatch", -1);
1074 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1076 if (att
->Type
== GL_RENDERBUFFER
&&
1077 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1078 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1079 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1083 /* Check that layered rendering is consistent. */
1085 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1086 att_layer_count
= 6;
1087 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1088 att_layer_count
= att
->Renderbuffer
->Height
;
1090 att_layer_count
= att
->Renderbuffer
->Depth
;
1092 att_layer_count
= 0;
1094 if (!layer_info_valid
) {
1095 is_layered
= att
->Layered
;
1096 max_layer_count
= att_layer_count
;
1097 layer_tex_target
= att_tex_target
;
1098 layer_info_valid
= true;
1099 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1100 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1101 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1103 } else if (is_layered
!= att
->Layered
) {
1104 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1106 "framebuffer attachment layer mode is inconsistent",
1109 } else if (att_layer_count
> max_layer_count
) {
1110 max_layer_count
= att_layer_count
;
1114 fb
->MaxNumLayers
= max_layer_count
;
1116 if (numImages
== 0) {
1117 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1118 fbo_incomplete(ctx
, "no attachments", -1);
1122 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1123 /* Check that all DrawBuffers are present */
1124 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1125 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1126 const struct gl_renderbuffer_attachment
*att
1127 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
1129 if (att
->Type
== GL_NONE
) {
1130 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1131 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1137 /* Check that the ReadBuffer is present */
1138 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1139 const struct gl_renderbuffer_attachment
*att
1140 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
1142 if (att
->Type
== GL_NONE
) {
1143 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1144 fbo_incomplete(ctx
, "missing readbuffer", -1);
1150 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1152 * "Depth and stencil attachments, if present, are the same image."
1154 * This restriction is not present in the OpenGL ES2 spec.
1156 if (_mesa_is_gles3(ctx
) &&
1157 has_stencil_attachment
&& has_depth_attachment
&&
1158 !_mesa_has_depthstencil_combined(fb
)) {
1159 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1160 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1164 /* Provisionally set status = COMPLETE ... */
1165 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1167 /* ... but the driver may say the FB is incomplete.
1168 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1171 if (ctx
->Driver
.ValidateFramebuffer
) {
1172 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1173 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1174 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1178 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
1180 * Note that if ARB_framebuffer_object is supported and the attached
1181 * renderbuffers/textures are different sizes, the framebuffer
1182 * width/height will be set to the smallest width/height.
1184 fb
->Width
= minWidth
;
1185 fb
->Height
= minHeight
;
1187 /* finally, update the visual info for the framebuffer */
1188 _mesa_update_framebuffer_visual(ctx
, fb
);
1193 GLboolean GLAPIENTRY
1194 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1196 GET_CURRENT_CONTEXT(ctx
);
1197 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1199 struct gl_renderbuffer
*rb
=
1200 _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1201 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
1209 bind_renderbuffer(GLenum target
, GLuint renderbuffer
, bool allow_user_names
)
1211 struct gl_renderbuffer
*newRb
;
1212 GET_CURRENT_CONTEXT(ctx
);
1214 if (target
!= GL_RENDERBUFFER_EXT
) {
1215 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1219 /* No need to flush here since the render buffer binding has no
1220 * effect on rendering state.
1224 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1225 if (newRb
== &DummyRenderbuffer
) {
1226 /* ID was reserved, but no real renderbuffer object made yet */
1229 else if (!newRb
&& !allow_user_names
) {
1230 /* All RB IDs must be Gen'd */
1231 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
1236 /* create new renderbuffer object */
1237 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1239 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
1242 assert(newRb
->AllocStorage
);
1243 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1244 newRb
->RefCount
= 1; /* referenced by hash table */
1251 assert(newRb
!= &DummyRenderbuffer
);
1253 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1257 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1259 GET_CURRENT_CONTEXT(ctx
);
1261 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1262 * entry point, but they allow the use of user-generated names.
1264 bind_renderbuffer(target
, renderbuffer
, _mesa_is_gles(ctx
));
1268 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1270 /* This function should not be in the dispatch table for core profile /
1271 * OpenGL 3.1, so execution should never get here in those cases -- no
1272 * need for an explicit test.
1274 bind_renderbuffer(target
, renderbuffer
, true);
1279 * Remove the specified renderbuffer or texture from any attachment point in
1283 * \c true if the renderbuffer was detached from an attachment point. \c
1287 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1288 struct gl_framebuffer
*fb
,
1292 bool progress
= false;
1294 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1295 if (fb
->Attachment
[i
].Texture
== att
1296 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1297 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1302 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1303 * Completeness," of the OpenGL 3.1 spec says:
1305 * "Performing any of the following actions may change whether the
1306 * framebuffer is considered complete or incomplete:
1310 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1311 * containing an image that is attached to a framebuffer object
1312 * that is bound to the framebuffer."
1315 invalidate_framebuffer(fb
);
1322 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1325 GET_CURRENT_CONTEXT(ctx
);
1328 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1332 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1334 for (i
= 0; i
< n
; i
++) {
1335 if (renderbuffers
[i
] > 0) {
1336 struct gl_renderbuffer
*rb
;
1337 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1339 /* check if deleting currently bound renderbuffer object */
1340 if (rb
== ctx
->CurrentRenderbuffer
) {
1342 assert(rb
->RefCount
>= 2);
1343 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1346 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1347 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1348 * of the OpenGL 3.1 spec says:
1350 * "If a renderbuffer object is deleted while its image is
1351 * attached to one or more attachment points in the currently
1352 * bound framebuffer, then it is as if FramebufferRenderbuffer
1353 * had been called, with a renderbuffer of 0, for each
1354 * attachment point to which this image was attached in the
1355 * currently bound framebuffer. In other words, this
1356 * renderbuffer image is first detached from all attachment
1357 * points in the currently bound framebuffer. Note that the
1358 * renderbuffer image is specifically not detached from any
1359 * non-bound framebuffers. Detaching the image from any
1360 * non-bound framebuffers is the responsibility of the
1363 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1364 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1366 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1367 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1368 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1371 /* Remove from hash table immediately, to free the ID.
1372 * But the object will not be freed until it's no longer
1373 * referenced anywhere else.
1375 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1377 if (rb
!= &DummyRenderbuffer
) {
1378 /* no longer referenced by hash table */
1379 _mesa_reference_renderbuffer(&rb
, NULL
);
1388 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1390 GET_CURRENT_CONTEXT(ctx
);
1395 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1402 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1404 for (i
= 0; i
< n
; i
++) {
1405 GLuint name
= first
+ i
;
1406 renderbuffers
[i
] = name
;
1407 /* insert dummy placeholder into hash table */
1408 mtx_lock(&ctx
->Shared
->Mutex
);
1409 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1410 mtx_unlock(&ctx
->Shared
->Mutex
);
1416 * Given an internal format token for a render buffer, return the
1417 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1418 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1419 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1421 * This is similar to _mesa_base_tex_format() but the set of valid
1422 * internal formats is different.
1424 * Note that even if a format is determined to be legal here, validation
1425 * of the FBO may fail if the format is not supported by the driver/GPU.
1427 * \param internalFormat as passed to glRenderbufferStorage()
1428 * \return the base internal format, or 0 if internalFormat is illegal
1431 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1434 * Notes: some formats such as alpha, luminance, etc. were added
1435 * with GL_ARB_framebuffer_object.
1437 switch (internalFormat
) {
1443 return (ctx
->API
== API_OPENGL_COMPAT
&&
1444 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
1448 case GL_LUMINANCE12
:
1449 case GL_LUMINANCE16
:
1450 return (ctx
->API
== API_OPENGL_COMPAT
&&
1451 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
1452 case GL_LUMINANCE_ALPHA
:
1453 case GL_LUMINANCE4_ALPHA4
:
1454 case GL_LUMINANCE6_ALPHA2
:
1455 case GL_LUMINANCE8_ALPHA8
:
1456 case GL_LUMINANCE12_ALPHA4
:
1457 case GL_LUMINANCE12_ALPHA12
:
1458 case GL_LUMINANCE16_ALPHA16
:
1459 return (ctx
->API
== API_OPENGL_COMPAT
&&
1460 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
1464 case GL_INTENSITY12
:
1465 case GL_INTENSITY16
:
1466 return (ctx
->API
== API_OPENGL_COMPAT
&&
1467 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
1477 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1479 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1488 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1490 case GL_SRGB8_ALPHA8_EXT
:
1491 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1492 case GL_STENCIL_INDEX
:
1493 case GL_STENCIL_INDEX1_EXT
:
1494 case GL_STENCIL_INDEX4_EXT
:
1495 case GL_STENCIL_INDEX16_EXT
:
1496 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1497 * OpenGL ES, but Mesa does not currently support them.
1499 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1500 case GL_STENCIL_INDEX8_EXT
:
1501 return GL_STENCIL_INDEX
;
1502 case GL_DEPTH_COMPONENT
:
1503 case GL_DEPTH_COMPONENT32
:
1504 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1505 case GL_DEPTH_COMPONENT16
:
1506 case GL_DEPTH_COMPONENT24
:
1507 return GL_DEPTH_COMPONENT
;
1508 case GL_DEPTH_STENCIL
:
1509 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
1510 case GL_DEPTH24_STENCIL8
:
1511 return GL_DEPTH_STENCIL
;
1512 case GL_DEPTH_COMPONENT32F
:
1513 return ctx
->Version
>= 30
1514 || (ctx
->API
== API_OPENGL_COMPAT
&&
1515 ctx
->Extensions
.ARB_depth_buffer_float
)
1516 ? GL_DEPTH_COMPONENT
: 0;
1517 case GL_DEPTH32F_STENCIL8
:
1518 return ctx
->Version
>= 30
1519 || (ctx
->API
== API_OPENGL_COMPAT
&&
1520 ctx
->Extensions
.ARB_depth_buffer_float
)
1521 ? GL_DEPTH_STENCIL
: 0;
1524 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1527 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1531 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1534 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1536 /* signed normalized texture formats */
1540 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1545 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1549 case GL_RGB16_SNORM
:
1550 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1553 case GL_RGBA8_SNORM
:
1554 case GL_RGBA16_SNORM
:
1555 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1557 case GL_ALPHA_SNORM
:
1558 case GL_ALPHA8_SNORM
:
1559 case GL_ALPHA16_SNORM
:
1560 return ctx
->API
== API_OPENGL_COMPAT
&&
1561 ctx
->Extensions
.EXT_texture_snorm
&&
1562 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1563 case GL_LUMINANCE_SNORM
:
1564 case GL_LUMINANCE8_SNORM
:
1565 case GL_LUMINANCE16_SNORM
:
1566 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1568 case GL_LUMINANCE_ALPHA_SNORM
:
1569 case GL_LUMINANCE8_ALPHA8_SNORM
:
1570 case GL_LUMINANCE16_ALPHA16_SNORM
:
1571 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1572 ? GL_LUMINANCE_ALPHA
: 0;
1573 case GL_INTENSITY_SNORM
:
1574 case GL_INTENSITY8_SNORM
:
1575 case GL_INTENSITY16_SNORM
:
1576 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1581 return ((_mesa_is_desktop_gl(ctx
) &&
1582 ctx
->Extensions
.ARB_texture_rg
&&
1583 ctx
->Extensions
.ARB_texture_float
) ||
1584 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1588 return ((_mesa_is_desktop_gl(ctx
) &&
1589 ctx
->Extensions
.ARB_texture_rg
&&
1590 ctx
->Extensions
.ARB_texture_float
) ||
1591 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1595 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1599 return ((_mesa_is_desktop_gl(ctx
) &&
1600 ctx
->Extensions
.ARB_texture_float
) ||
1601 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1603 case GL_ALPHA16F_ARB
:
1604 case GL_ALPHA32F_ARB
:
1605 return ctx
->API
== API_OPENGL_COMPAT
&&
1606 ctx
->Extensions
.ARB_texture_float
&&
1607 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1608 case GL_LUMINANCE16F_ARB
:
1609 case GL_LUMINANCE32F_ARB
:
1610 return ctx
->API
== API_OPENGL_COMPAT
&&
1611 ctx
->Extensions
.ARB_texture_float
&&
1612 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1613 case GL_LUMINANCE_ALPHA16F_ARB
:
1614 case GL_LUMINANCE_ALPHA32F_ARB
:
1615 return ctx
->API
== API_OPENGL_COMPAT
&&
1616 ctx
->Extensions
.ARB_texture_float
&&
1617 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1618 case GL_INTENSITY16F_ARB
:
1619 case GL_INTENSITY32F_ARB
:
1620 return ctx
->API
== API_OPENGL_COMPAT
&&
1621 ctx
->Extensions
.ARB_texture_float
&&
1622 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1623 case GL_R11F_G11F_B10F
:
1624 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
1625 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
1628 case GL_RGBA8UI_EXT
:
1629 case GL_RGBA16UI_EXT
:
1630 case GL_RGBA32UI_EXT
:
1632 case GL_RGBA16I_EXT
:
1633 case GL_RGBA32I_EXT
:
1634 return ctx
->Version
>= 30
1635 || (_mesa_is_desktop_gl(ctx
) &&
1636 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1639 case GL_RGB16UI_EXT
:
1640 case GL_RGB32UI_EXT
:
1644 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
1652 return ctx
->Version
>= 30
1653 || (_mesa_is_desktop_gl(ctx
) &&
1654 ctx
->Extensions
.ARB_texture_rg
&&
1655 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1663 return ctx
->Version
>= 30
1664 || (_mesa_is_desktop_gl(ctx
) &&
1665 ctx
->Extensions
.ARB_texture_rg
&&
1666 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1668 case GL_INTENSITY8I_EXT
:
1669 case GL_INTENSITY8UI_EXT
:
1670 case GL_INTENSITY16I_EXT
:
1671 case GL_INTENSITY16UI_EXT
:
1672 case GL_INTENSITY32I_EXT
:
1673 case GL_INTENSITY32UI_EXT
:
1674 return ctx
->API
== API_OPENGL_COMPAT
&&
1675 ctx
->Extensions
.EXT_texture_integer
&&
1676 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1678 case GL_LUMINANCE8I_EXT
:
1679 case GL_LUMINANCE8UI_EXT
:
1680 case GL_LUMINANCE16I_EXT
:
1681 case GL_LUMINANCE16UI_EXT
:
1682 case GL_LUMINANCE32I_EXT
:
1683 case GL_LUMINANCE32UI_EXT
:
1684 return ctx
->API
== API_OPENGL_COMPAT
&&
1685 ctx
->Extensions
.EXT_texture_integer
&&
1686 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1688 case GL_LUMINANCE_ALPHA8I_EXT
:
1689 case GL_LUMINANCE_ALPHA8UI_EXT
:
1690 case GL_LUMINANCE_ALPHA16I_EXT
:
1691 case GL_LUMINANCE_ALPHA16UI_EXT
:
1692 case GL_LUMINANCE_ALPHA32I_EXT
:
1693 case GL_LUMINANCE_ALPHA32UI_EXT
:
1694 return ctx
->API
== API_OPENGL_COMPAT
&&
1695 ctx
->Extensions
.EXT_texture_integer
&&
1696 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1698 case GL_ALPHA8I_EXT
:
1699 case GL_ALPHA8UI_EXT
:
1700 case GL_ALPHA16I_EXT
:
1701 case GL_ALPHA16UI_EXT
:
1702 case GL_ALPHA32I_EXT
:
1703 case GL_ALPHA32UI_EXT
:
1704 return ctx
->API
== API_OPENGL_COMPAT
&&
1705 ctx
->Extensions
.EXT_texture_integer
&&
1706 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1709 return (_mesa_is_desktop_gl(ctx
) &&
1710 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1711 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1714 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1723 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1726 invalidate_rb(GLuint key
, void *data
, void *userData
)
1728 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1729 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1731 /* If this is a user-created FBO */
1732 if (_mesa_is_user_fbo(fb
)) {
1734 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1735 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1736 if (att
->Type
== GL_RENDERBUFFER
&&
1737 att
->Renderbuffer
== rb
) {
1738 /* Mark fb status as indeterminate to force re-validation */
1747 /** sentinal value, see below */
1748 #define NO_SAMPLES 1000
1752 * Helper function used by _mesa_RenderbufferStorage() and
1753 * _mesa_RenderbufferStorageMultisample().
1754 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1757 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1758 GLsizei width
, GLsizei height
, GLsizei samples
)
1760 const char *func
= samples
== NO_SAMPLES
?
1761 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1762 struct gl_renderbuffer
*rb
;
1764 GLenum sample_count_error
;
1765 GET_CURRENT_CONTEXT(ctx
);
1767 if (MESA_VERBOSE
& VERBOSE_API
) {
1768 if (samples
== NO_SAMPLES
)
1769 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
1771 _mesa_lookup_enum_by_nr(target
),
1772 _mesa_lookup_enum_by_nr(internalFormat
),
1775 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
1777 _mesa_lookup_enum_by_nr(target
),
1778 _mesa_lookup_enum_by_nr(internalFormat
),
1779 width
, height
, samples
);
1782 if (target
!= GL_RENDERBUFFER_EXT
) {
1783 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1787 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1788 if (baseFormat
== 0) {
1789 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
1790 func
, _mesa_lookup_enum_by_nr(internalFormat
));
1794 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1795 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1799 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1800 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1804 if (samples
== NO_SAMPLES
) {
1805 /* NumSamples == 0 indicates non-multisampling */
1809 /* check the sample count;
1810 * note: driver may choose to use more samples than what's requested
1812 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
1813 internalFormat
, samples
);
1814 if (sample_count_error
!= GL_NO_ERROR
) {
1815 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
1820 rb
= ctx
->CurrentRenderbuffer
;
1822 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1826 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1828 if (rb
->InternalFormat
== internalFormat
&&
1829 rb
->Width
== (GLuint
) width
&&
1830 rb
->Height
== (GLuint
) height
&&
1831 rb
->NumSamples
== samples
) {
1832 /* no change in allocation needed */
1836 /* These MUST get set by the AllocStorage func */
1837 rb
->Format
= MESA_FORMAT_NONE
;
1838 rb
->NumSamples
= samples
;
1840 /* Now allocate the storage */
1841 assert(rb
->AllocStorage
);
1842 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1843 /* No error - check/set fields now */
1844 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1845 assert(rb
->Width
== (GLuint
) width
);
1846 assert(rb
->Height
== (GLuint
) height
);
1847 rb
->InternalFormat
= internalFormat
;
1848 rb
->_BaseFormat
= baseFormat
;
1849 assert(rb
->_BaseFormat
!= 0);
1852 /* Probably ran out of memory - clear the fields */
1855 rb
->Format
= MESA_FORMAT_NONE
;
1856 rb
->InternalFormat
= GL_NONE
;
1857 rb
->_BaseFormat
= GL_NONE
;
1861 /* Invalidate the framebuffers the renderbuffer is attached in. */
1862 if (rb
->AttachedAnytime
) {
1863 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1869 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1871 struct gl_renderbuffer
*rb
;
1872 GET_CURRENT_CONTEXT(ctx
);
1874 if (!ctx
->Extensions
.OES_EGL_image
) {
1875 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1876 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1880 if (target
!= GL_RENDERBUFFER
) {
1881 _mesa_error(ctx
, GL_INVALID_ENUM
,
1882 "EGLImageTargetRenderbufferStorageOES");
1886 rb
= ctx
->CurrentRenderbuffer
;
1888 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1889 "EGLImageTargetRenderbufferStorageOES");
1893 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1895 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1900 * Helper function for _mesa_GetRenderbufferParameteriv() and
1901 * _mesa_GetFramebufferAttachmentParameteriv()
1902 * We have to be careful to respect the base format. For example, if a
1903 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1904 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1905 * we need to return zero.
1908 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
1910 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1911 return _mesa_get_format_bits(format
, pname
);
1919 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1920 GLsizei width
, GLsizei height
)
1922 /* GL_ARB_fbo says calling this function is equivalent to calling
1923 * glRenderbufferStorageMultisample() with samples=0. We pass in
1924 * a token value here just for error reporting purposes.
1926 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1931 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1932 GLenum internalFormat
,
1933 GLsizei width
, GLsizei height
)
1935 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1940 * OpenGL ES version of glRenderBufferStorage.
1943 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1944 GLsizei width
, GLsizei height
)
1946 switch (internalFormat
) {
1948 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1949 /* choose a closest format */
1950 internalFormat
= GL_RGB5
;
1956 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1961 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1963 struct gl_renderbuffer
*rb
;
1964 GET_CURRENT_CONTEXT(ctx
);
1966 if (target
!= GL_RENDERBUFFER_EXT
) {
1967 _mesa_error(ctx
, GL_INVALID_ENUM
,
1968 "glGetRenderbufferParameterivEXT(target)");
1972 rb
= ctx
->CurrentRenderbuffer
;
1974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1975 "glGetRenderbufferParameterivEXT");
1979 /* No need to flush here since we're just quering state which is
1980 * not effected by rendering.
1984 case GL_RENDERBUFFER_WIDTH_EXT
:
1985 *params
= rb
->Width
;
1987 case GL_RENDERBUFFER_HEIGHT_EXT
:
1988 *params
= rb
->Height
;
1990 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1991 *params
= rb
->InternalFormat
;
1993 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1994 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1995 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1996 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1997 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1998 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1999 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2001 case GL_RENDERBUFFER_SAMPLES
:
2002 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2003 || _mesa_is_gles3(ctx
)) {
2004 *params
= rb
->NumSamples
;
2009 _mesa_error(ctx
, GL_INVALID_ENUM
,
2010 "glGetRenderbufferParameterivEXT(target)");
2016 GLboolean GLAPIENTRY
2017 _mesa_IsFramebuffer(GLuint framebuffer
)
2019 GET_CURRENT_CONTEXT(ctx
);
2020 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2022 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2023 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2031 * Check if any of the attachments of the given framebuffer are textures
2032 * (render to texture). Call ctx->Driver.RenderTexture() for such
2036 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2039 assert(ctx
->Driver
.RenderTexture
);
2041 if (_mesa_is_winsys_fbo(fb
))
2042 return; /* can't render to texture with winsys framebuffers */
2044 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2045 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2046 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2047 && driver_RenderTexture_is_safe(att
)) {
2048 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2055 * Examine all the framebuffer's attachments to see if any are textures.
2056 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2057 * notify the device driver that the texture image may have changed.
2060 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2062 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2063 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2066 if (ctx
->Driver
.FinishRenderTexture
) {
2068 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2069 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2070 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2071 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2072 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2080 bind_framebuffer(GLenum target
, GLuint framebuffer
, bool allow_user_names
)
2082 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2083 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
2084 GLboolean bindReadBuf
, bindDrawBuf
;
2085 GET_CURRENT_CONTEXT(ctx
);
2088 case GL_DRAW_FRAMEBUFFER_EXT
:
2089 bindDrawBuf
= GL_TRUE
;
2090 bindReadBuf
= GL_FALSE
;
2092 case GL_READ_FRAMEBUFFER_EXT
:
2093 bindDrawBuf
= GL_FALSE
;
2094 bindReadBuf
= GL_TRUE
;
2096 case GL_FRAMEBUFFER_EXT
:
2097 bindDrawBuf
= GL_TRUE
;
2098 bindReadBuf
= GL_TRUE
;
2101 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2106 /* Binding a user-created framebuffer object */
2107 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2108 if (newDrawFb
== &DummyFramebuffer
) {
2109 /* ID was reserved, but no real framebuffer object made yet */
2112 else if (!newDrawFb
&& !allow_user_names
) {
2113 /* All FBO IDs must be Gen'd */
2114 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
2119 /* create new framebuffer object */
2120 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2122 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2125 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2127 newReadFb
= newDrawFb
;
2130 /* Binding the window system framebuffer (which was originally set
2131 * with MakeCurrent).
2133 newDrawFb
= ctx
->WinSysDrawBuffer
;
2134 newReadFb
= ctx
->WinSysReadBuffer
;
2138 assert(newDrawFb
!= &DummyFramebuffer
);
2140 /* save pointers to current/old framebuffers */
2141 oldDrawFb
= ctx
->DrawBuffer
;
2142 oldReadFb
= ctx
->ReadBuffer
;
2144 /* check if really changing bindings */
2145 if (oldDrawFb
== newDrawFb
)
2146 bindDrawBuf
= GL_FALSE
;
2147 if (oldReadFb
== newReadFb
)
2148 bindReadBuf
= GL_FALSE
;
2151 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2153 * We also check if we're beginning and/or ending render-to-texture.
2154 * When a framebuffer with texture attachments is unbound, call
2155 * ctx->Driver.FinishRenderTexture().
2156 * When a framebuffer with texture attachments is bound, call
2157 * ctx->Driver.RenderTexture().
2159 * Note that if the ReadBuffer has texture attachments we don't consider
2160 * that a render-to-texture case.
2163 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2165 /* check if old readbuffer was render-to-texture */
2166 check_end_texture_render(ctx
, oldReadFb
);
2168 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2172 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2174 /* check if old framebuffer had any texture attachments */
2176 check_end_texture_render(ctx
, oldDrawFb
);
2178 /* check if newly bound framebuffer has any texture attachments */
2179 check_begin_texture_render(ctx
, newDrawFb
);
2181 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2184 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2185 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
2190 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2192 GET_CURRENT_CONTEXT(ctx
);
2194 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2195 * point, but they allow the use of user-generated names.
2197 bind_framebuffer(target
, framebuffer
, _mesa_is_gles(ctx
));
2202 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2204 /* This function should not be in the dispatch table for core profile /
2205 * OpenGL 3.1, so execution should never get here in those cases -- no
2206 * need for an explicit test.
2208 bind_framebuffer(target
, framebuffer
, true);
2213 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2216 GET_CURRENT_CONTEXT(ctx
);
2219 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2223 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2225 for (i
= 0; i
< n
; i
++) {
2226 if (framebuffers
[i
] > 0) {
2227 struct gl_framebuffer
*fb
;
2228 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
2230 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
2232 /* check if deleting currently bound framebuffer object */
2233 if (fb
== ctx
->DrawBuffer
) {
2235 assert(fb
->RefCount
>= 2);
2236 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
2238 if (fb
== ctx
->ReadBuffer
) {
2240 assert(fb
->RefCount
>= 2);
2241 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
2244 /* remove from hash table immediately, to free the ID */
2245 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
2247 if (fb
!= &DummyFramebuffer
) {
2248 /* But the object will not be freed until it's no longer
2249 * bound in any context.
2251 _mesa_reference_framebuffer(&fb
, NULL
);
2260 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
2262 GET_CURRENT_CONTEXT(ctx
);
2267 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
2274 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
2276 for (i
= 0; i
< n
; i
++) {
2277 GLuint name
= first
+ i
;
2278 framebuffers
[i
] = name
;
2279 /* insert dummy placeholder into hash table */
2280 mtx_lock(&ctx
->Shared
->Mutex
);
2281 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
2282 mtx_unlock(&ctx
->Shared
->Mutex
);
2288 _mesa_CheckFramebufferStatus(GLenum target
)
2290 struct gl_framebuffer
*buffer
;
2291 GET_CURRENT_CONTEXT(ctx
);
2293 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2295 if (MESA_VERBOSE
& VERBOSE_API
)
2296 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
2297 _mesa_lookup_enum_by_nr(target
));
2299 buffer
= get_framebuffer_target(ctx
, target
);
2301 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
2305 if (_mesa_is_winsys_fbo(buffer
)) {
2306 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2307 if (buffer
!= &IncompleteFramebuffer
) {
2308 return GL_FRAMEBUFFER_COMPLETE_EXT
;
2310 return GL_FRAMEBUFFER_UNDEFINED
;
2314 /* No need to flush here */
2316 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
2317 _mesa_test_framebuffer_completeness(ctx
, buffer
);
2320 return buffer
->_Status
;
2325 * Replicate the src attachment point. Used by framebuffer_texture() when
2326 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2327 * GL_STENCIL_ATTACHMENT.
2330 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
2331 gl_buffer_index dst
,
2332 gl_buffer_index src
)
2334 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
2335 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
2337 assert(src_att
->Texture
!= NULL
);
2338 assert(src_att
->Renderbuffer
!= NULL
);
2340 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
2341 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
2342 dst_att
->Type
= src_att
->Type
;
2343 dst_att
->Complete
= src_att
->Complete
;
2344 dst_att
->TextureLevel
= src_att
->TextureLevel
;
2345 dst_att
->Zoffset
= src_att
->Zoffset
;
2350 * Common code called by glFramebufferTexture1D/2D/3D() and
2351 * glFramebufferTextureLayer().
2353 * \param textarget is the textarget that was passed to the
2354 * glFramebufferTexture...() function, or 0 if the corresponding function
2355 * doesn't have a textarget parameter.
2357 * \param layered is true if this function was called from
2358 * glFramebufferTexture(), false otherwise.
2361 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
2362 GLenum attachment
, GLenum textarget
, GLuint texture
,
2363 GLint level
, GLuint zoffset
, GLboolean layered
)
2365 struct gl_renderbuffer_attachment
*att
;
2366 struct gl_texture_object
*texObj
= NULL
;
2367 struct gl_framebuffer
*fb
;
2368 GLenum maxLevelsTarget
;
2370 fb
= get_framebuffer_target(ctx
, target
);
2372 _mesa_error(ctx
, GL_INVALID_ENUM
,
2373 "glFramebufferTexture%s(target=0x%x)", caller
, target
);
2377 /* check framebuffer binding */
2378 if (_mesa_is_winsys_fbo(fb
)) {
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2380 "glFramebufferTexture%s", caller
);
2384 /* The textarget, level, and zoffset parameters are only validated if
2385 * texture is non-zero.
2388 GLboolean err
= GL_TRUE
;
2390 texObj
= _mesa_lookup_texture(ctx
, texture
);
2391 if (texObj
!= NULL
) {
2392 if (textarget
== 0) {
2394 /* We're being called by glFramebufferTexture() and textarget
2397 switch (texObj
->Target
) {
2399 case GL_TEXTURE_1D_ARRAY_EXT
:
2400 case GL_TEXTURE_2D_ARRAY_EXT
:
2401 case GL_TEXTURE_CUBE_MAP
:
2402 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2403 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2408 case GL_TEXTURE_RECTANGLE
:
2409 case GL_TEXTURE_2D_MULTISAMPLE
:
2410 /* These texture types are valid to pass to
2411 * glFramebufferTexture(), but since they aren't layered, it
2412 * is equivalent to calling glFramebufferTexture{1D,2D}().
2416 textarget
= texObj
->Target
;
2423 /* We're being called by glFramebufferTextureLayer() and
2424 * textarget is not used. The only legal texture types for
2425 * that function are 3D and 1D/2D arrays textures.
2427 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2428 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2429 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2430 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) &&
2431 (texObj
->Target
!= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
2435 /* Make sure textarget is consistent with the texture's type */
2436 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2437 ? !_mesa_is_cube_face(textarget
)
2438 : (texObj
->Target
!= textarget
);
2442 /* can't render to a non-existant texture */
2443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2444 "glFramebufferTexture%s(non existant texture)",
2450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2451 "glFramebufferTexture%s(texture target mismatch)",
2456 if (texObj
->Target
== GL_TEXTURE_3D
) {
2457 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2458 if (zoffset
>= maxSize
) {
2459 _mesa_error(ctx
, GL_INVALID_VALUE
,
2460 "glFramebufferTexture%s(zoffset)", caller
);
2464 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2465 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2466 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
2467 (texObj
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
2468 if (zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
2469 _mesa_error(ctx
, GL_INVALID_VALUE
,
2470 "glFramebufferTexture%s(layer)", caller
);
2475 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2477 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2478 _mesa_error(ctx
, GL_INVALID_VALUE
,
2479 "glFramebufferTexture%s(level)", caller
);
2484 att
= get_attachment(ctx
, fb
, attachment
);
2486 _mesa_error(ctx
, GL_INVALID_ENUM
,
2487 "glFramebufferTexture%s(attachment)", caller
);
2491 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2493 mtx_lock(&fb
->Mutex
);
2495 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2496 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2497 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2498 _mesa_tex_target_to_face(textarget
) ==
2499 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2500 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2501 /* The texture object is already attached to the stencil attachment
2502 * point. Don't create a new renderbuffer; just reuse the stencil
2503 * attachment's. This is required to prevent a GL error in
2504 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2506 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2508 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2509 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2510 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2511 _mesa_tex_target_to_face(textarget
) ==
2512 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2513 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2514 /* As above, but with depth and stencil transposed. */
2515 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2518 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2519 level
, zoffset
, layered
);
2520 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2521 /* Above we created a new renderbuffer and attached it to the
2522 * depth attachment point. Now attach it to the stencil attachment
2525 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2526 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2531 /* Set the render-to-texture flag. We'll check this flag in
2532 * glTexImage() and friends to determine if we need to revalidate
2533 * any FBOs that might be rendering into this texture.
2534 * This flag never gets cleared since it's non-trivial to determine
2535 * when all FBOs might be done rendering to this texture. That's OK
2536 * though since it's uncommon to render to a texture then repeatedly
2537 * call glTexImage() to change images in the texture.
2539 texObj
->_RenderToTexture
= GL_TRUE
;
2542 remove_attachment(ctx
, att
);
2543 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2544 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2545 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2549 invalidate_framebuffer(fb
);
2551 mtx_unlock(&fb
->Mutex
);
2556 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2557 GLenum textarget
, GLuint texture
, GLint level
)
2559 GET_CURRENT_CONTEXT(ctx
);
2564 switch (textarget
) {
2568 case GL_TEXTURE_1D_ARRAY
:
2569 error
= !ctx
->Extensions
.EXT_texture_array
;
2576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2577 "glFramebufferTexture1D(textarget=%s)",
2578 _mesa_lookup_enum_by_nr(textarget
));
2583 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2584 level
, 0, GL_FALSE
);
2589 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2590 GLenum textarget
, GLuint texture
, GLint level
)
2592 GET_CURRENT_CONTEXT(ctx
);
2597 switch (textarget
) {
2601 case GL_TEXTURE_RECTANGLE
:
2602 error
= _mesa_is_gles(ctx
)
2603 || !ctx
->Extensions
.NV_texture_rectangle
;
2605 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2606 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2607 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2608 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2609 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2610 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2611 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2613 case GL_TEXTURE_2D_ARRAY
:
2614 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2615 || !ctx
->Extensions
.EXT_texture_array
;
2617 case GL_TEXTURE_2D_MULTISAMPLE
:
2618 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2619 error
= _mesa_is_gles(ctx
)
2620 || !ctx
->Extensions
.ARB_texture_multisample
;
2627 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2628 "glFramebufferTexture2D(textarget=%s)",
2629 _mesa_lookup_enum_by_nr(textarget
));
2634 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2635 level
, 0, GL_FALSE
);
2640 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2641 GLenum textarget
, GLuint texture
,
2642 GLint level
, GLint zoffset
)
2644 GET_CURRENT_CONTEXT(ctx
);
2646 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2647 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2648 "glFramebufferTexture3D(textarget)");
2652 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2653 level
, zoffset
, GL_FALSE
);
2658 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2659 GLuint texture
, GLint level
, GLint layer
)
2661 GET_CURRENT_CONTEXT(ctx
);
2663 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2664 level
, layer
, GL_FALSE
);
2669 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
2670 GLuint texture
, GLint level
)
2672 GET_CURRENT_CONTEXT(ctx
);
2674 if (_mesa_has_geometry_shaders(ctx
)) {
2675 framebuffer_texture(ctx
, "", target
, attachment
, 0, texture
,
2678 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2679 "unsupported function (glFramebufferTexture) called");
2685 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2686 GLenum renderbufferTarget
,
2687 GLuint renderbuffer
)
2689 struct gl_renderbuffer_attachment
*att
;
2690 struct gl_framebuffer
*fb
;
2691 struct gl_renderbuffer
*rb
;
2692 GET_CURRENT_CONTEXT(ctx
);
2694 fb
= get_framebuffer_target(ctx
, target
);
2696 _mesa_error(ctx
, GL_INVALID_ENUM
,
2697 "glFramebufferRenderbuffer(target)");
2701 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2702 _mesa_error(ctx
, GL_INVALID_ENUM
,
2703 "glFramebufferRenderbuffer(renderbufferTarget)");
2707 if (_mesa_is_winsys_fbo(fb
)) {
2708 /* Can't attach new renderbuffers to a window system framebuffer */
2709 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbuffer");
2713 att
= get_attachment(ctx
, fb
, attachment
);
2715 _mesa_error(ctx
, GL_INVALID_ENUM
,
2716 "glFramebufferRenderbuffer(invalid attachment %s)",
2717 _mesa_lookup_enum_by_nr(attachment
));
2722 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2724 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2725 "glFramebufferRenderbuffer(non-existant"
2726 " renderbuffer %u)", renderbuffer
);
2729 else if (rb
== &DummyRenderbuffer
) {
2730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2731 "glFramebufferRenderbuffer(renderbuffer %u)",
2737 /* remove renderbuffer attachment */
2741 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2742 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2743 /* make sure the renderbuffer is a depth/stencil format */
2744 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2745 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2746 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2747 "glFramebufferRenderbuffer(renderbuffer"
2748 " is not DEPTH_STENCIL format)");
2753 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2755 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2756 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2758 /* Some subsequent GL commands may depend on the framebuffer's visual
2759 * after the binding is updated. Update visual info now.
2761 _mesa_update_framebuffer_visual(ctx
, fb
);
2766 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2767 GLenum pname
, GLint
*params
)
2769 const struct gl_renderbuffer_attachment
*att
;
2770 struct gl_framebuffer
*buffer
;
2772 GET_CURRENT_CONTEXT(ctx
);
2774 /* The error differs in GL and GLES. */
2775 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2777 buffer
= get_framebuffer_target(ctx
, target
);
2779 _mesa_error(ctx
, GL_INVALID_ENUM
,
2780 "glGetFramebufferAttachmentParameteriv(target)");
2784 if (_mesa_is_winsys_fbo(buffer
)) {
2785 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2788 * "If the framebuffer currently bound to target is zero, then
2789 * INVALID_OPERATION is generated."
2791 * The EXT_framebuffer_object spec has the same wording, and the
2792 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2795 if ((!_mesa_is_desktop_gl(ctx
) ||
2796 !ctx
->Extensions
.ARB_framebuffer_object
)
2797 && !_mesa_is_gles3(ctx
)) {
2798 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2799 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2803 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
2804 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
2805 _mesa_error(ctx
, GL_INVALID_ENUM
,
2806 "glGetFramebufferAttachmentParameteriv(attachment)");
2809 /* the default / window-system FBO */
2810 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2813 /* user-created framebuffer FBO */
2814 att
= get_attachment(ctx
, buffer
, attachment
);
2818 _mesa_error(ctx
, GL_INVALID_ENUM
,
2819 "glGetFramebufferAttachmentParameteriv(attachment)");
2823 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2824 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2825 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
2826 /* This behavior is first specified in OpenGL 4.4 specification.
2828 * From the OpenGL 4.4 spec page 275:
2829 * "This query cannot be performed for a combined depth+stencil
2830 * attachment, since it does not have a single format."
2832 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2833 "glGetFramebufferAttachmentParameteriv("
2834 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
2835 " is invalid for depth+stencil attachment)");
2838 /* the depth and stencil attachments must point to the same buffer */
2839 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2840 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2841 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2842 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2843 "glGetFramebufferAttachmentParameteriv(DEPTH/STENCIL"
2844 " attachments differ)");
2849 /* No need to flush here */
2852 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2853 *params
= _mesa_is_winsys_fbo(buffer
)
2854 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2856 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2857 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2858 *params
= att
->Renderbuffer
->Name
;
2860 else if (att
->Type
== GL_TEXTURE
) {
2861 *params
= att
->Texture
->Name
;
2864 assert(att
->Type
== GL_NONE
);
2865 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2868 goto invalid_pname_enum
;
2872 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2873 if (att
->Type
== GL_TEXTURE
) {
2874 *params
= att
->TextureLevel
;
2876 else if (att
->Type
== GL_NONE
) {
2877 _mesa_error(ctx
, err
,
2878 "glGetFramebufferAttachmentParameteriv(pname)");
2881 goto invalid_pname_enum
;
2884 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2885 if (att
->Type
== GL_TEXTURE
) {
2886 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2887 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2893 else if (att
->Type
== GL_NONE
) {
2894 _mesa_error(ctx
, err
,
2895 "glGetFramebufferAttachmentParameteriv(pname)");
2898 goto invalid_pname_enum
;
2901 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2902 if (ctx
->API
== API_OPENGLES
) {
2903 goto invalid_pname_enum
;
2904 } else if (att
->Type
== GL_NONE
) {
2905 _mesa_error(ctx
, err
,
2906 "glGetFramebufferAttachmentParameteriv(pname)");
2907 } else if (att
->Type
== GL_TEXTURE
) {
2908 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
2909 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
2910 *params
= att
->Zoffset
;
2917 goto invalid_pname_enum
;
2920 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2921 if ((!_mesa_is_desktop_gl(ctx
) ||
2922 !ctx
->Extensions
.ARB_framebuffer_object
)
2923 && !_mesa_is_gles3(ctx
)) {
2924 goto invalid_pname_enum
;
2926 else if (att
->Type
== GL_NONE
) {
2927 _mesa_error(ctx
, err
,
2928 "glGetFramebufferAttachmentParameteriv(pname)");
2931 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2933 _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2936 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2937 * if the sRGB conversion is unsupported. */
2938 *params
= GL_LINEAR
;
2942 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2943 if ((ctx
->API
!= API_OPENGL_COMPAT
||
2944 !ctx
->Extensions
.ARB_framebuffer_object
)
2945 && ctx
->API
!= API_OPENGL_CORE
2946 && !_mesa_is_gles3(ctx
)) {
2947 goto invalid_pname_enum
;
2949 else if (att
->Type
== GL_NONE
) {
2950 _mesa_error(ctx
, err
,
2951 "glGetFramebufferAttachmentParameteriv(pname)");
2954 mesa_format format
= att
->Renderbuffer
->Format
;
2956 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2959 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2960 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2961 * generate an INVALID_OPERATION error.
2963 if (_mesa_is_gles3(ctx
) &&
2964 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2965 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2966 "glGetFramebufferAttachmentParameteriv(cannot query "
2967 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2968 "GL_DEPTH_STENCIL_ATTACHMENT");
2972 if (format
== MESA_FORMAT_S_UINT8
) {
2976 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
2977 /* depends on the attachment parameter */
2978 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2986 *params
= _mesa_get_format_datatype(format
);
2990 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2991 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2992 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2993 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2994 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2995 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2996 if ((!_mesa_is_desktop_gl(ctx
) ||
2997 !ctx
->Extensions
.ARB_framebuffer_object
)
2998 && !_mesa_is_gles3(ctx
)) {
2999 goto invalid_pname_enum
;
3001 else if (att
->Type
== GL_NONE
) {
3002 _mesa_error(ctx
, err
,
3003 "glGetFramebufferAttachmentParameteriv(pname)");
3005 else if (att
->Texture
) {
3006 const struct gl_texture_image
*texImage
=
3007 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
3010 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
3011 texImage
->TexFormat
);
3017 else if (att
->Renderbuffer
) {
3018 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
3019 att
->Renderbuffer
->Format
);
3022 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
3023 " invalid FBO attachment structure");
3026 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
3027 if (!_mesa_has_geometry_shaders(ctx
)) {
3028 goto invalid_pname_enum
;
3029 } else if (att
->Type
== GL_TEXTURE
) {
3030 *params
= att
->Layered
;
3031 } else if (att
->Type
== GL_NONE
) {
3032 _mesa_error(ctx
, err
,
3033 "glGetFramebufferAttachmentParameteriv(pname)");
3035 goto invalid_pname_enum
;
3039 goto invalid_pname_enum
;
3045 _mesa_error(ctx
, GL_INVALID_ENUM
,
3046 "glGetFramebufferAttachmentParameteriv(pname)");
3052 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3053 const GLenum
*attachments
, GLint x
, GLint y
,
3054 GLsizei width
, GLsizei height
, const char *name
)
3057 struct gl_framebuffer
*fb
;
3058 GET_CURRENT_CONTEXT(ctx
);
3060 fb
= get_framebuffer_target(ctx
, target
);
3062 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3066 if (numAttachments
< 0) {
3067 _mesa_error(ctx
, GL_INVALID_VALUE
,
3068 "%s(numAttachments < 0)", name
);
3072 /* The GL_ARB_invalidate_subdata spec says:
3074 * "If an attachment is specified that does not exist in the
3075 * framebuffer bound to <target>, it is ignored."
3079 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3080 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3081 * INVALID_OPERATION is generated."
3083 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3084 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3085 * set of retrictions).
3087 for (i
= 0; i
< numAttachments
; i
++) {
3088 if (_mesa_is_winsys_fbo(fb
)) {
3089 switch (attachments
[i
]) {
3095 /* Accumulation buffers and auxilary buffers were removed in
3096 * OpenGL 3.1, and they never existed in OpenGL ES.
3098 if (ctx
->API
!= API_OPENGL_COMPAT
)
3108 case GL_FRONT_RIGHT
:
3109 if (!_mesa_is_desktop_gl(ctx
))
3116 switch (attachments
[i
]) {
3117 case GL_DEPTH_ATTACHMENT
:
3118 case GL_STENCIL_ATTACHMENT
:
3120 case GL_DEPTH_STENCIL_ATTACHMENT
:
3121 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3122 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3123 * extension does not make this attachment point valid on ES 2.0.
3125 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
3128 case GL_COLOR_ATTACHMENT0
:
3129 case GL_COLOR_ATTACHMENT1
:
3130 case GL_COLOR_ATTACHMENT2
:
3131 case GL_COLOR_ATTACHMENT3
:
3132 case GL_COLOR_ATTACHMENT4
:
3133 case GL_COLOR_ATTACHMENT5
:
3134 case GL_COLOR_ATTACHMENT6
:
3135 case GL_COLOR_ATTACHMENT7
:
3136 case GL_COLOR_ATTACHMENT8
:
3137 case GL_COLOR_ATTACHMENT9
:
3138 case GL_COLOR_ATTACHMENT10
:
3139 case GL_COLOR_ATTACHMENT11
:
3140 case GL_COLOR_ATTACHMENT12
:
3141 case GL_COLOR_ATTACHMENT13
:
3142 case GL_COLOR_ATTACHMENT14
:
3143 case GL_COLOR_ATTACHMENT15
: {
3144 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3145 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3146 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3147 "%s(attachment >= max. color attachments)", name
);
3158 /* We don't actually do anything for this yet. Just return after
3159 * validating the parameters and generating the required errors.
3164 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3170 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3171 const GLenum
*attachments
, GLint x
, GLint y
,
3172 GLsizei width
, GLsizei height
)
3174 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3175 x
, y
, width
, height
,
3176 "glInvalidateSubFramebuffer");
3181 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3182 const GLenum
*attachments
)
3184 /* The GL_ARB_invalidate_subdata spec says:
3188 * void InvalidateFramebuffer(enum target,
3189 * sizei numAttachments,
3190 * const enum *attachments);
3192 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3193 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3194 * <MAX_VIEWPORT_DIMS[1]> respectively."
3196 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3198 MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3199 "glInvalidateFramebuffer");
3204 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
3205 const GLenum
*attachments
)
3207 struct gl_framebuffer
*fb
;
3210 GET_CURRENT_CONTEXT(ctx
);
3212 fb
= get_framebuffer_target(ctx
, target
);
3214 _mesa_error(ctx
, GL_INVALID_ENUM
,
3215 "glDiscardFramebufferEXT(target %s)",
3216 _mesa_lookup_enum_by_nr(target
));
3220 if (numAttachments
< 0) {
3221 _mesa_error(ctx
, GL_INVALID_VALUE
,
3222 "glDiscardFramebufferEXT(numAttachments < 0)");
3226 for (i
= 0; i
< numAttachments
; i
++) {
3227 switch (attachments
[i
]) {
3231 if (_mesa_is_user_fbo(fb
))
3234 case GL_COLOR_ATTACHMENT0
:
3235 case GL_DEPTH_ATTACHMENT
:
3236 case GL_STENCIL_ATTACHMENT
:
3237 if (_mesa_is_winsys_fbo(fb
))
3245 if (ctx
->Driver
.DiscardFramebuffer
)
3246 ctx
->Driver
.DiscardFramebuffer(ctx
, target
, numAttachments
, attachments
);
3251 _mesa_error(ctx
, GL_INVALID_ENUM
,
3252 "glDiscardFramebufferEXT(attachment %s)",
3253 _mesa_lookup_enum_by_nr(attachments
[i
]));