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
38 #include "framebuffer.h"
42 #include "renderbuffer.h"
52 * None of the GL_EXT_framebuffer_object functions are compiled into
59 * When glGenRender/FramebuffersEXT() is called we insert pointers to
60 * these placeholder objects into the hash table.
61 * Later, when the object ID is first bound, we replace the placeholder
62 * with the real frame/renderbuffer.
64 static struct gl_framebuffer DummyFramebuffer
;
65 static struct gl_renderbuffer DummyRenderbuffer
;
68 #define IS_CUBE_FACE(TARGET) \
69 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
70 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
74 delete_dummy_renderbuffer(struct gl_renderbuffer
*rb
)
80 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
87 _mesa_init_fbobjects(GLcontext
*ctx
)
89 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
90 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
95 * Helper routine for getting a gl_renderbuffer.
97 struct gl_renderbuffer
*
98 _mesa_lookup_renderbuffer(GLcontext
*ctx
, GLuint id
)
100 struct gl_renderbuffer
*rb
;
105 rb
= (struct gl_renderbuffer
*)
106 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
112 * Helper routine for getting a gl_framebuffer.
114 struct gl_framebuffer
*
115 _mesa_lookup_framebuffer(GLcontext
*ctx
, GLuint id
)
117 struct gl_framebuffer
*fb
;
122 fb
= (struct gl_framebuffer
*)
123 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
129 * Mark the given framebuffer as invalid. This will force the
130 * test for framebuffer completeness to be done before the framebuffer
134 invalidate_framebuffer(struct gl_framebuffer
*fb
)
136 fb
->_Status
= 0; /* "indeterminate" */
141 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
142 * gl_renderbuffer_attachment object.
143 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
144 * the depth buffer attachment point.
146 struct gl_renderbuffer_attachment
*
147 _mesa_get_attachment(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
152 switch (attachment
) {
153 case GL_COLOR_ATTACHMENT0_EXT
:
154 case GL_COLOR_ATTACHMENT1_EXT
:
155 case GL_COLOR_ATTACHMENT2_EXT
:
156 case GL_COLOR_ATTACHMENT3_EXT
:
157 case GL_COLOR_ATTACHMENT4_EXT
:
158 case GL_COLOR_ATTACHMENT5_EXT
:
159 case GL_COLOR_ATTACHMENT6_EXT
:
160 case GL_COLOR_ATTACHMENT7_EXT
:
161 case GL_COLOR_ATTACHMENT8_EXT
:
162 case GL_COLOR_ATTACHMENT9_EXT
:
163 case GL_COLOR_ATTACHMENT10_EXT
:
164 case GL_COLOR_ATTACHMENT11_EXT
:
165 case GL_COLOR_ATTACHMENT12_EXT
:
166 case GL_COLOR_ATTACHMENT13_EXT
:
167 case GL_COLOR_ATTACHMENT14_EXT
:
168 case GL_COLOR_ATTACHMENT15_EXT
:
169 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
170 if (i
>= ctx
->Const
.MaxColorAttachments
) {
173 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
174 case GL_DEPTH_STENCIL_ATTACHMENT
:
176 case GL_DEPTH_ATTACHMENT_EXT
:
177 return &fb
->Attachment
[BUFFER_DEPTH
];
178 case GL_STENCIL_ATTACHMENT_EXT
:
179 return &fb
->Attachment
[BUFFER_STENCIL
];
187 * Remove any texture or renderbuffer attached to the given attachment
188 * point. Update reference counts, etc.
191 _mesa_remove_attachment(GLcontext
*ctx
, struct gl_renderbuffer_attachment
*att
)
193 if (att
->Type
== GL_TEXTURE
) {
194 ASSERT(att
->Texture
);
195 if (ctx
->Driver
.FinishRenderTexture
) {
196 /* tell driver we're done rendering to this texobj */
197 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
199 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
200 ASSERT(!att
->Texture
);
202 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
203 ASSERT(!att
->Texture
);
204 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
205 ASSERT(!att
->Renderbuffer
);
208 att
->Complete
= GL_TRUE
;
213 * Bind a texture object to an attachment point.
214 * The previous binding, if any, will be removed first.
217 _mesa_set_texture_attachment(GLcontext
*ctx
,
218 struct gl_framebuffer
*fb
,
219 struct gl_renderbuffer_attachment
*att
,
220 struct gl_texture_object
*texObj
,
221 GLenum texTarget
, GLuint level
, GLuint zoffset
)
223 if (att
->Texture
== texObj
) {
224 /* re-attaching same texture */
225 ASSERT(att
->Type
== GL_TEXTURE
);
229 _mesa_remove_attachment(ctx
, att
);
230 att
->Type
= GL_TEXTURE
;
231 assert(!att
->Texture
);
232 _mesa_reference_texobj(&att
->Texture
, texObj
);
235 /* always update these fields */
236 att
->TextureLevel
= level
;
237 if (IS_CUBE_FACE(texTarget
)) {
238 att
->CubeMapFace
= texTarget
- GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
241 att
->CubeMapFace
= 0;
243 att
->Zoffset
= zoffset
;
244 att
->Complete
= GL_FALSE
;
246 if (att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
247 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
250 invalidate_framebuffer(fb
);
255 * Bind a renderbuffer to an attachment point.
256 * The previous binding, if any, will be removed first.
259 _mesa_set_renderbuffer_attachment(GLcontext
*ctx
,
260 struct gl_renderbuffer_attachment
*att
,
261 struct gl_renderbuffer
*rb
)
263 /* XXX check if re-doing same attachment, exit early */
264 _mesa_remove_attachment(ctx
, att
);
265 att
->Type
= GL_RENDERBUFFER_EXT
;
266 att
->Texture
= NULL
; /* just to be safe */
267 att
->Complete
= GL_FALSE
;
268 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
273 * Fallback for ctx->Driver.FramebufferRenderbuffer()
274 * Attach a renderbuffer object to a framebuffer object.
277 _mesa_framebuffer_renderbuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
278 GLenum attachment
, struct gl_renderbuffer
*rb
)
280 struct gl_renderbuffer_attachment
*att
;
282 _glthread_LOCK_MUTEX(fb
->Mutex
);
284 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
287 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
288 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
289 /* do stencil attachment here (depth already done above) */
290 att
= _mesa_get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
);
292 _mesa_set_renderbuffer_attachment(ctx
, att
, rb
);
296 _mesa_remove_attachment(ctx
, att
);
299 invalidate_framebuffer(fb
);
301 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
306 * Test if an attachment point is complete and update its Complete field.
307 * \param format if GL_COLOR, this is a color attachment point,
308 * if GL_DEPTH, this is a depth component attachment point,
309 * if GL_STENCIL, this is a stencil component attachment point.
312 test_attachment_completeness(const GLcontext
*ctx
, GLenum format
,
313 struct gl_renderbuffer_attachment
*att
)
315 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
317 /* assume complete */
318 att
->Complete
= GL_TRUE
;
320 /* Look for reasons why the attachment might be incomplete */
321 if (att
->Type
== GL_TEXTURE
) {
322 const struct gl_texture_object
*texObj
= att
->Texture
;
323 struct gl_texture_image
*texImage
;
326 att
->Complete
= GL_FALSE
;
330 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
332 att
->Complete
= GL_FALSE
;
335 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
336 att
->Complete
= GL_FALSE
;
339 if (texObj
->Target
== GL_TEXTURE_3D
&& att
->Zoffset
>= texImage
->Depth
) {
340 att
->Complete
= GL_FALSE
;
344 if (format
== GL_COLOR
) {
345 if (texImage
->TexFormat
->BaseFormat
!= GL_RGB
&&
346 texImage
->TexFormat
->BaseFormat
!= GL_RGBA
) {
347 att
->Complete
= GL_FALSE
;
351 else if (format
== GL_DEPTH
) {
352 if (texImage
->TexFormat
->BaseFormat
== GL_DEPTH_COMPONENT
) {
355 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
356 texImage
->TexFormat
->BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
360 att
->Complete
= GL_FALSE
;
365 /* no such thing as stencil textures */
366 att
->Complete
= GL_FALSE
;
370 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
371 ASSERT(att
->Renderbuffer
);
372 if (!att
->Renderbuffer
->InternalFormat
||
373 att
->Renderbuffer
->Width
< 1 ||
374 att
->Renderbuffer
->Height
< 1) {
375 att
->Complete
= GL_FALSE
;
378 if (format
== GL_COLOR
) {
379 if (att
->Renderbuffer
->_BaseFormat
!= GL_RGB
&&
380 att
->Renderbuffer
->_BaseFormat
!= GL_RGBA
) {
381 ASSERT(att
->Renderbuffer
->RedBits
);
382 ASSERT(att
->Renderbuffer
->GreenBits
);
383 ASSERT(att
->Renderbuffer
->BlueBits
);
384 att
->Complete
= GL_FALSE
;
388 else if (format
== GL_DEPTH
) {
389 ASSERT(att
->Renderbuffer
->DepthBits
);
390 if (att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
393 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
394 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
398 att
->Complete
= GL_FALSE
;
403 assert(format
== GL_STENCIL
);
404 ASSERT(att
->Renderbuffer
->StencilBits
);
405 if (att
->Renderbuffer
->_BaseFormat
== GL_STENCIL_INDEX
) {
408 else if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
409 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
413 att
->Complete
= GL_FALSE
;
419 ASSERT(att
->Type
== GL_NONE
);
427 * Helpful for debugging
430 fbo_incomplete(const char *msg
, int index
)
435 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
441 * Test if the given framebuffer object is complete and update its
442 * Status field with the results.
443 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
444 * driver to make hardware-specific validation/completeness checks.
445 * Also update the framebuffer's Width and Height fields if the
446 * framebuffer is complete.
449 _mesa_test_framebuffer_completeness(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
452 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
453 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
454 GLint numSamples
= -1;
458 assert(fb
->Name
!= 0);
464 /* Start at -2 to more easily loop over all attachment points.
469 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
470 struct gl_renderbuffer_attachment
*att
;
474 * XXX for ARB_fbo, only check color buffers that are named by
475 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
478 /* check for attachment completeness
481 att
= &fb
->Attachment
[BUFFER_DEPTH
];
482 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
483 if (!att
->Complete
) {
484 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
485 fbo_incomplete("depth attachment incomplete", -1);
490 att
= &fb
->Attachment
[BUFFER_STENCIL
];
491 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
492 if (!att
->Complete
) {
493 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
494 fbo_incomplete("stencil attachment incomplete", -1);
499 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
500 test_attachment_completeness(ctx
, GL_COLOR
, att
);
501 if (!att
->Complete
) {
502 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
503 fbo_incomplete("color attachment incomplete", i
);
508 /* get width, height, format of the renderbuffer/texture
510 if (att
->Type
== GL_TEXTURE
) {
511 const struct gl_texture_image
*texImg
512 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
513 minWidth
= MIN2(minWidth
, texImg
->Width
);
514 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
515 minHeight
= MIN2(minHeight
, texImg
->Height
);
516 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
517 f
= texImg
->_BaseFormat
;
519 if (f
!= GL_RGB
&& f
!= GL_RGBA
&& f
!= GL_DEPTH_COMPONENT
520 && f
!= GL_DEPTH_STENCIL_EXT
) {
521 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
522 fbo_incomplete("texture attachment incomplete", -1);
526 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
527 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
528 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
529 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
530 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
531 f
= att
->Renderbuffer
->InternalFormat
;
535 assert(att
->Type
== GL_NONE
);
539 if (numSamples
< 0) {
541 numSamples
= att
->Renderbuffer
->NumSamples
;
544 /* Error-check width, height, format, samples
546 if (numImages
== 1) {
547 /* save format, num samples */
553 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
554 /* check that width, height, format are same */
555 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
556 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
557 fbo_incomplete("width or height mismatch", -1);
560 /* check that all color buffer have same format */
561 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
562 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
563 fbo_incomplete("format mismatch", -1);
567 if (att
->Renderbuffer
&&
568 att
->Renderbuffer
->NumSamples
!= numSamples
) {
569 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
570 fbo_incomplete("inconsistant number of samples", i
);
577 #ifndef FEATURE_OES_framebuffer_object
578 /* Check that all DrawBuffers are present */
579 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
580 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
581 const struct gl_renderbuffer_attachment
*att
582 = _mesa_get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
]);
584 if (att
->Type
== GL_NONE
) {
585 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
586 fbo_incomplete("missing drawbuffer", j
);
592 /* Check that the ReadBuffer is present */
593 if (fb
->ColorReadBuffer
!= GL_NONE
) {
594 const struct gl_renderbuffer_attachment
*att
595 = _mesa_get_attachment(ctx
, fb
, fb
->ColorReadBuffer
);
597 if (att
->Type
== GL_NONE
) {
598 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
599 fbo_incomplete("missing readbuffer", -1);
605 if (numImages
== 0) {
606 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
607 fbo_incomplete("no attachments", -1);
611 /* Provisionally set status = COMPLETE ... */
612 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
614 /* ... but the driver may say the FB is incomplete.
615 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
618 if (ctx
->Driver
.ValidateFramebuffer
) {
619 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
620 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
621 fbo_incomplete("driver marked FBO as incomplete", -1);
625 if (fb
->_Status
== GL_FRAMEBUFFER_COMPLETE_EXT
) {
627 * Note that if ARB_framebuffer_object is supported and the attached
628 * renderbuffers/textures are different sizes, the framebuffer
629 * width/height will be set to the smallest width/height.
631 fb
->Width
= minWidth
;
632 fb
->Height
= minHeight
;
634 /* finally, update the visual info for the framebuffer */
635 _mesa_update_framebuffer_visual(fb
);
641 _mesa_IsRenderbufferEXT(GLuint renderbuffer
)
643 GET_CURRENT_CONTEXT(ctx
);
644 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
646 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
647 if (rb
!= NULL
&& rb
!= &DummyRenderbuffer
)
655 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
657 struct gl_renderbuffer
*newRb
;
658 GET_CURRENT_CONTEXT(ctx
);
660 ASSERT_OUTSIDE_BEGIN_END(ctx
);
662 if (target
!= GL_RENDERBUFFER_EXT
) {
663 _mesa_error(ctx
, GL_INVALID_ENUM
,
664 "glBindRenderbufferEXT(target)");
668 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
669 /* The above doesn't fully flush the drivers in the way that a
670 * glFlush does, but that is required here:
672 if (ctx
->Driver
.Flush
)
673 ctx
->Driver
.Flush(ctx
);
677 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
678 if (newRb
== &DummyRenderbuffer
) {
679 /* ID was reserved, but no real renderbuffer object made yet */
682 else if (!newRb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
683 /* All RB IDs must be Gen'd */
684 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindRenderbuffer(buffer)");
689 /* create new renderbuffer object */
690 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
692 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindRenderbufferEXT");
695 ASSERT(newRb
->AllocStorage
);
696 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
697 newRb
->RefCount
= 1; /* referenced by hash table */
704 ASSERT(newRb
!= &DummyRenderbuffer
);
706 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
711 * If the given renderbuffer is anywhere attached to the framebuffer, detach
713 * This is used when a renderbuffer object is deleted.
714 * The spec calls for unbinding.
717 detach_renderbuffer(GLcontext
*ctx
,
718 struct gl_framebuffer
*fb
,
719 struct gl_renderbuffer
*rb
)
722 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
723 if (fb
->Attachment
[i
].Renderbuffer
== rb
) {
724 _mesa_remove_attachment(ctx
, &fb
->Attachment
[i
]);
727 invalidate_framebuffer(fb
);
732 _mesa_DeleteRenderbuffersEXT(GLsizei n
, const GLuint
*renderbuffers
)
735 GET_CURRENT_CONTEXT(ctx
);
737 ASSERT_OUTSIDE_BEGIN_END(ctx
);
738 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
740 for (i
= 0; i
< n
; i
++) {
741 if (renderbuffers
[i
] > 0) {
742 struct gl_renderbuffer
*rb
;
743 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
745 /* check if deleting currently bound renderbuffer object */
746 if (rb
== ctx
->CurrentRenderbuffer
) {
748 ASSERT(rb
->RefCount
>= 2);
749 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, 0);
752 if (ctx
->DrawBuffer
->Name
) {
753 detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
755 if (ctx
->ReadBuffer
->Name
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
756 detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
759 /* Remove from hash table immediately, to free the ID.
760 * But the object will not be freed until it's no longer
761 * referenced anywhere else.
763 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
765 if (rb
!= &DummyRenderbuffer
) {
766 /* no longer referenced by hash table */
767 _mesa_reference_renderbuffer(&rb
, NULL
);
776 _mesa_GenRenderbuffersEXT(GLsizei n
, GLuint
*renderbuffers
)
778 GET_CURRENT_CONTEXT(ctx
);
782 ASSERT_OUTSIDE_BEGIN_END(ctx
);
785 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenRenderbuffersEXT(n)");
792 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
794 for (i
= 0; i
< n
; i
++) {
795 GLuint name
= first
+ i
;
796 renderbuffers
[i
] = name
;
797 /* insert dummy placeholder into hash table */
798 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
799 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, name
, &DummyRenderbuffer
);
800 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
806 * Given an internal format token for a render buffer, return the
807 * corresponding base format.
808 * This is very similar to _mesa_base_tex_format() but the set of valid
809 * internal formats is somewhat different.
811 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
812 * GL_DEPTH_STENCIL_EXT or zero if error.
815 _mesa_base_fbo_format(GLcontext
*ctx
, GLenum internalFormat
)
817 switch (internalFormat
) {
836 case GL_STENCIL_INDEX
:
837 case GL_STENCIL_INDEX1_EXT
:
838 case GL_STENCIL_INDEX4_EXT
:
839 case GL_STENCIL_INDEX8_EXT
:
840 case GL_STENCIL_INDEX16_EXT
:
841 return GL_STENCIL_INDEX
;
842 case GL_DEPTH_COMPONENT
:
843 case GL_DEPTH_COMPONENT16
:
844 case GL_DEPTH_COMPONENT24
:
845 case GL_DEPTH_COMPONENT32
:
846 return GL_DEPTH_COMPONENT
;
847 case GL_DEPTH_STENCIL_EXT
:
848 case GL_DEPTH24_STENCIL8_EXT
:
849 if (ctx
->Extensions
.EXT_packed_depth_stencil
)
850 return GL_DEPTH_STENCIL_EXT
;
853 /* XXX add floating point formats eventually */
860 /** sentinal value, see below */
861 #define NO_SAMPLES 1000
865 * Helper function used by _mesa_RenderbufferStorageEXT() and
866 * _mesa_RenderbufferStorageMultisample().
867 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
870 renderbuffer_storage(GLenum target
, GLenum internalFormat
,
871 GLsizei width
, GLsizei height
, GLsizei samples
)
873 const char *func
= samples
== NO_SAMPLES
?
874 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
875 struct gl_renderbuffer
*rb
;
877 GET_CURRENT_CONTEXT(ctx
);
879 ASSERT_OUTSIDE_BEGIN_END(ctx
);
881 if (target
!= GL_RENDERBUFFER_EXT
) {
882 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
886 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
887 if (baseFormat
== 0) {
888 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat)", func
);
892 if (width
< 1 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
893 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width)", func
);
897 if (height
< 1 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
898 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height)", func
);
902 if (samples
== NO_SAMPLES
) {
903 /* NumSamples == 0 indicates non-multisampling */
906 else if (samples
> ctx
->Const
.MaxSamples
) {
907 /* note: driver may choose to use more samples than what's requested */
908 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples)", func
);
912 rb
= ctx
->CurrentRenderbuffer
;
914 _mesa_error(ctx
, GL_INVALID_OPERATION
, func
);
918 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
920 if (rb
->InternalFormat
== internalFormat
&&
921 rb
->Width
== (GLuint
) width
&&
922 rb
->Height
== (GLuint
) height
) {
923 /* no change in allocation needed */
927 /* These MUST get set by the AllocStorage func */
928 rb
->_ActualFormat
= 0;
936 rb
->NumSamples
= samples
;
938 /* Now allocate the storage */
939 ASSERT(rb
->AllocStorage
);
940 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
941 /* No error - check/set fields now */
942 assert(rb
->_ActualFormat
);
943 assert(rb
->Width
== (GLuint
) width
);
944 assert(rb
->Height
== (GLuint
) height
);
945 assert(rb
->RedBits
|| rb
->GreenBits
|| rb
->BlueBits
|| rb
->AlphaBits
||
946 rb
->DepthBits
|| rb
->StencilBits
|| rb
->IndexBits
);
947 rb
->InternalFormat
= internalFormat
;
948 rb
->_BaseFormat
= baseFormat
;
951 /* Probably ran out of memory - clear the fields */
954 rb
->InternalFormat
= GL_NONE
;
955 rb
->_ActualFormat
= GL_NONE
;
956 rb
->_BaseFormat
= GL_NONE
;
968 test_framebuffer_completeness(ctx, fb);
970 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
977 _mesa_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
978 GLsizei width
, GLsizei height
)
980 /* GL_ARB_fbo says calling this function is equivalent to calling
981 * glRenderbufferStorageMultisample() with samples=0. We pass in
982 * a token value here just for error reporting purposes.
984 renderbuffer_storage(target
, internalFormat
, width
, height
, NO_SAMPLES
);
989 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
990 GLenum internalFormat
,
991 GLsizei width
, GLsizei height
)
993 renderbuffer_storage(target
, internalFormat
, width
, height
, samples
);
999 _mesa_GetRenderbufferParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
1001 struct gl_renderbuffer
*rb
;
1002 GET_CURRENT_CONTEXT(ctx
);
1004 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1006 if (target
!= GL_RENDERBUFFER_EXT
) {
1007 _mesa_error(ctx
, GL_INVALID_ENUM
,
1008 "glGetRenderbufferParameterivEXT(target)");
1012 rb
= ctx
->CurrentRenderbuffer
;
1014 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1015 "glGetRenderbufferParameterivEXT");
1019 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1022 case GL_RENDERBUFFER_WIDTH_EXT
:
1023 *params
= rb
->Width
;
1025 case GL_RENDERBUFFER_HEIGHT_EXT
:
1026 *params
= rb
->Height
;
1028 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
1029 *params
= rb
->InternalFormat
;
1031 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1032 *params
= rb
->RedBits
;
1034 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1035 *params
= rb
->GreenBits
;
1037 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1038 *params
= rb
->BlueBits
;
1040 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1041 *params
= rb
->AlphaBits
;
1043 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1044 *params
= rb
->DepthBits
;
1046 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1047 *params
= rb
->StencilBits
;
1049 case GL_RENDERBUFFER_SAMPLES
:
1050 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1051 *params
= rb
->NumSamples
;
1056 _mesa_error(ctx
, GL_INVALID_ENUM
,
1057 "glGetRenderbufferParameterivEXT(target)");
1063 GLboolean GLAPIENTRY
1064 _mesa_IsFramebufferEXT(GLuint framebuffer
)
1066 GET_CURRENT_CONTEXT(ctx
);
1067 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1069 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1070 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
1078 check_begin_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1081 ASSERT(ctx
->Driver
.RenderTexture
);
1082 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1083 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1084 struct gl_texture_object
*texObj
= att
->Texture
;
1086 && att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]) {
1087 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
1094 * Examine all the framebuffer's attachments to see if any are textures.
1095 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1096 * notify the device driver that the texture image may have changed.
1099 check_end_texture_render(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
1101 if (ctx
->Driver
.FinishRenderTexture
) {
1103 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1104 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
1105 if (att
->Texture
&& att
->Renderbuffer
) {
1106 ctx
->Driver
.FinishRenderTexture(ctx
, att
);
1114 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
1116 struct gl_framebuffer
*newFb
, *newFbread
;
1117 GLboolean bindReadBuf
, bindDrawBuf
;
1118 GET_CURRENT_CONTEXT(ctx
);
1121 if (ctx
->Extensions
.ARB_framebuffer_object
) {
1122 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
1123 ASSERT(ctx
->Extensions
.EXT_framebuffer_blit
);
1127 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1129 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1130 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1131 "glBindFramebufferEXT(unsupported)");
1136 #if FEATURE_EXT_framebuffer_blit
1137 case GL_DRAW_FRAMEBUFFER_EXT
:
1138 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1139 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1142 bindDrawBuf
= GL_TRUE
;
1143 bindReadBuf
= GL_FALSE
;
1145 case GL_READ_FRAMEBUFFER_EXT
:
1146 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1147 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1150 bindDrawBuf
= GL_FALSE
;
1151 bindReadBuf
= GL_TRUE
;
1154 case GL_FRAMEBUFFER_EXT
:
1155 bindDrawBuf
= GL_TRUE
;
1156 bindReadBuf
= GL_TRUE
;
1159 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
1163 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1165 if (ctx
->Driver
.Flush
) {
1166 ctx
->Driver
.Flush(ctx
);
1170 /* Binding a user-created framebuffer object */
1171 newFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
1172 if (newFb
== &DummyFramebuffer
) {
1173 /* ID was reserved, but no real framebuffer object made yet */
1176 else if (!newFb
&& ctx
->Extensions
.ARB_framebuffer_object
) {
1177 /* All FBO IDs must be Gen'd */
1178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFramebuffer(buffer)");
1183 /* create new framebuffer object */
1184 newFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
1186 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
1189 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newFb
);
1194 /* Binding the window system framebuffer (which was originally set
1195 * with MakeCurrent).
1197 newFb
= ctx
->WinSysDrawBuffer
;
1198 newFbread
= ctx
->WinSysReadBuffer
;
1202 ASSERT(newFb
!= &DummyFramebuffer
);
1205 * XXX check if re-binding same buffer and skip some of this code.
1209 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newFbread
);
1213 /* check if old FB had any texture attachments */
1214 check_end_texture_render(ctx
, ctx
->DrawBuffer
);
1216 /* check if time to delete this framebuffer */
1217 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newFb
);
1219 if (newFb
->Name
!= 0) {
1220 /* check if newly bound framebuffer has any texture attachments */
1221 check_begin_texture_render(ctx
, newFb
);
1225 if (ctx
->Driver
.BindFramebuffer
) {
1226 ctx
->Driver
.BindFramebuffer(ctx
, target
, newFb
, newFbread
);
1232 _mesa_DeleteFramebuffersEXT(GLsizei n
, const GLuint
*framebuffers
)
1235 GET_CURRENT_CONTEXT(ctx
);
1237 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1238 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1239 /* The above doesn't fully flush the drivers in the way that a
1240 * glFlush does, but that is required here:
1242 if (ctx
->Driver
.Flush
)
1243 ctx
->Driver
.Flush(ctx
);
1245 for (i
= 0; i
< n
; i
++) {
1246 if (framebuffers
[i
] > 0) {
1247 struct gl_framebuffer
*fb
;
1248 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
1250 ASSERT(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
1252 /* check if deleting currently bound framebuffer object */
1253 if (fb
== ctx
->DrawBuffer
) {
1255 ASSERT(fb
->RefCount
>= 2);
1256 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, 0);
1258 if (fb
== ctx
->ReadBuffer
) {
1260 ASSERT(fb
->RefCount
>= 2);
1261 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, 0);
1264 /* remove from hash table immediately, to free the ID */
1265 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
1267 if (fb
!= &DummyFramebuffer
) {
1268 /* But the object will not be freed until it's no longer
1269 * bound in any context.
1271 _mesa_unreference_framebuffer(&fb
);
1280 _mesa_GenFramebuffersEXT(GLsizei n
, GLuint
*framebuffers
)
1282 GET_CURRENT_CONTEXT(ctx
);
1286 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1289 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenFramebuffersEXT(n)");
1296 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
1298 for (i
= 0; i
< n
; i
++) {
1299 GLuint name
= first
+ i
;
1300 framebuffers
[i
] = name
;
1301 /* insert dummy placeholder into hash table */
1302 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1303 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, name
, &DummyFramebuffer
);
1304 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1311 _mesa_CheckFramebufferStatusEXT(GLenum target
)
1313 struct gl_framebuffer
*buffer
;
1314 GET_CURRENT_CONTEXT(ctx
);
1316 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1319 #if FEATURE_EXT_framebuffer_blit
1320 case GL_DRAW_FRAMEBUFFER_EXT
:
1321 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1322 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1325 buffer
= ctx
->DrawBuffer
;
1327 case GL_READ_FRAMEBUFFER_EXT
:
1328 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1332 buffer
= ctx
->ReadBuffer
;
1335 case GL_FRAMEBUFFER_EXT
:
1336 buffer
= ctx
->DrawBuffer
;
1339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCheckFramebufferStatus(target)");
1340 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1343 if (buffer
->Name
== 0) {
1344 /* The window system / default framebuffer is always complete */
1345 return GL_FRAMEBUFFER_COMPLETE_EXT
;
1348 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1350 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
1351 _mesa_test_framebuffer_completeness(ctx
, buffer
);
1354 return buffer
->_Status
;
1360 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1363 framebuffer_texture(GLcontext
*ctx
, const char *caller
, GLenum target
,
1364 GLenum attachment
, GLenum textarget
, GLuint texture
,
1365 GLint level
, GLint zoffset
)
1367 struct gl_renderbuffer_attachment
*att
;
1368 struct gl_texture_object
*texObj
= NULL
;
1369 struct gl_framebuffer
*fb
;
1371 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1373 if (target
!= GL_FRAMEBUFFER_EXT
) {
1374 _mesa_error(ctx
, GL_INVALID_ENUM
,
1375 "glFramebufferTexture%sEXT(target)", caller
);
1379 fb
= ctx
->DrawBuffer
;
1382 /* check framebuffer binding */
1383 if (fb
->Name
== 0) {
1384 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1385 "glFramebufferTexture%sEXT", caller
);
1390 /* The textarget, level, and zoffset parameters are only validated if
1391 * texture is non-zero.
1394 GLboolean err
= GL_TRUE
;
1396 texObj
= _mesa_lookup_texture(ctx
, texture
);
1397 if (texObj
!= NULL
) {
1398 if (textarget
== 0) {
1399 err
= (texObj
->Target
!= GL_TEXTURE_3D
) &&
1400 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
) &&
1401 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
);
1404 err
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
1405 ? !IS_CUBE_FACE(textarget
)
1406 : (texObj
->Target
!= textarget
);
1411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1412 "glFramebufferTexture%sEXT(texture target mismatch)",
1417 if (texObj
->Target
== GL_TEXTURE_3D
) {
1418 const GLint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1419 if (zoffset
< 0 || zoffset
>= maxSize
) {
1420 _mesa_error(ctx
, GL_INVALID_VALUE
,
1421 "glFramebufferTexture%sEXT(zoffset)", caller
);
1425 else if ((texObj
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) ||
1426 (texObj
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
1427 if (zoffset
< 0 || zoffset
>= ctx
->Const
.MaxArrayTextureLayers
) {
1428 _mesa_error(ctx
, GL_INVALID_VALUE
,
1429 "glFramebufferTexture%sEXT(layer)", caller
);
1435 (level
>= _mesa_max_texture_levels(ctx
, texObj
->Target
))) {
1436 _mesa_error(ctx
, GL_INVALID_VALUE
,
1437 "glFramebufferTexture%sEXT(level)", caller
);
1442 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1444 _mesa_error(ctx
, GL_INVALID_ENUM
,
1445 "glFramebufferTexture%sEXT(attachment)", caller
);
1449 if (texObj
&& attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1450 /* the texture format must be depth+stencil */
1451 const struct gl_texture_image
*texImg
;
1452 texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
1453 if (!texImg
|| texImg
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1454 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1455 "glFramebufferTexture%sEXT(texture is not"
1456 " DEPTH_STENCIL format)", caller
);
1461 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1462 /* The above doesn't fully flush the drivers in the way that a
1463 * glFlush does, but that is required here:
1465 if (ctx
->Driver
.Flush
)
1466 ctx
->Driver
.Flush(ctx
);
1468 _glthread_LOCK_MUTEX(fb
->Mutex
);
1470 _mesa_set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
1474 _mesa_remove_attachment(ctx
, att
);
1477 invalidate_framebuffer(fb
);
1479 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
1485 _mesa_FramebufferTexture1DEXT(GLenum target
, GLenum attachment
,
1486 GLenum textarget
, GLuint texture
, GLint level
)
1488 GET_CURRENT_CONTEXT(ctx
);
1490 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_1D
)) {
1491 _mesa_error(ctx
, GL_INVALID_ENUM
,
1492 "glFramebufferTexture1DEXT(textarget)");
1496 framebuffer_texture(ctx
, "1D", target
, attachment
, textarget
, texture
,
1502 _mesa_FramebufferTexture2DEXT(GLenum target
, GLenum attachment
,
1503 GLenum textarget
, GLuint texture
, GLint level
)
1505 GET_CURRENT_CONTEXT(ctx
);
1507 if ((texture
!= 0) &&
1508 (textarget
!= GL_TEXTURE_2D
) &&
1509 (textarget
!= GL_TEXTURE_RECTANGLE_ARB
) &&
1510 (!IS_CUBE_FACE(textarget
))) {
1511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1512 "glFramebufferTexture2DEXT(textarget)");
1516 framebuffer_texture(ctx
, "2D", target
, attachment
, textarget
, texture
,
1522 _mesa_FramebufferTexture3DEXT(GLenum target
, GLenum attachment
,
1523 GLenum textarget
, GLuint texture
,
1524 GLint level
, GLint zoffset
)
1526 GET_CURRENT_CONTEXT(ctx
);
1528 if ((texture
!= 0) && (textarget
!= GL_TEXTURE_3D
)) {
1529 _mesa_error(ctx
, GL_INVALID_ENUM
,
1530 "glFramebufferTexture3DEXT(textarget)");
1534 framebuffer_texture(ctx
, "3D", target
, attachment
, textarget
, texture
,
1540 _mesa_FramebufferTextureLayerEXT(GLenum target
, GLenum attachment
,
1541 GLuint texture
, GLint level
, GLint layer
)
1543 GET_CURRENT_CONTEXT(ctx
);
1545 framebuffer_texture(ctx
, "Layer", target
, attachment
, 0, texture
,
1551 _mesa_FramebufferRenderbufferEXT(GLenum target
, GLenum attachment
,
1552 GLenum renderbufferTarget
,
1553 GLuint renderbuffer
)
1555 struct gl_renderbuffer_attachment
*att
;
1556 struct gl_framebuffer
*fb
;
1557 struct gl_renderbuffer
*rb
;
1558 GET_CURRENT_CONTEXT(ctx
);
1560 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1563 #if FEATURE_EXT_framebuffer_blit
1564 case GL_DRAW_FRAMEBUFFER_EXT
:
1565 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1566 _mesa_error(ctx
, GL_INVALID_ENUM
,
1567 "glFramebufferRenderbufferEXT(target)");
1570 fb
= ctx
->DrawBuffer
;
1572 case GL_READ_FRAMEBUFFER_EXT
:
1573 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1574 _mesa_error(ctx
, GL_INVALID_ENUM
,
1575 "glFramebufferRenderbufferEXT(target)");
1578 fb
= ctx
->ReadBuffer
;
1581 case GL_FRAMEBUFFER_EXT
:
1582 fb
= ctx
->DrawBuffer
;
1585 _mesa_error(ctx
, GL_INVALID_ENUM
,
1586 "glFramebufferRenderbufferEXT(target)");
1590 if (renderbufferTarget
!= GL_RENDERBUFFER_EXT
) {
1591 _mesa_error(ctx
, GL_INVALID_ENUM
,
1592 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1596 if (fb
->Name
== 0) {
1597 /* Can't attach new renderbuffers to a window system framebuffer */
1598 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glFramebufferRenderbufferEXT");
1602 att
= _mesa_get_attachment(ctx
, fb
, attachment
);
1604 _mesa_error(ctx
, GL_INVALID_ENUM
,
1605 "glFramebufferRenderbufferEXT(attachment)");
1610 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1612 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1613 "glFramebufferRenderbufferEXT(renderbuffer)");
1618 /* remove renderbuffer attachment */
1622 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1623 /* make sure the renderbuffer is a depth/stencil format */
1624 if (rb
->_BaseFormat
!= GL_DEPTH_STENCIL
) {
1625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1626 "glFramebufferRenderbufferEXT(renderbuffer"
1627 " is not DEPTH_STENCIL format)");
1633 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1634 /* The above doesn't fully flush the drivers in the way that a
1635 * glFlush does, but that is required here:
1637 if (ctx
->Driver
.Flush
)
1638 ctx
->Driver
.Flush(ctx
);
1640 assert(ctx
->Driver
.FramebufferRenderbuffer
);
1641 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
1643 /* Some subsequent GL commands may depend on the framebuffer's visual
1644 * after the binding is updated. Update visual info now.
1646 _mesa_update_framebuffer_visual(fb
);
1651 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target
, GLenum attachment
,
1652 GLenum pname
, GLint
*params
)
1654 const struct gl_renderbuffer_attachment
*att
;
1655 struct gl_framebuffer
*buffer
;
1656 GET_CURRENT_CONTEXT(ctx
);
1658 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1661 #if FEATURE_EXT_framebuffer_blit
1662 case GL_DRAW_FRAMEBUFFER_EXT
:
1663 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1664 _mesa_error(ctx
, GL_INVALID_ENUM
,
1665 "glGetFramebufferAttachmentParameterivEXT(target)");
1668 buffer
= ctx
->DrawBuffer
;
1670 case GL_READ_FRAMEBUFFER_EXT
:
1671 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
1672 _mesa_error(ctx
, GL_INVALID_ENUM
,
1673 "glGetFramebufferAttachmentParameterivEXT(target)");
1676 buffer
= ctx
->ReadBuffer
;
1679 case GL_FRAMEBUFFER_EXT
:
1680 buffer
= ctx
->DrawBuffer
;
1683 _mesa_error(ctx
, GL_INVALID_ENUM
,
1684 "glGetFramebufferAttachmentParameterivEXT(target)");
1688 if (buffer
->Name
== 0) {
1689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1690 "glGetFramebufferAttachmentParameterivEXT");
1694 att
= _mesa_get_attachment(ctx
, buffer
, attachment
);
1696 _mesa_error(ctx
, GL_INVALID_ENUM
,
1697 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1701 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
1702 /* the depth and stencil attachments must point to the same buffer */
1703 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
1704 depthAtt
= _mesa_get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
);
1705 stencilAtt
= _mesa_get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
);
1706 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
1707 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1708 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1709 " attachments differ)");
1714 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1715 /* The above doesn't fully flush the drivers in the way that a
1716 * glFlush does, but that is required here:
1718 if (ctx
->Driver
.Flush
)
1719 ctx
->Driver
.Flush(ctx
);
1722 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
1723 *params
= att
->Type
;
1725 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
1726 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1727 *params
= att
->Renderbuffer
->Name
;
1729 else if (att
->Type
== GL_TEXTURE
) {
1730 *params
= att
->Texture
->Name
;
1733 _mesa_error(ctx
, GL_INVALID_ENUM
,
1734 "glGetFramebufferAttachmentParameterivEXT(pname)");
1737 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
1738 if (att
->Type
== GL_TEXTURE
) {
1739 *params
= att
->TextureLevel
;
1742 _mesa_error(ctx
, GL_INVALID_ENUM
,
1743 "glGetFramebufferAttachmentParameterivEXT(pname)");
1746 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
1747 if (att
->Type
== GL_TEXTURE
) {
1748 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
1749 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
1756 _mesa_error(ctx
, GL_INVALID_ENUM
,
1757 "glGetFramebufferAttachmentParameterivEXT(pname)");
1760 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
1761 if (att
->Type
== GL_TEXTURE
) {
1762 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_3D
) {
1763 *params
= att
->Zoffset
;
1770 _mesa_error(ctx
, GL_INVALID_ENUM
,
1771 "glGetFramebufferAttachmentParameterivEXT(pname)");
1774 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
1775 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1776 _mesa_error(ctx
, GL_INVALID_ENUM
,
1777 "glGetFramebufferAttachmentParameterivEXT(pname)");
1780 *params
= att
->Renderbuffer
->ColorEncoding
;
1783 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
1784 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1785 _mesa_error(ctx
, GL_INVALID_ENUM
,
1786 "glGetFramebufferAttachmentParameterivEXT(pname)");
1790 *params
= att
->Renderbuffer
->ComponentType
;
1793 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1794 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1795 _mesa_error(ctx
, GL_INVALID_ENUM
,
1796 "glGetFramebufferAttachmentParameterivEXT(pname)");
1799 *params
= att
->Renderbuffer
->RedBits
;
1802 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1803 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1804 _mesa_error(ctx
, GL_INVALID_ENUM
,
1805 "glGetFramebufferAttachmentParameterivEXT(pname)");
1808 *params
= att
->Renderbuffer
->GreenBits
;
1811 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1812 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1813 _mesa_error(ctx
, GL_INVALID_ENUM
,
1814 "glGetFramebufferAttachmentParameterivEXT(pname)");
1817 *params
= att
->Renderbuffer
->BlueBits
;
1820 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1821 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1822 _mesa_error(ctx
, GL_INVALID_ENUM
,
1823 "glGetFramebufferAttachmentParameterivEXT(pname)");
1826 *params
= att
->Renderbuffer
->AlphaBits
;
1829 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1830 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1831 _mesa_error(ctx
, GL_INVALID_ENUM
,
1832 "glGetFramebufferAttachmentParameterivEXT(pname)");
1835 *params
= att
->Renderbuffer
->DepthBits
;
1838 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1839 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1840 _mesa_error(ctx
, GL_INVALID_ENUM
,
1841 "glGetFramebufferAttachmentParameterivEXT(pname)");
1844 *params
= att
->Renderbuffer
->StencilBits
;
1848 _mesa_error(ctx
, GL_INVALID_ENUM
,
1849 "glGetFramebufferAttachmentParameterivEXT(pname)");
1856 _mesa_GenerateMipmapEXT(GLenum target
)
1858 struct gl_texture_unit
*texUnit
;
1859 struct gl_texture_object
*texObj
;
1860 GET_CURRENT_CONTEXT(ctx
);
1862 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1863 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1869 case GL_TEXTURE_CUBE_MAP
:
1870 /* OK, legal value */
1873 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGenerateMipmapEXT(target)");
1877 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1878 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1880 _mesa_lock_texture(ctx
, texObj
);
1881 if (target
== GL_TEXTURE_CUBE_MAP
) {
1884 for (face
= 0; face
< 6; face
++)
1885 ctx
->Driver
.GenerateMipmap(ctx
,
1886 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
+ face
,
1889 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1891 _mesa_unlock_texture(ctx
, texObj
);
1895 #if FEATURE_EXT_framebuffer_blit
1897 * Blit rectangular region, optionally from one framebuffer to another.
1899 * Note, if the src buffer is multisampled and the dest is not, this is
1900 * when the samples must be resolved to a single color.
1903 _mesa_BlitFramebufferEXT(GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1904 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1905 GLbitfield mask
, GLenum filter
)
1907 const GLbitfield legalMaskBits
= (GL_COLOR_BUFFER_BIT
|
1908 GL_DEPTH_BUFFER_BIT
|
1909 GL_STENCIL_BUFFER_BIT
);
1910 const struct gl_framebuffer
*readFb
, *drawFb
;
1911 const struct gl_renderbuffer
*colorReadRb
, *colorDrawRb
;
1912 GET_CURRENT_CONTEXT(ctx
);
1914 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1915 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1917 if (ctx
->NewState
) {
1918 _mesa_update_state(ctx
);
1921 readFb
= ctx
->ReadBuffer
;
1922 drawFb
= ctx
->DrawBuffer
;
1924 if (!readFb
|| !drawFb
) {
1925 /* This will normally never happen but someday we may want to
1926 * support MakeCurrent() with no drawables.
1931 /* check for complete framebuffers */
1932 if (drawFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
1933 readFb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1934 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1935 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1939 if (filter
!= GL_NEAREST
&& filter
!= GL_LINEAR
) {
1940 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlitFramebufferEXT(filter)");
1944 if (mask
& ~legalMaskBits
) {
1945 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBlitFramebufferEXT(mask)");
1949 /* depth/stencil must be blitted with nearest filtering */
1950 if ((mask
& (GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
))
1951 && filter
!= GL_NEAREST
) {
1952 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1953 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
1957 /* get color read/draw renderbuffers */
1958 if (mask
& GL_COLOR_BUFFER_BIT
) {
1959 colorReadRb
= readFb
->_ColorReadBuffer
;
1960 colorDrawRb
= drawFb
->_ColorDrawBuffers
[0];
1963 colorReadRb
= colorDrawRb
= NULL
;
1966 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1967 struct gl_renderbuffer
*readRb
= readFb
->_StencilBuffer
;
1968 struct gl_renderbuffer
*drawRb
= drawFb
->_StencilBuffer
;
1969 if (readRb
->StencilBits
!= drawRb
->StencilBits
) {
1970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1971 "glBlitFramebufferEXT(stencil buffer size mismatch");
1976 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1977 struct gl_renderbuffer
*readRb
= readFb
->_DepthBuffer
;
1978 struct gl_renderbuffer
*drawRb
= drawFb
->_DepthBuffer
;
1979 if (readRb
->DepthBits
!= drawRb
->DepthBits
) {
1980 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1981 "glBlitFramebufferEXT(depth buffer size mismatch");
1986 if (readFb
->Visual
.samples
> 0 &&
1987 drawFb
->Visual
.samples
> 0 &&
1988 readFb
->Visual
.samples
!= drawFb
->Visual
.samples
) {
1989 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1990 "glBlitFramebufferEXT(mismatched samples");
1994 /* extra checks for multisample copies... */
1995 if (readFb
->Visual
.samples
> 0 || drawFb
->Visual
.samples
> 0) {
1996 /* src and dest region sizes must be the same */
1997 if (srcX1
- srcX0
!= dstX1
- dstX0
||
1998 srcY1
- srcY0
!= dstY1
- dstY0
) {
1999 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2000 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2004 /* color formats must match */
2007 colorReadRb
->_ActualFormat
!= colorDrawRb
->_ActualFormat
) {
2008 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2009 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2014 if (!ctx
->Extensions
.EXT_framebuffer_blit
) {
2015 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlitFramebufferEXT");
2019 ASSERT(ctx
->Driver
.BlitFramebuffer
);
2020 ctx
->Driver
.BlitFramebuffer(ctx
,
2021 srcX0
, srcY0
, srcX1
, srcY1
,
2022 dstX0
, dstY0
, dstX1
, dstY1
,
2025 #endif /* FEATURE_EXT_framebuffer_blit */