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
,
716 "glBindRenderbufferEXT(target)");
720 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
723 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
724 if (newRb
== &DummyRenderbuffer
) {
725 /* ID was reserved, but no real renderbuffer object made yet */
728 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
729 /* All RB IDs must be Gen'd */
730 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
735 /* create new renderbuffer object */
736 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
738 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
741 ASSERT(newRb
->AllocStorage
);
742 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
743 newRb
->RefCount
= 1; /* referenced by hash table */
750 ASSERT(newRb
!= &DummyRenderbuffer
);
752 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
757 * If the given renderbuffer is anywhere attached to the framebuffer, detach
759 * This is used when a renderbuffer object is deleted.
760 * The spec calls for unbinding.
763 detach_renderbuffer(GLcontext
*ctx
,
764 struct gl_framebuffer
*fb
,
765 struct gl_renderbuffer
*rb
)
768 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
769 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
770 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
773 invalidate_framebuffer(fb
);
778 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
781 GET_CURRENT_CONTEXT(ctx
);
783 ASSERT_OUTSIDE_BEGIN_END(ctx
);
784 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
786 for (i
= 0; i
< n
; i
++) {
787 if (renderbuffers
[i
] > 0) {
788 struct gl_renderbuffer
*rb
;
789 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
791 /* check if deleting currently bound renderbuffer object */
792 if (rb
== ctx
->CurrentRenderbuffer
) {
794 ASSERT(rb
->RefCount
>= 2);
795 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
798 if (ctx
->DrawBuffer
->Name
) {
799 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
801 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
802 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
805 /* Remove from hash table immediately, to free the ID.
806 * But the object will not be freed until it's no longer
807 * referenced anywhere else.
809 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
811 if (rb
!= &DummyRenderbuffer
) {
812 /* no longer referenced by hash table */
813 _mesa_reference_renderbuffer(&rb
, NULL
);
822 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
824 GET_CURRENT_CONTEXT(ctx
);
828 ASSERT_OUTSIDE_BEGIN_END(ctx
);
831 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
838 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
840 for (i
= 0; i
< n
; i
++) {
841 GLuint name
= first
+ i
;
842 renderbuffers
[i
] = name
;
843 /* insert dummy placeholder into hash table */
844 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
845 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
846 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
852 * Given an internal format token for a render buffer, return the
853 * corresponding base format.
854 * This is very similar to _mesa_base_tex_format() but the set of valid
855 * internal formats is somewhat different.
857 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
858 * GL_DEPTH_STENCIL_EXT or zero if error.
861 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
863 switch (internalFormat
) {
882 case GL_STENCIL_INDEX
:
883 case GL_STENCIL_INDEX1_EXT
:
884 case GL_STENCIL_INDEX4_EXT
:
885 case GL_STENCIL_INDEX8_EXT
:
886 case GL_STENCIL_INDEX16_EXT
:
887 return GL_STENCIL_INDEX
;
888 case GL_DEPTH_COMPONENT
:
889 case GL_DEPTH_COMPONENT16
:
890 case GL_DEPTH_COMPONENT24
:
891 case GL_DEPTH_COMPONENT32
:
892 return GL_DEPTH_COMPONENT
;
893 case GL_DEPTH_STENCIL_EXT
:
894 case GL_DEPTH24_STENCIL8_EXT
:
895 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
896 return GL_DEPTH_STENCIL_EXT
;
899 /* XXX add floating point formats eventually */
906 /** sentinal value, see below */
907 #define NO_SAMPLES 1000
911 * Helper function used by _mesa_RenderbufferStorageEXT() and
912 * _mesa_RenderbufferStorageMultisample().
913 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
916 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
917 GLsizei width
, GLsizei height
, GLsizei samples
)
919 const char *func
= samples
== NO_SAMPLES
?
920 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
921 struct gl_renderbuffer
*rb
;
923 GET_CURRENT_CONTEXT(ctx
);
925 ASSERT_OUTSIDE_BEGIN_END(ctx
);
927 if (target
!= GL_RENDERBUFFER_EXT
) {
928 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
932 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
933 if (baseFormat
== 0) {
934 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
938 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
939 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
943 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
944 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
948 if (samples
== NO_SAMPLES
) {
949 /* NumSamples == 0 indicates non-multisampling */
952 else if (samples
> ctx
->Const
.MaxSamples
) {
953 /* note: driver may choose to use more samples than what's requested */
954 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
958 rb
= ctx
->CurrentRenderbuffer
;
960 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
964 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
966 if (rb
->InternalFormat
== internalFormat
&&
967 rb
->Width
== (GLuint
) width
&&
968 rb
->Height
== (GLuint
) height
) {
969 /* no change in allocation needed */
973 /* These MUST get set by the AllocStorage func */
974 rb
->Format
= MESA_FORMAT_NONE
;
975 rb
->NumSamples
= samples
;
977 /* Now allocate the storage */
978 ASSERT(rb
->AllocStorage
);
979 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
980 /* No error - check/set fields now */
981 assert(rb
->Format
!= MESA_FORMAT_NONE
);
982 assert(rb
->Width
== (GLuint
) width
);
983 assert(rb
->Height
== (GLuint
) height
);
984 rb
->InternalFormat
= internalFormat
;
985 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
986 assert(rb
->_BaseFormat
!= 0);
989 /* Probably ran out of memory - clear the fields */
992 rb
->Format
= MESA_FORMAT_NONE
;
993 rb
->InternalFormat
= GL_NONE
;
994 rb
->_BaseFormat
= GL_NONE
;
999 test_framebuffer_completeness(ctx, fb);
1001 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1008 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1009 * _mesa_GetFramebufferAttachmentParameterivEXT()
1010 * We have to be careful to respect the base format. For example, if a
1011 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1012 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1013 * we need to return zero.
1016 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1019 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1020 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1021 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1022 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1023 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1024 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1025 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
)
1026 return _mesa_get_format_bits(format
, pname
);
1029 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1030 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1031 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_ALPHA
)
1032 return _mesa_get_format_bits(format
, pname
);
1035 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1036 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1037 if (baseFormat
== GL_DEPTH_COMPONENT
|| baseFormat
== GL_DEPTH_STENCIL
)
1038 return _mesa_get_format_bits(format
, pname
);
1041 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1042 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1043 if (baseFormat
== GL_STENCIL_INDEX
|| baseFormat
== GL_DEPTH_STENCIL
)
1044 return _mesa_get_format_bits(format
, pname
);
1055 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1056 GLsizei width
, GLsizei height
)
1058 /* GL_ARB_fbo says calling this function is equivalent to calling
1059 * glRenderbufferStorageMultisample() with samples=0. We pass in
1060 * a token value here just for error reporting purposes.
1062 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1067 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1068 GLenum internalFormat
,
1069 GLsizei width
, GLsizei height
)
1071 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1077 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1079 struct gl_renderbuffer
*rb
;
1080 GET_CURRENT_CONTEXT(ctx
);
1082 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1084 if (target
!= GL_RENDERBUFFER_EXT
) {
1085 _mesa_error(ctx
, GL_INVALID_ENUM
,
1086 "glGetRenderbufferParameterivEXT(target)");
1090 rb
= ctx
->CurrentRenderbuffer
;
1092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1093 "glGetRenderbufferParameterivEXT");
1097 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1100 case GL_RENDERBUFFER_WIDTH_EXT
:
1101 *params
= rb
->Width
;
1103 case GL_RENDERBUFFER_HEIGHT_EXT
:
1104 *params
= rb
->Height
;
1106 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1107 *params
= rb
->InternalFormat
;
1109 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1110 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1111 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1112 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1113 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1114 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1115 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1117 case GL_RENDERBUFFER_SAMPLES
:
1118 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1119 *params
= rb
->NumSamples
;
1124 _mesa_error(ctx
, GL_INVALID_ENUM
,
1125 "glGetRenderbufferParameterivEXT(target)");
1131 GLboolean GLAPIENTRY
1132 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1134 GET_CURRENT_CONTEXT(ctx
);
1135 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1137 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1138 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1146 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1149 ASSERT(ctx
->Driver
.RenderTexture
);
1150 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1151 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1152 struct gl_texture_object
*texObj
= att
->Texture
;
1154 && texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1155 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1162 * Examine all the framebuffer's attachments to see if any are textures.
1163 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1164 * notify the device driver that the texture image may have changed.
1167 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1169 if (ctx
->Driver
.FinishRenderTexture
) {
1171 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1172 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1173 if (att
->Texture
&& att
->Renderbuffer
) {
1174 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1182 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1184 struct gl_framebuffer
*newFb
, *newFbread
;
1185 GLboolean bindReadBuf
, bindDrawBuf
;
1186 GET_CURRENT_CONTEXT(ctx
);
1189 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1190 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1191 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1195 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1197 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1199 "glBindFramebufferEXT(unsupported)");
1204 #if FEATURE_EXT_framebuffer_blit
1205 case GL_DRAW_FRAMEBUFFER_EXT
:
1206 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1207 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1210 bindDrawBuf
= GL_TRUE
;
1211 bindReadBuf
= GL_FALSE
;
1213 case GL_READ_FRAMEBUFFER_EXT
:
1214 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1215 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1218 bindDrawBuf
= GL_FALSE
;
1219 bindReadBuf
= GL_TRUE
;
1222 case GL_FRAMEBUFFER_EXT
:
1223 bindDrawBuf
= GL_TRUE
;
1224 bindReadBuf
= GL_TRUE
;
1227 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1231 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1234 /* Binding a user-created framebuffer object */
1235 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1236 if (newFb
== &DummyFramebuffer
) {
1237 /* ID was reserved, but no real framebuffer object made yet */
1240 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1241 /* All FBO IDs must be Gen'd */
1242 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1247 /* create new framebuffer object */
1248 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1250 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1253 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1258 /* Binding the window system framebuffer (which was originally set
1259 * with MakeCurrent).
1261 newFb
= ctx
->WinSysDrawBuffer
;
1262 newFbread
= ctx
->WinSysReadBuffer
;
1266 ASSERT(newFb
!= &DummyFramebuffer
);
1269 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1273 if (ctx
->ReadBuffer
== newFbread
)
1274 bindReadBuf
= GL_FALSE
; /* no change */
1276 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1280 /* check if old FB had any texture attachments */
1281 if (ctx
->DrawBuffer
->Name
!= 0) {
1282 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1285 if (ctx
->DrawBuffer
== newFb
)
1286 bindDrawBuf
= GL_FALSE
; /* no change */
1288 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1290 if (newFb
->Name
!= 0) {
1291 /* check if newly bound framebuffer has any texture attachments */
1292 check_begin_texture_render(ctx
, newFb
);
1296 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1297 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1303 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1306 GET_CURRENT_CONTEXT(ctx
);
1308 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1309 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1311 for (i
= 0; i
< n
; i
++) {
1312 if (framebuffers
[i
] > 0) {
1313 struct gl_framebuffer
*fb
;
1314 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1316 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1318 /* check if deleting currently bound framebuffer object */
1319 if (fb
== ctx
->DrawBuffer
) {
1321 ASSERT(fb
->RefCount
>= 2);
1322 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1324 if (fb
== ctx
->ReadBuffer
) {
1326 ASSERT(fb
->RefCount
>= 2);
1327 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1330 /* remove from hash table immediately, to free the ID */
1331 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1333 if (fb
!= &DummyFramebuffer
) {
1334 /* But the object will not be freed until it's no longer
1335 * bound in any context.
1337 _mesa_reference_framebuffer(&fb
, NULL
);
1346 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1348 GET_CURRENT_CONTEXT(ctx
);
1352 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1355 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1362 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1364 for (i
= 0; i
< n
; i
++) {
1365 GLuint name
= first
+ i
;
1366 framebuffers
[i
] = name
;
1367 /* insert dummy placeholder into hash table */
1368 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1369 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1370 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1377 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1379 struct gl_framebuffer
*buffer
;
1380 GET_CURRENT_CONTEXT(ctx
);
1382 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1385 #if FEATURE_EXT_framebuffer_blit
1386 case GL_DRAW_FRAMEBUFFER_EXT
:
1387 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1388 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1391 buffer
= ctx
->DrawBuffer
;
1393 case GL_READ_FRAMEBUFFER_EXT
:
1394 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1395 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1398 buffer
= ctx
->ReadBuffer
;
1401 case GL_FRAMEBUFFER_EXT
:
1402 buffer
= ctx
->DrawBuffer
;
1405 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1406 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1409 if (buffer
->Name
== 0) {
1410 /* The window system / default framebuffer is always complete */
1411 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1414 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1416 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1417 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1420 return buffer
->_Status
;
1426 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1429 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1430 GLenum attachment
, GLenum textarget
, GLuint texture
,
1431 GLint level
, GLint zoffset
)
1433 struct gl_renderbuffer_attachment
*att
;
1434 struct gl_texture_object
*texObj
= NULL
;
1435 struct gl_framebuffer
*fb
;
1436 GLboolean error
= GL_FALSE
;
1438 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1441 case GL_READ_FRAMEBUFFER_EXT
:
1442 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1443 fb
= ctx
->ReadBuffer
;
1445 case GL_DRAW_FRAMEBUFFER_EXT
:
1446 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1448 case GL_FRAMEBUFFER_EXT
:
1449 fb
= ctx
->DrawBuffer
;
1456 _mesa_error(ctx
, GL_INVALID_ENUM
,
1457 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1463 /* check framebuffer binding */
1464 if (fb
->Name
== 0) {
1465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1466 "glFramebufferTexture%sEXT", caller
);
1471 /* The textarget, level, and zoffset parameters are only validated if
1472 * texture is non-zero.
1475 GLboolean err
= GL_TRUE
;
1477 texObj
= _mesa_lookup_texture(ctx
, texture
);
1478 if (texObj
!= NULL
) {
1479 if (textarget
== 0) {
1480 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1481 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1482 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1485 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1486 ? !IS_CUBE_FACE(textarget
)
1487 : (texObj
->Target
!= textarget
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1493 "glFramebufferTexture%sEXT(texture target mismatch)",
1498 if (texObj
->Target
== GL_TEXTURE_3D
) {
1499 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1500 if (zoffset
< 0 || zoffset
>= maxSize
) {
1501 _mesa_error(ctx
, GL_INVALID_VALUE
,
1502 "glFramebufferTexture%sEXT(zoffset)", caller
);
1506 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1507 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1508 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glFramebufferTexture%sEXT(layer)", caller
);
1516 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1517 _mesa_error(ctx
, GL_INVALID_VALUE
,
1518 "glFramebufferTexture%sEXT(level)", caller
);
1523 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1525 _mesa_error(ctx
, GL_INVALID_ENUM
,
1526 "glFramebufferTexture%sEXT(attachment)", caller
);
1530 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1532 _glthread_LOCK_MUTEX(fb
->Mutex
);
1534 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1536 /* Set the render-to-texture flag. We'll check this flag in
1537 * glTexImage() and friends to determine if we need to revalidate
1538 * any FBOs that might be rendering into this texture.
1539 * This flag never gets cleared since it's non-trivial to determine
1540 * when all FBOs might be done rendering to this texture. That's OK
1541 * though since it's uncommon to render to a texture then repeatedly
1542 * call glTexImage() to change images in the texture.
1544 texObj
->_RenderToTexture
= GL_TRUE
;
1547 _mesa_remove_attachment(ctx
, att
);
1550 invalidate_framebuffer(fb
);
1552 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1558 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1559 GLenum textarget
, GLuint texture
, GLint level
)
1561 GET_CURRENT_CONTEXT(ctx
);
1563 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1564 _mesa_error(ctx
, GL_INVALID_ENUM
,
1565 "glFramebufferTexture1DEXT(textarget)");
1569 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1575 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1576 GLenum textarget
, GLuint texture
, GLint level
)
1578 GET_CURRENT_CONTEXT(ctx
);
1580 if ((texture
!= 0) &&
1581 (textarget
!= GL_TEXTURE_2D
) &&
1582 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1583 (!IS_CUBE_FACE(textarget
))) {
1584 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1585 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1589 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1595 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1596 GLenum textarget
, GLuint texture
,
1597 GLint level
, GLint zoffset
)
1599 GET_CURRENT_CONTEXT(ctx
);
1601 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1602 _mesa_error(ctx
, GL_INVALID_ENUM
,
1603 "glFramebufferTexture3DEXT(textarget)");
1607 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1613 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1614 GLuint texture
, GLint level
, GLint layer
)
1616 GET_CURRENT_CONTEXT(ctx
);
1618 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1624 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1625 GLenum renderbufferTarget
,
1626 GLuint renderbuffer
)
1628 struct gl_renderbuffer_attachment
*att
;
1629 struct gl_framebuffer
*fb
;
1630 struct gl_renderbuffer
*rb
;
1631 GET_CURRENT_CONTEXT(ctx
);
1633 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1636 #if FEATURE_EXT_framebuffer_blit
1637 case GL_DRAW_FRAMEBUFFER_EXT
:
1638 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1639 _mesa_error(ctx
, GL_INVALID_ENUM
,
1640 "glFramebufferRenderbufferEXT(target)");
1643 fb
= ctx
->DrawBuffer
;
1645 case GL_READ_FRAMEBUFFER_EXT
:
1646 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1647 _mesa_error(ctx
, GL_INVALID_ENUM
,
1648 "glFramebufferRenderbufferEXT(target)");
1651 fb
= ctx
->ReadBuffer
;
1654 case GL_FRAMEBUFFER_EXT
:
1655 fb
= ctx
->DrawBuffer
;
1658 _mesa_error(ctx
, GL_INVALID_ENUM
,
1659 "glFramebufferRenderbufferEXT(target)");
1663 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1664 _mesa_error(ctx
, GL_INVALID_ENUM
,
1665 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1669 if (fb
->Name
== 0) {
1670 /* Can't attach new renderbuffers to a window system framebuffer */
1671 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1675 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1677 _mesa_error(ctx
, GL_INVALID_ENUM
,
1678 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1679 _mesa_lookup_enum_by_nr(attachment
));
1684 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1686 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1687 "glFramebufferRenderbufferEXT(non-existant"
1688 " renderbuffer %u)", renderbuffer
);
1693 /* remove renderbuffer attachment */
1697 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1698 /* make sure the renderbuffer is a depth/stencil format */
1699 const GLenum baseFormat
=
1700 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
1701 if (baseFormat
!= GL_DEPTH_STENCIL
) {
1702 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1703 "glFramebufferRenderbufferEXT(renderbuffer"
1704 " is not DEPTH_STENCIL format)");
1710 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1712 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1713 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1715 /* Some subsequent GL commands may depend on the framebuffer's visual
1716 * after the binding is updated. Update visual info now.
1718 _mesa_update_framebuffer_visual(fb
);
1723 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1724 GLenum pname
, GLint
*params
)
1726 const struct gl_renderbuffer_attachment
*att
;
1727 struct gl_framebuffer
*buffer
;
1728 GET_CURRENT_CONTEXT(ctx
);
1730 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1733 #if FEATURE_EXT_framebuffer_blit
1734 case GL_DRAW_FRAMEBUFFER_EXT
:
1735 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1736 _mesa_error(ctx
, GL_INVALID_ENUM
,
1737 "glGetFramebufferAttachmentParameterivEXT(target)");
1740 buffer
= ctx
->DrawBuffer
;
1742 case GL_READ_FRAMEBUFFER_EXT
:
1743 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1744 _mesa_error(ctx
, GL_INVALID_ENUM
,
1745 "glGetFramebufferAttachmentParameterivEXT(target)");
1748 buffer
= ctx
->ReadBuffer
;
1751 case GL_FRAMEBUFFER_EXT
:
1752 buffer
= ctx
->DrawBuffer
;
1755 _mesa_error(ctx
, GL_INVALID_ENUM
,
1756 "glGetFramebufferAttachmentParameterivEXT(target)");
1760 if (buffer
->Name
== 0) {
1761 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1762 "glGetFramebufferAttachmentParameterivEXT");
1766 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1768 _mesa_error(ctx
, GL_INVALID_ENUM
,
1769 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1773 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1774 /* the depth and stencil attachments must point to the same buffer */
1775 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1776 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1777 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1778 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1781 " attachments differ)");
1786 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1789 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1790 *params
= att
->Type
;
1792 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1793 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1794 *params
= att
->Renderbuffer
->Name
;
1796 else if (att
->Type
== GL_TEXTURE
) {
1797 *params
= att
->Texture
->Name
;
1800 _mesa_error(ctx
, GL_INVALID_ENUM
,
1801 "glGetFramebufferAttachmentParameterivEXT(pname)");
1804 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1805 if (att
->Type
== GL_TEXTURE
) {
1806 *params
= att
->TextureLevel
;
1809 _mesa_error(ctx
, GL_INVALID_ENUM
,
1810 "glGetFramebufferAttachmentParameterivEXT(pname)");
1813 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1814 if (att
->Type
== GL_TEXTURE
) {
1815 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1816 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1823 _mesa_error(ctx
, GL_INVALID_ENUM
,
1824 "glGetFramebufferAttachmentParameterivEXT(pname)");
1827 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1828 if (att
->Type
== GL_TEXTURE
) {
1829 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1830 *params
= att
->Zoffset
;
1837 _mesa_error(ctx
, GL_INVALID_ENUM
,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1841 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1842 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1843 _mesa_error(ctx
, GL_INVALID_ENUM
,
1844 "glGetFramebufferAttachmentParameterivEXT(pname)");
1847 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
1850 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1851 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1852 _mesa_error(ctx
, GL_INVALID_ENUM
,
1853 "glGetFramebufferAttachmentParameterivEXT(pname)");
1857 gl_format format
= att
->Renderbuffer
->Format
;
1858 if (format
== MESA_FORMAT_CI8
|| format
== MESA_FORMAT_S8
) {
1863 *params
= _mesa_get_format_datatype(format
);
1867 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1868 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1869 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1870 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1871 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1872 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1873 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1874 _mesa_error(ctx
, GL_INVALID_ENUM
,
1875 "glGetFramebufferAttachmentParameterivEXT(pname)");
1877 else if (att
->Texture
) {
1878 const struct gl_texture_image
*texImage
=
1879 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
1882 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
1883 texImage
->TexFormat
);
1889 else if (att
->Renderbuffer
) {
1890 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
1891 att
->Renderbuffer
->Format
);
1898 _mesa_error(ctx
, GL_INVALID_ENUM
,
1899 "glGetFramebufferAttachmentParameterivEXT(pname)");
1906 _mesa_GenerateMipmapEXT(GLenum target
)
1908 struct gl_texture_unit
*texUnit
;
1909 struct gl_texture_object
*texObj
;
1910 GET_CURRENT_CONTEXT(ctx
);
1912 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1913 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1919 case GL_TEXTURE_CUBE_MAP
:
1920 /* OK, legal value */
1923 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1927 texUnit
= _mesa_get_current_tex_unit(ctx
);
1928 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1930 _mesa_lock_texture(ctx
, texObj
);
1931 if (target
== GL_TEXTURE_CUBE_MAP
) {
1933 for (face
= 0; face
< 6; face
++)
1934 ctx
->Driver
.GenerateMipmap(ctx
,
1935 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1939 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1941 _mesa_unlock_texture(ctx
, texObj
);
1945 #if FEATURE_EXT_framebuffer_blit
1947 static const struct gl_renderbuffer_attachment
*
1948 find_attachment(const struct gl_framebuffer
*fb
, const struct gl_renderbuffer
*rb
)
1951 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
1952 if (fb
->Attachment
[i
].Renderbuffer
== rb
)
1953 return &fb
->Attachment
[i
];
1961 * Blit rectangular region, optionally from one framebuffer to another.
1963 * Note, if the src buffer is multisampled and the dest is not, this is
1964 * when the samples must be resolved to a single color.
1967 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1968 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1969 GLbitfield mask
, GLenum filter
)
1971 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1972 GL_DEPTH_BUFFER_BIT
|
1973 GL_STENCIL_BUFFER_BIT
);
1974 const struct gl_framebuffer
*readFb
, *drawFb
;
1975 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1976 GET_CURRENT_CONTEXT(ctx
);
1978 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1979 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1981 if (ctx
->NewState
) {
1982 _mesa_update_state(ctx
);
1985 readFb
= ctx
->ReadBuffer
;
1986 drawFb
= ctx
->DrawBuffer
;
1988 if (!readFb
|| !drawFb
) {
1989 /* This will normally never happen but someday we may want to
1990 * support MakeCurrent() with no drawables.
1995 /* check for complete framebuffers */
1996 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
1997 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1998 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1999 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2003 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2004 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2008 if (mask
& ~legalMaskBits
) {
2009 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2013 /* depth/stencil must be blitted with nearest filtering */
2014 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2015 && filter
!= GL_NEAREST
) {
2016 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2017 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2021 /* get color read/draw renderbuffers */
2022 if (mask
& GL_COLOR_BUFFER_BIT
) {
2023 colorReadRb
= readFb
->_ColorReadBuffer
;
2024 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2027 colorReadRb
= colorDrawRb
= NULL
;
2030 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2031 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2032 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2035 _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2036 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2037 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2038 "glBlitFramebufferEXT(stencil buffer size mismatch");
2043 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2044 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2045 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2048 _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2049 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) {
2050 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2051 "glBlitFramebufferEXT(depth buffer size mismatch");
2056 if (readFb
->Visual
.samples
> 0 &&
2057 drawFb
->Visual
.samples
> 0 &&
2058 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2059 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2060 "glBlitFramebufferEXT(mismatched samples");
2064 /* extra checks for multisample copies... */
2065 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2066 /* src and dest region sizes must be the same */
2067 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2068 srcY1
- srcY0
!= dstY1
- dstY0
) {
2069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2070 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2074 /* color formats must match */
2077 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2078 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2079 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2084 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2085 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2091 _mesa_printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2093 srcX0
, srcY0
, srcX1
, srcY1
,
2094 dstX0
, dstY0
, dstX1
, dstY1
,
2097 const struct gl_renderbuffer_attachment
*att
;
2099 att
= find_attachment(readFb
, colorReadRb
);
2100 _mesa_printf(" Src FBO %u RB %u (%dx%d) ",
2101 readFb
->Name
, colorReadRb
->Name
,
2102 colorReadRb
->Width
, colorReadRb
->Height
);
2103 if (att
&& att
->Texture
) {
2104 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2106 att
->Texture
->Target
,
2112 att
= find_attachment(drawFb
, colorDrawRb
);
2113 _mesa_printf(" Dst FBO %u RB %u (%dx%d) ",
2114 drawFb
->Name
, colorDrawRb
->Name
,
2115 colorDrawRb
->Width
, colorDrawRb
->Height
);
2116 if (att
&& att
->Texture
) {
2117 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2119 att
->Texture
->Target
,
2127 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2128 ctx
->Driver
.BlitFramebuffer(ctx
,
2129 srcX0
, srcY0
, srcX1
, srcY1
,
2130 dstX0
, dstY0
, dstX1
, dstY1
,
2133 #endif /* FEATURE_EXT_framebuffer_blit */