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
39 #include "framebuffer.h"
43 #include "renderbuffer.h"
50 /** Set this to 1 to help debug FBO incompleteness problems */
57 * None of the GL_EXT_framebuffer_object functions are compiled into
64 * When glGenRender/FramebuffersEXT() is called we insert pointers to
65 * these placeholder objects into the hash table.
66 * Later, when the object ID is first bound, we replace the placeholder
67 * with the real frame/renderbuffer.
69 static struct gl_framebuffer DummyFramebuffer
;
70 static struct gl_renderbuffer DummyRenderbuffer
;
73 #define IS_CUBE_FACE(TARGET) \
74 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
75 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
79 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
85 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
92 _mesa_init_fbobjects(GLcontext
*ctx
)
94 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
95 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
100 * Helper routine for getting a gl_renderbuffer.
102 struct gl_renderbuffer
*
103 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
105 struct gl_renderbuffer
*rb
;
110 rb
= (struct gl_renderbuffer
*)
111 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
117 * Helper routine for getting a gl_framebuffer.
119 struct gl_framebuffer
*
120 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
122 struct gl_framebuffer
*fb
;
127 fb
= (struct gl_framebuffer
*)
128 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
134 * Mark the given framebuffer as invalid. This will force the
135 * test for framebuffer completeness to be done before the framebuffer
139 invalidate_framebuffer(struct gl_framebuffer
*fb
)
141 fb
->_Status
= 0; /* "indeterminate" */
146 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
147 * gl_renderbuffer_attachment object.
148 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
149 * the depth buffer attachment point.
151 struct gl_renderbuffer_attachment
*
152 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
157 switch (attachment
) {
158 case GL_COLOR_ATTACHMENT0_EXT
:
159 case GL_COLOR_ATTACHMENT1_EXT
:
160 case GL_COLOR_ATTACHMENT2_EXT
:
161 case GL_COLOR_ATTACHMENT3_EXT
:
162 case GL_COLOR_ATTACHMENT4_EXT
:
163 case GL_COLOR_ATTACHMENT5_EXT
:
164 case GL_COLOR_ATTACHMENT6_EXT
:
165 case GL_COLOR_ATTACHMENT7_EXT
:
166 case GL_COLOR_ATTACHMENT8_EXT
:
167 case GL_COLOR_ATTACHMENT9_EXT
:
168 case GL_COLOR_ATTACHMENT10_EXT
:
169 case GL_COLOR_ATTACHMENT11_EXT
:
170 case GL_COLOR_ATTACHMENT12_EXT
:
171 case GL_COLOR_ATTACHMENT13_EXT
:
172 case GL_COLOR_ATTACHMENT14_EXT
:
173 case GL_COLOR_ATTACHMENT15_EXT
:
174 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
175 if (i
>= ctx
->Const
.MaxColorAttachments
) {
178 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
179 case GL_DEPTH_STENCIL_ATTACHMENT
:
181 case GL_DEPTH_ATTACHMENT_EXT
:
182 return &fb
->Attachment
[BUFFER_DEPTH
];
183 case GL_STENCIL_ATTACHMENT_EXT
:
184 return &fb
->Attachment
[BUFFER_STENCIL
];
192 * Remove any texture or renderbuffer attached to the given attachment
193 * point. Update reference counts, etc.
196 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
198 if (att
->Type
== GL_TEXTURE
) {
199 ASSERT(att
->Texture
);
200 if (ctx
->Driver
.FinishRenderTexture
) {
201 /* tell driver that we're done rendering to this texture. */
202 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
204 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
205 ASSERT(!att
->Texture
);
207 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
208 ASSERT(!att
->Texture
);
209 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
210 ASSERT(!att
->Renderbuffer
);
213 att
->Complete
= GL_TRUE
;
218 * Bind a texture object to an attachment point.
219 * The previous binding, if any, will be removed first.
222 _mesa_set_texture_attachment(GLcontext
*ctx
,
223 struct gl_framebuffer
*fb
,
224 struct gl_renderbuffer_attachment
*att
,
225 struct gl_texture_object
*texObj
,
226 GLenum texTarget
, GLuint level
, GLuint zoffset
)
228 if (att
->Texture
== texObj
) {
229 /* re-attaching same texture */
230 ASSERT(att
->Type
== GL_TEXTURE
);
234 _mesa_remove_attachment(ctx
, att
);
235 att
->Type
= GL_TEXTURE
;
236 assert(!att
->Texture
);
237 _mesa_reference_texobj(&att
->Texture
, texObj
);
240 /* always update these fields */
241 att
->TextureLevel
= level
;
242 if (IS_CUBE_FACE(texTarget
)) {
243 att
->CubeMapFace
= texTarget
- GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
246 att
->CubeMapFace
= 0;
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);
655 if (numImages
== 0) {
656 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
657 fbo_incomplete("no attachments", -1);
661 /* Provisionally set status = COMPLETE ... */
662 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
664 /* ... but the driver may say the FB is incomplete.
665 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
668 if (ctx
->Driver
.ValidateFramebuffer
) {
669 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
670 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
671 fbo_incomplete("driver marked FBO as incomplete", -1);
675 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
677 * Note that if ARB_framebuffer_object is supported and the attached
678 * renderbuffers/textures are different sizes, the framebuffer
679 * width/height will be set to the smallest width/height.
681 fb
->Width
= minWidth
;
682 fb
->Height
= minHeight
;
684 /* finally, update the visual info for the framebuffer */
685 _mesa_update_framebuffer_visual(fb
);
691 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
693 GET_CURRENT_CONTEXT(ctx
);
694 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
696 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
697 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
705 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
707 struct gl_renderbuffer
*newRb
;
708 GET_CURRENT_CONTEXT(ctx
);
710 ASSERT_OUTSIDE_BEGIN_END(ctx
);
712 if (target
!= GL_RENDERBUFFER_EXT
) {
713 _mesa_error(ctx
, GL_INVALID_ENUM
,
714 "glBindRenderbufferEXT(target)");
718 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
719 /* The above doesn't fully flush the drivers in the way that a
720 * glFlush does, but that is required here:
722 if (ctx
->Driver
.Flush
)
723 ctx
->Driver
.Flush(ctx
);
727 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
728 if (newRb
== &DummyRenderbuffer
) {
729 /* ID was reserved, but no real renderbuffer object made yet */
732 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
733 /* All RB IDs must be Gen'd */
734 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
739 /* create new renderbuffer object */
740 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
742 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
745 ASSERT(newRb
->AllocStorage
);
746 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
747 newRb
->RefCount
= 1; /* referenced by hash table */
754 ASSERT(newRb
!= &DummyRenderbuffer
);
756 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
761 * If the given renderbuffer is anywhere attached to the framebuffer, detach
763 * This is used when a renderbuffer object is deleted.
764 * The spec calls for unbinding.
767 detach_renderbuffer(GLcontext
*ctx
,
768 struct gl_framebuffer
*fb
,
769 struct gl_renderbuffer
*rb
)
772 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
773 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
774 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
777 invalidate_framebuffer(fb
);
782 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
785 GET_CURRENT_CONTEXT(ctx
);
787 ASSERT_OUTSIDE_BEGIN_END(ctx
);
788 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
790 for (i
= 0; i
< n
; i
++) {
791 if (renderbuffers
[i
] > 0) {
792 struct gl_renderbuffer
*rb
;
793 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
795 /* check if deleting currently bound renderbuffer object */
796 if (rb
== ctx
->CurrentRenderbuffer
) {
798 ASSERT(rb
->RefCount
>= 2);
799 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
802 if (ctx
->DrawBuffer
->Name
) {
803 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
805 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
806 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
809 /* Remove from hash table immediately, to free the ID.
810 * But the object will not be freed until it's no longer
811 * referenced anywhere else.
813 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
815 if (rb
!= &DummyRenderbuffer
) {
816 /* no longer referenced by hash table */
817 _mesa_reference_renderbuffer(&rb
, NULL
);
826 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
828 GET_CURRENT_CONTEXT(ctx
);
832 ASSERT_OUTSIDE_BEGIN_END(ctx
);
835 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
842 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
844 for (i
= 0; i
< n
; i
++) {
845 GLuint name
= first
+ i
;
846 renderbuffers
[i
] = name
;
847 /* insert dummy placeholder into hash table */
848 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
849 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
850 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
856 * Given an internal format token for a render buffer, return the
857 * corresponding base format.
858 * This is very similar to _mesa_base_tex_format() but the set of valid
859 * internal formats is somewhat different.
861 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
862 * GL_DEPTH_STENCIL_EXT or zero if error.
865 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
867 switch (internalFormat
) {
886 case GL_STENCIL_INDEX
:
887 case GL_STENCIL_INDEX1_EXT
:
888 case GL_STENCIL_INDEX4_EXT
:
889 case GL_STENCIL_INDEX8_EXT
:
890 case GL_STENCIL_INDEX16_EXT
:
891 return GL_STENCIL_INDEX
;
892 case GL_DEPTH_COMPONENT
:
893 case GL_DEPTH_COMPONENT16
:
894 case GL_DEPTH_COMPONENT24
:
895 case GL_DEPTH_COMPONENT32
:
896 return GL_DEPTH_COMPONENT
;
897 case GL_DEPTH_STENCIL_EXT
:
898 case GL_DEPTH24_STENCIL8_EXT
:
899 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
900 return GL_DEPTH_STENCIL_EXT
;
903 /* XXX add floating point formats eventually */
910 /** sentinal value, see below */
911 #define NO_SAMPLES 1000
915 * Helper function used by _mesa_RenderbufferStorageEXT() and
916 * _mesa_RenderbufferStorageMultisample().
917 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
920 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
921 GLsizei width
, GLsizei height
, GLsizei samples
)
923 const char *func
= samples
== NO_SAMPLES
?
924 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
925 struct gl_renderbuffer
*rb
;
927 GET_CURRENT_CONTEXT(ctx
);
929 ASSERT_OUTSIDE_BEGIN_END(ctx
);
931 if (target
!= GL_RENDERBUFFER_EXT
) {
932 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
936 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
937 if (baseFormat
== 0) {
938 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
942 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
943 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
947 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
948 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
952 if (samples
== NO_SAMPLES
) {
953 /* NumSamples == 0 indicates non-multisampling */
956 else if (samples
> ctx
->Const
.MaxSamples
) {
957 /* note: driver may choose to use more samples than what's requested */
958 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
962 rb
= ctx
->CurrentRenderbuffer
;
964 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
968 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
970 if (rb
->InternalFormat
== internalFormat
&&
971 rb
->Width
== (GLuint
) width
&&
972 rb
->Height
== (GLuint
) height
) {
973 /* no change in allocation needed */
977 /* These MUST get set by the AllocStorage func */
978 rb
->Format
= MESA_FORMAT_NONE
;
979 rb
->NumSamples
= samples
;
981 /* Now allocate the storage */
982 ASSERT(rb
->AllocStorage
);
983 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
984 /* No error - check/set fields now */
985 assert(rb
->Format
!= MESA_FORMAT_NONE
);
986 assert(rb
->Width
== (GLuint
) width
);
987 assert(rb
->Height
== (GLuint
) height
);
988 rb
->InternalFormat
= internalFormat
;
989 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
990 assert(rb
->_BaseFormat
!= 0);
993 /* Probably ran out of memory - clear the fields */
996 rb
->Format
= MESA_FORMAT_NONE
;
997 rb
->InternalFormat
= GL_NONE
;
998 rb
->_BaseFormat
= GL_NONE
;
1003 test_framebuffer_completeness(ctx, fb);
1005 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1012 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1013 * _mesa_GetFramebufferAttachmentParameterivEXT()
1014 * We have to be careful to respect the base format. For example, if a
1015 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1016 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1017 * we need to return zero.
1020 get_component_bits(GLenum pname
, GLenum baseFormat
, gl_format format
)
1023 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1024 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1025 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1026 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1027 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1028 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1029 if (baseFormat
== GL_RGB
|| baseFormat
== GL_RGBA
)
1030 return _mesa_get_format_bits(format
, pname
);
1033 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1034 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1035 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_ALPHA
)
1036 return _mesa_get_format_bits(format
, pname
);
1039 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1040 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1041 if (baseFormat
== GL_DEPTH_COMPONENT
|| baseFormat
== GL_DEPTH_STENCIL
)
1042 return _mesa_get_format_bits(format
, pname
);
1045 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1046 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1047 if (baseFormat
== GL_STENCIL_INDEX
|| baseFormat
== GL_DEPTH_STENCIL
)
1048 return _mesa_get_format_bits(format
, pname
);
1059 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
1060 GLsizei width
, GLsizei height
)
1062 /* GL_ARB_fbo says calling this function is equivalent to calling
1063 * glRenderbufferStorageMultisample() with samples=0. We pass in
1064 * a token value here just for error reporting purposes.
1066 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
1071 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
1072 GLenum internalFormat
,
1073 GLsizei width
, GLsizei height
)
1075 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
1081 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1083 struct gl_renderbuffer
*rb
;
1084 GET_CURRENT_CONTEXT(ctx
);
1086 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1088 if (target
!= GL_RENDERBUFFER_EXT
) {
1089 _mesa_error(ctx
, GL_INVALID_ENUM
,
1090 "glGetRenderbufferParameterivEXT(target)");
1094 rb
= ctx
->CurrentRenderbuffer
;
1096 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1097 "glGetRenderbufferParameterivEXT");
1101 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1104 case GL_RENDERBUFFER_WIDTH_EXT
:
1105 *params
= rb
->Width
;
1107 case GL_RENDERBUFFER_HEIGHT_EXT
:
1108 *params
= rb
->Height
;
1110 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1111 *params
= rb
->InternalFormat
;
1113 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1114 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1115 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1116 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1117 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1118 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1119 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
1121 case GL_RENDERBUFFER_SAMPLES
:
1122 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1123 *params
= rb
->NumSamples
;
1128 _mesa_error(ctx
, GL_INVALID_ENUM
,
1129 "glGetRenderbufferParameterivEXT(target)");
1135 GLboolean GLAPIENTRY
1136 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1138 GET_CURRENT_CONTEXT(ctx
);
1139 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1141 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1142 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1150 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1153 ASSERT(ctx
->Driver
.RenderTexture
);
1154 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1155 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1156 struct gl_texture_object
*texObj
= att
->Texture
;
1158 && att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1159 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1166 * Examine all the framebuffer's attachments to see if any are textures.
1167 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1168 * notify the device driver that the texture image may have changed.
1171 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1173 if (ctx
->Driver
.FinishRenderTexture
) {
1175 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1176 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1177 if (att
->Texture
&& att
->Renderbuffer
) {
1178 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1186 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1188 struct gl_framebuffer
*newFb
, *newFbread
;
1189 GLboolean bindReadBuf
, bindDrawBuf
;
1190 GET_CURRENT_CONTEXT(ctx
);
1193 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1194 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1195 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1199 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1201 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1202 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1203 "glBindFramebufferEXT(unsupported)");
1208 #if FEATURE_EXT_framebuffer_blit
1209 case GL_DRAW_FRAMEBUFFER_EXT
:
1210 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1211 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1214 bindDrawBuf
= GL_TRUE
;
1215 bindReadBuf
= GL_FALSE
;
1217 case GL_READ_FRAMEBUFFER_EXT
:
1218 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1219 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1222 bindDrawBuf
= GL_FALSE
;
1223 bindReadBuf
= GL_TRUE
;
1226 case GL_FRAMEBUFFER_EXT
:
1227 bindDrawBuf
= GL_TRUE
;
1228 bindReadBuf
= GL_TRUE
;
1231 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1235 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1236 if (ctx
->Driver
.Flush
) {
1237 ctx
->Driver
.Flush(ctx
);
1241 /* Binding a user-created framebuffer object */
1242 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1243 if (newFb
== &DummyFramebuffer
) {
1244 /* ID was reserved, but no real framebuffer object made yet */
1247 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1248 /* All FBO IDs must be Gen'd */
1249 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1254 /* create new framebuffer object */
1255 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1257 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1260 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1265 /* Binding the window system framebuffer (which was originally set
1266 * with MakeCurrent).
1268 newFb
= ctx
->WinSysDrawBuffer
;
1269 newFbread
= ctx
->WinSysReadBuffer
;
1273 ASSERT(newFb
!= &DummyFramebuffer
);
1276 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1280 if (ctx
->ReadBuffer
== newFbread
)
1281 bindReadBuf
= GL_FALSE
; /* no change */
1283 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1287 /* check if old FB had any texture attachments */
1288 if (ctx
->DrawBuffer
->Name
!= 0) {
1289 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1292 if (ctx
->DrawBuffer
== newFb
)
1293 bindDrawBuf
= GL_FALSE
; /* no change */
1295 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1297 if (newFb
->Name
!= 0) {
1298 /* check if newly bound framebuffer has any texture attachments */
1299 check_begin_texture_render(ctx
, newFb
);
1303 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
1304 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1310 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1313 GET_CURRENT_CONTEXT(ctx
);
1315 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1316 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1317 /* The above doesn't fully flush the drivers in the way that a
1318 * glFlush does, but that is required here:
1320 if (ctx
->Driver
.Flush
)
1321 ctx
->Driver
.Flush(ctx
);
1323 for (i
= 0; i
< n
; i
++) {
1324 if (framebuffers
[i
] > 0) {
1325 struct gl_framebuffer
*fb
;
1326 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1328 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1330 /* check if deleting currently bound framebuffer object */
1331 if (fb
== ctx
->DrawBuffer
) {
1333 ASSERT(fb
->RefCount
>= 2);
1334 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1336 if (fb
== ctx
->ReadBuffer
) {
1338 ASSERT(fb
->RefCount
>= 2);
1339 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1342 /* remove from hash table immediately, to free the ID */
1343 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1345 if (fb
!= &DummyFramebuffer
) {
1346 /* But the object will not be freed until it's no longer
1347 * bound in any context.
1349 _mesa_reference_framebuffer(&fb
, NULL
);
1358 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1360 GET_CURRENT_CONTEXT(ctx
);
1364 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1367 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1374 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1376 for (i
= 0; i
< n
; i
++) {
1377 GLuint name
= first
+ i
;
1378 framebuffers
[i
] = name
;
1379 /* insert dummy placeholder into hash table */
1380 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1381 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1382 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1389 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1391 struct gl_framebuffer
*buffer
;
1392 GET_CURRENT_CONTEXT(ctx
);
1394 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1397 #if FEATURE_EXT_framebuffer_blit
1398 case GL_DRAW_FRAMEBUFFER_EXT
:
1399 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1400 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1403 buffer
= ctx
->DrawBuffer
;
1405 case GL_READ_FRAMEBUFFER_EXT
:
1406 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1407 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1410 buffer
= ctx
->ReadBuffer
;
1413 case GL_FRAMEBUFFER_EXT
:
1414 buffer
= ctx
->DrawBuffer
;
1417 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1418 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1421 if (buffer
->Name
== 0) {
1422 /* The window system / default framebuffer is always complete */
1423 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1426 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1428 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1429 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1432 return buffer
->_Status
;
1438 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1441 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1442 GLenum attachment
, GLenum textarget
, GLuint texture
,
1443 GLint level
, GLint zoffset
)
1445 struct gl_renderbuffer_attachment
*att
;
1446 struct gl_texture_object
*texObj
= NULL
;
1447 struct gl_framebuffer
*fb
;
1448 GLboolean error
= GL_FALSE
;
1450 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1453 case GL_READ_FRAMEBUFFER_EXT
:
1454 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1455 fb
= ctx
->ReadBuffer
;
1457 case GL_DRAW_FRAMEBUFFER_EXT
:
1458 error
= !ctx
->Extensions
.EXT_framebuffer_blit
;
1460 case GL_FRAMEBUFFER_EXT
:
1461 fb
= ctx
->DrawBuffer
;
1468 _mesa_error(ctx
, GL_INVALID_ENUM
,
1469 "glFramebufferTexture%sEXT(target=0x%x)", caller
, target
);
1475 /* check framebuffer binding */
1476 if (fb
->Name
== 0) {
1477 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1478 "glFramebufferTexture%sEXT", caller
);
1483 /* The textarget, level, and zoffset parameters are only validated if
1484 * texture is non-zero.
1487 GLboolean err
= GL_TRUE
;
1489 texObj
= _mesa_lookup_texture(ctx
, texture
);
1490 if (texObj
!= NULL
) {
1491 if (textarget
== 0) {
1492 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1493 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1494 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1497 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1498 ? !IS_CUBE_FACE(textarget
)
1499 : (texObj
->Target
!= textarget
);
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glFramebufferTexture%sEXT(texture target mismatch)",
1510 if (texObj
->Target
== GL_TEXTURE_3D
) {
1511 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1512 if (zoffset
< 0 || zoffset
>= maxSize
) {
1513 _mesa_error(ctx
, GL_INVALID_VALUE
,
1514 "glFramebufferTexture%sEXT(zoffset)", caller
);
1518 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1519 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1520 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1521 _mesa_error(ctx
, GL_INVALID_VALUE
,
1522 "glFramebufferTexture%sEXT(layer)", caller
);
1528 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1529 _mesa_error(ctx
, GL_INVALID_VALUE
,
1530 "glFramebufferTexture%sEXT(level)", caller
);
1535 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1537 _mesa_error(ctx
, GL_INVALID_ENUM
,
1538 "glFramebufferTexture%sEXT(attachment)", caller
);
1542 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1543 /* the texture format must be depth+stencil */
1544 const struct gl_texture_image
*texImg
;
1545 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1546 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1548 "glFramebufferTexture%sEXT(texture is not"
1549 " DEPTH_STENCIL format)", caller
);
1554 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1555 /* The above doesn't fully flush the drivers in the way that a
1556 * glFlush does, but that is required here:
1558 if (ctx
->Driver
.Flush
)
1559 ctx
->Driver
.Flush(ctx
);
1561 _glthread_LOCK_MUTEX(fb
->Mutex
);
1563 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1565 /* Set the render-to-texture flag. We'll check this flag in
1566 * glTexImage() and friends to determine if we need to revalidate
1567 * any FBOs that might be rendering into this texture.
1568 * This flag never gets cleared since it's non-trivial to determine
1569 * when all FBOs might be done rendering to this texture. That's OK
1570 * though since it's uncommon to render to a texture then repeatedly
1571 * call glTexImage() to change images in the texture.
1573 texObj
->_RenderToTexture
= GL_TRUE
;
1576 _mesa_remove_attachment(ctx
, att
);
1579 invalidate_framebuffer(fb
);
1581 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1587 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1588 GLenum textarget
, GLuint texture
, GLint level
)
1590 GET_CURRENT_CONTEXT(ctx
);
1592 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1593 _mesa_error(ctx
, GL_INVALID_ENUM
,
1594 "glFramebufferTexture1DEXT(textarget)");
1598 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1604 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1605 GLenum textarget
, GLuint texture
, GLint level
)
1607 GET_CURRENT_CONTEXT(ctx
);
1609 if ((texture
!= 0) &&
1610 (textarget
!= GL_TEXTURE_2D
) &&
1611 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1612 (!IS_CUBE_FACE(textarget
))) {
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1614 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget
);
1618 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1624 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1625 GLenum textarget
, GLuint texture
,
1626 GLint level
, GLint zoffset
)
1628 GET_CURRENT_CONTEXT(ctx
);
1630 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1631 _mesa_error(ctx
, GL_INVALID_ENUM
,
1632 "glFramebufferTexture3DEXT(textarget)");
1636 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1642 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1643 GLuint texture
, GLint level
, GLint layer
)
1645 GET_CURRENT_CONTEXT(ctx
);
1647 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1653 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1654 GLenum renderbufferTarget
,
1655 GLuint renderbuffer
)
1657 struct gl_renderbuffer_attachment
*att
;
1658 struct gl_framebuffer
*fb
;
1659 struct gl_renderbuffer
*rb
;
1660 GET_CURRENT_CONTEXT(ctx
);
1662 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1665 #if FEATURE_EXT_framebuffer_blit
1666 case GL_DRAW_FRAMEBUFFER_EXT
:
1667 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1668 _mesa_error(ctx
, GL_INVALID_ENUM
,
1669 "glFramebufferRenderbufferEXT(target)");
1672 fb
= ctx
->DrawBuffer
;
1674 case GL_READ_FRAMEBUFFER_EXT
:
1675 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1676 _mesa_error(ctx
, GL_INVALID_ENUM
,
1677 "glFramebufferRenderbufferEXT(target)");
1680 fb
= ctx
->ReadBuffer
;
1683 case GL_FRAMEBUFFER_EXT
:
1684 fb
= ctx
->DrawBuffer
;
1687 _mesa_error(ctx
, GL_INVALID_ENUM
,
1688 "glFramebufferRenderbufferEXT(target)");
1692 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1693 _mesa_error(ctx
, GL_INVALID_ENUM
,
1694 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1698 if (fb
->Name
== 0) {
1699 /* Can't attach new renderbuffers to a window system framebuffer */
1700 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1704 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1706 _mesa_error(ctx
, GL_INVALID_ENUM
,
1707 "glFramebufferRenderbufferEXT(attachment)");
1712 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1714 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1715 "glFramebufferRenderbufferEXT(renderbuffer)");
1720 /* remove renderbuffer attachment */
1724 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1725 /* make sure the renderbuffer is a depth/stencil format */
1726 const GLenum baseFormat
=
1727 _mesa_get_format_base_format(att
->Renderbuffer
->Format
);
1728 if (baseFormat
!= GL_DEPTH_STENCIL
) {
1729 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1730 "glFramebufferRenderbufferEXT(renderbuffer"
1731 " is not DEPTH_STENCIL format)");
1737 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1738 /* The above doesn't fully flush the drivers in the way that a
1739 * glFlush does, but that is required here:
1741 if (ctx
->Driver
.Flush
)
1742 ctx
->Driver
.Flush(ctx
);
1744 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1745 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1747 /* Some subsequent GL commands may depend on the framebuffer's visual
1748 * after the binding is updated. Update visual info now.
1750 _mesa_update_framebuffer_visual(fb
);
1755 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1756 GLenum pname
, GLint
*params
)
1758 const struct gl_renderbuffer_attachment
*att
;
1759 struct gl_framebuffer
*buffer
;
1760 GET_CURRENT_CONTEXT(ctx
);
1762 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1765 #if FEATURE_EXT_framebuffer_blit
1766 case GL_DRAW_FRAMEBUFFER_EXT
:
1767 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1768 _mesa_error(ctx
, GL_INVALID_ENUM
,
1769 "glGetFramebufferAttachmentParameterivEXT(target)");
1772 buffer
= ctx
->DrawBuffer
;
1774 case GL_READ_FRAMEBUFFER_EXT
:
1775 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1776 _mesa_error(ctx
, GL_INVALID_ENUM
,
1777 "glGetFramebufferAttachmentParameterivEXT(target)");
1780 buffer
= ctx
->ReadBuffer
;
1783 case GL_FRAMEBUFFER_EXT
:
1784 buffer
= ctx
->DrawBuffer
;
1787 _mesa_error(ctx
, GL_INVALID_ENUM
,
1788 "glGetFramebufferAttachmentParameterivEXT(target)");
1792 if (buffer
->Name
== 0) {
1793 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1794 "glGetFramebufferAttachmentParameterivEXT");
1798 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1800 _mesa_error(ctx
, GL_INVALID_ENUM
,
1801 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1805 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1806 /* the depth and stencil attachments must point to the same buffer */
1807 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1808 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1809 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1810 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1811 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1812 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1813 " attachments differ)");
1818 FLUSH_CURRENT(ctx
, _NEW_BUFFERS
);
1819 /* The above doesn't fully flush the drivers in the way that a
1820 * glFlush does, but that is required here:
1822 if (ctx
->Driver
.Flush
)
1823 ctx
->Driver
.Flush(ctx
);
1826 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1827 *params
= att
->Type
;
1829 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1830 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1831 *params
= att
->Renderbuffer
->Name
;
1833 else if (att
->Type
== GL_TEXTURE
) {
1834 *params
= att
->Texture
->Name
;
1837 _mesa_error(ctx
, GL_INVALID_ENUM
,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1841 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1842 if (att
->Type
== GL_TEXTURE
) {
1843 *params
= att
->TextureLevel
;
1846 _mesa_error(ctx
, GL_INVALID_ENUM
,
1847 "glGetFramebufferAttachmentParameterivEXT(pname)");
1850 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1851 if (att
->Type
== GL_TEXTURE
) {
1852 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1853 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1860 _mesa_error(ctx
, GL_INVALID_ENUM
,
1861 "glGetFramebufferAttachmentParameterivEXT(pname)");
1864 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1865 if (att
->Type
== GL_TEXTURE
) {
1866 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1867 *params
= att
->Zoffset
;
1874 _mesa_error(ctx
, GL_INVALID_ENUM
,
1875 "glGetFramebufferAttachmentParameterivEXT(pname)");
1878 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1879 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1880 _mesa_error(ctx
, GL_INVALID_ENUM
,
1881 "glGetFramebufferAttachmentParameterivEXT(pname)");
1884 *params
= _mesa_get_format_color_encoding(att
->Renderbuffer
->Format
);
1887 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1888 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1889 _mesa_error(ctx
, GL_INVALID_ENUM
,
1890 "glGetFramebufferAttachmentParameterivEXT(pname)");
1894 gl_format format
= att
->Renderbuffer
->Format
;
1895 if (format
== MESA_FORMAT_CI8
|| format
== MESA_FORMAT_S8
) {
1900 *params
= _mesa_get_format_datatype(format
);
1904 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1905 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1906 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1907 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1908 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1909 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1910 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1911 _mesa_error(ctx
, GL_INVALID_ENUM
,
1912 "glGetFramebufferAttachmentParameterivEXT(pname)");
1914 else if (att
->Texture
) {
1915 const struct gl_texture_image
*texImage
=
1916 _mesa_select_tex_image(ctx
, att
->Texture
, att
->Texture
->Target
,
1919 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
1920 texImage
->TexFormat
);
1926 else if (att
->Renderbuffer
) {
1927 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
1928 att
->Renderbuffer
->Format
);
1935 _mesa_error(ctx
, GL_INVALID_ENUM
,
1936 "glGetFramebufferAttachmentParameterivEXT(pname)");
1943 _mesa_GenerateMipmapEXT(GLenum target
)
1945 struct gl_texture_unit
*texUnit
;
1946 struct gl_texture_object
*texObj
;
1947 GET_CURRENT_CONTEXT(ctx
);
1949 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1950 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1956 case GL_TEXTURE_CUBE_MAP
:
1957 /* OK, legal value */
1960 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1964 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1965 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1967 _mesa_lock_texture(ctx
, texObj
);
1968 if (target
== GL_TEXTURE_CUBE_MAP
) {
1970 for (face
= 0; face
< 6; face
++)
1971 ctx
->Driver
.GenerateMipmap(ctx
,
1972 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1976 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1978 _mesa_unlock_texture(ctx
, texObj
);
1982 #if FEATURE_EXT_framebuffer_blit
1984 * Blit rectangular region, optionally from one framebuffer to another.
1986 * Note, if the src buffer is multisampled and the dest is not, this is
1987 * when the samples must be resolved to a single color.
1990 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1991 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1992 GLbitfield mask
, GLenum filter
)
1994 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1995 GL_DEPTH_BUFFER_BIT
|
1996 GL_STENCIL_BUFFER_BIT
);
1997 const struct gl_framebuffer
*readFb
, *drawFb
;
1998 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1999 GET_CURRENT_CONTEXT(ctx
);
2001 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2002 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2004 if (ctx
->NewState
) {
2005 _mesa_update_state(ctx
);
2008 readFb
= ctx
->ReadBuffer
;
2009 drawFb
= ctx
->DrawBuffer
;
2011 if (!readFb
|| !drawFb
) {
2012 /* This will normally never happen but someday we may want to
2013 * support MakeCurrent() with no drawables.
2018 /* check for complete framebuffers */
2019 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
2020 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2021 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2022 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2026 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
2027 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
2031 if (mask
& ~legalMaskBits
) {
2032 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
2036 /* depth/stencil must be blitted with nearest filtering */
2037 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
2038 && filter
!= GL_NEAREST
) {
2039 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2040 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2044 /* get color read/draw renderbuffers */
2045 if (mask
& GL_COLOR_BUFFER_BIT
) {
2046 colorReadRb
= readFb
->_ColorReadBuffer
;
2047 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
2050 colorReadRb
= colorDrawRb
= NULL
;
2053 if (mask
& GL_STENCIL_BUFFER_BIT
) {
2054 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
2055 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
2058 _mesa_get_format_bits(readRb
->Format
, GL_STENCIL_BITS
) !=
2059 _mesa_get_format_bits(drawRb
->Format
, GL_STENCIL_BITS
)) {
2060 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2061 "glBlitFramebufferEXT(stencil buffer size mismatch");
2066 if (mask
& GL_DEPTH_BUFFER_BIT
) {
2067 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
2068 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
2071 _mesa_get_format_bits(readRb
->Format
, GL_DEPTH_BITS
) !=
2072 _mesa_get_format_bits(drawRb
->Format
, GL_DEPTH_BITS
)) {
2073 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2074 "glBlitFramebufferEXT(depth buffer size mismatch");
2079 if (readFb
->Visual
.samples
> 0 &&
2080 drawFb
->Visual
.samples
> 0 &&
2081 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
2082 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2083 "glBlitFramebufferEXT(mismatched samples");
2087 /* extra checks for multisample copies... */
2088 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
2089 /* src and dest region sizes must be the same */
2090 if (srcX1
- srcX0
!= dstX1
- dstX0
||
2091 srcY1
- srcY0
!= dstY1
- dstY0
) {
2092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2093 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2097 /* color formats must match */
2100 colorReadRb
->Format
!= colorDrawRb
->Format
) {
2101 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2102 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2107 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2108 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2112 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2113 ctx
->Driver
.BlitFramebuffer(ctx
,
2114 srcX0
, srcY0
, srcX1
, srcY1
,
2115 dstX0
, dstY0
, dstX1
, dstY1
,
2118 #endif /* FEATURE_EXT_framebuffer_blit */