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
40 #include "framebuffer.h"
41 #include "glformats.h"
44 #include "mfeatures.h"
46 #include "renderbuffer.h"
52 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
59 * None of the GL_EXT_framebuffer_object functions are compiled into
66 * When glGenRender/FramebuffersEXT() is called we insert pointers to
67 * these placeholder objects into the hash table.
68 * Later, when the object ID is first bound, we replace the placeholder
69 * with the real frame/renderbuffer.
71 static struct gl_framebuffer DummyFramebuffer
;
72 static struct gl_renderbuffer DummyRenderbuffer
;
74 /* We bind this framebuffer when applications pass a NULL
75 * drawable/surface in make current. */
76 static struct gl_framebuffer IncompleteFramebuffer
;
80 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
86 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
93 _mesa_init_fbobjects(struct gl_context
*ctx
)
95 _glthread_INIT_MUTEX(DummyFramebuffer
.Mutex
);
96 _glthread_INIT_MUTEX(DummyRenderbuffer
.Mutex
);
97 _glthread_INIT_MUTEX(IncompleteFramebuffer
.Mutex
);
98 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
99 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
100 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
103 struct gl_framebuffer
*
104 _mesa_get_incomplete_framebuffer(void)
106 return &IncompleteFramebuffer
;
110 * Helper routine for getting a gl_renderbuffer.
112 struct gl_renderbuffer
*
113 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
115 struct gl_renderbuffer
*rb
;
120 rb
= (struct gl_renderbuffer
*)
121 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
127 * Helper routine for getting a gl_framebuffer.
129 struct gl_framebuffer
*
130 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
132 struct gl_framebuffer
*fb
;
137 fb
= (struct gl_framebuffer
*)
138 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
144 * Mark the given framebuffer as invalid. This will force the
145 * test for framebuffer completeness to be done before the framebuffer
149 invalidate_framebuffer(struct gl_framebuffer
*fb
)
151 fb
->_Status
= 0; /* "indeterminate" */
156 * Return the gl_framebuffer object which corresponds to the given
157 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
158 * Check support for GL_EXT_framebuffer_blit to determine if certain
160 * \return gl_framebuffer pointer or NULL if target is illegal
162 static struct gl_framebuffer
*
163 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
166 case GL_DRAW_FRAMEBUFFER
:
167 return ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
)
168 ? ctx
->DrawBuffer
: NULL
;
169 case GL_READ_FRAMEBUFFER
:
170 return ctx
->Extensions
.EXT_framebuffer_blit
&& _mesa_is_desktop_gl(ctx
)
171 ? ctx
->ReadBuffer
: NULL
;
172 case GL_FRAMEBUFFER_EXT
:
173 return ctx
->DrawBuffer
;
181 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
182 * gl_renderbuffer_attachment object.
183 * This function is only used for user-created FB objects, not the
184 * default / window-system FB object.
185 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
186 * the depth buffer attachment point.
188 struct gl_renderbuffer_attachment
*
189 _mesa_get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
194 assert(_mesa_is_user_fbo(fb
));
196 switch (attachment
) {
197 case GL_COLOR_ATTACHMENT0_EXT
:
198 case GL_COLOR_ATTACHMENT1_EXT
:
199 case GL_COLOR_ATTACHMENT2_EXT
:
200 case GL_COLOR_ATTACHMENT3_EXT
:
201 case GL_COLOR_ATTACHMENT4_EXT
:
202 case GL_COLOR_ATTACHMENT5_EXT
:
203 case GL_COLOR_ATTACHMENT6_EXT
:
204 case GL_COLOR_ATTACHMENT7_EXT
:
205 case GL_COLOR_ATTACHMENT8_EXT
:
206 case GL_COLOR_ATTACHMENT9_EXT
:
207 case GL_COLOR_ATTACHMENT10_EXT
:
208 case GL_COLOR_ATTACHMENT11_EXT
:
209 case GL_COLOR_ATTACHMENT12_EXT
:
210 case GL_COLOR_ATTACHMENT13_EXT
:
211 case GL_COLOR_ATTACHMENT14_EXT
:
212 case GL_COLOR_ATTACHMENT15_EXT
:
213 /* Only OpenGL ES 1.x forbids color attachments other than
214 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
217 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
218 if (i
>= ctx
->Const
.MaxColorAttachments
219 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
222 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
223 case GL_DEPTH_STENCIL_ATTACHMENT
:
224 if (!_mesa_is_desktop_gl(ctx
))
227 case GL_DEPTH_ATTACHMENT_EXT
:
228 return &fb
->Attachment
[BUFFER_DEPTH
];
229 case GL_STENCIL_ATTACHMENT_EXT
:
230 return &fb
->Attachment
[BUFFER_STENCIL
];
238 * As above, but only used for getting attachments of the default /
239 * window-system framebuffer (not user-created framebuffer objects).
241 static struct gl_renderbuffer_attachment
*
242 _mesa_get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
245 assert(_mesa_is_winsys_fbo(fb
));
247 switch (attachment
) {
249 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
251 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
253 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
255 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
257 if (fb
->Visual
.numAuxBuffers
== 1) {
258 return &fb
->Attachment
[BUFFER_AUX0
];
262 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
264 * "If the default framebuffer is bound to target, then attachment must
265 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
266 * identifying a color buffer; DEPTH, identifying the depth buffer; or
267 * STENCIL, identifying the stencil buffer."
269 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
270 * language. However, revision #33 of the ARB_framebuffer_object spec
273 * "If the default framebuffer is bound to <target>, then <attachment>
274 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
275 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
276 * depth buffer, or the stencil buffer, and <pname> may be
277 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
278 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
280 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
281 * from glext.h, so shipping apps should not use those values.
283 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
284 * support queries of the window system FBO.
287 return &fb
->Attachment
[BUFFER_DEPTH
];
289 return &fb
->Attachment
[BUFFER_STENCIL
];
298 * Remove any texture or renderbuffer attached to the given attachment
299 * point. Update reference counts, etc.
302 _mesa_remove_attachment(struct gl_context
*ctx
,
303 struct gl_renderbuffer_attachment
*att
)
305 if (att
->Type
== GL_TEXTURE
) {
306 ASSERT(att
->Texture
);
307 if (ctx
->Driver
.FinishRenderTexture
) {
308 /* tell driver that we're done rendering to this texture. */
309 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
311 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
312 ASSERT(!att
->Texture
);
314 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
315 ASSERT(!att
->Texture
);
316 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
317 ASSERT(!att
->Renderbuffer
);
320 att
->Complete
= GL_TRUE
;
325 * Bind a texture object to an attachment point.
326 * The previous binding, if any, will be removed first.
329 _mesa_set_texture_attachment(struct gl_context
*ctx
,
330 struct gl_framebuffer
*fb
,
331 struct gl_renderbuffer_attachment
*att
,
332 struct gl_texture_object
*texObj
,
333 GLenum texTarget
, GLuint level
, GLuint zoffset
)
335 if (att
->Texture
== texObj
) {
336 /* re-attaching same texture */
337 ASSERT(att
->Type
== GL_TEXTURE
);
338 if (ctx
->Driver
.FinishRenderTexture
)
339 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
343 if (ctx
->Driver
.FinishRenderTexture
&& att
->Texture
)
344 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
345 _mesa_remove_attachment(ctx
, att
);
346 att
->Type
= GL_TEXTURE
;
347 assert(!att
->Texture
);
348 _mesa_reference_texobj(&att
->Texture
, texObj
);
351 /* always update these fields */
352 att
->TextureLevel
= level
;
353 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
354 att
->Zoffset
= zoffset
;
355 att
->Complete
= GL_FALSE
;
357 if (_mesa_get_attachment_teximage(att
)) {
358 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
361 invalidate_framebuffer(fb
);
366 * Bind a renderbuffer to an attachment point.
367 * The previous binding, if any, will be removed first.
370 _mesa_set_renderbuffer_attachment(struct gl_context
*ctx
,
371 struct gl_renderbuffer_attachment
*att
,
372 struct gl_renderbuffer
*rb
)
374 /* XXX check if re-doing same attachment, exit early */
375 _mesa_remove_attachment(ctx
, att
);
376 att
->Type
= GL_RENDERBUFFER_EXT
;
377 att
->Texture
= NULL
; /* just to be safe */
378 att
->Complete
= GL_FALSE
;
379 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
384 * Fallback for ctx->Driver.FramebufferRenderbuffer()
385 * Attach a renderbuffer object to a framebuffer object.
388 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
389 struct gl_framebuffer
*fb
,
390 GLenum attachment
, struct gl_renderbuffer
*rb
)
392 struct gl_renderbuffer_attachment
*att
;
394 _glthread_LOCK_MUTEX(fb
->Mutex
);
396 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
399 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
400 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
401 /* do stencil attachment here (depth already done above) */
402 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
404 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
406 rb
->AttachedAnytime
= GL_TRUE
;
409 _mesa_remove_attachment(ctx
, att
);
412 invalidate_framebuffer(fb
);
414 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
419 * Fallback for ctx->Driver.ValidateFramebuffer()
420 * Check if the renderbuffer's formats are supported by the software
422 * Drivers should probably override this.
425 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
428 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
429 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
431 switch (rb
->_BaseFormat
) {
433 case GL_LUMINANCE_ALPHA
:
438 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
442 switch (rb
->Format
) {
443 /* XXX This list is likely incomplete. */
444 case MESA_FORMAT_RGB9_E5_FLOAT
:
445 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
448 /* render buffer format is supported by software rendering */
460 att_incomplete(const char *msg
)
462 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
463 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
472 fbo_incomplete(const char *msg
, int index
)
474 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
475 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
481 * Is the given base format a legal format for a color renderbuffer?
484 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
486 switch (baseFormat
) {
491 case GL_LUMINANCE_ALPHA
:
494 return ctx
->Extensions
.ARB_framebuffer_object
;
497 return ctx
->Extensions
.ARB_texture_rg
;
505 * Is the given base format a legal format for a depth/stencil renderbuffer?
508 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
510 switch (baseFormat
) {
511 case GL_DEPTH_COMPONENT
:
512 case GL_DEPTH_STENCIL_EXT
:
521 * Test if an attachment point is complete and update its Complete field.
522 * \param format if GL_COLOR, this is a color attachment point,
523 * if GL_DEPTH, this is a depth component attachment point,
524 * if GL_STENCIL, this is a stencil component attachment point.
527 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
528 struct gl_renderbuffer_attachment
*att
)
530 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
532 /* assume complete */
533 att
->Complete
= GL_TRUE
;
535 /* Look for reasons why the attachment might be incomplete */
536 if (att
->Type
== GL_TEXTURE
) {
537 const struct gl_texture_object
*texObj
= att
->Texture
;
538 struct gl_texture_image
*texImage
;
542 att_incomplete("no texobj");
543 att
->Complete
= GL_FALSE
;
547 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
549 att_incomplete("no teximage");
550 att
->Complete
= GL_FALSE
;
553 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
554 att_incomplete("teximage width/height=0");
555 printf("texobj = %u\n", texObj
->Name
);
556 printf("level = %d\n", att
->TextureLevel
);
557 att
->Complete
= GL_FALSE
;
560 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
561 att_incomplete("bad z offset");
562 att
->Complete
= GL_FALSE
;
566 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
568 if (format
== GL_COLOR
) {
569 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
570 att_incomplete("bad format");
571 att
->Complete
= GL_FALSE
;
574 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
575 att_incomplete("compressed internalformat");
576 att
->Complete
= GL_FALSE
;
580 else if (format
== GL_DEPTH
) {
581 if (baseFormat
== GL_DEPTH_COMPONENT
) {
584 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
585 ctx
->Extensions
.ARB_depth_texture
&&
586 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
590 att
->Complete
= GL_FALSE
;
591 att_incomplete("bad depth format");
596 ASSERT(format
== GL_STENCIL
);
597 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
598 ctx
->Extensions
.ARB_depth_texture
&&
599 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
603 /* no such thing as stencil-only textures */
604 att_incomplete("illegal stencil texture");
605 att
->Complete
= GL_FALSE
;
610 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
611 const GLenum baseFormat
=
612 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
614 ASSERT(att
->Renderbuffer
);
615 if (!att
->Renderbuffer
->InternalFormat
||
616 att
->Renderbuffer
->Width
< 1 ||
617 att
->Renderbuffer
->Height
< 1) {
618 att_incomplete("0x0 renderbuffer");
619 att
->Complete
= GL_FALSE
;
622 if (format
== GL_COLOR
) {
623 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
624 att_incomplete("bad renderbuffer color format");
625 att
->Complete
= GL_FALSE
;
629 else if (format
== GL_DEPTH
) {
630 if (baseFormat
== GL_DEPTH_COMPONENT
) {
633 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
634 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
638 att_incomplete("bad renderbuffer depth format");
639 att
->Complete
= GL_FALSE
;
644 assert(format
== GL_STENCIL
);
645 if (baseFormat
== GL_STENCIL_INDEX
) {
648 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
649 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
653 att
->Complete
= GL_FALSE
;
654 att_incomplete("bad renderbuffer stencil format");
660 ASSERT(att
->Type
== GL_NONE
);
668 * Test if the given framebuffer object is complete and update its
669 * Status field with the results.
670 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
671 * driver to make hardware-specific validation/completeness checks.
672 * Also update the framebuffer's Width and Height fields if the
673 * framebuffer is complete.
676 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
677 struct gl_framebuffer
*fb
)
680 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
681 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
682 GLint numSamples
= -1;
686 assert(_mesa_is_user_fbo(fb
));
688 /* we're changing framebuffer fields here */
689 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
695 /* Start at -2 to more easily loop over all attachment points.
700 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
701 struct gl_renderbuffer_attachment
*att
;
706 * XXX for ARB_fbo, only check color buffers that are named by
707 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
710 /* check for attachment completeness
713 att
= &fb
->Attachment
[BUFFER_DEPTH
];
714 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
715 if (!att
->Complete
) {
716 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
717 fbo_incomplete("depth attachment incomplete", -1);
722 att
= &fb
->Attachment
[BUFFER_STENCIL
];
723 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
724 if (!att
->Complete
) {
725 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
726 fbo_incomplete("stencil attachment incomplete", -1);
731 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
732 test_attachment_completeness(ctx
, GL_COLOR
, att
);
733 if (!att
->Complete
) {
734 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
735 fbo_incomplete("color attachment incomplete", i
);
740 /* get width, height, format of the renderbuffer/texture
742 if (att
->Type
== GL_TEXTURE
) {
743 const struct gl_texture_image
*texImg
=
744 _mesa_get_attachment_teximage(att
);
745 minWidth
= MIN2(minWidth
, texImg
->Width
);
746 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
747 minHeight
= MIN2(minHeight
, texImg
->Height
);
748 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
749 f
= texImg
->_BaseFormat
;
750 attFormat
= texImg
->TexFormat
;
752 if (!_mesa_is_legal_color_format(ctx
, f
) &&
753 !is_legal_depth_format(ctx
, f
)) {
754 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
755 fbo_incomplete("texture attachment incomplete", -1);
759 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
760 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
761 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
762 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
763 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
764 f
= att
->Renderbuffer
->InternalFormat
;
765 attFormat
= att
->Renderbuffer
->Format
;
769 assert(att
->Type
== GL_NONE
);
773 if (att
->Renderbuffer
&& numSamples
< 0) {
775 numSamples
= att
->Renderbuffer
->NumSamples
;
778 /* check if integer color */
779 fb
->_IntegerColor
= _mesa_is_format_integer_color(attFormat
);
781 /* Error-check width, height, format, samples
783 if (numImages
== 1) {
784 /* save format, num samples */
790 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
791 /* check that width, height, format are same */
792 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
793 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
794 fbo_incomplete("width or height mismatch", -1);
797 /* check that all color buffers are the same format */
798 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
799 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
800 fbo_incomplete("format mismatch", -1);
804 if (att
->Renderbuffer
&&
805 att
->Renderbuffer
->NumSamples
!= numSamples
) {
806 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
807 fbo_incomplete("inconsistant number of samples", i
);
812 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
814 if (att
->Type
== GL_RENDERBUFFER
&&
815 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
816 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
817 fbo_incomplete("unsupported renderbuffer format", i
);
822 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
823 /* Check that all DrawBuffers are present */
824 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
825 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
826 const struct gl_renderbuffer_attachment
*att
827 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
829 if (att
->Type
== GL_NONE
) {
830 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
831 fbo_incomplete("missing drawbuffer", j
);
837 /* Check that the ReadBuffer is present */
838 if (fb
->ColorReadBuffer
!= GL_NONE
) {
839 const struct gl_renderbuffer_attachment
*att
840 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
842 if (att
->Type
== GL_NONE
) {
843 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
844 fbo_incomplete("missing readbuffer", -1);
850 if (numImages
== 0) {
851 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
852 fbo_incomplete("no attachments", -1);
856 /* Provisionally set status = COMPLETE ... */
857 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
859 /* ... but the driver may say the FB is incomplete.
860 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
863 if (ctx
->Driver
.ValidateFramebuffer
) {
864 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
865 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
866 fbo_incomplete("driver marked FBO as incomplete", -1);
870 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
872 * Note that if ARB_framebuffer_object is supported and the attached
873 * renderbuffers/textures are different sizes, the framebuffer
874 * width/height will be set to the smallest width/height.
876 fb
->Width
= minWidth
;
877 fb
->Height
= minHeight
;
879 /* finally, update the visual info for the framebuffer */
880 _mesa_update_framebuffer_visual(ctx
, fb
);
886 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
888 GET_CURRENT_CONTEXT(ctx
);
889 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
891 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
892 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
900 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
902 struct gl_renderbuffer
*newRb
;
903 GET_CURRENT_CONTEXT(ctx
);
905 ASSERT_OUTSIDE_BEGIN_END(ctx
);
907 if (target
!= GL_RENDERBUFFER_EXT
) {
908 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
912 /* No need to flush here since the render buffer binding has no
913 * effect on rendering state.
917 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
918 if (newRb
== &DummyRenderbuffer
) {
919 /* ID was reserved, but no real renderbuffer object made yet */
922 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
923 /* All RB IDs must be Gen'd */
924 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
929 /* create new renderbuffer object */
930 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
932 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
935 ASSERT(newRb
->AllocStorage
);
936 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
937 newRb
->RefCount
= 1; /* referenced by hash table */
944 ASSERT(newRb
!= &DummyRenderbuffer
);
946 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
951 * If the given renderbuffer is anywhere attached to the framebuffer, detach
953 * This is used when a renderbuffer object is deleted.
954 * The spec calls for unbinding.
957 detach_renderbuffer(struct gl_context
*ctx
,
958 struct gl_framebuffer
*fb
,
959 struct gl_renderbuffer
*rb
)
962 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
963 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
964 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
967 invalidate_framebuffer(fb
);
972 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
975 GET_CURRENT_CONTEXT(ctx
);
977 ASSERT_OUTSIDE_BEGIN_END(ctx
);
978 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
980 for (i
= 0; i
< n
; i
++) {
981 if (renderbuffers
[i
] > 0) {
982 struct gl_renderbuffer
*rb
;
983 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
985 /* check if deleting currently bound renderbuffer object */
986 if (rb
== ctx
->CurrentRenderbuffer
) {
988 ASSERT(rb
->RefCount
>= 2);
989 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
992 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
993 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
995 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
996 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
997 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1000 /* Remove from hash table immediately, to free the ID.
1001 * But the object will not be freed until it's no longer
1002 * referenced anywhere else.
1004 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1006 if (rb
!= &DummyRenderbuffer
) {
1007 /* no longer referenced by hash table */
1008 _mesa_reference_renderbuffer(&rb
, NULL
);
1017 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
1019 GET_CURRENT_CONTEXT(ctx
);
1023 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1026 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
1033 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1035 for (i
= 0; i
< n
; i
++) {
1036 GLuint name
= first
+ i
;
1037 renderbuffers
[i
] = name
;
1038 /* insert dummy placeholder into hash table */
1039 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1040 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
1041 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1047 * Given an internal format token for a render buffer, return the
1048 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1049 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1050 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1052 * This is similar to _mesa_base_tex_format() but the set of valid
1053 * internal formats is different.
1055 * Note that even if a format is determined to be legal here, validation
1056 * of the FBO may fail if the format is not supported by the driver/GPU.
1058 * \param internalFormat as passed to glRenderbufferStorage()
1059 * \return the base internal format, or 0 if internalFormat is illegal
1062 _mesa_base_fbo_format(struct gl_context
*ctx
, GLenum internalFormat
)
1065 * Notes: some formats such as alpha, luminance, etc. were added
1066 * with GL_ARB_framebuffer_object.
1068 switch (internalFormat
) {
1074 return ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_framebuffer_object
1079 case GL_LUMINANCE12
:
1080 case GL_LUMINANCE16
:
1081 return ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_framebuffer_object
1083 case GL_LUMINANCE_ALPHA
:
1084 case GL_LUMINANCE4_ALPHA4
:
1085 case GL_LUMINANCE6_ALPHA2
:
1086 case GL_LUMINANCE8_ALPHA8
:
1087 case GL_LUMINANCE12_ALPHA4
:
1088 case GL_LUMINANCE12_ALPHA12
:
1089 case GL_LUMINANCE16_ALPHA16
:
1090 return ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_framebuffer_object
1091 ? GL_LUMINANCE_ALPHA
: 0;
1095 case GL_INTENSITY12
:
1096 case GL_INTENSITY16
:
1097 return ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_framebuffer_object
1108 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
1110 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1119 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
1121 case GL_SRGB8_ALPHA8_EXT
:
1122 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1123 case GL_STENCIL_INDEX
:
1124 case GL_STENCIL_INDEX1_EXT
:
1125 case GL_STENCIL_INDEX4_EXT
:
1126 case GL_STENCIL_INDEX16_EXT
:
1127 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1128 * OpenGL ES, but Mesa does not currently support them.
1130 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
1131 case GL_STENCIL_INDEX8_EXT
:
1132 return GL_STENCIL_INDEX
;
1133 case GL_DEPTH_COMPONENT
:
1134 case GL_DEPTH_COMPONENT32
:
1135 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
1136 case GL_DEPTH_COMPONENT16
:
1137 case GL_DEPTH_COMPONENT24
:
1138 return GL_DEPTH_COMPONENT
;
1139 case GL_DEPTH_STENCIL_EXT
:
1140 return _mesa_is_desktop_gl(ctx
)
1141 && ctx
->Extensions
.EXT_packed_depth_stencil
1142 ? GL_DEPTH_STENCIL_EXT
: 0;
1143 case GL_DEPTH24_STENCIL8_EXT
:
1144 return ctx
->Extensions
.EXT_packed_depth_stencil
1145 ? GL_DEPTH_STENCIL_EXT
: 0;
1146 case GL_DEPTH_COMPONENT32F
:
1147 return ctx
->Version
>= 30
1148 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1149 ? GL_DEPTH_COMPONENT
: 0;
1150 case GL_DEPTH32F_STENCIL8
:
1151 return ctx
->Version
>= 30
1152 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_depth_buffer_float
)
1153 ? GL_DEPTH_STENCIL
: 0;
1156 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1159 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1163 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_rg
1166 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
1168 /* signed normalized texture formats */
1170 return ctx
->Version
>= 30
1171 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_texture_snorm
)
1175 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1178 return ctx
->Version
>= 30
1179 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_texture_snorm
)
1183 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1186 return ctx
->Version
>= 30
1187 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_texture_snorm
)
1190 case GL_RGB16_SNORM
:
1191 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1193 case GL_RGBA8_SNORM
:
1194 return ctx
->Version
>= 30
1195 || (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_texture_snorm
)
1198 case GL_RGBA16_SNORM
:
1199 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
1201 case GL_ALPHA_SNORM
:
1202 case GL_ALPHA8_SNORM
:
1203 case GL_ALPHA16_SNORM
:
1204 return ctx
->API
== API_OPENGL
&&
1205 ctx
->Extensions
.EXT_texture_snorm
&&
1206 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1207 case GL_LUMINANCE_SNORM
:
1208 case GL_LUMINANCE8_SNORM
:
1209 case GL_LUMINANCE16_SNORM
:
1210 return ctx
->API
== API_OPENGL
&&
1211 ctx
->Extensions
.EXT_texture_snorm
&&
1212 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1213 case GL_LUMINANCE_ALPHA_SNORM
:
1214 case GL_LUMINANCE8_ALPHA8_SNORM
:
1215 case GL_LUMINANCE16_ALPHA16_SNORM
:
1216 return ctx
->API
== API_OPENGL
&&
1217 ctx
->Extensions
.EXT_texture_snorm
&&
1218 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1219 case GL_INTENSITY_SNORM
:
1220 case GL_INTENSITY8_SNORM
:
1221 case GL_INTENSITY16_SNORM
:
1222 return ctx
->API
== API_OPENGL
&&
1223 ctx
->Extensions
.EXT_texture_snorm
&&
1224 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1227 return ctx
->Version
>= 30
1228 || (ctx
->API
== API_OPENGL
&&
1229 ctx
->Extensions
.ARB_texture_rg
&&
1230 ctx
->Extensions
.ARB_texture_float
) ? GL_RED
: 0;
1233 return ctx
->Version
>= 30
1234 || (ctx
->API
== API_OPENGL
&&
1235 ctx
->Extensions
.ARB_texture_rg
&&
1236 ctx
->Extensions
.ARB_texture_float
) ? GL_RG
: 0;
1239 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1240 || _mesa_is_gles3(ctx
)
1244 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
1245 || _mesa_is_gles3(ctx
)
1247 case GL_ALPHA16F_ARB
:
1248 case GL_ALPHA32F_ARB
:
1249 return ctx
->API
== API_OPENGL
&&
1250 ctx
->Extensions
.ARB_texture_float
&&
1251 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1252 case GL_LUMINANCE16F_ARB
:
1253 case GL_LUMINANCE32F_ARB
:
1254 return ctx
->API
== API_OPENGL
&&
1255 ctx
->Extensions
.ARB_texture_float
&&
1256 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1257 case GL_LUMINANCE_ALPHA16F_ARB
:
1258 case GL_LUMINANCE_ALPHA32F_ARB
:
1259 return ctx
->API
== API_OPENGL
&&
1260 ctx
->Extensions
.ARB_texture_float
&&
1261 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1262 case GL_INTENSITY16F_ARB
:
1263 case GL_INTENSITY32F_ARB
:
1264 return ctx
->API
== API_OPENGL
&&
1265 ctx
->Extensions
.ARB_texture_float
&&
1266 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1268 return (_mesa_is_desktop_gl(ctx
)
1269 && ctx
->Extensions
.EXT_texture_shared_exponent
)
1270 || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1271 case GL_R11F_G11F_B10F
:
1272 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
)
1273 || _mesa_is_gles3(ctx
) ? GL_RGB
: 0;
1275 case GL_RGBA8UI_EXT
:
1276 case GL_RGBA16UI_EXT
:
1277 case GL_RGBA32UI_EXT
:
1279 case GL_RGBA16I_EXT
:
1280 case GL_RGBA32I_EXT
:
1281 return ctx
->Version
>= 30
1282 || (_mesa_is_desktop_gl(ctx
) &&
1283 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
1286 case GL_RGB16UI_EXT
:
1287 case GL_RGB32UI_EXT
:
1291 return ctx
->Version
>= 30
1292 || (_mesa_is_desktop_gl(ctx
) &&
1293 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGB
: 0;
1301 return ctx
->Version
>= 30
1302 || (_mesa_is_desktop_gl(ctx
) &&
1303 ctx
->Extensions
.ARB_texture_rg
&&
1304 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
1312 return ctx
->Version
>= 30
1313 || (_mesa_is_desktop_gl(ctx
) &&
1314 ctx
->Extensions
.ARB_texture_rg
&&
1315 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
1317 case GL_INTENSITY8I_EXT
:
1318 case GL_INTENSITY8UI_EXT
:
1319 case GL_INTENSITY16I_EXT
:
1320 case GL_INTENSITY16UI_EXT
:
1321 case GL_INTENSITY32I_EXT
:
1322 case GL_INTENSITY32UI_EXT
:
1323 return ctx
->API
== API_OPENGL
&&
1324 ctx
->Extensions
.EXT_texture_integer
&&
1325 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
1327 case GL_LUMINANCE8I_EXT
:
1328 case GL_LUMINANCE8UI_EXT
:
1329 case GL_LUMINANCE16I_EXT
:
1330 case GL_LUMINANCE16UI_EXT
:
1331 case GL_LUMINANCE32I_EXT
:
1332 case GL_LUMINANCE32UI_EXT
:
1333 return ctx
->API
== API_OPENGL
&&
1334 ctx
->Extensions
.EXT_texture_integer
&&
1335 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
1337 case GL_LUMINANCE_ALPHA8I_EXT
:
1338 case GL_LUMINANCE_ALPHA8UI_EXT
:
1339 case GL_LUMINANCE_ALPHA16I_EXT
:
1340 case GL_LUMINANCE_ALPHA16UI_EXT
:
1341 case GL_LUMINANCE_ALPHA32I_EXT
:
1342 case GL_LUMINANCE_ALPHA32UI_EXT
:
1343 return ctx
->API
== API_OPENGL
&&
1344 ctx
->Extensions
.EXT_texture_integer
&&
1345 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
1347 case GL_ALPHA8I_EXT
:
1348 case GL_ALPHA8UI_EXT
:
1349 case GL_ALPHA16I_EXT
:
1350 case GL_ALPHA16UI_EXT
:
1351 case GL_ALPHA32I_EXT
:
1352 case GL_ALPHA32UI_EXT
:
1353 return ctx
->API
== API_OPENGL
&&
1354 ctx
->Extensions
.EXT_texture_integer
&&
1355 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
1358 return (_mesa_is_desktop_gl(ctx
) &&
1359 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
1360 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
1363 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
1372 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1375 invalidate_rb(GLuint key
, void *data
, void *userData
)
1377 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
1378 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
1380 /* If this is a user-created FBO */
1381 if (_mesa_is_user_fbo(fb
)) {
1383 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1384 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1385 if (att
->Type
== GL_RENDERBUFFER
&&
1386 att
->Renderbuffer
== rb
) {
1387 /* Mark fb status as indeterminate to force re-validation */
1396 /** sentinal value, see below */
1397 #define NO_SAMPLES 1000
1401 * Helper function used by _mesa_RenderbufferStorageEXT() and
1402 * _mesa_RenderbufferStorageMultisample().
1403 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1406 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
1407 GLsizei width
, GLsizei height
, GLsizei samples
)
1409 const char *func
= samples
== NO_SAMPLES
?
1410 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1411 struct gl_renderbuffer
*rb
;
1413 GET_CURRENT_CONTEXT(ctx
);
1415 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1417 if (target
!= GL_RENDERBUFFER_EXT
) {
1418 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
1422 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
1423 if (baseFormat
== 0) {
1424 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
1428 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1429 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
1433 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
1434 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
1438 if (samples
== NO_SAMPLES
) {
1439 /* NumSamples == 0 indicates non-multisampling */
1442 else if (samples
> (GLsizei
) ctx
->Const
.MaxSamples
) {
1443 /* note: driver may choose to use more samples than what's requested */
1444 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
1448 rb
= ctx
->CurrentRenderbuffer
;
1450 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1454 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1456 if (rb
->InternalFormat
== internalFormat
&&
1457 rb
->Width
== (GLuint
) width
&&
1458 rb
->Height
== (GLuint
) height
&&
1459 rb
->NumSamples
== samples
) {
1460 /* no change in allocation needed */
1464 /* These MUST get set by the AllocStorage func */
1465 rb
->Format
= MESA_FORMAT_NONE
;
1466 rb
->NumSamples
= samples
;
1468 /* Now allocate the storage */
1469 ASSERT(rb
->AllocStorage
);
1470 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
1471 /* No error - check/set fields now */
1472 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1473 assert(rb
->Width
== (GLuint
) width
);
1474 assert(rb
->Height
== (GLuint
) height
);
1475 rb
->InternalFormat
= internalFormat
;
1476 rb
->_BaseFormat
= baseFormat
;
1477 assert(rb
->_BaseFormat
!= 0);
1480 /* Probably ran out of memory - clear the fields */
1483 rb
->Format
= MESA_FORMAT_NONE
;
1484 rb
->InternalFormat
= GL_NONE
;
1485 rb
->_BaseFormat
= GL_NONE
;
1489 /* Invalidate the framebuffers the renderbuffer is attached in. */
1490 if (rb
->AttachedAnytime
) {
1491 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
1497 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
1499 struct gl_renderbuffer
*rb
;
1500 GET_CURRENT_CONTEXT(ctx
);
1501 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1503 if (!ctx
->Extensions
.OES_EGL_image
) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1509 if (target
!= GL_RENDERBUFFER
) {
1510 _mesa_error(ctx
, GL_INVALID_ENUM
,
1511 "EGLImageTargetRenderbufferStorageOES");
1515 rb
= ctx
->CurrentRenderbuffer
;
1517 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1518 "EGLImageTargetRenderbufferStorageOES");
1522 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1524 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
1529 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1530 * _mesa_GetFramebufferAttachmentParameterivEXT()
1531 * We have to be careful to respect the base format. For example, if a
1532 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1533 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1534 * we need to return zero.
1537 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1539 if (_mesa_base_format_has_channel(baseFormat
, pname
))
1540 return _mesa_get_format_bits(format
, pname
);
1548 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1549 GLsizei width
, GLsizei height
)
1551 /* GL_ARB_fbo says calling this function is equivalent to calling
1552 * glRenderbufferStorageMultisample() with samples=0. We pass in
1553 * a token value here just for error reporting purposes.
1555 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1560 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1561 GLenum internalFormat
,
1562 GLsizei width
, GLsizei height
)
1564 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1569 * OpenGL ES version of glRenderBufferStorage.
1572 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1573 GLsizei width
, GLsizei height
)
1575 switch (internalFormat
) {
1577 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1578 /* choose a closest format */
1579 internalFormat
= GL_RGB5
;
1585 renderbuffer_storage(target
, internalFormat
, width
, height
, 0);
1590 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1592 struct gl_renderbuffer
*rb
;
1593 GET_CURRENT_CONTEXT(ctx
);
1595 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1597 if (target
!= GL_RENDERBUFFER_EXT
) {
1598 _mesa_error(ctx
, GL_INVALID_ENUM
,
1599 "glGetRenderbufferParameterivEXT(target)");
1603 rb
= ctx
->CurrentRenderbuffer
;
1605 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1606 "glGetRenderbufferParameterivEXT");
1610 /* No need to flush here since we're just quering state which is
1611 * not effected by rendering.
1615 case GL_RENDERBUFFER_WIDTH_EXT
:
1616 *params
= rb
->Width
;
1618 case GL_RENDERBUFFER_HEIGHT_EXT
:
1619 *params
= rb
->Height
;
1621 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1622 *params
= rb
->InternalFormat
;
1624 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1625 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1626 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1627 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1628 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1629 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1630 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1632 case GL_RENDERBUFFER_SAMPLES
:
1633 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
1634 || _mesa_is_gles3(ctx
)) {
1635 *params
= rb
->NumSamples
;
1640 _mesa_error(ctx
, GL_INVALID_ENUM
,
1641 "glGetRenderbufferParameterivEXT(target)");
1647 GLboolean GLAPIENTRY
1648 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1650 GET_CURRENT_CONTEXT(ctx
);
1651 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1653 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1654 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1662 * Check if any of the attachments of the given framebuffer are textures
1663 * (render to texture). Call ctx->Driver.RenderTexture() for such
1667 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1670 ASSERT(ctx
->Driver
.RenderTexture
);
1672 if (_mesa_is_winsys_fbo(fb
))
1673 return; /* can't render to texture with winsys framebuffers */
1675 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1676 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1677 if (att
->Texture
&& _mesa_get_attachment_teximage(att
)) {
1678 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1685 * Examine all the framebuffer's attachments to see if any are textures.
1686 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1687 * notify the device driver that the texture image may have changed.
1690 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
1692 if (_mesa_is_winsys_fbo(fb
))
1693 return; /* can't render to texture with winsys framebuffers */
1695 if (ctx
->Driver
.FinishRenderTexture
) {
1697 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1698 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1699 if (att
->Texture
&& att
->Renderbuffer
) {
1700 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1708 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1710 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
1711 struct gl_framebuffer
*oldDrawFb
, *oldReadFb
;
1712 GLboolean bindReadBuf
, bindDrawBuf
;
1713 GET_CURRENT_CONTEXT(ctx
);
1716 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1717 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1718 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1722 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1724 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1725 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1726 "glBindFramebufferEXT(unsupported)");
1731 case GL_DRAW_FRAMEBUFFER_EXT
:
1732 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1733 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1736 bindDrawBuf
= GL_TRUE
;
1737 bindReadBuf
= GL_FALSE
;
1739 case GL_READ_FRAMEBUFFER_EXT
:
1740 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1741 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1744 bindDrawBuf
= GL_FALSE
;
1745 bindReadBuf
= GL_TRUE
;
1747 case GL_FRAMEBUFFER_EXT
:
1748 bindDrawBuf
= GL_TRUE
;
1749 bindReadBuf
= GL_TRUE
;
1752 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1757 /* Binding a user-created framebuffer object */
1758 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1759 if (newDrawFb
== &DummyFramebuffer
) {
1760 /* ID was reserved, but no real framebuffer object made yet */
1763 else if (!newDrawFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1764 /* All FBO IDs must be Gen'd */
1765 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1770 /* create new framebuffer object */
1771 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1773 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1776 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
1778 newReadFb
= newDrawFb
;
1781 /* Binding the window system framebuffer (which was originally set
1782 * with MakeCurrent).
1784 newDrawFb
= ctx
->WinSysDrawBuffer
;
1785 newReadFb
= ctx
->WinSysReadBuffer
;
1789 ASSERT(newDrawFb
!= &DummyFramebuffer
);
1791 /* save pointers to current/old framebuffers */
1792 oldDrawFb
= ctx
->DrawBuffer
;
1793 oldReadFb
= ctx
->ReadBuffer
;
1795 /* check if really changing bindings */
1796 if (oldDrawFb
== newDrawFb
)
1797 bindDrawBuf
= GL_FALSE
;
1798 if (oldReadFb
== newReadFb
)
1799 bindReadBuf
= GL_FALSE
;
1802 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1804 * We also check if we're beginning and/or ending render-to-texture.
1805 * When a framebuffer with texture attachments is unbound, call
1806 * ctx->Driver.FinishRenderTexture().
1807 * When a framebuffer with texture attachments is bound, call
1808 * ctx->Driver.RenderTexture().
1810 * Note that if the ReadBuffer has texture attachments we don't consider
1811 * that a render-to-texture case.
1814 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1816 /* check if old readbuffer was render-to-texture */
1817 check_end_texture_render(ctx
, oldReadFb
);
1819 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
1823 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1825 /* check if old framebuffer had any texture attachments */
1827 check_end_texture_render(ctx
, oldDrawFb
);
1829 /* check if newly bound framebuffer has any texture attachments */
1830 check_begin_texture_render(ctx
, newDrawFb
);
1832 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
1835 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1836 ctx
->Driver
.BindFramebuffer(ctx
, target
, newDrawFb
, newReadFb
);
1842 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1845 GET_CURRENT_CONTEXT(ctx
);
1847 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1848 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1850 for (i
= 0; i
< n
; i
++) {
1851 if (framebuffers
[i
] > 0) {
1852 struct gl_framebuffer
*fb
;
1853 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1855 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1857 /* check if deleting currently bound framebuffer object */
1858 if (ctx
->Extensions
.EXT_framebuffer_blit
) {
1859 /* separate draw/read binding points */
1860 if (fb
== ctx
->DrawBuffer
) {
1862 ASSERT(fb
->RefCount
>= 2);
1863 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1865 if (fb
== ctx
->ReadBuffer
) {
1867 ASSERT(fb
->RefCount
>= 2);
1868 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1872 /* only one binding point for read/draw buffers */
1873 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
) {
1875 ASSERT(fb
->RefCount
>= 2);
1876 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
1880 /* remove from hash table immediately, to free the ID */
1881 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1883 if (fb
!= &DummyFramebuffer
) {
1884 /* But the object will not be freed until it's no longer
1885 * bound in any context.
1887 _mesa_reference_framebuffer(&fb
, NULL
);
1896 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1898 GET_CURRENT_CONTEXT(ctx
);
1902 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1905 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1912 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1914 for (i
= 0; i
< n
; i
++) {
1915 GLuint name
= first
+ i
;
1916 framebuffers
[i
] = name
;
1917 /* insert dummy placeholder into hash table */
1918 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1919 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1920 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1927 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1929 struct gl_framebuffer
*buffer
;
1930 GET_CURRENT_CONTEXT(ctx
);
1932 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1934 buffer
= get_framebuffer_target(ctx
, target
);
1936 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1940 if (_mesa_is_winsys_fbo(buffer
)) {
1941 /* The window system / default framebuffer is always complete */
1942 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1945 /* No need to flush here */
1947 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1948 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1951 return buffer
->_Status
;
1956 * Replicate the src attachment point. Used by framebuffer_texture() when
1957 * the same texture is attached at GL_DEPTH_ATTACHMENT and
1958 * GL_STENCIL_ATTACHMENT.
1961 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
1962 gl_buffer_index dst
,
1963 gl_buffer_index src
)
1965 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
1966 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
1968 assert(src_att
->Texture
!= NULL
);
1969 assert(src_att
->Renderbuffer
!= NULL
);
1971 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
1972 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
1973 dst_att
->Type
= src_att
->Type
;
1974 dst_att
->Complete
= src_att
->Complete
;
1975 dst_att
->TextureLevel
= src_att
->TextureLevel
;
1976 dst_att
->Zoffset
= src_att
->Zoffset
;
1981 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1982 * glFramebufferTextureLayerEXT().
1983 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
1984 * get textarget=0 in that case.
1987 framebuffer_texture(struct gl_context
*ctx
, const char *caller
, GLenum target
,
1988 GLenum attachment
, GLenum textarget
, GLuint texture
,
1989 GLint level
, GLint zoffset
)
1991 struct gl_renderbuffer_attachment
*att
;
1992 struct gl_texture_object
*texObj
= NULL
;
1993 struct gl_framebuffer
*fb
;
1994 GLenum maxLevelsTarget
;
1996 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1998 fb
= get_framebuffer_target(ctx
, target
);
2000 _mesa_error(ctx
, GL_INVALID_ENUM
,
2001 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
2005 /* check framebuffer binding */
2006 if (_mesa_is_winsys_fbo(fb
)) {
2007 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2008 "glFramebufferTexture%sEXT", caller
);
2012 /* The textarget, level, and zoffset parameters are only validated if
2013 * texture is non-zero.
2016 GLboolean err
= GL_TRUE
;
2018 texObj
= _mesa_lookup_texture(ctx
, texture
);
2019 if (texObj
!= NULL
) {
2020 if (textarget
== 0) {
2021 /* If textarget == 0 it means we're being called by
2022 * glFramebufferTextureLayer() and textarget is not used.
2023 * The only legal texture types for that function are 3D and
2024 * 1D/2D arrays textures.
2026 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
2027 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
2028 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
2031 /* Make sure textarget is consistent with the texture's type */
2032 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
2033 ? !_mesa_is_cube_face(textarget
)
2034 : (texObj
->Target
!= textarget
);
2038 /* can't render to a non-existant texture */
2039 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2040 "glFramebufferTexture%sEXT(non existant texture)",
2046 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2047 "glFramebufferTexture%sEXT(texture target mismatch)",
2052 if (texObj
->Target
== GL_TEXTURE_3D
) {
2053 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
2054 if (zoffset
< 0 || zoffset
>= maxSize
) {
2055 _mesa_error(ctx
, GL_INVALID_VALUE
,
2056 "glFramebufferTexture%sEXT(zoffset)", caller
);
2060 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
2061 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2062 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
2063 _mesa_error(ctx
, GL_INVALID_VALUE
,
2064 "glFramebufferTexture%sEXT(layer)", caller
);
2069 maxLevelsTarget
= textarget
? textarget
: texObj
->Target
;
2071 (level
>= _mesa_max_texture_levels(ctx
, maxLevelsTarget
))) {
2072 _mesa_error(ctx
, GL_INVALID_VALUE
,
2073 "glFramebufferTexture%sEXT(level)", caller
);
2078 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2080 _mesa_error(ctx
, GL_INVALID_ENUM
,
2081 "glFramebufferTexture%sEXT(attachment)", caller
);
2085 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2087 _glthread_LOCK_MUTEX(fb
->Mutex
);
2089 if (attachment
== GL_DEPTH_ATTACHMENT
&&
2090 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
2091 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
2092 _mesa_tex_target_to_face(textarget
) ==
2093 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
2094 zoffset
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
2095 /* The texture object is already attached to the stencil attachment
2096 * point. Don't create a new renderbuffer; just reuse the stencil
2097 * attachment's. This is required to prevent a GL error in
2098 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2100 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
2102 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
2103 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
2104 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
2105 _mesa_tex_target_to_face(textarget
) ==
2106 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
2107 zoffset
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
2108 /* As above, but with depth and stencil transposed. */
2109 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
2112 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
2114 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2115 /* Above we created a new renderbuffer and attached it to the
2116 * depth attachment point. Now attach it to the stencil attachment
2119 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2120 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
2125 /* Set the render-to-texture flag. We'll check this flag in
2126 * glTexImage() and friends to determine if we need to revalidate
2127 * any FBOs that might be rendering into this texture.
2128 * This flag never gets cleared since it's non-trivial to determine
2129 * when all FBOs might be done rendering to this texture. That's OK
2130 * though since it's uncommon to render to a texture then repeatedly
2131 * call glTexImage() to change images in the texture.
2133 texObj
->_RenderToTexture
= GL_TRUE
;
2136 _mesa_remove_attachment(ctx
, att
);
2137 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2138 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
2139 _mesa_remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
2143 invalidate_framebuffer(fb
);
2145 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
2151 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
2152 GLenum textarget
, GLuint texture
, GLint level
)
2154 GET_CURRENT_CONTEXT(ctx
);
2159 switch (textarget
) {
2163 case GL_TEXTURE_1D_ARRAY
:
2164 error
= !ctx
->Extensions
.EXT_texture_array
;
2171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2172 "glFramebufferTexture1DEXT(textarget=%s)",
2173 _mesa_lookup_enum_by_nr(textarget
));
2178 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
2184 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
2185 GLenum textarget
, GLuint texture
, GLint level
)
2187 GET_CURRENT_CONTEXT(ctx
);
2192 switch (textarget
) {
2196 case GL_TEXTURE_RECTANGLE
:
2197 error
= _mesa_is_gles(ctx
)
2198 || !ctx
->Extensions
.NV_texture_rectangle
;
2200 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2201 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2202 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2203 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2204 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2206 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2208 case GL_TEXTURE_2D_ARRAY
:
2209 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2210 || !ctx
->Extensions
.EXT_texture_array
;
2217 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2218 "glFramebufferTexture2DEXT(textarget=%s)",
2219 _mesa_lookup_enum_by_nr(textarget
));
2224 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
2230 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
2231 GLenum textarget
, GLuint texture
,
2232 GLint level
, GLint zoffset
)
2234 GET_CURRENT_CONTEXT(ctx
);
2236 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
2237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2238 "glFramebufferTexture3DEXT(textarget)");
2242 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
2248 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
2249 GLuint texture
, GLint level
, GLint layer
)
2251 GET_CURRENT_CONTEXT(ctx
);
2253 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
2259 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
2260 GLenum renderbufferTarget
,
2261 GLuint renderbuffer
)
2263 struct gl_renderbuffer_attachment
*att
;
2264 struct gl_framebuffer
*fb
;
2265 struct gl_renderbuffer
*rb
;
2266 GET_CURRENT_CONTEXT(ctx
);
2268 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2270 fb
= get_framebuffer_target(ctx
, target
);
2272 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFramebufferRenderbufferEXT(target)");
2276 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
2277 _mesa_error(ctx
, GL_INVALID_ENUM
,
2278 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2282 if (_mesa_is_winsys_fbo(fb
)) {
2283 /* Can't attach new renderbuffers to a window system framebuffer */
2284 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
2288 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
2290 _mesa_error(ctx
, GL_INVALID_ENUM
,
2291 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2292 _mesa_lookup_enum_by_nr(attachment
));
2297 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2300 "glFramebufferRenderbufferEXT(non-existant"
2301 " renderbuffer %u)", renderbuffer
);
2304 else if (rb
== &DummyRenderbuffer
) {
2305 /* This is what NVIDIA does */
2306 _mesa_error(ctx
, GL_INVALID_VALUE
,
2307 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2313 /* remove renderbuffer attachment */
2317 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
2318 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
2319 /* make sure the renderbuffer is a depth/stencil format */
2320 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
2321 if (baseFormat
!= GL_DEPTH_STENCIL
) {
2322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2323 "glFramebufferRenderbufferEXT(renderbuffer"
2324 " is not DEPTH_STENCIL format)");
2330 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2332 assert(ctx
->Driver
.FramebufferRenderbuffer
);
2333 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
2335 /* Some subsequent GL commands may depend on the framebuffer's visual
2336 * after the binding is updated. Update visual info now.
2338 _mesa_update_framebuffer_visual(ctx
, fb
);
2343 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
2344 GLenum pname
, GLint
*params
)
2346 const struct gl_renderbuffer_attachment
*att
;
2347 struct gl_framebuffer
*buffer
;
2349 GET_CURRENT_CONTEXT(ctx
);
2351 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2353 /* The error differs in GL and GLES. */
2354 err
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
2356 buffer
= get_framebuffer_target(ctx
, target
);
2358 _mesa_error(ctx
, GL_INVALID_ENUM
,
2359 "glGetFramebufferAttachmentParameterivEXT(target)");
2363 if (_mesa_is_winsys_fbo(buffer
)) {
2364 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2367 * "If the framebuffer currently bound to target is zero, then
2368 * INVALID_OPERATION is generated."
2370 * The EXT_framebuffer_object spec has the same wording, and the
2371 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2374 if (!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
) {
2375 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2376 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2379 /* the default / window-system FBO */
2380 att
= _mesa_get_fb0_attachment(ctx
, buffer
, attachment
);
2383 /* user-created framebuffer FBO */
2384 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
2388 _mesa_error(ctx
, GL_INVALID_ENUM
,
2389 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2393 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
2394 /* the depth and stencil attachments must point to the same buffer */
2395 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
2396 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
2397 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
2398 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2400 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2401 " attachments differ)");
2406 /* No need to flush here */
2409 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
2410 *params
= _mesa_is_winsys_fbo(buffer
)
2411 ? GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
2413 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
2414 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
2415 *params
= att
->Renderbuffer
->Name
;
2417 else if (att
->Type
== GL_TEXTURE
) {
2418 *params
= att
->Texture
->Name
;
2421 assert(att
->Type
== GL_NONE
);
2422 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2425 goto invalid_pname_enum
;
2429 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
2430 if (att
->Type
== GL_TEXTURE
) {
2431 *params
= att
->TextureLevel
;
2433 else if (att
->Type
== GL_NONE
) {
2434 _mesa_error(ctx
, err
,
2435 "glGetFramebufferAttachmentParameterivEXT(pname)");
2438 goto invalid_pname_enum
;
2441 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
2442 if (att
->Type
== GL_TEXTURE
) {
2443 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
2444 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
2450 else if (att
->Type
== GL_NONE
) {
2451 _mesa_error(ctx
, err
,
2452 "glGetFramebufferAttachmentParameterivEXT(pname)");
2455 goto invalid_pname_enum
;
2458 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
2459 if (ctx
->API
== API_OPENGLES
) {
2460 goto invalid_pname_enum
;
2461 } else if (att
->Type
== GL_NONE
) {
2462 _mesa_error(ctx
, err
,
2463 "glGetFramebufferAttachmentParameterivEXT(pname)");
2464 } else if (att
->Type
== GL_TEXTURE
) {
2465 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
2466 *params
= att
->Zoffset
;
2473 goto invalid_pname_enum
;
2476 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
2477 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2478 && !_mesa_is_gles3(ctx
)) {
2479 goto invalid_pname_enum
;
2481 else if (att
->Type
== GL_NONE
) {
2482 _mesa_error(ctx
, err
,
2483 "glGetFramebufferAttachmentParameterivEXT(pname)");
2486 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2487 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
2490 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2491 * if the sRGB conversion is unsupported. */
2492 *params
= GL_LINEAR
;
2496 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
2497 if ((ctx
->API
!= API_OPENGL
|| !ctx
->Extensions
.ARB_framebuffer_object
)
2498 && ctx
->API
!= API_OPENGL_CORE
2499 && !_mesa_is_gles3(ctx
)) {
2500 goto invalid_pname_enum
;
2502 else if (att
->Type
== GL_NONE
) {
2503 _mesa_error(ctx
, err
,
2504 "glGetFramebufferAttachmentParameterivEXT(pname)");
2507 gl_format format
= att
->Renderbuffer
->Format
;
2508 if (format
== MESA_FORMAT_S8
) {
2512 else if (format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
2513 /* depends on the attachment parameter */
2514 if (attachment
== GL_STENCIL_ATTACHMENT
) {
2522 *params
= _mesa_get_format_datatype(format
);
2526 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
2527 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
2528 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
2529 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
2530 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
2531 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
2532 if ((!_mesa_is_desktop_gl(ctx
) || !ctx
->Extensions
.ARB_framebuffer_object
)
2533 && !_mesa_is_gles3(ctx
)) {
2534 goto invalid_pname_enum
;
2536 else if (att
->Type
== GL_NONE
) {
2537 _mesa_error(ctx
, err
,
2538 "glGetFramebufferAttachmentParameterivEXT(pname)");
2540 else if (att
->Texture
) {
2541 const struct gl_texture_image
*texImage
=
2542 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
2545 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
2546 texImage
->TexFormat
);
2552 else if (att
->Renderbuffer
) {
2553 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
2554 att
->Renderbuffer
->Format
);
2557 _mesa_problem(ctx
, "glGetFramebufferAttachmentParameterivEXT:"
2558 " invalid FBO attachment structure");
2562 goto invalid_pname_enum
;
2568 _mesa_error(ctx
, GL_INVALID_ENUM
,
2569 "glGetFramebufferAttachmentParameteriv(pname)");
2575 _mesa_GenerateMipmapEXT(GLenum target
)
2577 struct gl_texture_image
*srcImage
;
2578 struct gl_texture_object
*texObj
;
2581 GET_CURRENT_CONTEXT(ctx
);
2583 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2584 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2588 error
= _mesa_is_gles(ctx
);
2594 error
= ctx
->API
== API_OPENGLES
;
2596 case GL_TEXTURE_CUBE_MAP
:
2597 error
= !ctx
->Extensions
.ARB_texture_cube_map
;
2599 case GL_TEXTURE_1D_ARRAY
:
2600 error
= _mesa_is_gles(ctx
) || !ctx
->Extensions
.EXT_texture_array
;
2602 case GL_TEXTURE_2D_ARRAY
:
2603 error
= (_mesa_is_gles(ctx
) && ctx
->Version
< 30)
2604 || !ctx
->Extensions
.EXT_texture_array
;
2611 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target=%s)",
2612 _mesa_lookup_enum_by_nr(target
));
2616 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2618 if (texObj
->BaseLevel
>= texObj
->MaxLevel
) {
2623 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
&&
2624 !_mesa_cube_complete(texObj
)) {
2625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2626 "glGenerateMipmap(incomplete cube map)");
2630 _mesa_lock_texture(ctx
, texObj
);
2632 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
2634 _mesa_unlock_texture(ctx
, texObj
);
2635 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2636 "glGenerateMipmap(zero size base image)");
2640 if (target
== GL_TEXTURE_CUBE_MAP
) {
2642 for (face
= 0; face
< 6; face
++)
2643 ctx
->Driver
.GenerateMipmap(ctx
,
2644 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
2648 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2650 _mesa_unlock_texture(ctx
, texObj
);
2654 static const struct gl_renderbuffer_attachment
*
2655 find_attachment(const struct gl_framebuffer
*fb
,
2656 const struct gl_renderbuffer
*rb
)
2659 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
2660 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
2661 return &fb
->Attachment
[i
];
2668 * Helper function for checking if the datatypes of color buffers are
2669 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2671 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2672 * and any of the following conditions hold:
2673 * - The read buffer contains fixed-point or floating-point values and any
2674 * draw buffer contains neither fixed-point nor floating-point values.
2675 * - The read buffer contains unsigned integer values and any draw buffer
2676 * does not contain unsigned integer values.
2677 * - The read buffer contains signed integer values and any draw buffer
2678 * does not contain signed integer values."
2681 compatible_color_datatypes(gl_format srcFormat
, gl_format dstFormat
)
2683 GLenum srcType
= _mesa_get_format_datatype(srcFormat
);
2684 GLenum dstType
= _mesa_get_format_datatype(dstFormat
);
2686 if (srcType
!= GL_INT
&& srcType
!= GL_UNSIGNED_INT
) {
2687 assert(srcType
== GL_UNSIGNED_NORMALIZED
||
2688 srcType
== GL_SIGNED_NORMALIZED
||
2689 srcType
== GL_FLOAT
);
2690 /* Boil any of those types down to GL_FLOAT */
2694 if (dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
) {
2695 assert(dstType
== GL_UNSIGNED_NORMALIZED
||
2696 dstType
== GL_SIGNED_NORMALIZED
||
2697 dstType
== GL_FLOAT
);
2698 /* Boil any of those types down to GL_FLOAT */
2702 return srcType
== dstType
;
2707 * Return the equivalent non-generic internal format.
2708 * This is useful for comparing whether two internal formats are semantically
2712 get_nongeneric_internalformat(GLenum format
)
2715 /* GL 1.1 formats. */
2725 case GL_LUMINANCE_ALPHA
:
2726 return GL_LUMINANCE8_ALPHA8
;
2730 return GL_LUMINANCE8
;
2736 return GL_INTENSITY8
;
2738 /* GL_ARB_texture_rg */
2745 /* GL_EXT_texture_sRGB */
2750 return GL_SRGB8_ALPHA8
;
2753 return GL_SLUMINANCE8
;
2755 case GL_SLUMINANCE_ALPHA
:
2756 return GL_SLUMINANCE8_ALPHA8
;
2758 /* GL_EXT_texture_snorm */
2760 return GL_RGBA8_SNORM
;
2763 return GL_RGB8_SNORM
;
2766 return GL_RG8_SNORM
;
2771 case GL_LUMINANCE_ALPHA_SNORM
:
2772 return GL_LUMINANCE8_ALPHA8_SNORM
;
2774 case GL_LUMINANCE_SNORM
:
2775 return GL_LUMINANCE8_SNORM
;
2777 case GL_ALPHA_SNORM
:
2778 return GL_ALPHA8_SNORM
;
2780 case GL_INTENSITY_SNORM
:
2781 return GL_INTENSITY8_SNORM
;
2790 compatible_resolve_formats(const struct gl_renderbuffer
*colorReadRb
,
2791 const struct gl_renderbuffer
*colorDrawRb
)
2793 /* The simple case where we know the backing formats are the same.
2795 if (colorReadRb
->Format
== colorDrawRb
->Format
) {
2799 /* The Mesa formats are different, so we must check whether the internal
2800 * formats are compatible.
2802 * Under some circumstances, the user may request e.g. two GL_RGBA8
2803 * textures and get two entirely different Mesa formats like RGBA8888 and
2804 * ARGB8888. Drivers behaving like that should be able to cope with
2805 * non-matching formats by themselves, because it's not the user's fault.
2807 if (get_nongeneric_internalformat(colorReadRb
->InternalFormat
) ==
2808 get_nongeneric_internalformat(colorDrawRb
->InternalFormat
)) {
2817 * Blit rectangular region, optionally from one framebuffer to another.
2819 * Note, if the src buffer is multisampled and the dest is not, this is
2820 * when the samples must be resolved to a single color.
2823 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
2824 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
2825 GLbitfield mask
, GLenum filter
)
2827 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
2828 GL_DEPTH_BUFFER_BIT
|
2829 GL_STENCIL_BUFFER_BIT
);
2830 const struct gl_framebuffer
*readFb
, *drawFb
;
2831 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
2832 GET_CURRENT_CONTEXT(ctx
);
2834 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2835 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2837 if (MESA_VERBOSE
& VERBOSE_API
)
2839 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2840 srcX0
, srcY0
, srcX1
, srcY1
,
2841 dstX0
, dstY0
, dstX1
, dstY1
,
2842 mask
, _mesa_lookup_enum_by_nr(filter
));
2844 if (ctx
->NewState
) {
2845 _mesa_update_state(ctx
);
2848 readFb
= ctx
->ReadBuffer
;
2849 drawFb
= ctx
->DrawBuffer
;
2851 if (!readFb
|| !drawFb
) {
2852 /* This will normally never happen but someday we may want to
2853 * support MakeCurrent() with no drawables.
2858 /* check for complete framebuffers */
2859 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2860 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2861 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2862 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2866 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2867 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2871 if (mask
& ~legalMaskBits
) {
2872 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2876 /* depth/stencil must be blitted with nearest filtering */
2877 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2878 && filter
!= GL_NEAREST
) {
2879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2880 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2884 /* get color read/draw renderbuffers */
2885 if (mask
& GL_COLOR_BUFFER_BIT
) {
2886 colorReadRb
= readFb
->_ColorReadBuffer
;
2887 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2889 /* From the EXT_framebuffer_object spec:
2891 * "If a buffer is specified in <mask> and does not exist in both
2892 * the read and draw framebuffers, the corresponding bit is silently
2895 if ((colorReadRb
== NULL
) || (colorDrawRb
== NULL
)) {
2896 colorReadRb
= colorDrawRb
= NULL
;
2897 mask
&= ~GL_COLOR_BUFFER_BIT
;
2899 else if (!compatible_color_datatypes(colorReadRb
->Format
,
2900 colorDrawRb
->Format
)) {
2901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2902 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2907 colorReadRb
= colorDrawRb
= NULL
;
2910 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2911 struct gl_renderbuffer
*readRb
=
2912 readFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2913 struct gl_renderbuffer
*drawRb
=
2914 drawFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
2916 /* From the EXT_framebuffer_object spec:
2918 * "If a buffer is specified in <mask> and does not exist in both
2919 * the read and draw framebuffers, the corresponding bit is silently
2922 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2923 mask
&= ~GL_STENCIL_BUFFER_BIT
;
2925 else if (_mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2926 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2927 /* There is no need to check the stencil datatype here, because
2928 * there is only one: GL_UNSIGNED_INT.
2930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2931 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2936 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2937 struct gl_renderbuffer
*readRb
=
2938 readFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2939 struct gl_renderbuffer
*drawRb
=
2940 drawFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2942 /* From the EXT_framebuffer_object spec:
2944 * "If a buffer is specified in <mask> and does not exist in both
2945 * the read and draw framebuffers, the corresponding bit is silently
2948 if ((readRb
== NULL
) || (drawRb
== NULL
)) {
2949 mask
&= ~GL_DEPTH_BUFFER_BIT
;
2951 else if ((_mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2952 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) ||
2953 (_mesa_get_format_datatype(readRb
->Format
) !=
2954 _mesa_get_format_datatype(drawRb
->Format
))) {
2955 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2956 "glBlitFramebufferEXT(depth buffer format mismatch)");
2961 if (readFb
->Visual
.samples
> 0 &&
2962 drawFb
->Visual
.samples
> 0 &&
2963 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2964 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2965 "glBlitFramebufferEXT(mismatched samples)");
2969 /* extra checks for multisample copies... */
2970 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2971 /* src and dest region sizes must be the same */
2972 if (abs(srcX1
- srcX0
) != abs(dstX1
- dstX0
) ||
2973 abs(srcY1
- srcY0
) != abs(dstY1
- dstY0
)) {
2974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2975 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2979 /* color formats must match */
2982 !compatible_resolve_formats(colorReadRb
, colorDrawRb
)) {
2983 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2984 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2989 if (filter
== GL_LINEAR
&& (mask
& GL_COLOR_BUFFER_BIT
)) {
2990 /* 3.1 spec, page 199:
2991 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2992 * if filter is LINEAR and read buffer contains integer data."
2994 GLenum type
= _mesa_get_format_datatype(colorReadRb
->Format
);
2995 if (type
== GL_INT
|| type
== GL_UNSIGNED_INT
) {
2996 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2997 "glBlitFramebufferEXT(integer color type)");
3002 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
3003 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
3009 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3011 srcX0
, srcY0
, srcX1
, srcY1
,
3012 dstX0
, dstY0
, dstX1
, dstY1
,
3015 const struct gl_renderbuffer_attachment
*att
;
3017 att
= find_attachment(readFb
, colorReadRb
);
3018 printf(" Src FBO %u RB %u (%dx%d) ",
3019 readFb
->Name
, colorReadRb
->Name
,
3020 colorReadRb
->Width
, colorReadRb
->Height
);
3021 if (att
&& att
->Texture
) {
3022 printf("Tex %u tgt 0x%x level %u face %u",
3024 att
->Texture
->Target
,
3030 att
= find_attachment(drawFb
, colorDrawRb
);
3031 printf(" Dst FBO %u RB %u (%dx%d) ",
3032 drawFb
->Name
, colorDrawRb
->Name
,
3033 colorDrawRb
->Width
, colorDrawRb
->Height
);
3034 if (att
&& att
->Texture
) {
3035 printf("Tex %u tgt 0x%x level %u face %u",
3037 att
->Texture
->Target
,
3049 ASSERT(ctx
->Driver
.BlitFramebuffer
);
3050 ctx
->Driver
.BlitFramebuffer(ctx
,
3051 srcX0
, srcY0
, srcX1
, srcY1
,
3052 dstX0
, dstY0
, dstX1
, dstY1
,
3058 _mesa_FramebufferTextureARB(GLenum target
, GLenum attachment
,
3059 GLuint texture
, GLint level
)
3061 GET_CURRENT_CONTEXT(ctx
);
3062 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3063 "glFramebufferTextureARB "
3064 "not implemented!");
3069 _mesa_FramebufferTextureFaceARB(GLenum target
, GLenum attachment
,
3070 GLuint texture
, GLint level
, GLenum face
)
3072 GET_CURRENT_CONTEXT(ctx
);
3073 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3074 "glFramebufferTextureFaceARB "
3075 "not implemented!");
3079 invalidate_framebuffer_storage(GLenum target
, GLsizei numAttachments
,
3080 const GLenum
*attachments
, GLint x
, GLint y
,
3081 GLsizei width
, GLsizei height
, const char *name
)
3084 struct gl_framebuffer
*fb
;
3085 GET_CURRENT_CONTEXT(ctx
);
3087 ASSERT_OUTSIDE_BEGIN_END(ctx
);
3089 fb
= get_framebuffer_target(ctx
, target
);
3091 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", name
);
3095 if (numAttachments
< 0) {
3096 _mesa_error(ctx
, GL_INVALID_VALUE
,
3097 "%s(numAttachments < 0)", name
);
3101 /* The GL_ARB_invalidate_subdata spec says:
3103 * "If an attachment is specified that does not exist in the
3104 * framebuffer bound to <target>, it is ignored."
3108 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3109 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3110 * INVALID_OPERATION is generated."
3112 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3113 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3114 * set of retrictions).
3116 for (i
= 0; i
< numAttachments
; i
++) {
3117 if (_mesa_is_winsys_fbo(fb
)) {
3118 switch (attachments
[i
]) {
3124 /* Accumulation buffers and auxilary buffers were removed in
3125 * OpenGL 3.1, and they never existed in OpenGL ES.
3127 if (ctx
->API
!= API_OPENGL
)
3137 case GL_FRONT_RIGHT
:
3138 if (!_mesa_is_desktop_gl(ctx
))
3145 switch (attachments
[i
]) {
3146 case GL_DEPTH_ATTACHMENT
:
3147 case GL_STENCIL_ATTACHMENT
:
3149 case GL_COLOR_ATTACHMENT0
:
3150 case GL_COLOR_ATTACHMENT1
:
3151 case GL_COLOR_ATTACHMENT2
:
3152 case GL_COLOR_ATTACHMENT3
:
3153 case GL_COLOR_ATTACHMENT4
:
3154 case GL_COLOR_ATTACHMENT5
:
3155 case GL_COLOR_ATTACHMENT6
:
3156 case GL_COLOR_ATTACHMENT7
:
3157 case GL_COLOR_ATTACHMENT8
:
3158 case GL_COLOR_ATTACHMENT9
:
3159 case GL_COLOR_ATTACHMENT10
:
3160 case GL_COLOR_ATTACHMENT11
:
3161 case GL_COLOR_ATTACHMENT12
:
3162 case GL_COLOR_ATTACHMENT13
:
3163 case GL_COLOR_ATTACHMENT14
:
3164 case GL_COLOR_ATTACHMENT15
: {
3165 const int k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
3166 if (k
>= ctx
->Const
.MaxColorAttachments
) {
3167 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3168 "%s(attachment >= max. color attachments)", name
);
3178 /* We don't actually do anything for this yet. Just return after
3179 * validating the parameters and generating the required errors.
3184 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(attachment)", name
);
3189 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
3190 const GLenum
*attachments
, GLint x
, GLint y
,
3191 GLsizei width
, GLsizei height
)
3193 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3194 x
, y
, width
, height
,
3195 "glInvalidateSubFramebuffer");
3199 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
3200 const GLenum
*attachments
)
3202 /* The GL_ARB_invalidate_subdata spec says:
3206 * void InvalidateFramebuffer(enum target,
3207 * sizei numAttachments,
3208 * const enum *attachments);
3210 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3211 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3212 * <MAX_VIEWPORT_DIMS[1]> respectively."
3214 invalidate_framebuffer_storage(target
, numAttachments
, attachments
,
3215 0, 0, MAX_VIEWPORT_WIDTH
, MAX_VIEWPORT_HEIGHT
,
3216 "glInvalidateFramebuffer");