2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * GL_EXT/ARB_framebuffer_object extensions
41 #include "framebuffer.h"
42 #include "glformats.h"
45 #include "mfeatures.h"
47 #include "renderbuffer.h"
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
60 * None of the GL_EXT_framebuffer_object functions are compiled into
67 * When glGenRender/FramebuffersEXT() is called we insert pointers to
68 * these placeholder objects into the hash table.
69 * Later, when the object ID is first bound, we replace the placeholder
70 * with the real frame/renderbuffer.
72 static struct gl_framebuffer DummyFramebuffer
;
73 static struct gl_renderbuffer DummyRenderbuffer
;
75 /* We bind this framebuffer when applications pass a NULL
76 * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer
;
81 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
87 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
94 _mesa_init_fbobjects(struct gl_context
*ctx
)
96 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
97 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
98 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
99 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
101 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
104 struct gl_framebuffer
*
105 _mesa_get_incomplete_framebuffer(void)
107 return &IncompleteFramebuffer
;
111 * Helper routine for getting a gl_renderbuffer.
113 struct gl_renderbuffer
*
114 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
116 struct gl_renderbuffer
*rb
;
121 rb
= (struct gl_renderbuffer
*)
122 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
128 * Helper routine for getting a gl_framebuffer.
130 struct gl_framebuffer
*
131 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
133 struct gl_framebuffer
*fb
;
138 fb
= (struct gl_framebuffer
*)
139 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
145 * Mark the given framebuffer as invalid. This will force the
146 * test for framebuffer completeness to be done before the framebuffer
150 invalidate_framebuffer(struct gl_framebuffer
*fb
)
152 fb
->_Status
= 0; /* "indeterminate" */
157 * Return the gl_framebuffer object which corresponds to the given
158 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159 * Check support for GL_EXT_framebuffer_blit to determine if certain
161 * \return gl_framebuffer pointer or NULL if target is illegal
163 static struct gl_framebuffer
*
164 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
166 bool have_fb_blit
= _mesa_is_gles3(ctx
) ||
167 (ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
));
169 case GL_DRAW_FRAMEBUFFER
:
170 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
171 case GL_READ_FRAMEBUFFER
:
172 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
173 case GL_FRAMEBUFFER_EXT
:
174 return ctx
->DrawBuffer
;
182 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183 * gl_renderbuffer_attachment object.
184 * This function is only used for user-created FB objects, not the
185 * default / window-system FB object.
186 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187 * the depth buffer attachment point.
189 struct gl_renderbuffer_attachment
*
190 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
195 assert(_mesa_is_user_fbo(fb
));
197 switch (attachment
) {
198 case GL_COLOR_ATTACHMENT0_EXT
:
199 case GL_COLOR_ATTACHMENT1_EXT
:
200 case GL_COLOR_ATTACHMENT2_EXT
:
201 case GL_COLOR_ATTACHMENT3_EXT
:
202 case GL_COLOR_ATTACHMENT4_EXT
:
203 case GL_COLOR_ATTACHMENT5_EXT
:
204 case GL_COLOR_ATTACHMENT6_EXT
:
205 case GL_COLOR_ATTACHMENT7_EXT
:
206 case GL_COLOR_ATTACHMENT8_EXT
:
207 case GL_COLOR_ATTACHMENT9_EXT
:
208 case GL_COLOR_ATTACHMENT10_EXT
:
209 case GL_COLOR_ATTACHMENT11_EXT
:
210 case GL_COLOR_ATTACHMENT12_EXT
:
211 case GL_COLOR_ATTACHMENT13_EXT
:
212 case GL_COLOR_ATTACHMENT14_EXT
:
213 case GL_COLOR_ATTACHMENT15_EXT
:
214 /* Only OpenGL ES 1.x forbids color attachments other than
215 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
218 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
219 if (i
>= ctx
->Const
.MaxColorAttachments
220 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
223 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
224 case GL_DEPTH_STENCIL_ATTACHMENT
:
225 if (!_mesa_is_desktop_gl(ctx
))
228 case GL_DEPTH_ATTACHMENT_EXT
:
229 return &fb
->Attachment
[BUFFER_DEPTH
];
230 case GL_STENCIL_ATTACHMENT_EXT
:
231 return &fb
->Attachment
[BUFFER_STENCIL
];
239 * As above, but only used for getting attachments of the default /
240 * window-system framebuffer (not user-created framebuffer objects).
242 static struct gl_renderbuffer_attachment
*
243 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
246 assert(_mesa_is_winsys_fbo(fb
));
248 switch (attachment
) {
250 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
252 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
254 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
256 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
258 if (fb
->Visual
.numAuxBuffers
== 1) {
259 return &fb
->Attachment
[BUFFER_AUX0
];
263 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
265 * "If the default framebuffer is bound to target, then attachment must
266 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
267 * identifying a color buffer; DEPTH, identifying the depth buffer; or
268 * STENCIL, identifying the stencil buffer."
270 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
271 * language. However, revision #33 of the ARB_framebuffer_object spec
274 * "If the default framebuffer is bound to <target>, then <attachment>
275 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
276 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
277 * depth buffer, or the stencil buffer, and <pname> may be
278 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
279 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
281 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
282 * from glext.h, so shipping apps should not use those values.
284 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
285 * support queries of the window system FBO.
288 return &fb
->Attachment
[BUFFER_DEPTH
];
290 return &fb
->Attachment
[BUFFER_STENCIL
];
299 * Remove any texture or renderbuffer attached to the given attachment
300 * point. Update reference counts, etc.
303 _mesa_remove_attachment(struct gl_context
*ctx
,
304 struct gl_renderbuffer_attachment
*att
)
306 if (att
->Type
== GL_TEXTURE
) {
307 ASSERT(att
->Texture
);
308 if (ctx
->Driver
.FinishRenderTexture
) {
309 /* tell driver that we're done rendering to this texture. */
310 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
312 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
313 ASSERT(!att
->Texture
);
315 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
316 ASSERT(!att
->Texture
);
317 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
318 ASSERT(!att
->Renderbuffer
);
321 att
->Complete
= GL_TRUE
;
326 * Bind a texture object to an attachment point.
327 * The previous binding, if any, will be removed first.
330 _mesa_set_texture_attachment(struct gl_context
*ctx
,
331 struct gl_framebuffer
*fb
,
332 struct gl_renderbuffer_attachment
*att
,
333 struct gl_texture_object
*texObj
,
334 GLenum texTarget
, GLuint level
, GLuint zoffset
)
336 if (att
->Texture
== texObj
) {
337 /* re-attaching same texture */
338 ASSERT(att
->Type
== GL_TEXTURE
);
339 if (ctx
->Driver
.FinishRenderTexture
)
340 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
344 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
345 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
346 _mesa_remove_attachment(ctx
, att
);
347 att
->Type
= GL_TEXTURE
;
348 assert(!att
->Texture
);
349 _mesa_reference_texobj(&att
->Texture
, texObj
);
352 /* always update these fields */
353 att
->TextureLevel
= level
;
354 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
355 att
->Zoffset
= zoffset
;
356 att
->Complete
= GL_FALSE
;
358 if (_mesa_get_attachment_teximage(att
)) {
359 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
362 invalidate_framebuffer(fb
);
367 * Bind a renderbuffer to an attachment point.
368 * The previous binding, if any, will be removed first.
371 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
372 struct gl_renderbuffer_attachment
*att
,
373 struct gl_renderbuffer
*rb
)
375 /* XXX check if re-doing same attachment, exit early */
376 _mesa_remove_attachment(ctx
, att
);
377 att
->Type
= GL_RENDERBUFFER_EXT
;
378 att
->Texture
= NULL
; /* just to be safe */
379 att
->Complete
= GL_FALSE
;
380 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
385 * Fallback for ctx->Driver.FramebufferRenderbuffer()
386 * Attach a renderbuffer object to a framebuffer object.
389 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
390 struct gl_framebuffer
*fb
,
391 GLenum attachment
, struct gl_renderbuffer
*rb
)
393 struct gl_renderbuffer_attachment
*att
;
395 _glthread_LOCK_MUTEX(fb
->Mutex
);
397 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
400 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
401 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
402 /* do stencil attachment here (depth already done above) */
403 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
405 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
407 rb
->AttachedAnytime
= GL_TRUE
;
410 _mesa_remove_attachment(ctx
, att
);
413 invalidate_framebuffer(fb
);
415 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
420 * Fallback for ctx->Driver.ValidateFramebuffer()
421 * Check if the renderbuffer's formats are supported by the software
423 * Drivers should probably override this.
426 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
429 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
430 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
432 switch (rb
->_BaseFormat
) {
434 case GL_LUMINANCE_ALPHA
:
439 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
443 switch (rb
->Format
) {
444 /* XXX This list is likely incomplete. */
445 case MESA_FORMAT_RGB9_E5_FLOAT
:
446 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
449 /* render buffer format is supported by software rendering */
461 att_incomplete(const char *msg
)
463 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
464 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
473 fbo_incomplete(const char *msg
, int index
)
475 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
476 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
482 * Is the given base format a legal format for a color renderbuffer?
485 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
487 switch (baseFormat
) {
492 case GL_LUMINANCE_ALPHA
:
495 return ctx
->Extensions
.ARB_framebuffer_object
;
498 return ctx
->Extensions
.ARB_texture_rg
;
506 * Is the given base format a legal format for a depth/stencil renderbuffer?
509 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
511 switch (baseFormat
) {
512 case GL_DEPTH_COMPONENT
:
513 case GL_DEPTH_STENCIL_EXT
:
522 * Test if an attachment point is complete and update its Complete field.
523 * \param format if GL_COLOR, this is a color attachment point,
524 * if GL_DEPTH, this is a depth component attachment point,
525 * if GL_STENCIL, this is a stencil component attachment point.
528 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
529 struct gl_renderbuffer_attachment
*att
)
531 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
533 /* assume complete */
534 att
->Complete
= GL_TRUE
;
536 /* Look for reasons why the attachment might be incomplete */
537 if (att
->Type
== GL_TEXTURE
) {
538 const struct gl_texture_object
*texObj
= att
->Texture
;
539 struct gl_texture_image
*texImage
;
543 att_incomplete("no texobj");
544 att
->Complete
= GL_FALSE
;
548 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
550 att_incomplete("no teximage");
551 att
->Complete
= GL_FALSE
;
554 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
555 att_incomplete("teximage width/height=0");
556 printf("texobj = %u\n", texObj
->Name
);
557 printf("level = %d\n", att
->TextureLevel
);
558 att
->Complete
= GL_FALSE
;
561 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
562 att_incomplete("bad z offset");
563 att
->Complete
= GL_FALSE
;
567 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
569 if (format
== GL_COLOR
) {
570 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
571 att_incomplete("bad format");
572 att
->Complete
= GL_FALSE
;
575 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
576 att_incomplete("compressed internalformat");
577 att
->Complete
= GL_FALSE
;
581 else if (format
== GL_DEPTH
) {
582 if (baseFormat
== GL_DEPTH_COMPONENT
) {
585 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
586 ctx
->Extensions
.ARB_depth_texture
&&
587 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
591 att
->Complete
= GL_FALSE
;
592 att_incomplete("bad depth format");
597 ASSERT(format
== GL_STENCIL
);
598 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
599 ctx
->Extensions
.ARB_depth_texture
&&
600 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
604 /* no such thing as stencil-only textures */
605 att_incomplete("illegal stencil texture");
606 att
->Complete
= GL_FALSE
;
611 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
612 const GLenum baseFormat
=
613 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
615 ASSERT(att
->Renderbuffer
);
616 if (!att
->Renderbuffer
->InternalFormat
||
617 att
->Renderbuffer
->Width
< 1 ||
618 att
->Renderbuffer
->Height
< 1) {
619 att_incomplete("0x0 renderbuffer");
620 att
->Complete
= GL_FALSE
;
623 if (format
== GL_COLOR
) {
624 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
625 att_incomplete("bad renderbuffer color format");
626 att
->Complete
= GL_FALSE
;
630 else if (format
== GL_DEPTH
) {
631 if (baseFormat
== GL_DEPTH_COMPONENT
) {
634 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
635 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
639 att_incomplete("bad renderbuffer depth format");
640 att
->Complete
= GL_FALSE
;
645 assert(format
== GL_STENCIL
);
646 if (baseFormat
== GL_STENCIL_INDEX
) {
649 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
650 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
654 att
->Complete
= GL_FALSE
;
655 att_incomplete("bad renderbuffer stencil format");
661 ASSERT(att
->Type
== GL_NONE
);
669 * Test if the given framebuffer object is complete and update its
670 * Status field with the results.
671 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
672 * driver to make hardware-specific validation/completeness checks.
673 * Also update the framebuffer's Width and Height fields if the
674 * framebuffer is complete.
677 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
678 struct gl_framebuffer
*fb
)
681 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
682 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
683 GLint numSamples
= -1;
687 assert(_mesa_is_user_fbo(fb
));
689 /* we're changing framebuffer fields here */
690 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
696 /* Start at -2 to more easily loop over all attachment points.
701 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
702 struct gl_renderbuffer_attachment
*att
;
707 * XXX for ARB_fbo, only check color buffers that are named by
708 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
711 /* check for attachment completeness
714 att
= &fb
->Attachment
[BUFFER_DEPTH
];
715 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
716 if (!att
->Complete
) {
717 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
718 fbo_incomplete("depth attachment incomplete", -1);
723 att
= &fb
->Attachment
[BUFFER_STENCIL
];
724 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
725 if (!att
->Complete
) {
726 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
727 fbo_incomplete("stencil attachment incomplete", -1);
732 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
733 test_attachment_completeness(ctx
, GL_COLOR
, att
);
734 if (!att
->Complete
) {
735 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
736 fbo_incomplete("color attachment incomplete", i
);
741 /* get width, height, format of the renderbuffer/texture
743 if (att
->Type
== GL_TEXTURE
) {
744 const struct gl_texture_image
*texImg
=
745 _mesa_get_attachment_teximage(att
);
746 minWidth
= MIN2(minWidth
, texImg
->Width
);
747 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
748 minHeight
= MIN2(minHeight
, texImg
->Height
);
749 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
750 f
= texImg
->_BaseFormat
;
751 attFormat
= texImg
->TexFormat
;
753 if (!_mesa_is_legal_color_format(ctx
, f
) &&
754 !is_legal_depth_format(ctx
, f
)) {
755 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
756 fbo_incomplete("texture attachment incomplete", -1);
760 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
761 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
762 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
763 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
764 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
765 f
= att
->Renderbuffer
->InternalFormat
;
766 attFormat
= att
->Renderbuffer
->Format
;
770 assert(att
->Type
== GL_NONE
);
774 if (att
->Renderbuffer
&& numSamples
< 0) {
776 numSamples
= att
->Renderbuffer
->NumSamples
;
779 /* check if integer color */
780 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
782 /* Error-check width, height, format, samples
784 if (numImages
== 1) {
785 /* save format, num samples */
791 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
792 /* check that width, height, format are same */
793 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
794 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
795 fbo_incomplete("width or height mismatch", -1);
798 /* check that all color buffers are the same format */
799 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
800 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
801 fbo_incomplete("format mismatch", -1);
805 if (att
->Renderbuffer
&&
806 att
->Renderbuffer
->NumSamples
!= numSamples
) {
807 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
808 fbo_incomplete("inconsistant number of samples", i
);
813 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
815 if (att
->Type
== GL_RENDERBUFFER
&&
816 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
817 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
818 fbo_incomplete("unsupported renderbuffer format", i
);
823 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
824 /* Check that all DrawBuffers are present */
825 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
826 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
827 const struct gl_renderbuffer_attachment
*att
828 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
830 if (att
->Type
== GL_NONE
) {
831 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
832 fbo_incomplete("missing drawbuffer", j
);
838 /* Check that the ReadBuffer is present */
839 if (fb
->ColorReadBuffer
!= GL_NONE
) {
840 const struct gl_renderbuffer_attachment
*att
841 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
843 if (att
->Type
== GL_NONE
) {
844 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
845 fbo_incomplete("missing readbuffer", -1);
851 if (numImages
== 0) {
852 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
853 fbo_incomplete("no attachments", -1);
857 /* Provisionally set status = COMPLETE ... */
858 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
860 /* ... but the driver may say the FB is incomplete.
861 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
864 if (ctx
->Driver
.ValidateFramebuffer
) {
865 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
866 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
867 fbo_incomplete("driver marked FBO as incomplete", -1);
871 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
873 * Note that if ARB_framebuffer_object is supported and the attached
874 * renderbuffers/textures are different sizes, the framebuffer
875 * width/height will be set to the smallest width/height.
877 fb
->Width
= minWidth
;
878 fb
->Height
= minHeight
;
880 /* finally, update the visual info for the framebuffer */
881 _mesa_update_framebuffer_visual(ctx
, fb
);
887 _mesa_IsRenderbuffer(GLuint renderbuffer
)
889 GET_CURRENT_CONTEXT(ctx
);
890 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
892 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
893 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
901 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
903 struct gl_renderbuffer
*newRb
;
904 GET_CURRENT_CONTEXT(ctx
);
906 ASSERT_OUTSIDE_BEGIN_END(ctx
);
908 if (target
!= GL_RENDERBUFFER_EXT
) {
909 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
913 /* No need to flush here since the render buffer binding has no
914 * effect on rendering state.
918 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
919 if (newRb
== &DummyRenderbuffer
) {
920 /* ID was reserved, but no real renderbuffer object made yet */
923 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
924 /* All RB IDs must be Gen'd */
925 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
930 /* create new renderbuffer object */
931 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
933 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
936 ASSERT(newRb
->AllocStorage
);
937 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
938 newRb
->RefCount
= 1; /* referenced by hash table */
945 ASSERT(newRb
!= &DummyRenderbuffer
);
947 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
952 * If the given renderbuffer is anywhere attached to the framebuffer, detach
954 * This is used when a renderbuffer object is deleted.
955 * The spec calls for unbinding.
958 detach_renderbuffer(struct gl_context
*ctx
,
959 struct gl_framebuffer
*fb
,
960 struct gl_renderbuffer
*rb
)
963 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
964 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
965 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
968 invalidate_framebuffer(fb
);
973 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
976 GET_CURRENT_CONTEXT(ctx
);
978 ASSERT_OUTSIDE_BEGIN_END(ctx
);
979 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
981 for (i
= 0; i
< n
; i
++) {
982 if (renderbuffers
[i
] > 0) {
983 struct gl_renderbuffer
*rb
;
984 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
986 /* check if deleting currently bound renderbuffer object */
987 if (rb
== ctx
->CurrentRenderbuffer
) {
989 ASSERT(rb
->RefCount
>= 2);
990 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
993 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
994 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
996 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
997 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
998 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1001 /* Remove from hash table immediately, to free the ID.
1002 * But the object will not be freed until it's no longer
1003 * referenced anywhere else.
1005 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1007 if (rb
!= &DummyRenderbuffer
) {
1008 /* no longer referenced by hash table */
1009 _mesa_reference_renderbuffer(&rb
, NULL
);
1018 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1020 GET_CURRENT_CONTEXT(ctx
);
1024 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1027 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1034 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1036 for (i
= 0; i
< n
; i
++) {
1037 GLuint name
= first
+ i
;
1038 renderbuffers
[i
] = name
;
1039 /* insert dummy placeholder into hash table */
1040 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1041 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1042 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1048 * Given an internal format token for a render buffer, return the
1049 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1050 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1051 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1053 * This is similar to _mesa_base_tex_format() but the set of valid
1054 * internal formats is different.
1056 * Note that even if a format is determined to be legal here, validation
1057 * of the FBO may fail if the format is not supported by the driver/GPU.
1059 * \param internalFormat as passed to glRenderbufferStorage()
1060 * \return the base internal format, or 0 if internalFormat is illegal
1063 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1066 * Notes: some formats such as alpha, luminance, etc. were added
1067 * with GL_ARB_framebuffer_object.
1069 switch (internalFormat
) {
1075 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1080 case GL_LUMINANCE12
:
1081 case GL_LUMINANCE16
:
1082 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1084 case GL_LUMINANCE_ALPHA
:
1085 case GL_LUMINANCE4_ALPHA4
:
1086 case GL_LUMINANCE6_ALPHA2
:
1087 case GL_LUMINANCE8_ALPHA8
:
1088 case GL_LUMINANCE12_ALPHA4
:
1089 case GL_LUMINANCE12_ALPHA12
:
1090 case GL_LUMINANCE16_ALPHA16
:
1091 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1092 ? GL_LUMINANCE_ALPHA
: 0;
1096 case GL_INTENSITY12
:
1097 case GL_INTENSITY16
:
1098 return ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_framebuffer_object
1109 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1111 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1120 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1122 case GL_SRGB8_ALPHA8_EXT
:
1123 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1124 case GL_STENCIL_INDEX
:
1125 case GL_STENCIL_INDEX1_EXT
:
1126 case GL_STENCIL_INDEX4_EXT
:
1127 case GL_STENCIL_INDEX16_EXT
:
1128 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1129 * OpenGL ES, but Mesa does not currently support them.
1131 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1132 case GL_STENCIL_INDEX8_EXT
:
1133 return GL_STENCIL_INDEX
;
1134 case GL_DEPTH_COMPONENT
:
1135 case GL_DEPTH_COMPONENT32
:
1136 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1137 case GL_DEPTH_COMPONENT16
:
1138 case GL_DEPTH_COMPONENT24
:
1139 return GL_DEPTH_COMPONENT
;
1140 case GL_DEPTH_STENCIL_EXT
:
1141 return _mesa_is_desktop_gl(ctx
)
1142 && ctx
->Extensions
.EXT_packed_depth_stencil
1143 ? GL_DEPTH_STENCIL_EXT
: 0;
1144 case GL_DEPTH24_STENCIL8_EXT
:
1145 return ctx
->Extensions
.EXT_packed_depth_stencil
1146 ? GL_DEPTH_STENCIL_EXT
: 0;
1147 case GL_DEPTH_COMPONENT32F
:
1148 return ctx
->Version
>= 30
1149 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1150 ? GL_DEPTH_COMPONENT
: 0;
1151 case GL_DEPTH32F_STENCIL8
:
1152 return ctx
->Version
>= 30
1153 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1154 ? GL_DEPTH_STENCIL
: 0;
1157 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1160 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1164 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1167 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1169 /* signed normalized texture formats */
1171 return ctx
->Version
>= 30
1172 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_texture_snorm
)
1176 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1179 return ctx
->Version
>= 30
1180 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_texture_snorm
)
1184 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1187 return ctx
->Version
>= 30
1188 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_texture_snorm
)
1191 case GL_RGB16_SNORM
:
1192 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1194 case GL_RGBA8_SNORM
:
1195 return ctx
->Version
>= 30
1196 || (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_texture_snorm
)
1199 case GL_RGBA16_SNORM
:
1200 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1202 case GL_ALPHA_SNORM
:
1203 case GL_ALPHA8_SNORM
:
1204 case GL_ALPHA16_SNORM
:
1205 return ctx
->API
== API_OPENGL_COMPAT
&&
1206 ctx
->Extensions
.EXT_texture_snorm
&&
1207 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1208 case GL_LUMINANCE_SNORM
:
1209 case GL_LUMINANCE8_SNORM
:
1210 case GL_LUMINANCE16_SNORM
:
1211 return ctx
->API
== API_OPENGL_COMPAT
&&
1212 ctx
->Extensions
.EXT_texture_snorm
&&
1213 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1214 case GL_LUMINANCE_ALPHA_SNORM
:
1215 case GL_LUMINANCE8_ALPHA8_SNORM
:
1216 case GL_LUMINANCE16_ALPHA16_SNORM
:
1217 return ctx
->API
== API_OPENGL_COMPAT
&&
1218 ctx
->Extensions
.EXT_texture_snorm
&&
1219 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1220 case GL_INTENSITY_SNORM
:
1221 case GL_INTENSITY8_SNORM
:
1222 case GL_INTENSITY16_SNORM
:
1223 return ctx
->API
== API_OPENGL_COMPAT
&&
1224 ctx
->Extensions
.EXT_texture_snorm
&&
1225 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1228 return ctx
->Version
>= 30
1229 || (ctx
->API
== API_OPENGL_COMPAT
&&
1230 ctx
->Extensions
.ARB_texture_rg
&&
1231 ctx
->Extensions
.ARB_texture_float
) ? GL_RED
: 0;
1234 return ctx
->Version
>= 30
1235 || (ctx
->API
== API_OPENGL_COMPAT
&&
1236 ctx
->Extensions
.ARB_texture_rg
&&
1237 ctx
->Extensions
.ARB_texture_float
) ? GL_RG
: 0;
1240 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1241 || _mesa_is_gles3(ctx
)
1245 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1246 || _mesa_is_gles3(ctx
)
1248 case GL_ALPHA16F_ARB
:
1249 case GL_ALPHA32F_ARB
:
1250 return ctx
->API
== API_OPENGL_COMPAT
&&
1251 ctx
->Extensions
.ARB_texture_float
&&
1252 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1253 case GL_LUMINANCE16F_ARB
:
1254 case GL_LUMINANCE32F_ARB
:
1255 return ctx
->API
== API_OPENGL_COMPAT
&&
1256 ctx
->Extensions
.ARB_texture_float
&&
1257 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1258 case GL_LUMINANCE_ALPHA16F_ARB
:
1259 case GL_LUMINANCE_ALPHA32F_ARB
:
1260 return ctx
->API
== API_OPENGL_COMPAT
&&
1261 ctx
->Extensions
.ARB_texture_float
&&
1262 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1263 case GL_INTENSITY16F_ARB
:
1264 case GL_INTENSITY32F_ARB
:
1265 return ctx
->API
== API_OPENGL_COMPAT
&&
1266 ctx
->Extensions
.ARB_texture_float
&&
1267 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1269 return (_mesa_is_desktop_gl(ctx
)
1270 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1271 || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1272 case GL_R11F_G11F_B10F
:
1273 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
)
1274 || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1276 case GL_RGBA8UI_EXT
:
1277 case GL_RGBA16UI_EXT
:
1278 case GL_RGBA32UI_EXT
:
1280 case GL_RGBA16I_EXT
:
1281 case GL_RGBA32I_EXT
:
1282 return ctx
->Version
>= 30
1283 || (_mesa_is_desktop_gl(ctx
) &&
1284 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1287 case GL_RGB16UI_EXT
:
1288 case GL_RGB32UI_EXT
:
1292 return ctx
->Version
>= 30
1293 || (_mesa_is_desktop_gl(ctx
) &&
1294 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGB
: 0;
1302 return ctx
->Version
>= 30
1303 || (_mesa_is_desktop_gl(ctx
) &&
1304 ctx
->Extensions
.ARB_texture_rg
&&
1305 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1313 return ctx
->Version
>= 30
1314 || (_mesa_is_desktop_gl(ctx
) &&
1315 ctx
->Extensions
.ARB_texture_rg
&&
1316 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1318 case GL_INTENSITY8I_EXT
:
1319 case GL_INTENSITY8UI_EXT
:
1320 case GL_INTENSITY16I_EXT
:
1321 case GL_INTENSITY16UI_EXT
:
1322 case GL_INTENSITY32I_EXT
:
1323 case GL_INTENSITY32UI_EXT
:
1324 return ctx
->API
== API_OPENGL_COMPAT
&&
1325 ctx
->Extensions
.EXT_texture_integer
&&
1326 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1328 case GL_LUMINANCE8I_EXT
:
1329 case GL_LUMINANCE8UI_EXT
:
1330 case GL_LUMINANCE16I_EXT
:
1331 case GL_LUMINANCE16UI_EXT
:
1332 case GL_LUMINANCE32I_EXT
:
1333 case GL_LUMINANCE32UI_EXT
:
1334 return ctx
->API
== API_OPENGL_COMPAT
&&
1335 ctx
->Extensions
.EXT_texture_integer
&&
1336 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1338 case GL_LUMINANCE_ALPHA8I_EXT
:
1339 case GL_LUMINANCE_ALPHA8UI_EXT
:
1340 case GL_LUMINANCE_ALPHA16I_EXT
:
1341 case GL_LUMINANCE_ALPHA16UI_EXT
:
1342 case GL_LUMINANCE_ALPHA32I_EXT
:
1343 case GL_LUMINANCE_ALPHA32UI_EXT
:
1344 return ctx
->API
== API_OPENGL_COMPAT
&&
1345 ctx
->Extensions
.EXT_texture_integer
&&
1346 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1348 case GL_ALPHA8I_EXT
:
1349 case GL_ALPHA8UI_EXT
:
1350 case GL_ALPHA16I_EXT
:
1351 case GL_ALPHA16UI_EXT
:
1352 case GL_ALPHA32I_EXT
:
1353 case GL_ALPHA32UI_EXT
:
1354 return ctx
->API
== API_OPENGL_COMPAT
&&
1355 ctx
->Extensions
.EXT_texture_integer
&&
1356 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1359 return (_mesa_is_desktop_gl(ctx
) &&
1360 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1361 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1364 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1373 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1376 invalidate_rb(GLuint key
, void *data
, void *userData
)
1378 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1379 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1381 /* If this is a user-created FBO */
1382 if (_mesa_is_user_fbo(fb
)) {
1384 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1385 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1386 if (att
->Type
== GL_RENDERBUFFER
&&
1387 att
->Renderbuffer
== rb
) {
1388 /* Mark fb status as indeterminate to force re-validation */
1397 /** sentinal value, see below */
1398 #define NO_SAMPLES 1000
1402 * Helper function used by _mesa_RenderbufferStorage() and
1403 * _mesa_RenderbufferStorageMultisample().
1404 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1407 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1408 GLsizei width
, GLsizei height
, GLsizei samples
)
1410 const char *func
= samples
== NO_SAMPLES
?
1411 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1412 struct gl_renderbuffer
*rb
;
1414 GET_CURRENT_CONTEXT(ctx
);
1416 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1418 if (target
!= GL_RENDERBUFFER_EXT
) {
1419 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1423 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1424 if (baseFormat
== 0) {
1425 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1429 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1430 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1434 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1435 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1439 if (samples
== NO_SAMPLES
) {
1440 /* NumSamples == 0 indicates non-multisampling */
1443 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1444 /* note: driver may choose to use more samples than what's requested */
1445 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1449 rb
= ctx
->CurrentRenderbuffer
;
1451 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1455 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1457 if (rb
->InternalFormat
== internalFormat
&&
1458 rb
->Width
== (GLuint
) width
&&
1459 rb
->Height
== (GLuint
) height
&&
1460 rb
->NumSamples
== samples
) {
1461 /* no change in allocation needed */
1465 /* These MUST get set by the AllocStorage func */
1466 rb
->Format
= MESA_FORMAT_NONE
;
1467 rb
->NumSamples
= samples
;
1469 /* Now allocate the storage */
1470 ASSERT(rb
->AllocStorage
);
1471 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1472 /* No error - check/set fields now */
1473 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1474 assert(rb
->Width
== (GLuint
) width
);
1475 assert(rb
->Height
== (GLuint
) height
);
1476 rb
->InternalFormat
= internalFormat
;
1477 rb
->_BaseFormat
= baseFormat
;
1478 assert(rb
->_BaseFormat
!= 0);
1481 /* Probably ran out of memory - clear the fields */
1484 rb
->Format
= MESA_FORMAT_NONE
;
1485 rb
->InternalFormat
= GL_NONE
;
1486 rb
->_BaseFormat
= GL_NONE
;
1490 /* Invalidate the framebuffers the renderbuffer is attached in. */
1491 if (rb
->AttachedAnytime
) {
1492 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1498 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1500 struct gl_renderbuffer
*rb
;
1501 GET_CURRENT_CONTEXT(ctx
);
1502 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1504 if (!ctx
->Extensions
.OES_EGL_image
) {
1505 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1506 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1510 if (target
!= GL_RENDERBUFFER
) {
1511 _mesa_error(ctx
, GL_INVALID_ENUM
,
1512 "EGLImageTargetRenderbufferStorageOES");
1516 rb
= ctx
->CurrentRenderbuffer
;
1518 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1519 "EGLImageTargetRenderbufferStorageOES");
1523 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1525 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1530 * Helper function for _mesa_GetRenderbufferParameteriv() and
1531 * _mesa_GetFramebufferAttachmentParameteriv()
1532 * We have to be careful to respect the base format. For example, if a
1533 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1534 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1535 * we need to return zero.
1538 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1540 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1541 return _mesa_get_format_bits(format
, pname
);
1549 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
1550 GLsizei width
, GLsizei height
)
1552 /* GL_ARB_fbo says calling this function is equivalent to calling
1553 * glRenderbufferStorageMultisample() with samples=0. We pass in
1554 * a token value here just for error reporting purposes.
1556 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1561 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1562 GLenum internalFormat
,
1563 GLsizei width
, GLsizei height
)
1565 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1570 * OpenGL ES version of glRenderBufferStorage.
1573 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1574 GLsizei width
, GLsizei height
)
1576 switch (internalFormat
) {
1578 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1579 /* choose a closest format */
1580 internalFormat
= GL_RGB5
;
1586 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1591 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1593 struct gl_renderbuffer
*rb
;
1594 GET_CURRENT_CONTEXT(ctx
);
1596 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1598 if (target
!= GL_RENDERBUFFER_EXT
) {
1599 _mesa_error(ctx
, GL_INVALID_ENUM
,
1600 "glGetRenderbufferParameterivEXT(target)");
1604 rb
= ctx
->CurrentRenderbuffer
;
1606 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1607 "glGetRenderbufferParameterivEXT");
1611 /* No need to flush here since we're just quering state which is
1612 * not effected by rendering.
1616 case GL_RENDERBUFFER_WIDTH_EXT
:
1617 *params
= rb
->Width
;
1619 case GL_RENDERBUFFER_HEIGHT_EXT
:
1620 *params
= rb
->Height
;
1622 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1623 *params
= rb
->InternalFormat
;
1625 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1626 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1627 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1628 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1629 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1630 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1631 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1633 case GL_RENDERBUFFER_SAMPLES
:
1634 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1635 || _mesa_is_gles3(ctx
)) {
1636 *params
= rb
->NumSamples
;
1641 _mesa_error(ctx
, GL_INVALID_ENUM
,
1642 "glGetRenderbufferParameterivEXT(target)");
1648 GLboolean GLAPIENTRY
1649 _mesa_IsFramebuffer(GLuint framebuffer
)
1651 GET_CURRENT_CONTEXT(ctx
);
1652 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1654 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1655 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1663 * Check if any of the attachments of the given framebuffer are textures
1664 * (render to texture). Call ctx->Driver.RenderTexture() for such
1668 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1671 ASSERT(ctx
->Driver
.RenderTexture
);
1673 if (_mesa_is_winsys_fbo(fb
))
1674 return; /* can't render to texture with winsys framebuffers */
1676 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1677 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1678 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1679 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1686 * Examine all the framebuffer's attachments to see if any are textures.
1687 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1688 * notify the device driver that the texture image may have changed.
1691 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1693 if (_mesa_is_winsys_fbo(fb
))
1694 return; /* can't render to texture with winsys framebuffers */
1696 if (ctx
->Driver
.FinishRenderTexture
) {
1698 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1699 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1700 if (att
->Texture
&& att
->Renderbuffer
) {
1701 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1709 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
1711 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1712 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1713 GLboolean bindReadBuf
, bindDrawBuf
;
1714 GET_CURRENT_CONTEXT(ctx
);
1717 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1718 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1719 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1723 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1725 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1726 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1727 "glBindFramebufferEXT(unsupported)");
1732 case GL_DRAW_FRAMEBUFFER_EXT
:
1733 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1734 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1737 bindDrawBuf
= GL_TRUE
;
1738 bindReadBuf
= GL_FALSE
;
1740 case GL_READ_FRAMEBUFFER_EXT
:
1741 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1742 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1745 bindDrawBuf
= GL_FALSE
;
1746 bindReadBuf
= GL_TRUE
;
1748 case GL_FRAMEBUFFER_EXT
:
1749 bindDrawBuf
= GL_TRUE
;
1750 bindReadBuf
= GL_TRUE
;
1753 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1758 /* Binding a user-created framebuffer object */
1759 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1760 if (newDrawFb
== &DummyFramebuffer
) {
1761 /* ID was reserved, but no real framebuffer object made yet */
1764 else if (!newDrawFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1765 /* All FBO IDs must be Gen'd */
1766 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1771 /* create new framebuffer object */
1772 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1774 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1777 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1779 newReadFb
= newDrawFb
;
1782 /* Binding the window system framebuffer (which was originally set
1783 * with MakeCurrent).
1785 newDrawFb
= ctx
->WinSysDrawBuffer
;
1786 newReadFb
= ctx
->WinSysReadBuffer
;
1790 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1792 /* save pointers to current/old framebuffers */
1793 oldDrawFb
= ctx
->DrawBuffer
;
1794 oldReadFb
= ctx
->ReadBuffer
;
1796 /* check if really changing bindings */
1797 if (oldDrawFb
== newDrawFb
)
1798 bindDrawBuf
= GL_FALSE
;
1799 if (oldReadFb
== newReadFb
)
1800 bindReadBuf
= GL_FALSE
;
1803 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1805 * We also check if we're beginning and/or ending render-to-texture.
1806 * When a framebuffer with texture attachments is unbound, call
1807 * ctx->Driver.FinishRenderTexture().
1808 * When a framebuffer with texture attachments is bound, call
1809 * ctx->Driver.RenderTexture().
1811 * Note that if the ReadBuffer has texture attachments we don't consider
1812 * that a render-to-texture case.
1815 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1817 /* check if old readbuffer was render-to-texture */
1818 check_end_texture_render(ctx
, oldReadFb
);
1820 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1824 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1826 /* check if old framebuffer had any texture attachments */
1828 check_end_texture_render(ctx
, oldDrawFb
);
1830 /* check if newly bound framebuffer has any texture attachments */
1831 check_begin_texture_render(ctx
, newDrawFb
);
1833 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1836 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1837 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1843 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
1846 GET_CURRENT_CONTEXT(ctx
);
1848 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1849 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1851 for (i
= 0; i
< n
; i
++) {
1852 if (framebuffers
[i
] > 0) {
1853 struct gl_framebuffer
*fb
;
1854 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1856 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1858 /* check if deleting currently bound framebuffer object */
1859 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1860 /* separate draw/read binding points */
1861 if (fb
== ctx
->DrawBuffer
) {
1863 ASSERT(fb
->RefCount
>= 2);
1864 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1866 if (fb
== ctx
->ReadBuffer
) {
1868 ASSERT(fb
->RefCount
>= 2);
1869 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, 0);
1873 /* only one binding point for read/draw buffers */
1874 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1876 ASSERT(fb
->RefCount
>= 2);
1877 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
1881 /* remove from hash table immediately, to free the ID */
1882 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1884 if (fb
!= &DummyFramebuffer
) {
1885 /* But the object will not be freed until it's no longer
1886 * bound in any context.
1888 _mesa_reference_framebuffer(&fb
, NULL
);
1897 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
1899 GET_CURRENT_CONTEXT(ctx
);
1903 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1906 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1913 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1915 for (i
= 0; i
< n
; i
++) {
1916 GLuint name
= first
+ i
;
1917 framebuffers
[i
] = name
;
1918 /* insert dummy placeholder into hash table */
1919 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1920 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1921 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1928 _mesa_CheckFramebufferStatus(GLenum target
)
1930 struct gl_framebuffer
*buffer
;
1931 GET_CURRENT_CONTEXT(ctx
);
1933 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1935 buffer
= get_framebuffer_target(ctx
, target
);
1937 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1941 if (_mesa_is_winsys_fbo(buffer
)) {
1942 /* The window system / default framebuffer is always complete */
1943 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1946 /* No need to flush here */
1948 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1949 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1952 return buffer
->_Status
;
1957 * Replicate the src attachment point. Used by framebuffer_texture() when
1958 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1959 * GL_STENCIL_ATTACHMENT.
1962 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
1963 gl_buffer_index dst
,
1964 gl_buffer_index src
)
1966 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
1967 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
1969 assert(src_att
->Texture
!= NULL
);
1970 assert(src_att
->Renderbuffer
!= NULL
);
1972 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
1973 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
1974 dst_att
->Type
= src_att
->Type
;
1975 dst_att
->Complete
= src_att
->Complete
;
1976 dst_att
->TextureLevel
= src_att
->TextureLevel
;
1977 dst_att
->Zoffset
= src_att
->Zoffset
;
1982 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1983 * glFramebufferTextureLayerEXT().
1984 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
1985 * get textarget=0 in that case.
1988 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1989 GLenum attachment
, GLenum textarget
, GLuint texture
,
1990 GLint level
, GLint zoffset
)
1992 struct gl_renderbuffer_attachment
*att
;
1993 struct gl_texture_object
*texObj
= NULL
;
1994 struct gl_framebuffer
*fb
;
1995 GLenum maxLevelsTarget
;
1997 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1999 fb
= get_framebuffer_target(ctx
, target
);
2001 _mesa_error(ctx
, GL_INVALID_ENUM
,
2002 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2006 /* check framebuffer binding */
2007 if (_mesa_is_winsys_fbo(fb
)) {
2008 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2009 "glFramebufferTexture%sEXT", caller
);
2013 /* The textarget, level, and zoffset parameters are only validated if
2014 * texture is non-zero.
2017 GLboolean err
= GL_TRUE
;
2019 texObj
= _mesa_lookup_texture(ctx
, texture
);
2020 if (texObj
!= NULL
) {
2021 if (textarget
== 0) {
2022 /* If textarget == 0 it means we're being called by
2023 * glFramebufferTextureLayer() and textarget is not used.
2024 * The only legal texture types for that function are 3D and
2025 * 1D/2D arrays textures.
2027 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2028 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2029 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
) &&
2030 (texObj
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
);
2033 /* Make sure textarget is consistent with the texture's type */
2034 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2035 ? !_mesa_is_cube_face(textarget
)
2036 : (texObj
->Target
!= textarget
);
2040 /* can't render to a non-existant texture */
2041 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2042 "glFramebufferTexture%sEXT(non existant texture)",
2048 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2049 "glFramebufferTexture%sEXT(texture target mismatch)",
2054 if (texObj
->Target
== GL_TEXTURE_3D
) {
2055 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2056 if (zoffset
< 0 || zoffset
>= maxSize
) {
2057 _mesa_error(ctx
, GL_INVALID_VALUE
,
2058 "glFramebufferTexture%sEXT(zoffset)", caller
);
2062 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2063 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
) ||
2064 (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)) {
2066 zoffset
>= (GLint
) ctx
->Const
.MaxArrayTextureLayers
) {
2067 _mesa_error(ctx
, GL_INVALID_VALUE
,
2068 "glFramebufferTexture%sEXT(layer)", caller
);
2073 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2075 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2076 _mesa_error(ctx
, GL_INVALID_VALUE
,
2077 "glFramebufferTexture%sEXT(level)", caller
);
2082 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2084 _mesa_error(ctx
, GL_INVALID_ENUM
,
2085 "glFramebufferTexture%sEXT(attachment)", caller
);
2089 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2091 _glthread_LOCK_MUTEX(fb
->Mutex
);
2093 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2094 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2095 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2096 _mesa_tex_target_to_face(textarget
) ==
2097 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2098 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2099 /* The texture object is already attached to the stencil attachment
2100 * point. Don't create a new renderbuffer; just reuse the stencil
2101 * attachment's. This is required to prevent a GL error in
2102 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2104 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2106 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2107 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2108 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2109 _mesa_tex_target_to_face(textarget
) ==
2110 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2111 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2112 /* As above, but with depth and stencil transposed. */
2113 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2116 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2118 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2119 /* Above we created a new renderbuffer and attached it to the
2120 * depth attachment point. Now attach it to the stencil attachment
2123 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2124 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2129 /* Set the render-to-texture flag. We'll check this flag in
2130 * glTexImage() and friends to determine if we need to revalidate
2131 * any FBOs that might be rendering into this texture.
2132 * This flag never gets cleared since it's non-trivial to determine
2133 * when all FBOs might be done rendering to this texture. That's OK
2134 * though since it's uncommon to render to a texture then repeatedly
2135 * call glTexImage() to change images in the texture.
2137 texObj
->_RenderToTexture
= GL_TRUE
;
2140 _mesa_remove_attachment(ctx
, att
);
2141 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2142 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2143 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2147 invalidate_framebuffer(fb
);
2149 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2155 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
2156 GLenum textarget
, GLuint texture
, GLint level
)
2158 GET_CURRENT_CONTEXT(ctx
);
2163 switch (textarget
) {
2167 case GL_TEXTURE_1D_ARRAY
:
2168 error
= !ctx
->Extensions
.EXT_texture_array
;
2175 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2176 "glFramebufferTexture1DEXT(textarget=%s)",
2177 _mesa_lookup_enum_by_nr(textarget
));
2182 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2188 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
2189 GLenum textarget
, GLuint texture
, GLint level
)
2191 GET_CURRENT_CONTEXT(ctx
);
2196 switch (textarget
) {
2200 case GL_TEXTURE_RECTANGLE
:
2201 error
= _mesa_is_gles(ctx
)
2202 || !ctx
->Extensions
.NV_texture_rectangle
;
2204 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2206 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2207 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2208 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2209 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2210 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2212 case GL_TEXTURE_2D_ARRAY
:
2213 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2214 || !ctx
->Extensions
.EXT_texture_array
;
2221 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2222 "glFramebufferTexture2DEXT(textarget=%s)",
2223 _mesa_lookup_enum_by_nr(textarget
));
2228 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2234 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
2235 GLenum textarget
, GLuint texture
,
2236 GLint level
, GLint zoffset
)
2238 GET_CURRENT_CONTEXT(ctx
);
2240 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2241 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2242 "glFramebufferTexture3DEXT(textarget)");
2246 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2252 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
2253 GLuint texture
, GLint level
, GLint layer
)
2255 GET_CURRENT_CONTEXT(ctx
);
2257 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2263 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
2264 GLenum renderbufferTarget
,
2265 GLuint renderbuffer
)
2267 struct gl_renderbuffer_attachment
*att
;
2268 struct gl_framebuffer
*fb
;
2269 struct gl_renderbuffer
*rb
;
2270 GET_CURRENT_CONTEXT(ctx
);
2272 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2274 fb
= get_framebuffer_target(ctx
, target
);
2276 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2280 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2281 _mesa_error(ctx
, GL_INVALID_ENUM
,
2282 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2286 if (_mesa_is_winsys_fbo(fb
)) {
2287 /* Can't attach new renderbuffers to a window system framebuffer */
2288 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2292 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2294 _mesa_error(ctx
, GL_INVALID_ENUM
,
2295 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2296 _mesa_lookup_enum_by_nr(attachment
));
2301 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2304 "glFramebufferRenderbufferEXT(non-existant"
2305 " renderbuffer %u)", renderbuffer
);
2308 else if (rb
== &DummyRenderbuffer
) {
2309 /* This is what NVIDIA does */
2310 _mesa_error(ctx
, GL_INVALID_VALUE
,
2311 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2317 /* remove renderbuffer attachment */
2321 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2322 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2323 /* make sure the renderbuffer is a depth/stencil format */
2324 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2325 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2327 "glFramebufferRenderbufferEXT(renderbuffer"
2328 " is not DEPTH_STENCIL format)");
2334 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2336 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2337 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2339 /* Some subsequent GL commands may depend on the framebuffer's visual
2340 * after the binding is updated. Update visual info now.
2342 _mesa_update_framebuffer_visual(ctx
, fb
);
2347 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
2348 GLenum pname
, GLint
*params
)
2350 const struct gl_renderbuffer_attachment
*att
;
2351 struct gl_framebuffer
*buffer
;
2353 GET_CURRENT_CONTEXT(ctx
);
2355 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2357 /* The error differs in GL and GLES. */
2358 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2360 buffer
= get_framebuffer_target(ctx
, target
);
2362 _mesa_error(ctx
, GL_INVALID_ENUM
,
2363 "glGetFramebufferAttachmentParameterivEXT(target)");
2367 if (_mesa_is_winsys_fbo(buffer
)) {
2368 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2371 * "If the framebuffer currently bound to target is zero, then
2372 * INVALID_OPERATION is generated."
2374 * The EXT_framebuffer_object spec has the same wording, and the
2375 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2378 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
) {
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2380 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2383 /* the default / window-system FBO */
2384 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2387 /* user-created framebuffer FBO */
2388 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2392 _mesa_error(ctx
, GL_INVALID_ENUM
,
2393 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2397 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2398 /* the depth and stencil attachments must point to the same buffer */
2399 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2400 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2401 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2402 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2404 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2405 " attachments differ)");
2410 /* No need to flush here */
2413 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2414 *params
= _mesa_is_winsys_fbo(buffer
)
2415 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2417 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2418 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2419 *params
= att
->Renderbuffer
->Name
;
2421 else if (att
->Type
== GL_TEXTURE
) {
2422 *params
= att
->Texture
->Name
;
2425 assert(att
->Type
== GL_NONE
);
2426 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2429 goto invalid_pname_enum
;
2433 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2434 if (att
->Type
== GL_TEXTURE
) {
2435 *params
= att
->TextureLevel
;
2437 else if (att
->Type
== GL_NONE
) {
2438 _mesa_error(ctx
, err
,
2439 "glGetFramebufferAttachmentParameterivEXT(pname)");
2442 goto invalid_pname_enum
;
2445 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2446 if (att
->Type
== GL_TEXTURE
) {
2447 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2448 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2454 else if (att
->Type
== GL_NONE
) {
2455 _mesa_error(ctx
, err
,
2456 "glGetFramebufferAttachmentParameterivEXT(pname)");
2459 goto invalid_pname_enum
;
2462 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2463 if (ctx
->API
== API_OPENGLES
) {
2464 goto invalid_pname_enum
;
2465 } else if (att
->Type
== GL_NONE
) {
2466 _mesa_error(ctx
, err
,
2467 "glGetFramebufferAttachmentParameterivEXT(pname)");
2468 } else if (att
->Type
== GL_TEXTURE
) {
2469 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2470 *params
= att
->Zoffset
;
2477 goto invalid_pname_enum
;
2480 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2481 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2482 && !_mesa_is_gles3(ctx
)) {
2483 goto invalid_pname_enum
;
2485 else if (att
->Type
== GL_NONE
) {
2486 _mesa_error(ctx
, err
,
2487 "glGetFramebufferAttachmentParameterivEXT(pname)");
2490 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2491 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2494 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2495 * if the sRGB conversion is unsupported. */
2496 *params
= GL_LINEAR
;
2500 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2501 if ((ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2502 && ctx
->API
!= API_OPENGL_CORE
2503 && !_mesa_is_gles3(ctx
)) {
2504 goto invalid_pname_enum
;
2506 else if (att
->Type
== GL_NONE
) {
2507 _mesa_error(ctx
, err
,
2508 "glGetFramebufferAttachmentParameterivEXT(pname)");
2511 gl_format format
= att
->Renderbuffer
->Format
;
2512 if (format
== MESA_FORMAT_S8
) {
2516 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2517 /* depends on the attachment parameter */
2518 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2526 *params
= _mesa_get_format_datatype(format
);
2530 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2531 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2532 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2533 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2534 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2535 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2536 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2537 && !_mesa_is_gles3(ctx
)) {
2538 goto invalid_pname_enum
;
2540 else if (att
->Type
== GL_NONE
) {
2541 _mesa_error(ctx
, err
,
2542 "glGetFramebufferAttachmentParameterivEXT(pname)");
2544 else if (att
->Texture
) {
2545 const struct gl_texture_image
*texImage
=
2546 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2549 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2550 texImage
->TexFormat
);
2556 else if (att
->Renderbuffer
) {
2557 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2558 att
->Renderbuffer
->Format
);
2561 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2562 " invalid FBO attachment structure");
2566 goto invalid_pname_enum
;
2572 _mesa_error(ctx
, GL_INVALID_ENUM
,
2573 "glGetFramebufferAttachmentParameteriv(pname)");
2579 _mesa_GenerateMipmap(GLenum target
)
2581 struct gl_texture_image
*srcImage
;
2582 struct gl_texture_object
*texObj
;
2585 GET_CURRENT_CONTEXT(ctx
);
2587 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2588 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2592 error
= _mesa_is_gles(ctx
);
2598 error
= ctx
->API
== API_OPENGLES
;
2600 case GL_TEXTURE_CUBE_MAP
:
2601 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2603 case GL_TEXTURE_1D_ARRAY
:
2604 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2606 case GL_TEXTURE_2D_ARRAY
:
2607 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2608 || !ctx
->Extensions
.EXT_texture_array
;
2615 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2616 _mesa_lookup_enum_by_nr(target
));
2620 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2622 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2627 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2628 !_mesa_cube_complete(texObj
)) {
2629 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2630 "glGenerateMipmap(incomplete cube map)");
2634 _mesa_lock_texture(ctx
, texObj
);
2636 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2638 _mesa_unlock_texture(ctx
, texObj
);
2639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2640 "glGenerateMipmap(zero size base image)");
2644 if (_mesa_is_enum_format_integer(srcImage
->InternalFormat
) ||
2645 _mesa_is_depthstencil_format(srcImage
->InternalFormat
) ||
2646 _mesa_is_stencil_format(srcImage
->InternalFormat
)) {
2647 _mesa_unlock_texture(ctx
, texObj
);
2648 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2649 "glGenerateMipmap(invalid internal format)");
2653 if (target
== GL_TEXTURE_CUBE_MAP
) {
2655 for (face
= 0; face
< 6; face
++)
2656 ctx
->Driver
.GenerateMipmap(ctx
,
2657 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2661 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2663 _mesa_unlock_texture(ctx
, texObj
);
2667 static const struct gl_renderbuffer_attachment
*
2668 find_attachment(const struct gl_framebuffer
*fb
,
2669 const struct gl_renderbuffer
*rb
)
2672 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2673 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2674 return &fb
->Attachment
[i
];
2681 * Helper function for checking if the datatypes of color buffers are
2682 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2684 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2685 * and any of the following conditions hold:
2686 * - The read buffer contains fixed-point or floating-point values and any
2687 * draw buffer contains neither fixed-point nor floating-point values.
2688 * - The read buffer contains unsigned integer values and any draw buffer
2689 * does not contain unsigned integer values.
2690 * - The read buffer contains signed integer values and any draw buffer
2691 * does not contain signed integer values."
2694 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2696 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2697 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2699 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2700 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2701 srcType
== GL_SIGNED_NORMALIZED
||
2702 srcType
== GL_FLOAT
);
2703 /* Boil any of those types down to GL_FLOAT */
2707 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2708 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2709 dstType
== GL_SIGNED_NORMALIZED
||
2710 dstType
== GL_FLOAT
);
2711 /* Boil any of those types down to GL_FLOAT */
2715 return srcType
== dstType
;
2720 compatible_resolve_formats(const struct gl_renderbuffer
*readRb
,
2721 const struct gl_renderbuffer
*drawRb
)
2723 GLenum readFormat
, drawFormat
;
2725 /* The simple case where we know the backing Mesa formats are the same.
2727 if (_mesa_get_srgb_format_linear(readRb
->Format
) ==
2728 _mesa_get_srgb_format_linear(drawRb
->Format
)) {
2732 /* The Mesa formats are different, so we must check whether the internal
2733 * formats are compatible.
2735 * Under some circumstances, the user may request e.g. two GL_RGBA8
2736 * textures and get two entirely different Mesa formats like RGBA8888 and
2737 * ARGB8888. Drivers behaving like that should be able to cope with
2738 * non-matching formats by themselves, because it's not the user's fault.
2740 * Blits between linear and sRGB formats are also allowed.
2742 readFormat
= _mesa_get_nongeneric_internalformat(readRb
->InternalFormat
);
2743 drawFormat
= _mesa_get_nongeneric_internalformat(drawRb
->InternalFormat
);
2744 readFormat
= _mesa_get_linear_internalformat(readFormat
);
2745 drawFormat
= _mesa_get_linear_internalformat(drawFormat
);
2747 if (readFormat
== drawFormat
) {
2756 * Blit rectangular region, optionally from one framebuffer to another.
2758 * Note, if the src buffer is multisampled and the dest is not, this is
2759 * when the samples must be resolved to a single color.
2762 _mesa_BlitFramebuffer(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2763 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2764 GLbitfield mask
, GLenum filter
)
2766 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2767 GL_DEPTH_BUFFER_BIT
|
2768 GL_STENCIL_BUFFER_BIT
);
2769 const struct gl_framebuffer
*readFb
, *drawFb
;
2770 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
2771 GET_CURRENT_CONTEXT(ctx
);
2773 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2774 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2776 if (MESA_VERBOSE
& VERBOSE_API
)
2778 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2779 srcX0
, srcY0
, srcX1
, srcY1
,
2780 dstX0
, dstY0
, dstX1
, dstY1
,
2781 mask
, _mesa_lookup_enum_by_nr(filter
));
2783 if (ctx
->NewState
) {
2784 _mesa_update_state(ctx
);
2787 readFb
= ctx
->ReadBuffer
;
2788 drawFb
= ctx
->DrawBuffer
;
2790 if (!readFb
|| !drawFb
) {
2791 /* This will normally never happen but someday we may want to
2792 * support MakeCurrent() with no drawables.
2797 /* check for complete framebuffers */
2798 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2799 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2800 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2801 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2805 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2806 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2810 if (mask
& ~legalMaskBits
) {
2811 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2815 /* depth/stencil must be blitted with nearest filtering */
2816 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2817 && filter
!= GL_NEAREST
) {
2818 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2819 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2823 /* get color read/draw renderbuffers */
2824 if (mask
& GL_COLOR_BUFFER_BIT
) {
2825 colorReadRb
= readFb
->_ColorReadBuffer
;
2826 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2828 /* From the EXT_framebuffer_object spec:
2830 * "If a buffer is specified in <mask> and does not exist in both
2831 * the read and draw framebuffers, the corresponding bit is silently
2834 if ((colorReadRb
== NULL
) || (colorDrawRb
== NULL
)) {
2835 colorReadRb
= colorDrawRb
= NULL
;
2836 mask
&= ~GL_COLOR_BUFFER_BIT
;
2838 else if (!compatible_color_datatypes(colorReadRb
->Format
,
2839 colorDrawRb
->Format
)) {
2840 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2841 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2846 colorReadRb
= colorDrawRb
= NULL
;
2849 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2850 struct gl_renderbuffer
*readRb
=
2851 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2852 struct gl_renderbuffer
*drawRb
=
2853 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2855 /* From the EXT_framebuffer_object spec:
2857 * "If a buffer is specified in <mask> and does not exist in both
2858 * the read and draw framebuffers, the corresponding bit is silently
2861 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2862 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2864 else if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2865 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2866 /* There is no need to check the stencil datatype here, because
2867 * there is only one: GL_UNSIGNED_INT.
2869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2870 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2875 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2876 struct gl_renderbuffer
*readRb
=
2877 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2878 struct gl_renderbuffer
*drawRb
=
2879 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2881 /* From the EXT_framebuffer_object spec:
2883 * "If a buffer is specified in <mask> and does not exist in both
2884 * the read and draw framebuffers, the corresponding bit is silently
2887 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2888 mask
&= ~GL_DEPTH_BUFFER_BIT
;
2890 else if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2891 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
2892 (_mesa_get_format_datatype(readRb
->Format
) !=
2893 _mesa_get_format_datatype(drawRb
->Format
))) {
2894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2895 "glBlitFramebufferEXT(depth buffer format mismatch)");
2900 if (readFb
->Visual
.samples
> 0 &&
2901 drawFb
->Visual
.samples
> 0 &&
2902 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2903 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2904 "glBlitFramebufferEXT(mismatched samples)");
2908 /* extra checks for multisample copies... */
2909 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2910 /* src and dest region sizes must be the same */
2911 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
2912 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
2913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2914 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2918 /* color formats must match */
2921 !compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2922 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2923 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2928 if (filter
== GL_LINEAR
&& (mask
& GL_COLOR_BUFFER_BIT
)) {
2929 /* 3.1 spec, page 199:
2930 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2931 * if filter is LINEAR and read buffer contains integer data."
2933 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2934 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2935 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2936 "glBlitFramebufferEXT(integer color type)");
2941 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2942 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2948 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2950 srcX0
, srcY0
, srcX1
, srcY1
,
2951 dstX0
, dstY0
, dstX1
, dstY1
,
2954 const struct gl_renderbuffer_attachment
*att
;
2956 att
= find_attachment(readFb
, colorReadRb
);
2957 printf(" Src FBO %u RB %u (%dx%d) ",
2958 readFb
->Name
, colorReadRb
->Name
,
2959 colorReadRb
->Width
, colorReadRb
->Height
);
2960 if (att
&& att
->Texture
) {
2961 printf("Tex %u tgt 0x%x level %u face %u",
2963 att
->Texture
->Target
,
2969 att
= find_attachment(drawFb
, colorDrawRb
);
2970 printf(" Dst FBO %u RB %u (%dx%d) ",
2971 drawFb
->Name
, colorDrawRb
->Name
,
2972 colorDrawRb
->Width
, colorDrawRb
->Height
);
2973 if (att
&& att
->Texture
) {
2974 printf("Tex %u tgt 0x%x level %u face %u",
2976 att
->Texture
->Target
,
2988 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2989 ctx
->Driver
.BlitFramebuffer(ctx
,
2990 srcX0
, srcY0
, srcX1
, srcY1
,
2991 dstX0
, dstY0
, dstX1
, dstY1
,
2997 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
2998 const GLenum
*attachments
, GLint x
, GLint y
,
2999 GLsizei width
, GLsizei height
, const char *name
)
3002 struct gl_framebuffer
*fb
;
3003 GET_CURRENT_CONTEXT(ctx
);
3005 ASSERT_OUTSIDE_BEGIN_END(ctx
);
3007 fb
= get_framebuffer_target(ctx
, target
);
3009 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3013 if (numAttachments
< 0) {
3014 _mesa_error(ctx
, GL_INVALID_VALUE
,
3015 "%s(numAttachments < 0)", name
);
3019 /* The GL_ARB_invalidate_subdata spec says:
3021 * "If an attachment is specified that does not exist in the
3022 * framebuffer bound to <target>, it is ignored."
3026 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3027 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3028 * INVALID_OPERATION is generated."
3030 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3031 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3032 * set of retrictions).
3034 for (i
= 0; i
< numAttachments
; i
++) {
3035 if (_mesa_is_winsys_fbo(fb
)) {
3036 switch (attachments
[i
]) {
3042 /* Accumulation buffers and auxilary buffers were removed in
3043 * OpenGL 3.1, and they never existed in OpenGL ES.
3045 if (ctx
->API
!= API_OPENGL_COMPAT
)
3055 case GL_FRONT_RIGHT
:
3056 if (!_mesa_is_desktop_gl(ctx
))
3063 switch (attachments
[i
]) {
3064 case GL_DEPTH_ATTACHMENT
:
3065 case GL_STENCIL_ATTACHMENT
:
3067 case GL_COLOR_ATTACHMENT0
:
3068 case GL_COLOR_ATTACHMENT1
:
3069 case GL_COLOR_ATTACHMENT2
:
3070 case GL_COLOR_ATTACHMENT3
:
3071 case GL_COLOR_ATTACHMENT4
:
3072 case GL_COLOR_ATTACHMENT5
:
3073 case GL_COLOR_ATTACHMENT6
:
3074 case GL_COLOR_ATTACHMENT7
:
3075 case GL_COLOR_ATTACHMENT8
:
3076 case GL_COLOR_ATTACHMENT9
:
3077 case GL_COLOR_ATTACHMENT10
:
3078 case GL_COLOR_ATTACHMENT11
:
3079 case GL_COLOR_ATTACHMENT12
:
3080 case GL_COLOR_ATTACHMENT13
:
3081 case GL_COLOR_ATTACHMENT14
:
3082 case GL_COLOR_ATTACHMENT15
: {
3083 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3084 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3086 "%s(attachment >= max. color attachments)", name
);
3096 /* We don't actually do anything for this yet. Just return after
3097 * validating the parameters and generating the required errors.
3102 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3107 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3108 const GLenum
*attachments
, GLint x
, GLint y
,
3109 GLsizei width
, GLsizei height
)
3111 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3112 x
, y
, width
, height
,
3113 "glInvalidateSubFramebuffer");
3117 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3118 const GLenum
*attachments
)
3120 /* The GL_ARB_invalidate_subdata spec says:
3124 * void InvalidateFramebuffer(enum target,
3125 * sizei numAttachments,
3126 * const enum *attachments);
3128 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3129 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3130 * <MAX_VIEWPORT_DIMS[1]> respectively."
3132 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3133 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3134 "glInvalidateFramebuffer");