2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Functions for allocating/managing framebuffers and renderbuffers.
28 * Also, routines for reading/writing renderbuffer data as ubytes,
29 * ushorts, uints, etc.
37 #include "depthstencil.h"
41 #include "framebuffer.h"
42 #include "renderbuffer.h"
48 * Compute/set the _DepthMax field for the given framebuffer.
49 * This value depends on the Z buffer resolution.
52 compute_depth_max(struct gl_framebuffer
*fb
)
54 if (fb
->Visual
.depthBits
== 0) {
55 /* Special case. Even if we don't have a depth buffer we need
56 * good values for DepthMax for Z vertex transformation purposes
57 * and for per-fragment fog computation.
59 fb
->_DepthMax
= (1 << 16) - 1;
61 else if (fb
->Visual
.depthBits
< 32) {
62 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
65 /* Special case since shift values greater than or equal to the
66 * number of bits in the left hand expression's type are undefined.
68 fb
->_DepthMax
= 0xffffffff;
70 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
72 /* Minimum resolvable depth value, for polygon offset */
73 fb
->_MRD
= (GLfloat
)1.0 / fb
->_DepthMaxF
;
78 * Create and initialize a gl_framebuffer object.
79 * This is intended for creating _window_system_ framebuffers, not generic
80 * framebuffer objects ala GL_EXT_framebuffer_object.
82 * \sa _mesa_new_framebuffer
84 struct gl_framebuffer
*
85 _mesa_create_framebuffer(const GLvisual
*visual
)
87 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
90 _mesa_initialize_framebuffer(fb
, visual
);
97 * Allocate a new gl_framebuffer object.
98 * This is the default function for ctx->Driver.NewFramebuffer().
99 * This is for allocating user-created framebuffers, not window-system
101 * \sa _mesa_create_framebuffer
103 struct gl_framebuffer
*
104 _mesa_new_framebuffer(GLcontext
*ctx
, GLuint name
)
106 struct gl_framebuffer
*fb
;
109 fb
= CALLOC_STRUCT(gl_framebuffer
);
113 fb
->_NumColorDrawBuffers
= 1;
114 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
115 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
116 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
117 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
118 fb
->Delete
= _mesa_destroy_framebuffer
;
125 * Initialize a gl_framebuffer object. Typically used to initialize
126 * window system-created framebuffers, not user-created framebuffers.
127 * \sa _mesa_create_framebuffer
130 _mesa_initialize_framebuffer(struct gl_framebuffer
*fb
, const GLvisual
*visual
)
135 _mesa_bzero(fb
, sizeof(struct gl_framebuffer
));
137 _glthread_INIT_MUTEX(fb
->Mutex
);
141 /* save the visual */
142 fb
->Visual
= *visual
;
144 /* Init read/draw renderbuffer state */
145 if (visual
->doubleBufferMode
) {
146 fb
->_NumColorDrawBuffers
= 1;
147 fb
->ColorDrawBuffer
[0] = GL_BACK
;
148 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_BACK_LEFT
;
149 fb
->ColorReadBuffer
= GL_BACK
;
150 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
153 fb
->_NumColorDrawBuffers
= 1;
154 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
155 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_FRONT_LEFT
;
156 fb
->ColorReadBuffer
= GL_FRONT
;
157 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
160 fb
->Delete
= _mesa_destroy_framebuffer
;
161 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
163 compute_depth_max(fb
);
168 * Deallocate buffer and everything attached to it.
169 * Typically called via the gl_framebuffer->Delete() method.
172 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
175 _mesa_free_framebuffer_data(fb
);
182 * Free all the data hanging off the given gl_framebuffer, but don't free
183 * the gl_framebuffer object itself.
186 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
191 assert(fb
->RefCount
== 0);
193 _glthread_DESTROY_MUTEX(fb
->Mutex
);
195 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
196 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
197 if (att
->Renderbuffer
) {
198 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
201 _mesa_reference_texobj(&att
->Texture
, NULL
);
203 ASSERT(!att
->Renderbuffer
);
204 ASSERT(!att
->Texture
);
208 /* unbind _Depth/_StencilBuffer to decr ref counts */
209 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, NULL
);
210 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, NULL
);
215 * Set *ptr to point to fb, with refcounting and locking.
218 _mesa_reference_framebuffer(struct gl_framebuffer
**ptr
,
219 struct gl_framebuffer
*fb
)
228 /* unreference old renderbuffer */
229 GLboolean deleteFlag
= GL_FALSE
;
230 struct gl_framebuffer
*oldFb
= *ptr
;
232 _glthread_LOCK_MUTEX(oldFb
->Mutex
);
233 ASSERT(oldFb
->RefCount
> 0);
235 deleteFlag
= (oldFb
->RefCount
== 0);
236 _glthread_UNLOCK_MUTEX(oldFb
->Mutex
);
239 oldFb
->Delete(oldFb
);
246 _glthread_LOCK_MUTEX(fb
->Mutex
);
248 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
255 * XXX this function is deprecated.
256 * Undo/remove a reference to a framebuffer object.
257 * Decrement the framebuffer object's reference count and delete it when
258 * the refcount hits zero.
259 * Note: we pass the address of a pointer and set it to NULL.
262 _mesa_unreference_framebuffer(struct gl_framebuffer
**fb
)
264 _mesa_reference_framebuffer(fb
, NULL
);
271 * Resize the given framebuffer's renderbuffers to the new width and height.
272 * This should only be used for window-system framebuffers, not
273 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
274 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
275 * from a device driver.
277 * \note it's possible for ctx to be null since a window can be resized
278 * without a currently bound rendering context.
281 _mesa_resize_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
282 GLuint width
, GLuint height
)
286 /* XXX I think we could check if the size is not changing
290 /* For window system framebuffers, Name is zero */
291 assert(fb
->Name
== 0);
293 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
294 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
295 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
296 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
297 /* only resize if size is changing */
298 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
299 /* could just as well pass rb->_ActualFormat here */
300 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
301 ASSERT(rb
->Width
== width
);
302 ASSERT(rb
->Height
== height
);
305 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
312 if (fb
->_DepthBuffer
) {
313 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
314 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
315 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
316 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
321 if (fb
->_StencilBuffer
) {
322 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
323 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
324 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
325 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
334 /* update scissor / window bounds */
335 _mesa_update_draw_buffer_bounds(ctx
);
336 /* Signal new buffer state so that swrast will update its clipping
337 * info (the CLIP_BIT flag).
339 ctx
->NewState
|= _NEW_BUFFERS
;
346 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
347 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
349 * GL_MESA_resize_buffers extension.
351 * When this function is called, we'll ask the window system how large
352 * the current window is. If it's a new size, we'll call the driver's
353 * ResizeBuffers function. The driver will then resize its color buffers
354 * as needed, and maybe call the swrast's routine for reallocating
355 * swrast-managed depth/stencil/accum/etc buffers.
356 * \note This function should only be called through the GL API, not
357 * from device drivers (as was done in the past).
360 _mesa_resizebuffers( GLcontext
*ctx
)
362 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
364 if (MESA_VERBOSE
& VERBOSE_API
)
365 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
367 if (!ctx
->Driver
.GetBufferSize
) {
371 if (ctx
->WinSysDrawBuffer
) {
372 GLuint newWidth
, newHeight
;
373 GLframebuffer
*buffer
= ctx
->WinSysDrawBuffer
;
375 assert(buffer
->Name
== 0);
377 /* ask device driver for size of output buffer */
378 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
380 /* see if size of device driver's color buffer (window) has changed */
381 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
382 if (ctx
->Driver
.ResizeBuffers
)
383 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
387 if (ctx
->WinSysReadBuffer
388 && ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
389 GLuint newWidth
, newHeight
;
390 GLframebuffer
*buffer
= ctx
->WinSysReadBuffer
;
392 assert(buffer
->Name
== 0);
394 /* ask device driver for size of read buffer */
395 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
397 /* see if size of device driver's color buffer (window) has changed */
398 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
399 if (ctx
->Driver
.ResizeBuffers
)
400 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
404 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
409 * XXX THIS IS OBSOLETE
412 _mesa_ResizeBuffersMESA( void )
414 GET_CURRENT_CONTEXT(ctx
);
416 if (ctx
->Extensions
.MESA_resize_buffers
)
417 _mesa_resizebuffers( ctx
);
423 * Examine all the framebuffer's renderbuffers to update the Width/Height
424 * fields of the framebuffer. If we have renderbuffers with different
425 * sizes, set the framebuffer's width and height to the min size.
426 * Note: this is only intended for user-created framebuffers, not
427 * window-system framebuffes.
430 update_framebuffer_size(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
432 GLuint minWidth
= ~0, minHeight
= ~0;
435 /* user-created framebuffers only */
438 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
439 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
440 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
442 minWidth
= MIN2(minWidth
, rb
->Width
);
443 minHeight
= MIN2(minHeight
, rb
->Height
);
447 if (minWidth
!= ~0) {
448 fb
->Width
= minWidth
;
449 fb
->Height
= minHeight
;
459 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
460 * These values are computed from the buffer's width and height and
461 * the scissor box, if it's enabled.
462 * \param ctx the GL context.
465 _mesa_update_draw_buffer_bounds(GLcontext
*ctx
)
467 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
473 /* user-created framebuffer size depends on the renderbuffers */
474 update_framebuffer_size(ctx
, buffer
);
479 buffer
->_Xmax
= buffer
->Width
;
480 buffer
->_Ymax
= buffer
->Height
;
482 if (ctx
->Scissor
.Enabled
) {
483 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
484 buffer
->_Xmin
= ctx
->Scissor
.X
;
486 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
487 buffer
->_Ymin
= ctx
->Scissor
.Y
;
489 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
490 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
492 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
493 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
495 /* finally, check for empty region */
496 if (buffer
->_Xmin
> buffer
->_Xmax
) {
497 buffer
->_Xmin
= buffer
->_Xmax
;
499 if (buffer
->_Ymin
> buffer
->_Ymax
) {
500 buffer
->_Ymin
= buffer
->_Ymax
;
504 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
505 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
510 * The glGet queries of the framebuffer red/green/blue size, stencil size,
511 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
512 * change depending on the renderbuffer bindings. This function updates
513 * the given framebuffer's Visual from the current renderbuffer bindings.
515 * This may apply to user-created framebuffers or window system framebuffers.
517 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
518 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
519 * The former one is used to convert floating point depth values into
523 _mesa_update_framebuffer_visual(struct gl_framebuffer
*fb
)
527 _mesa_bzero(&fb
->Visual
, sizeof(fb
->Visual
));
528 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
530 #if 0 /* this _might_ be needed */
531 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
532 /* leave visual fields zero'd */
537 /* find first RGB or CI renderbuffer */
538 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
539 if (fb
->Attachment
[i
].Renderbuffer
) {
540 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
541 if (rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
) {
542 fb
->Visual
.redBits
= rb
->RedBits
;
543 fb
->Visual
.greenBits
= rb
->GreenBits
;
544 fb
->Visual
.blueBits
= rb
->BlueBits
;
545 fb
->Visual
.alphaBits
= rb
->AlphaBits
;
546 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
547 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
548 fb
->Visual
.floatMode
= GL_FALSE
;
549 fb
->Visual
.samples
= rb
->NumSamples
;
552 else if (rb
->_BaseFormat
== GL_COLOR_INDEX
) {
553 fb
->Visual
.indexBits
= rb
->IndexBits
;
554 fb
->Visual
.rgbMode
= GL_FALSE
;
560 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
561 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
563 = fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
;
566 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
567 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
568 fb
->Visual
.stencilBits
569 = fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
;
572 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
573 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
574 fb
->Visual
.accumRedBits
575 = fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
->RedBits
;
576 fb
->Visual
.accumGreenBits
577 = fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
->GreenBits
;
578 fb
->Visual
.accumBlueBits
579 = fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
->BlueBits
;
580 fb
->Visual
.accumAlphaBits
581 = fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
->AlphaBits
;
584 compute_depth_max(fb
);
589 * Update the framebuffer's _DepthBuffer field using the renderbuffer
590 * found at the given attachment index.
592 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
593 * create and install a depth wrapper/adaptor.
595 * \param fb the framebuffer whose _DepthBuffer field to update
596 * \param attIndex indicates the renderbuffer to possibly wrap
599 _mesa_update_depth_buffer(GLcontext
*ctx
,
600 struct gl_framebuffer
*fb
,
603 struct gl_renderbuffer
*depthRb
;
605 /* only one possiblity for now */
606 ASSERT(attIndex
== BUFFER_DEPTH
);
608 depthRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
610 if (depthRb
&& depthRb
->_ActualFormat
== GL_DEPTH24_STENCIL8_EXT
) {
611 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
612 if (!fb
->_DepthBuffer
613 || fb
->_DepthBuffer
->Wrapped
!= depthRb
614 || fb
->_DepthBuffer
->_BaseFormat
!= GL_DEPTH_COMPONENT
) {
615 /* need to update wrapper */
616 struct gl_renderbuffer
*wrapper
617 = _mesa_new_z24_renderbuffer_wrapper(ctx
, depthRb
);
618 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, wrapper
);
619 ASSERT(fb
->_DepthBuffer
->Wrapped
== depthRb
);
623 /* depthRb may be null */
624 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, depthRb
);
630 * Update the framebuffer's _StencilBuffer field using the renderbuffer
631 * found at the given attachment index.
633 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
634 * create and install a stencil wrapper/adaptor.
636 * \param fb the framebuffer whose _StencilBuffer field to update
637 * \param attIndex indicates the renderbuffer to possibly wrap
640 _mesa_update_stencil_buffer(GLcontext
*ctx
,
641 struct gl_framebuffer
*fb
,
644 struct gl_renderbuffer
*stencilRb
;
646 ASSERT(attIndex
== BUFFER_DEPTH
||
647 attIndex
== BUFFER_STENCIL
);
649 stencilRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
651 if (stencilRb
&& stencilRb
->_ActualFormat
== GL_DEPTH24_STENCIL8_EXT
) {
652 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
653 if (!fb
->_StencilBuffer
654 || fb
->_StencilBuffer
->Wrapped
!= stencilRb
655 || fb
->_StencilBuffer
->_BaseFormat
!= GL_STENCIL_INDEX
) {
656 /* need to update wrapper */
657 struct gl_renderbuffer
*wrapper
658 = _mesa_new_s8_renderbuffer_wrapper(ctx
, stencilRb
);
659 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, wrapper
);
660 ASSERT(fb
->_StencilBuffer
->Wrapped
== stencilRb
);
664 /* stencilRb may be null */
665 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, stencilRb
);
671 * Example DrawBuffers scenarios:
673 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
674 * "gl_FragColor" or program writes to the "result.color" register:
676 * fragment color output renderbuffer
677 * --------------------- ---------------
678 * color[0] Front, Back
681 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
682 * gl_FragData[i] or program writes to result.color[i] registers:
684 * fragment color output renderbuffer
685 * --------------------- ---------------
691 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
692 * gl_FragColor, or fixed function:
694 * fragment color output renderbuffer
695 * --------------------- ---------------
696 * color[0] Front, Aux0, Aux1
699 * In either case, the list of renderbuffers is stored in the
700 * framebuffer->_ColorDrawBuffers[] array and
701 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
702 * The renderer (like swrast) has to look at the current fragment shader
703 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
704 * how to map color outputs to renderbuffers.
706 * Note that these two calls are equivalent (for fixed function fragment
708 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
709 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
716 * Update the (derived) list of color drawing renderbuffer pointers.
717 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
721 update_color_draw_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
725 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
726 fb
->_ColorDrawBuffers
[0] = NULL
;
728 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
729 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
731 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
734 fb
->_ColorDrawBuffers
[output
] = NULL
;
741 * Update the (derived) color read renderbuffer pointer.
742 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
745 update_color_read_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
748 if (fb
->_ColorReadBufferIndex
== -1 ||
752 fb
->_ColorReadBuffer
= NULL
; /* legal! */
755 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
756 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
758 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
764 * Update a gl_framebuffer's derived state.
766 * Specifically, update these framebuffer fields:
768 * _NumColorDrawBuffers
773 * If the framebuffer is user-created, make sure it's complete.
775 * The following functions (at least) can effect framebuffer state:
776 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
777 * glRenderbufferStorageEXT.
780 update_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
783 /* This is a window-system framebuffer */
784 /* Need to update the FB's GL_DRAW_BUFFER state to match the
785 * context state (GL_READ_BUFFER too).
787 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
788 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
,
789 ctx
->Color
.DrawBuffer
, NULL
);
791 if (fb
->ColorReadBuffer
!= ctx
->Pixel
.ReadBuffer
) {
796 /* This is a user-created framebuffer.
797 * Completeness only matters for user-created framebuffers.
799 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
800 _mesa_test_framebuffer_completeness(ctx
, fb
);
804 /* Strictly speaking, we don't need to update the draw-state
805 * if this FB is bound as ctx->ReadBuffer (and conversely, the
806 * read-state if this FB is bound as ctx->DrawBuffer), but no
809 update_color_draw_buffers(ctx
, fb
);
810 update_color_read_buffer(ctx
, fb
);
811 _mesa_update_depth_buffer(ctx
, fb
, BUFFER_DEPTH
);
812 _mesa_update_stencil_buffer(ctx
, fb
, BUFFER_STENCIL
);
814 compute_depth_max(fb
);
819 * Update state related to the current draw/read framebuffers.
822 _mesa_update_framebuffer(GLcontext
*ctx
)
824 struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
825 struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
827 update_framebuffer(ctx
, drawFb
);
828 if (readFb
!= drawFb
)
829 update_framebuffer(ctx
, readFb
);
834 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
835 * glCopyTex[Sub]Image, etc. exists.
836 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
837 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
838 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
841 _mesa_source_buffer_exists(GLcontext
*ctx
, GLenum format
)
843 const struct gl_renderbuffer_attachment
*att
844 = ctx
->ReadBuffer
->Attachment
;
846 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
857 case GL_LUMINANCE_ALPHA
:
865 if (ctx
->ReadBuffer
->_ColorReadBuffer
== NULL
) {
868 /* XXX enable this post 6.5 release:
869 ASSERT(ctx->ReadBuffer->_ColorReadBuffer->RedBits > 0 ||
870 ctx->ReadBuffer->_ColorReadBuffer->IndexBits > 0);
874 case GL_DEPTH_COMPONENT
:
875 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
878 ASSERT(att
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
> 0);
881 case GL_STENCIL_INDEX
:
882 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
885 ASSERT(att
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
> 0);
887 case GL_DEPTH_STENCIL_EXT
:
888 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
889 !att
[BUFFER_STENCIL
].Renderbuffer
) {
892 ASSERT(att
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
> 0);
893 ASSERT(att
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
> 0);
897 "Unexpected format 0x%x in _mesa_source_buffer_exists",
908 * As above, but for drawing operations.
909 * XXX code do some code merging w/ above function.
912 _mesa_dest_buffer_exists(GLcontext
*ctx
, GLenum format
)
914 const struct gl_renderbuffer_attachment
*att
915 = ctx
->ReadBuffer
->Attachment
;
917 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
928 case GL_LUMINANCE_ALPHA
:
936 /* nothing special */
937 /* Could assert that colorbuffer has RedBits > 0 */
940 case GL_DEPTH_COMPONENT
:
941 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
944 ASSERT(att
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
> 0);
947 case GL_STENCIL_INDEX
:
948 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
951 ASSERT(att
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
> 0);
953 case GL_DEPTH_STENCIL_EXT
:
954 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
955 !att
[BUFFER_STENCIL
].Renderbuffer
) {
958 ASSERT(att
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
> 0);
959 ASSERT(att
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
> 0);
963 "Unexpected format 0x%x in _mesa_source_buffer_exists",