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"
44 #include "renderbuffer.h"
52 /** Set this to 1 to help debug FBO incompleteness problems */
55 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
62 * None of the GL_EXT_framebuffer_object functions are compiled into
69 * When glGenRender/FramebuffersEXT() is called we insert pointers to
70 * these placeholder objects into the hash table.
71 * Later, when the object ID is first bound, we replace the placeholder
72 * with the real frame/renderbuffer.
74 static struct gl_framebuffer DummyFramebuffer
;
75 static struct gl_renderbuffer DummyRenderbuffer
;
78 #define IS_CUBE_FACE(TARGET) \
79 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
80 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
84 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
90 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
97 _mesa_init_fbobjects(GLcontext
*ctx
)
99 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
100 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
105 * Helper routine for getting a gl_renderbuffer.
107 struct gl_renderbuffer
*
108 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
110 struct gl_renderbuffer
*rb
;
115 rb
= (struct gl_renderbuffer
*)
116 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
122 * Helper routine for getting a gl_framebuffer.
124 struct gl_framebuffer
*
125 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
127 struct gl_framebuffer
*fb
;
132 fb
= (struct gl_framebuffer
*)
133 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
139 * Mark the given framebuffer as invalid. This will force the
140 * test for framebuffer completeness to be done before the framebuffer
144 invalidate_framebuffer(struct gl_framebuffer
*fb
)
146 fb
->_Status
= 0; /* "indeterminate" */
151 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
152 * gl_renderbuffer_attachment object.
153 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
154 * the depth buffer attachment point.
156 struct gl_renderbuffer_attachment
*
157 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
162 switch (attachment
) {
163 case GL_COLOR_ATTACHMENT0_EXT
:
164 case GL_COLOR_ATTACHMENT1_EXT
:
165 case GL_COLOR_ATTACHMENT2_EXT
:
166 case GL_COLOR_ATTACHMENT3_EXT
:
167 case GL_COLOR_ATTACHMENT4_EXT
:
168 case GL_COLOR_ATTACHMENT5_EXT
:
169 case GL_COLOR_ATTACHMENT6_EXT
:
170 case GL_COLOR_ATTACHMENT7_EXT
:
171 case GL_COLOR_ATTACHMENT8_EXT
:
172 case GL_COLOR_ATTACHMENT9_EXT
:
173 case GL_COLOR_ATTACHMENT10_EXT
:
174 case GL_COLOR_ATTACHMENT11_EXT
:
175 case GL_COLOR_ATTACHMENT12_EXT
:
176 case GL_COLOR_ATTACHMENT13_EXT
:
177 case GL_COLOR_ATTACHMENT14_EXT
:
178 case GL_COLOR_ATTACHMENT15_EXT
:
179 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
180 if (i
>= ctx
->Const
.MaxColorAttachments
) {
183 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
184 case GL_DEPTH_STENCIL_ATTACHMENT
:
186 case GL_DEPTH_ATTACHMENT_EXT
:
187 return &fb
->Attachment
[BUFFER_DEPTH
];
188 case GL_STENCIL_ATTACHMENT_EXT
:
189 return &fb
->Attachment
[BUFFER_STENCIL
];
197 * Remove any texture or renderbuffer attached to the given attachment
198 * point. Update reference counts, etc.
201 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
203 if (att
->Type
== GL_TEXTURE
) {
204 ASSERT(att
->Texture
);
205 if (ctx
->Driver
.FinishRenderTexture
) {
206 /* tell driver that we're done rendering to this texture. */
207 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
209 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
210 ASSERT(!att
->Texture
);
212 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
213 ASSERT(!att
->Texture
);
214 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
215 ASSERT(!att
->Renderbuffer
);
218 att
->Complete
= GL_TRUE
;
223 * Bind a texture object to an attachment point.
224 * The previous binding, if any, will be removed first.
227 _mesa_set_texture_attachment(GLcontext
*ctx
,
228 struct gl_framebuffer
*fb
,
229 struct gl_renderbuffer_attachment
*att
,
230 struct gl_texture_object
*texObj
,
231 GLenum texTarget
, GLuint level
, GLuint zoffset
)
233 if (att
->Texture
== texObj
) {
234 /* re-attaching same texture */
235 ASSERT(att
->Type
== GL_TEXTURE
);
239 _mesa_remove_attachment(ctx
, att
);
240 att
->Type
= GL_TEXTURE
;
241 assert(!att
->Texture
);
242 _mesa_reference_texobj(&att
->Texture
, texObj
);
245 /* always update these fields */
246 att
->TextureLevel
= level
;
247 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
248 att
->Zoffset
= zoffset
;
249 att
->Complete
= GL_FALSE
;
251 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
252 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
255 invalidate_framebuffer(fb
);
260 * Bind a renderbuffer to an attachment point.
261 * The previous binding, if any, will be removed first.
264 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
265 struct gl_renderbuffer_attachment
*att
,
266 struct gl_renderbuffer
*rb
)
268 /* XXX check if re-doing same attachment, exit early */
269 _mesa_remove_attachment(ctx
, att
);
270 att
->Type
= GL_RENDERBUFFER_EXT
;
271 att
->Texture
= NULL
; /* just to be safe */
272 att
->Complete
= GL_FALSE
;
273 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
278 * Fallback for ctx->Driver.FramebufferRenderbuffer()
279 * Attach a renderbuffer object to a framebuffer object.
282 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
283 GLenum attachment
, struct gl_renderbuffer
*rb
)
285 struct gl_renderbuffer_attachment
*att
;
287 _glthread_LOCK_MUTEX(fb
->Mutex
);
289 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
292 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
293 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
294 /* do stencil attachment here (depth already done above) */
295 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
297 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
301 _mesa_remove_attachment(ctx
, att
);
304 invalidate_framebuffer(fb
);
306 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
314 att_incomplete(const char *msg
)
317 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
328 fbo_incomplete(const char *msg
, int index
)
331 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
342 * Test if an attachment point is complete and update its Complete field.
343 * \param format if GL_COLOR, this is a color attachment point,
344 * if GL_DEPTH, this is a depth component attachment point,
345 * if GL_STENCIL, this is a stencil component attachment point.
348 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
349 struct gl_renderbuffer_attachment
*att
)
351 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
353 /* assume complete */
354 att
->Complete
= GL_TRUE
;
356 /* Look for reasons why the attachment might be incomplete */
357 if (att
->Type
== GL_TEXTURE
) {
358 const struct gl_texture_object
*texObj
= att
->Texture
;
359 struct gl_texture_image
*texImage
;
363 att_incomplete("no texobj");
364 att
->Complete
= GL_FALSE
;
368 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
370 att_incomplete("no teximage");
371 att
->Complete
= GL_FALSE
;
374 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
375 att_incomplete("teximage width/height=0");
376 _mesa_printf("texobj = %u\n", texObj
->Name
);
377 _mesa_printf("level = %d\n", att
->TextureLevel
);
378 att
->Complete
= GL_FALSE
;
381 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
382 att_incomplete("bad z offset");
383 att
->Complete
= GL_FALSE
;
387 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
389 if (format
== GL_COLOR
) {
390 if (baseFormat
!= GL_RGB
&&
391 baseFormat
!= GL_RGBA
) {
392 att_incomplete("bad format");
393 att
->Complete
= GL_FALSE
;
396 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
397 att_incomplete("compressed internalformat");
398 att
->Complete
= GL_FALSE
;
402 else if (format
== GL_DEPTH
) {
403 if (baseFormat
== GL_DEPTH_COMPONENT
) {
406 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
407 ctx
->Extensions
.ARB_depth_texture
&&
408 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
412 att
->Complete
= GL_FALSE
;
413 att_incomplete("bad depth format");
418 ASSERT(format
== GL_STENCIL
);
419 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
420 ctx
->Extensions
.ARB_depth_texture
&&
421 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
425 /* no such thing as stencil-only textures */
426 att_incomplete("illegal stencil texture");
427 att
->Complete
= GL_FALSE
;
432 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
433 const GLenum baseFormat
=
434 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
436 ASSERT(att
->Renderbuffer
);
437 if (!att
->Renderbuffer
->InternalFormat
||
438 att
->Renderbuffer
->Width
< 1 ||
439 att
->Renderbuffer
->Height
< 1) {
440 att_incomplete("0x0 renderbuffer");
441 att
->Complete
= GL_FALSE
;
444 if (format
== GL_COLOR
) {
445 if (baseFormat
!= GL_RGB
&&
446 baseFormat
!= GL_RGBA
) {
447 att_incomplete("bad renderbuffer color format");
448 att
->Complete
= GL_FALSE
;
452 else if (format
== GL_DEPTH
) {
453 if (baseFormat
== GL_DEPTH_COMPONENT
) {
456 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
457 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
461 att_incomplete("bad renderbuffer depth format");
462 att
->Complete
= GL_FALSE
;
467 assert(format
== GL_STENCIL
);
468 if (baseFormat
== GL_STENCIL_INDEX
) {
471 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
472 baseFormat
== GL_DEPTH_STENCIL_EXT
) {
476 att
->Complete
= GL_FALSE
;
477 att_incomplete("bad renderbuffer stencil format");
483 ASSERT(att
->Type
== GL_NONE
);
491 * Test if the given framebuffer object is complete and update its
492 * Status field with the results.
493 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
494 * driver to make hardware-specific validation/completeness checks.
495 * Also update the framebuffer's Width and Height fields if the
496 * framebuffer is complete.
499 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
502 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
503 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
504 GLint numSamples
= -1;
508 assert(fb
->Name
!= 0);
514 /* Start at -2 to more easily loop over all attachment points.
519 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
520 struct gl_renderbuffer_attachment
*att
;
524 * XXX for ARB_fbo, only check color buffers that are named by
525 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
528 /* check for attachment completeness
531 att
= &fb
->Attachment
[BUFFER_DEPTH
];
532 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
533 if (!att
->Complete
) {
534 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
535 fbo_incomplete("depth attachment incomplete", -1);
540 att
= &fb
->Attachment
[BUFFER_STENCIL
];
541 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
542 if (!att
->Complete
) {
543 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
544 fbo_incomplete("stencil attachment incomplete", -1);
549 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
550 test_attachment_completeness(ctx
, GL_COLOR
, att
);
551 if (!att
->Complete
) {
552 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
553 fbo_incomplete("color attachment incomplete", i
);
558 /* get width, height, format of the renderbuffer/texture
560 if (att
->Type
== GL_TEXTURE
) {
561 const struct gl_texture_image
*texImg
562 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
563 minWidth
= MIN2(minWidth
, texImg
->Width
);
564 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
565 minHeight
= MIN2(minHeight
, texImg
->Height
);
566 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
567 f
= texImg
->_BaseFormat
;
569 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
570 && f
!= GL_DEPTH_STENCIL_EXT
) {
571 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
572 fbo_incomplete("texture attachment incomplete", -1);
576 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
577 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
578 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
579 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
580 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
581 f
= att
->Renderbuffer
->InternalFormat
;
585 assert(att
->Type
== GL_NONE
);
589 if (numSamples
< 0) {
591 numSamples
= att
->Renderbuffer
->NumSamples
;
594 /* Error-check width, height, format, samples
596 if (numImages
== 1) {
597 /* save format, num samples */
603 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
604 /* check that width, height, format are same */
605 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
606 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
607 fbo_incomplete("width or height mismatch", -1);
610 /* check that all color buffer have same format */
611 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
612 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
613 fbo_incomplete("format mismatch", -1);
617 if (att
->Renderbuffer
&&
618 att
->Renderbuffer
->NumSamples
!= numSamples
) {
619 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
620 fbo_incomplete("inconsistant number of samples", i
);
627 #ifndef FEATURE_OES_framebuffer_object
628 /* Check that all DrawBuffers are present */
629 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
630 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
631 const struct gl_renderbuffer_attachment
*att
632 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
634 if (att
->Type
== GL_NONE
) {
635 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
636 fbo_incomplete("missing drawbuffer", j
);
642 /* Check that the ReadBuffer is present */
643 if (fb
->ColorReadBuffer
!= GL_NONE
) {
644 const struct gl_renderbuffer_attachment
*att
645 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
647 if (att
->Type
== GL_NONE
) {
648 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
649 fbo_incomplete("missing readbuffer", -1);
657 if (numImages
== 0) {
658 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
659 fbo_incomplete("no attachments", -1);
663 /* Provisionally set status = COMPLETE ... */
664 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
666 /* ... but the driver may say the FB is incomplete.
667 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
670 if (ctx
->Driver
.ValidateFramebuffer
) {
671 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
672 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
673 fbo_incomplete("driver marked FBO as incomplete", -1);
677 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
679 * Note that if ARB_framebuffer_object is supported and the attached
680 * renderbuffers/textures are different sizes, the framebuffer
681 * width/height will be set to the smallest width/height.
683 fb
->Width
= minWidth
;
684 fb
->Height
= minHeight
;
686 /* finally, update the visual info for the framebuffer */
687 _mesa_update_framebuffer_visual(fb
);
693 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
695 GET_CURRENT_CONTEXT(ctx
);
696 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
698 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
699 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
707 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
709 struct gl_renderbuffer
*newRb
;
710 GET_CURRENT_CONTEXT(ctx
);
712 ASSERT_OUTSIDE_BEGIN_END(ctx
);
714 if (target
!= GL_RENDERBUFFER_EXT
) {
715 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
719 /* No need to flush here since the render buffer binding has no
720 * effect on rendering state.
724 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
725 if (newRb
== &DummyRenderbuffer
) {
726 /* ID was reserved, but no real renderbuffer object made yet */
729 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
730 /* All RB IDs must be Gen'd */
731 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
736 /* create new renderbuffer object */
737 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
739 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
742 ASSERT(newRb
->AllocStorage
);
743 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
744 newRb
->RefCount
= 1; /* referenced by hash table */
751 ASSERT(newRb
!= &DummyRenderbuffer
);
753 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
758 * If the given renderbuffer is anywhere attached to the framebuffer, detach
760 * This is used when a renderbuffer object is deleted.
761 * The spec calls for unbinding.
764 detach_renderbuffer(GLcontext
*ctx
,
765 struct gl_framebuffer
*fb
,
766 struct gl_renderbuffer
*rb
)
769 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
770 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
771 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
774 invalidate_framebuffer(fb
);
779 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
782 GET_CURRENT_CONTEXT(ctx
);
784 ASSERT_OUTSIDE_BEGIN_END(ctx
);
785 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
787 for (i
= 0; i
< n
; i
++) {
788 if (renderbuffers
[i
] > 0) {
789 struct gl_renderbuffer
*rb
;
790 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
792 /* check if deleting currently bound renderbuffer object */
793 if (rb
== ctx
->CurrentRenderbuffer
) {
795 ASSERT(rb
->RefCount
>= 2);
796 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
799 if (ctx
->DrawBuffer
->Name
) {
800 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
802 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
803 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
806 /* Remove from hash table immediately, to free the ID.
807 * But the object will not be freed until it's no longer
808 * referenced anywhere else.
810 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
812 if (rb
!= &DummyRenderbuffer
) {
813 /* no longer referenced by hash table */
814 _mesa_reference_renderbuffer(&rb
, NULL
);
823 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
825 GET_CURRENT_CONTEXT(ctx
);
829 ASSERT_OUTSIDE_BEGIN_END(ctx
);
832 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
839 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
841 for (i
= 0; i
< n
; i
++) {
842 GLuint name
= first
+ i
;
843 renderbuffers
[i
] = name
;
844 /* insert dummy placeholder into hash table */
845 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
846 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
847 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
853 * Given an internal format token for a render buffer, return the
854 * corresponding base format.
855 * This is very similar to _mesa_base_tex_format() but the set of valid
856 * internal formats is somewhat different.
858 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
859 * GL_DEPTH_STENCIL_EXT or zero if error.
862 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
864 switch (internalFormat
) {
883 case GL_STENCIL_INDEX
:
884 case GL_STENCIL_INDEX1_EXT
:
885 case GL_STENCIL_INDEX4_EXT
:
886 case GL_STENCIL_INDEX8_EXT
:
887 case GL_STENCIL_INDEX16_EXT
:
888 return GL_STENCIL_INDEX
;
889 case GL_DEPTH_COMPONENT
:
890 case GL_DEPTH_COMPONENT16
:
891 case GL_DEPTH_COMPONENT24
:
892 case GL_DEPTH_COMPONENT32
:
893 return GL_DEPTH_COMPONENT
;
894 case GL_DEPTH_STENCIL_EXT
:
895 case GL_DEPTH24_STENCIL8_EXT
:
896 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
897 return GL_DEPTH_STENCIL_EXT
;
900 /* XXX add floating point formats eventually */
907 /** sentinal value, see below */
908 #define NO_SAMPLES 1000
912 * Helper function used by _mesa_RenderbufferStorageEXT() and
913 * _mesa_RenderbufferStorageMultisample().
914 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
917 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
918 GLsizei width
, GLsizei height
, GLsizei samples
)
920 const char *func
= samples
== NO_SAMPLES
?
921 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
922 struct gl_renderbuffer
*rb
;
924 GET_CURRENT_CONTEXT(ctx
);
926 ASSERT_OUTSIDE_BEGIN_END(ctx
);
928 if (target
!= GL_RENDERBUFFER_EXT
) {
929 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
933 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
934 if (baseFormat
== 0) {
935 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
939 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
940 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
944 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
945 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
949 if (samples
== NO_SAMPLES
) {
950 /* NumSamples == 0 indicates non-multisampling */
953 else if (samples
> ctx
->Const
.MaxSamples
) {
954 /* note: driver may choose to use more samples than what's requested */
955 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
959 rb
= ctx
->CurrentRenderbuffer
;
961 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
965 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
967 if (rb
->InternalFormat
== internalFormat
&&
968 rb
->Width
== (GLuint
) width
&&
969 rb
->Height
== (GLuint
) height
) {
970 /* no change in allocation needed */
974 /* These MUST get set by the AllocStorage func */
975 rb
->Format
= MESA_FORMAT_NONE
;
976 rb
->NumSamples
= samples
;
978 /* Now allocate the storage */
979 ASSERT(rb
->AllocStorage
);
980 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
981 /* No error - check/set fields now */
982 assert(rb
->Format
!= MESA_FORMAT_NONE
);
983 assert(rb
->Width
== (GLuint
) width
);
984 assert(rb
->Height
== (GLuint
) height
);
985 rb
->InternalFormat
= internalFormat
;
986 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
987 assert(rb
->_BaseFormat
!= 0);
990 /* Probably ran out of memory - clear the fields */
993 rb
->Format
= MESA_FORMAT_NONE
;
994 rb
->InternalFormat
= GL_NONE
;
995 rb
->_BaseFormat
= GL_NONE
;
1000 test_framebuffer_completeness(ctx, fb);
1002 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1009 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1010 * _mesa_GetFramebufferAttachmentParameterivEXT()
1011 * We have to be careful to respect the base format. For example, if a
1012 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1013 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1014 * we need to return zero.
1017 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1020 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1021 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1022 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1023 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1024 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1025 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1026 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
)
1027 return _mesa_get_format_bits(format
, pname
);
1030 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1031 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1032 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_ALPHA
)
1033 return _mesa_get_format_bits(format
, pname
);
1036 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1037 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1038 if (baseFormat
== GL_DEPTH_COMPONENT
|| baseFormat
== GL_DEPTH_STENCIL
)
1039 return _mesa_get_format_bits(format
, pname
);
1042 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1043 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1044 if (baseFormat
== GL_STENCIL_INDEX
|| baseFormat
== GL_DEPTH_STENCIL
)
1045 return _mesa_get_format_bits(format
, pname
);
1056 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1057 GLsizei width
, GLsizei height
)
1059 /* GL_ARB_fbo says calling this function is equivalent to calling
1060 * glRenderbufferStorageMultisample() with samples=0. We pass in
1061 * a token value here just for error reporting purposes.
1063 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1068 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1069 GLenum internalFormat
,
1070 GLsizei width
, GLsizei height
)
1072 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1078 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1080 struct gl_renderbuffer
*rb
;
1081 GET_CURRENT_CONTEXT(ctx
);
1083 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1085 if (target
!= GL_RENDERBUFFER_EXT
) {
1086 _mesa_error(ctx
, GL_INVALID_ENUM
,
1087 "glGetRenderbufferParameterivEXT(target)");
1091 rb
= ctx
->CurrentRenderbuffer
;
1093 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1094 "glGetRenderbufferParameterivEXT");
1098 /* No need to flush here since we're just quering state which is
1099 * not effected by rendering.
1103 case GL_RENDERBUFFER_WIDTH_EXT
:
1104 *params
= rb
->Width
;
1106 case GL_RENDERBUFFER_HEIGHT_EXT
:
1107 *params
= rb
->Height
;
1109 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1110 *params
= rb
->InternalFormat
;
1112 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1113 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1114 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1115 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1116 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1117 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1118 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1120 case GL_RENDERBUFFER_SAMPLES
:
1121 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1122 *params
= rb
->NumSamples
;
1127 _mesa_error(ctx
, GL_INVALID_ENUM
,
1128 "glGetRenderbufferParameterivEXT(target)");
1134 GLboolean GLAPIENTRY
1135 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1137 GET_CURRENT_CONTEXT(ctx
);
1138 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1140 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1141 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1149 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1152 ASSERT(ctx
->Driver
.RenderTexture
);
1153 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1154 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1155 struct gl_texture_object
*texObj
= att
->Texture
;
1157 && texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1158 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1165 * Examine all the framebuffer's attachments to see if any are textures.
1166 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1167 * notify the device driver that the texture image may have changed.
1170 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1172 if (ctx
->Driver
.FinishRenderTexture
) {
1174 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1175 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1176 if (att
->Texture
&& att
->Renderbuffer
) {
1177 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1185 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1187 struct gl_framebuffer
*newFb
, *newFbread
;
1188 GLboolean bindReadBuf
, bindDrawBuf
;
1189 GET_CURRENT_CONTEXT(ctx
);
1192 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1193 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1194 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1198 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1200 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1202 "glBindFramebufferEXT(unsupported)");
1207 #if FEATURE_EXT_framebuffer_blit
1208 case GL_DRAW_FRAMEBUFFER_EXT
:
1209 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1210 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1213 bindDrawBuf
= GL_TRUE
;
1214 bindReadBuf
= GL_FALSE
;
1216 case GL_READ_FRAMEBUFFER_EXT
:
1217 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1218 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1221 bindDrawBuf
= GL_FALSE
;
1222 bindReadBuf
= GL_TRUE
;
1225 case GL_FRAMEBUFFER_EXT
:
1226 bindDrawBuf
= GL_TRUE
;
1227 bindReadBuf
= GL_TRUE
;
1230 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1235 /* Binding a user-created framebuffer object */
1236 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1237 if (newFb
== &DummyFramebuffer
) {
1238 /* ID was reserved, but no real framebuffer object made yet */
1241 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1242 /* All FBO IDs must be Gen'd */
1243 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1248 /* create new framebuffer object */
1249 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1251 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1254 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1259 /* Binding the window system framebuffer (which was originally set
1260 * with MakeCurrent).
1262 newFb
= ctx
->WinSysDrawBuffer
;
1263 newFbread
= ctx
->WinSysReadBuffer
;
1267 ASSERT(newFb
!= &DummyFramebuffer
);
1270 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1273 if (ctx
->ReadBuffer
== newFbread
) {
1274 bindReadBuf
= GL_FALSE
; /* no change */
1277 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1278 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1283 /* check if old FB had any texture attachments */
1284 if (ctx
->DrawBuffer
->Name
!= 0) {
1285 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1288 if (ctx
->DrawBuffer
== newFb
) {
1289 bindDrawBuf
= GL_FALSE
; /* no change */
1292 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1293 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1296 if (newFb
->Name
!= 0) {
1297 /* check if newly bound framebuffer has any texture attachments */
1298 check_begin_texture_render(ctx
, newFb
);
1302 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1303 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1309 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1312 GET_CURRENT_CONTEXT(ctx
);
1314 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1315 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1317 for (i
= 0; i
< n
; i
++) {
1318 if (framebuffers
[i
] > 0) {
1319 struct gl_framebuffer
*fb
;
1320 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1322 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1324 /* check if deleting currently bound framebuffer object */
1325 if (fb
== ctx
->DrawBuffer
) {
1327 ASSERT(fb
->RefCount
>= 2);
1328 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1330 if (fb
== ctx
->ReadBuffer
) {
1332 ASSERT(fb
->RefCount
>= 2);
1333 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1336 /* remove from hash table immediately, to free the ID */
1337 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1339 if (fb
!= &DummyFramebuffer
) {
1340 /* But the object will not be freed until it's no longer
1341 * bound in any context.
1343 _mesa_reference_framebuffer(&fb
, NULL
);
1352 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1354 GET_CURRENT_CONTEXT(ctx
);
1358 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1361 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1368 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1370 for (i
= 0; i
< n
; i
++) {
1371 GLuint name
= first
+ i
;
1372 framebuffers
[i
] = name
;
1373 /* insert dummy placeholder into hash table */
1374 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1375 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1376 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1383 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1385 struct gl_framebuffer
*buffer
;
1386 GET_CURRENT_CONTEXT(ctx
);
1388 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1391 #if FEATURE_EXT_framebuffer_blit
1392 case GL_DRAW_FRAMEBUFFER_EXT
:
1393 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1394 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1397 buffer
= ctx
->DrawBuffer
;
1399 case GL_READ_FRAMEBUFFER_EXT
:
1400 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1401 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1404 buffer
= ctx
->ReadBuffer
;
1407 case GL_FRAMEBUFFER_EXT
:
1408 buffer
= ctx
->DrawBuffer
;
1411 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1412 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1415 if (buffer
->Name
== 0) {
1416 /* The window system / default framebuffer is always complete */
1417 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1420 /* No need to flush here */
1422 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1423 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1426 return buffer
->_Status
;
1432 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1435 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1436 GLenum attachment
, GLenum textarget
, GLuint texture
,
1437 GLint level
, GLint zoffset
)
1439 struct gl_renderbuffer_attachment
*att
;
1440 struct gl_texture_object
*texObj
= NULL
;
1441 struct gl_framebuffer
*fb
;
1442 GLboolean error
= GL_FALSE
;
1444 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1447 case GL_READ_FRAMEBUFFER_EXT
:
1448 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1449 fb
= ctx
->ReadBuffer
;
1451 case GL_DRAW_FRAMEBUFFER_EXT
:
1452 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1454 case GL_FRAMEBUFFER_EXT
:
1455 fb
= ctx
->DrawBuffer
;
1462 _mesa_error(ctx
, GL_INVALID_ENUM
,
1463 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1469 /* check framebuffer binding */
1470 if (fb
->Name
== 0) {
1471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1472 "glFramebufferTexture%sEXT", caller
);
1477 /* The textarget, level, and zoffset parameters are only validated if
1478 * texture is non-zero.
1481 GLboolean err
= GL_TRUE
;
1483 texObj
= _mesa_lookup_texture(ctx
, texture
);
1484 if (texObj
!= NULL
) {
1485 if (textarget
== 0) {
1486 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1487 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1488 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1491 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1492 ? !IS_CUBE_FACE(textarget
)
1493 : (texObj
->Target
!= textarget
);
1498 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1499 "glFramebufferTexture%sEXT(texture target mismatch)",
1504 if (texObj
->Target
== GL_TEXTURE_3D
) {
1505 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1506 if (zoffset
< 0 || zoffset
>= maxSize
) {
1507 _mesa_error(ctx
, GL_INVALID_VALUE
,
1508 "glFramebufferTexture%sEXT(zoffset)", caller
);
1512 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1513 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1514 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1515 _mesa_error(ctx
, GL_INVALID_VALUE
,
1516 "glFramebufferTexture%sEXT(layer)", caller
);
1522 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1523 _mesa_error(ctx
, GL_INVALID_VALUE
,
1524 "glFramebufferTexture%sEXT(level)", caller
);
1529 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1531 _mesa_error(ctx
, GL_INVALID_ENUM
,
1532 "glFramebufferTexture%sEXT(attachment)", caller
);
1536 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1538 _glthread_LOCK_MUTEX(fb
->Mutex
);
1540 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1542 /* Set the render-to-texture flag. We'll check this flag in
1543 * glTexImage() and friends to determine if we need to revalidate
1544 * any FBOs that might be rendering into this texture.
1545 * This flag never gets cleared since it's non-trivial to determine
1546 * when all FBOs might be done rendering to this texture. That's OK
1547 * though since it's uncommon to render to a texture then repeatedly
1548 * call glTexImage() to change images in the texture.
1550 texObj
->_RenderToTexture
= GL_TRUE
;
1553 _mesa_remove_attachment(ctx
, att
);
1556 invalidate_framebuffer(fb
);
1558 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1564 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1565 GLenum textarget
, GLuint texture
, GLint level
)
1567 GET_CURRENT_CONTEXT(ctx
);
1569 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1570 _mesa_error(ctx
, GL_INVALID_ENUM
,
1571 "glFramebufferTexture1DEXT(textarget)");
1575 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1581 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1582 GLenum textarget
, GLuint texture
, GLint level
)
1584 GET_CURRENT_CONTEXT(ctx
);
1586 if ((texture
!= 0) &&
1587 (textarget
!= GL_TEXTURE_2D
) &&
1588 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1589 (!IS_CUBE_FACE(textarget
))) {
1590 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1591 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1595 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1601 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1602 GLenum textarget
, GLuint texture
,
1603 GLint level
, GLint zoffset
)
1605 GET_CURRENT_CONTEXT(ctx
);
1607 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1608 _mesa_error(ctx
, GL_INVALID_ENUM
,
1609 "glFramebufferTexture3DEXT(textarget)");
1613 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1619 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1620 GLuint texture
, GLint level
, GLint layer
)
1622 GET_CURRENT_CONTEXT(ctx
);
1624 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1630 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1631 GLenum renderbufferTarget
,
1632 GLuint renderbuffer
)
1634 struct gl_renderbuffer_attachment
*att
;
1635 struct gl_framebuffer
*fb
;
1636 struct gl_renderbuffer
*rb
;
1637 GET_CURRENT_CONTEXT(ctx
);
1639 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1642 #if FEATURE_EXT_framebuffer_blit
1643 case GL_DRAW_FRAMEBUFFER_EXT
:
1644 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1645 _mesa_error(ctx
, GL_INVALID_ENUM
,
1646 "glFramebufferRenderbufferEXT(target)");
1649 fb
= ctx
->DrawBuffer
;
1651 case GL_READ_FRAMEBUFFER_EXT
:
1652 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1653 _mesa_error(ctx
, GL_INVALID_ENUM
,
1654 "glFramebufferRenderbufferEXT(target)");
1657 fb
= ctx
->ReadBuffer
;
1660 case GL_FRAMEBUFFER_EXT
:
1661 fb
= ctx
->DrawBuffer
;
1664 _mesa_error(ctx
, GL_INVALID_ENUM
,
1665 "glFramebufferRenderbufferEXT(target)");
1669 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1670 _mesa_error(ctx
, GL_INVALID_ENUM
,
1671 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1675 if (fb
->Name
== 0) {
1676 /* Can't attach new renderbuffers to a window system framebuffer */
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1681 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1683 _mesa_error(ctx
, GL_INVALID_ENUM
,
1684 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1685 _mesa_lookup_enum_by_nr(attachment
));
1690 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1692 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1693 "glFramebufferRenderbufferEXT(non-existant"
1694 " renderbuffer %u)", renderbuffer
);
1699 /* remove renderbuffer attachment */
1703 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1704 /* make sure the renderbuffer is a depth/stencil format */
1705 const GLenum baseFormat
=
1706 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
1707 if (baseFormat
!= GL_DEPTH_STENCIL
) {
1708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1709 "glFramebufferRenderbufferEXT(renderbuffer"
1710 " is not DEPTH_STENCIL format)");
1716 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1718 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1719 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1721 /* Some subsequent GL commands may depend on the framebuffer's visual
1722 * after the binding is updated. Update visual info now.
1724 _mesa_update_framebuffer_visual(fb
);
1729 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1730 GLenum pname
, GLint
*params
)
1732 const struct gl_renderbuffer_attachment
*att
;
1733 struct gl_framebuffer
*buffer
;
1734 GET_CURRENT_CONTEXT(ctx
);
1736 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1739 #if FEATURE_EXT_framebuffer_blit
1740 case GL_DRAW_FRAMEBUFFER_EXT
:
1741 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1742 _mesa_error(ctx
, GL_INVALID_ENUM
,
1743 "glGetFramebufferAttachmentParameterivEXT(target)");
1746 buffer
= ctx
->DrawBuffer
;
1748 case GL_READ_FRAMEBUFFER_EXT
:
1749 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1750 _mesa_error(ctx
, GL_INVALID_ENUM
,
1751 "glGetFramebufferAttachmentParameterivEXT(target)");
1754 buffer
= ctx
->ReadBuffer
;
1757 case GL_FRAMEBUFFER_EXT
:
1758 buffer
= ctx
->DrawBuffer
;
1761 _mesa_error(ctx
, GL_INVALID_ENUM
,
1762 "glGetFramebufferAttachmentParameterivEXT(target)");
1766 if (buffer
->Name
== 0) {
1767 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1768 "glGetFramebufferAttachmentParameterivEXT");
1772 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1774 _mesa_error(ctx
, GL_INVALID_ENUM
,
1775 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1779 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1780 /* the depth and stencil attachments must point to the same buffer */
1781 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1782 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1783 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1784 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1785 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1786 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1787 " attachments differ)");
1792 /* No need to flush here */
1795 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1796 *params
= att
->Type
;
1798 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1799 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1800 *params
= att
->Renderbuffer
->Name
;
1802 else if (att
->Type
== GL_TEXTURE
) {
1803 *params
= att
->Texture
->Name
;
1806 _mesa_error(ctx
, GL_INVALID_ENUM
,
1807 "glGetFramebufferAttachmentParameterivEXT(pname)");
1810 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1811 if (att
->Type
== GL_TEXTURE
) {
1812 *params
= att
->TextureLevel
;
1815 _mesa_error(ctx
, GL_INVALID_ENUM
,
1816 "glGetFramebufferAttachmentParameterivEXT(pname)");
1819 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1820 if (att
->Type
== GL_TEXTURE
) {
1821 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1822 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1829 _mesa_error(ctx
, GL_INVALID_ENUM
,
1830 "glGetFramebufferAttachmentParameterivEXT(pname)");
1833 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1834 if (att
->Type
== GL_TEXTURE
) {
1835 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1836 *params
= att
->Zoffset
;
1843 _mesa_error(ctx
, GL_INVALID_ENUM
,
1844 "glGetFramebufferAttachmentParameterivEXT(pname)");
1847 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1848 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1849 _mesa_error(ctx
, GL_INVALID_ENUM
,
1850 "glGetFramebufferAttachmentParameterivEXT(pname)");
1853 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
1856 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1857 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1858 _mesa_error(ctx
, GL_INVALID_ENUM
,
1859 "glGetFramebufferAttachmentParameterivEXT(pname)");
1863 gl_format format
= att
->Renderbuffer
->Format
;
1864 if (format
== MESA_FORMAT_CI8
|| format
== MESA_FORMAT_S8
) {
1869 *params
= _mesa_get_format_datatype(format
);
1873 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1874 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1875 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1876 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1877 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1878 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1879 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1880 _mesa_error(ctx
, GL_INVALID_ENUM
,
1881 "glGetFramebufferAttachmentParameterivEXT(pname)");
1883 else if (att
->Texture
) {
1884 const struct gl_texture_image
*texImage
=
1885 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
1888 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
1889 texImage
->TexFormat
);
1895 else if (att
->Renderbuffer
) {
1896 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
1897 att
->Renderbuffer
->Format
);
1904 _mesa_error(ctx
, GL_INVALID_ENUM
,
1905 "glGetFramebufferAttachmentParameterivEXT(pname)");
1912 _mesa_GenerateMipmapEXT(GLenum target
)
1914 struct gl_texture_unit
*texUnit
;
1915 struct gl_texture_object
*texObj
;
1916 GET_CURRENT_CONTEXT(ctx
);
1918 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1919 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1925 case GL_TEXTURE_CUBE_MAP
:
1926 /* OK, legal value */
1929 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1933 texUnit
= _mesa_get_current_tex_unit(ctx
);
1934 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1936 _mesa_lock_texture(ctx
, texObj
);
1937 if (target
== GL_TEXTURE_CUBE_MAP
) {
1939 for (face
= 0; face
< 6; face
++)
1940 ctx
->Driver
.GenerateMipmap(ctx
,
1941 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1945 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1947 _mesa_unlock_texture(ctx
, texObj
);
1951 #if FEATURE_EXT_framebuffer_blit
1953 static const struct gl_renderbuffer_attachment
*
1954 find_attachment(const struct gl_framebuffer
*fb
, const struct gl_renderbuffer
*rb
)
1957 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
1958 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
1959 return &fb
->Attachment
[i
];
1967 * Blit rectangular region, optionally from one framebuffer to another.
1969 * Note, if the src buffer is multisampled and the dest is not, this is
1970 * when the samples must be resolved to a single color.
1973 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1974 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1975 GLbitfield mask
, GLenum filter
)
1977 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1978 GL_DEPTH_BUFFER_BIT
|
1979 GL_STENCIL_BUFFER_BIT
);
1980 const struct gl_framebuffer
*readFb
, *drawFb
;
1981 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1982 GET_CURRENT_CONTEXT(ctx
);
1984 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1985 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1987 if (ctx
->NewState
) {
1988 _mesa_update_state(ctx
);
1991 readFb
= ctx
->ReadBuffer
;
1992 drawFb
= ctx
->DrawBuffer
;
1994 if (!readFb
|| !drawFb
) {
1995 /* This will normally never happen but someday we may want to
1996 * support MakeCurrent() with no drawables.
2001 /* check for complete framebuffers */
2002 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2003 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2004 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2005 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2009 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2010 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2014 if (mask
& ~legalMaskBits
) {
2015 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2019 /* depth/stencil must be blitted with nearest filtering */
2020 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2021 && filter
!= GL_NEAREST
) {
2022 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2023 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2027 /* get color read/draw renderbuffers */
2028 if (mask
& GL_COLOR_BUFFER_BIT
) {
2029 colorReadRb
= readFb
->_ColorReadBuffer
;
2030 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2033 colorReadRb
= colorDrawRb
= NULL
;
2036 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2037 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2038 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2041 _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2042 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2043 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2044 "glBlitFramebufferEXT(stencil buffer size mismatch");
2049 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2050 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2051 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2054 _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2055 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) {
2056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2057 "glBlitFramebufferEXT(depth buffer size mismatch");
2062 if (readFb
->Visual
.samples
> 0 &&
2063 drawFb
->Visual
.samples
> 0 &&
2064 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2066 "glBlitFramebufferEXT(mismatched samples");
2070 /* extra checks for multisample copies... */
2071 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2072 /* src and dest region sizes must be the same */
2073 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2074 srcY1
- srcY0
!= dstY1
- dstY0
) {
2075 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2076 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2080 /* color formats must match */
2083 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2085 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2090 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2091 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2097 _mesa_printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2099 srcX0
, srcY0
, srcX1
, srcY1
,
2100 dstX0
, dstY0
, dstX1
, dstY1
,
2103 const struct gl_renderbuffer_attachment
*att
;
2105 att
= find_attachment(readFb
, colorReadRb
);
2106 _mesa_printf(" Src FBO %u RB %u (%dx%d) ",
2107 readFb
->Name
, colorReadRb
->Name
,
2108 colorReadRb
->Width
, colorReadRb
->Height
);
2109 if (att
&& att
->Texture
) {
2110 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2112 att
->Texture
->Target
,
2118 att
= find_attachment(drawFb
, colorDrawRb
);
2119 _mesa_printf(" Dst FBO %u RB %u (%dx%d) ",
2120 drawFb
->Name
, colorDrawRb
->Name
,
2121 colorDrawRb
->Width
, colorDrawRb
->Height
);
2122 if (att
&& att
->Texture
) {
2123 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2125 att
->Texture
->Target
,
2133 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2134 ctx
->Driver
.BlitFramebuffer(ctx
,
2135 srcX0
, srcY0
, srcX1
, srcY1
,
2136 dstX0
, dstY0
, dstX1
, dstY1
,
2139 #endif /* FEATURE_EXT_framebuffer_blit */