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"
42 #include "framebuffer.h"
43 #include "renderbuffer.h"
49 * Compute/set the _DepthMax field for the given framebuffer.
50 * This value depends on the Z buffer resolution.
53 compute_depth_max(struct gl_framebuffer
*fb
)
55 if (fb
->Visual
.depthBits
== 0) {
56 /* Special case. Even if we don't have a depth buffer we need
57 * good values for DepthMax for Z vertex transformation purposes
58 * and for per-fragment fog computation.
60 fb
->_DepthMax
= (1 << 16) - 1;
62 else if (fb
->Visual
.depthBits
< 32) {
63 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
66 /* Special case since shift values greater than or equal to the
67 * number of bits in the left hand expression's type are undefined.
69 fb
->_DepthMax
= 0xffffffff;
71 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
73 /* Minimum resolvable depth value, for polygon offset */
74 fb
->_MRD
= (GLfloat
)1.0 / fb
->_DepthMaxF
;
79 * Create and initialize a gl_framebuffer object.
80 * This is intended for creating _window_system_ framebuffers, not generic
81 * framebuffer objects ala GL_EXT_framebuffer_object.
83 * \sa _mesa_new_framebuffer
85 struct gl_framebuffer
*
86 _mesa_create_framebuffer(const GLvisual
*visual
)
88 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
91 _mesa_initialize_framebuffer(fb
, visual
);
98 * Allocate a new gl_framebuffer object.
99 * This is the default function for ctx->Driver.NewFramebuffer().
100 * This is for allocating user-created framebuffers, not window-system
102 * \sa _mesa_create_framebuffer
104 struct gl_framebuffer
*
105 _mesa_new_framebuffer(GLcontext
*ctx
, GLuint name
)
107 struct gl_framebuffer
*fb
;
110 fb
= CALLOC_STRUCT(gl_framebuffer
);
114 fb
->_NumColorDrawBuffers
= 1;
115 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
116 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
117 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
118 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
119 fb
->Delete
= _mesa_destroy_framebuffer
;
120 _glthread_INIT_MUTEX(fb
->Mutex
);
127 * Initialize a gl_framebuffer object. Typically used to initialize
128 * window system-created framebuffers, not user-created framebuffers.
129 * \sa _mesa_create_framebuffer
132 _mesa_initialize_framebuffer(struct gl_framebuffer
*fb
, const GLvisual
*visual
)
137 _mesa_bzero(fb
, sizeof(struct gl_framebuffer
));
139 _glthread_INIT_MUTEX(fb
->Mutex
);
143 /* save the visual */
144 fb
->Visual
= *visual
;
146 /* Init read/draw renderbuffer state */
147 if (visual
->doubleBufferMode
) {
148 fb
->_NumColorDrawBuffers
= 1;
149 fb
->ColorDrawBuffer
[0] = GL_BACK
;
150 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_BACK_LEFT
;
151 fb
->ColorReadBuffer
= GL_BACK
;
152 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
155 fb
->_NumColorDrawBuffers
= 1;
156 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
157 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_FRONT_LEFT
;
158 fb
->ColorReadBuffer
= GL_FRONT
;
159 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
162 fb
->Delete
= _mesa_destroy_framebuffer
;
163 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
165 compute_depth_max(fb
);
170 * Deallocate buffer and everything attached to it.
171 * Typically called via the gl_framebuffer->Delete() method.
174 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
177 _mesa_free_framebuffer_data(fb
);
184 * Free all the data hanging off the given gl_framebuffer, but don't free
185 * the gl_framebuffer object itself.
188 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
193 assert(fb
->RefCount
== 0);
195 _glthread_DESTROY_MUTEX(fb
->Mutex
);
197 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
198 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
199 if (att
->Renderbuffer
) {
200 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
203 _mesa_reference_texobj(&att
->Texture
, NULL
);
205 ASSERT(!att
->Renderbuffer
);
206 ASSERT(!att
->Texture
);
210 /* unbind _Depth/_StencilBuffer to decr ref counts */
211 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, NULL
);
212 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, NULL
);
217 * Set *ptr to point to fb, with refcounting and locking.
220 _mesa_reference_framebuffer(struct gl_framebuffer
**ptr
,
221 struct gl_framebuffer
*fb
)
230 /* unreference old renderbuffer */
231 GLboolean deleteFlag
= GL_FALSE
;
232 struct gl_framebuffer
*oldFb
= *ptr
;
234 _glthread_LOCK_MUTEX(oldFb
->Mutex
);
235 ASSERT(oldFb
->RefCount
> 0);
237 deleteFlag
= (oldFb
->RefCount
== 0);
238 _glthread_UNLOCK_MUTEX(oldFb
->Mutex
);
241 oldFb
->Delete(oldFb
);
248 _glthread_LOCK_MUTEX(fb
->Mutex
);
250 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
257 * Resize the given framebuffer's renderbuffers to the new width and height.
258 * This should only be used for window-system framebuffers, not
259 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
260 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
261 * from a device driver.
263 * \note it's possible for ctx to be null since a window can be resized
264 * without a currently bound rendering context.
267 _mesa_resize_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
268 GLuint width
, GLuint height
)
272 /* XXX I think we could check if the size is not changing
276 /* For window system framebuffers, Name is zero */
277 assert(fb
->Name
== 0);
279 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
280 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
281 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
282 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
283 /* only resize if size is changing */
284 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
285 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
286 ASSERT(rb
->Width
== width
);
287 ASSERT(rb
->Height
== height
);
290 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
297 if (fb
->_DepthBuffer
) {
298 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
299 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
300 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
301 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
306 if (fb
->_StencilBuffer
) {
307 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
308 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
309 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
310 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
319 /* update scissor / window bounds */
320 _mesa_update_draw_buffer_bounds(ctx
);
321 /* Signal new buffer state so that swrast will update its clipping
322 * info (the CLIP_BIT flag).
324 ctx
->NewState
|= _NEW_BUFFERS
;
331 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
332 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
334 * GL_MESA_resize_buffers extension.
336 * When this function is called, we'll ask the window system how large
337 * the current window is. If it's a new size, we'll call the driver's
338 * ResizeBuffers function. The driver will then resize its color buffers
339 * as needed, and maybe call the swrast's routine for reallocating
340 * swrast-managed depth/stencil/accum/etc buffers.
341 * \note This function should only be called through the GL API, not
342 * from device drivers (as was done in the past).
345 _mesa_resizebuffers( GLcontext
*ctx
)
347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
349 if (MESA_VERBOSE
& VERBOSE_API
)
350 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
352 if (!ctx
->Driver
.GetBufferSize
) {
356 if (ctx
->WinSysDrawBuffer
) {
357 GLuint newWidth
, newHeight
;
358 GLframebuffer
*buffer
= ctx
->WinSysDrawBuffer
;
360 assert(buffer
->Name
== 0);
362 /* ask device driver for size of output buffer */
363 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
365 /* see if size of device driver's color buffer (window) has changed */
366 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
367 if (ctx
->Driver
.ResizeBuffers
)
368 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
372 if (ctx
->WinSysReadBuffer
373 && ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
374 GLuint newWidth
, newHeight
;
375 GLframebuffer
*buffer
= ctx
->WinSysReadBuffer
;
377 assert(buffer
->Name
== 0);
379 /* ask device driver for size of read buffer */
380 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
382 /* see if size of device driver's color buffer (window) has changed */
383 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
384 if (ctx
->Driver
.ResizeBuffers
)
385 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
389 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
394 * XXX THIS IS OBSOLETE
397 _mesa_ResizeBuffersMESA( void )
399 GET_CURRENT_CONTEXT(ctx
);
401 if (ctx
->Extensions
.MESA_resize_buffers
)
402 _mesa_resizebuffers( ctx
);
408 * Examine all the framebuffer's renderbuffers to update the Width/Height
409 * fields of the framebuffer. If we have renderbuffers with different
410 * sizes, set the framebuffer's width and height to the min size.
411 * Note: this is only intended for user-created framebuffers, not
412 * window-system framebuffes.
415 update_framebuffer_size(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
417 GLuint minWidth
= ~0, minHeight
= ~0;
420 /* user-created framebuffers only */
423 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
424 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
425 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
427 minWidth
= MIN2(minWidth
, rb
->Width
);
428 minHeight
= MIN2(minHeight
, rb
->Height
);
432 if (minWidth
!= ~0) {
433 fb
->Width
= minWidth
;
434 fb
->Height
= minHeight
;
444 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
445 * These values are computed from the buffer's width and height and
446 * the scissor box, if it's enabled.
447 * \param ctx the GL context.
450 _mesa_update_draw_buffer_bounds(GLcontext
*ctx
)
452 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
458 /* user-created framebuffer size depends on the renderbuffers */
459 update_framebuffer_size(ctx
, buffer
);
464 buffer
->_Xmax
= buffer
->Width
;
465 buffer
->_Ymax
= buffer
->Height
;
467 if (ctx
->Scissor
.Enabled
) {
468 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
469 buffer
->_Xmin
= ctx
->Scissor
.X
;
471 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
472 buffer
->_Ymin
= ctx
->Scissor
.Y
;
474 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
475 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
477 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
478 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
480 /* finally, check for empty region */
481 if (buffer
->_Xmin
> buffer
->_Xmax
) {
482 buffer
->_Xmin
= buffer
->_Xmax
;
484 if (buffer
->_Ymin
> buffer
->_Ymax
) {
485 buffer
->_Ymin
= buffer
->_Ymax
;
489 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
490 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
495 * The glGet queries of the framebuffer red/green/blue size, stencil size,
496 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
497 * change depending on the renderbuffer bindings. This function updates
498 * the given framebuffer's Visual from the current renderbuffer bindings.
500 * This may apply to user-created framebuffers or window system framebuffers.
502 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
503 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
504 * The former one is used to convert floating point depth values into
508 _mesa_update_framebuffer_visual(struct gl_framebuffer
*fb
)
512 _mesa_bzero(&fb
->Visual
, sizeof(fb
->Visual
));
513 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
515 #if 0 /* this _might_ be needed */
516 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
517 /* leave visual fields zero'd */
522 /* find first RGB or CI renderbuffer */
523 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
524 if (fb
->Attachment
[i
].Renderbuffer
) {
525 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
526 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
527 const gl_format fmt
= rb
->Format
;
529 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_RGB
) {
530 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
531 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
532 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
533 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
534 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
535 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
536 fb
->Visual
.floatMode
= GL_FALSE
;
537 fb
->Visual
.samples
= rb
->NumSamples
;
540 else if (baseFormat
== GL_COLOR_INDEX
) {
541 fb
->Visual
.indexBits
= _mesa_get_format_bits(fmt
, GL_INDEX_BITS
);
542 fb
->Visual
.rgbMode
= GL_FALSE
;
548 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
549 const struct gl_renderbuffer
*rb
=
550 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
551 const gl_format fmt
= rb
->Format
;
552 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
553 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
556 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
557 const struct gl_renderbuffer
*rb
=
558 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
559 const gl_format fmt
= rb
->Format
;
560 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
561 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
564 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
565 const struct gl_renderbuffer
*rb
=
566 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
567 const gl_format fmt
= rb
->Format
;
568 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
569 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
570 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
571 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
572 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
575 compute_depth_max(fb
);
580 * Update the framebuffer's _DepthBuffer field using the renderbuffer
581 * found at the given attachment index.
583 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
584 * create and install a depth wrapper/adaptor.
586 * \param fb the framebuffer whose _DepthBuffer field to update
587 * \param attIndex indicates the renderbuffer to possibly wrap
590 _mesa_update_depth_buffer(GLcontext
*ctx
,
591 struct gl_framebuffer
*fb
,
594 struct gl_renderbuffer
*depthRb
;
596 /* only one possiblity for now */
597 ASSERT(attIndex
== BUFFER_DEPTH
);
599 depthRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
601 if (depthRb
&& depthRb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
602 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
603 if (!fb
->_DepthBuffer
604 || fb
->_DepthBuffer
->Wrapped
!= depthRb
605 || _mesa_get_format_base_format(fb
->_DepthBuffer
->Format
) != GL_DEPTH_COMPONENT
) {
606 /* need to update wrapper */
607 struct gl_renderbuffer
*wrapper
608 = _mesa_new_z24_renderbuffer_wrapper(ctx
, depthRb
);
609 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, wrapper
);
610 ASSERT(fb
->_DepthBuffer
->Wrapped
== depthRb
);
614 /* depthRb may be null */
615 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, depthRb
);
621 * Update the framebuffer's _StencilBuffer field using the renderbuffer
622 * found at the given attachment index.
624 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
625 * create and install a stencil wrapper/adaptor.
627 * \param fb the framebuffer whose _StencilBuffer field to update
628 * \param attIndex indicates the renderbuffer to possibly wrap
631 _mesa_update_stencil_buffer(GLcontext
*ctx
,
632 struct gl_framebuffer
*fb
,
635 struct gl_renderbuffer
*stencilRb
;
637 ASSERT(attIndex
== BUFFER_DEPTH
||
638 attIndex
== BUFFER_STENCIL
);
640 stencilRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
642 if (stencilRb
&& stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
643 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
644 if (!fb
->_StencilBuffer
645 || fb
->_StencilBuffer
->Wrapped
!= stencilRb
646 || _mesa_get_format_base_format(fb
->_StencilBuffer
->Format
) != GL_STENCIL_INDEX
) {
647 /* need to update wrapper */
648 struct gl_renderbuffer
*wrapper
649 = _mesa_new_s8_renderbuffer_wrapper(ctx
, stencilRb
);
650 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, wrapper
);
651 ASSERT(fb
->_StencilBuffer
->Wrapped
== stencilRb
);
655 /* stencilRb may be null */
656 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, stencilRb
);
662 * Example DrawBuffers scenarios:
664 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
665 * "gl_FragColor" or program writes to the "result.color" register:
667 * fragment color output renderbuffer
668 * --------------------- ---------------
669 * color[0] Front, Back
672 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
673 * gl_FragData[i] or program writes to result.color[i] registers:
675 * fragment color output renderbuffer
676 * --------------------- ---------------
682 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
683 * gl_FragColor, or fixed function:
685 * fragment color output renderbuffer
686 * --------------------- ---------------
687 * color[0] Front, Aux0, Aux1
690 * In either case, the list of renderbuffers is stored in the
691 * framebuffer->_ColorDrawBuffers[] array and
692 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
693 * The renderer (like swrast) has to look at the current fragment shader
694 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
695 * how to map color outputs to renderbuffers.
697 * Note that these two calls are equivalent (for fixed function fragment
699 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
700 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
707 * Update the (derived) list of color drawing renderbuffer pointers.
708 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
712 update_color_draw_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
716 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
717 fb
->_ColorDrawBuffers
[0] = NULL
;
719 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
720 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
722 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
725 fb
->_ColorDrawBuffers
[output
] = NULL
;
732 * Update the (derived) color read renderbuffer pointer.
733 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
736 update_color_read_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
739 if (fb
->_ColorReadBufferIndex
== -1 ||
743 fb
->_ColorReadBuffer
= NULL
; /* legal! */
746 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
747 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
749 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
755 * Update a gl_framebuffer's derived state.
757 * Specifically, update these framebuffer fields:
759 * _NumColorDrawBuffers
764 * If the framebuffer is user-created, make sure it's complete.
766 * The following functions (at least) can effect framebuffer state:
767 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
768 * glRenderbufferStorageEXT.
771 update_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
774 /* This is a window-system framebuffer */
775 /* Need to update the FB's GL_DRAW_BUFFER state to match the
776 * context state (GL_READ_BUFFER too).
778 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
779 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
,
780 ctx
->Color
.DrawBuffer
, NULL
);
782 if (fb
->ColorReadBuffer
!= ctx
->Pixel
.ReadBuffer
) {
787 /* This is a user-created framebuffer.
788 * Completeness only matters for user-created framebuffers.
790 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
791 _mesa_test_framebuffer_completeness(ctx
, fb
);
795 /* Strictly speaking, we don't need to update the draw-state
796 * if this FB is bound as ctx->ReadBuffer (and conversely, the
797 * read-state if this FB is bound as ctx->DrawBuffer), but no
800 update_color_draw_buffers(ctx
, fb
);
801 update_color_read_buffer(ctx
, fb
);
802 _mesa_update_depth_buffer(ctx
, fb
, BUFFER_DEPTH
);
803 _mesa_update_stencil_buffer(ctx
, fb
, BUFFER_STENCIL
);
805 compute_depth_max(fb
);
810 * Update state related to the current draw/read framebuffers.
813 _mesa_update_framebuffer(GLcontext
*ctx
)
815 struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
816 struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
818 update_framebuffer(ctx
, drawFb
);
819 if (readFb
!= drawFb
)
820 update_framebuffer(ctx
, readFb
);
825 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
826 * glCopyTex[Sub]Image, etc) exists.
827 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
828 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
829 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
832 _mesa_source_buffer_exists(GLcontext
*ctx
, GLenum format
)
834 const struct gl_renderbuffer_attachment
*att
= ctx
->ReadBuffer
->Attachment
;
836 /* If we don't know the framebuffer status, update it now */
837 if (ctx
->ReadBuffer
->_Status
== 0) {
838 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
841 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
852 case GL_LUMINANCE_ALPHA
:
860 if (ctx
->ReadBuffer
->_ColorReadBuffer
== NULL
) {
863 ASSERT(_mesa_get_format_bits(ctx
->ReadBuffer
->_ColorReadBuffer
->Format
, GL_RED_BITS
) > 0 ||
864 _mesa_get_format_bits(ctx
->ReadBuffer
->_ColorReadBuffer
->Format
, GL_INDEX_BITS
) > 0);
867 case GL_DEPTH_COMPONENT
:
868 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
871 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
874 case GL_STENCIL_INDEX
:
875 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
878 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
880 case GL_DEPTH_STENCIL_EXT
:
881 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
882 !att
[BUFFER_STENCIL
].Renderbuffer
) {
886 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
887 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
892 "Unexpected format 0x%x in _mesa_source_buffer_exists",
903 * As above, but for drawing operations.
904 * XXX could do some code merging w/ above function.
907 _mesa_dest_buffer_exists(GLcontext
*ctx
, GLenum format
)
909 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
911 /* If we don't know the framebuffer status, update it now */
912 if (ctx
->DrawBuffer
->_Status
== 0) {
913 _mesa_test_framebuffer_completeness(ctx
, ctx
->DrawBuffer
);
916 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
927 case GL_LUMINANCE_ALPHA
:
935 /* Nothing special since GL_DRAW_BUFFER could be GL_NONE. */
936 /* Could assert that colorbuffer has RedBits > 0 */
939 case GL_DEPTH_COMPONENT
:
940 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
943 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
946 case GL_STENCIL_INDEX
:
947 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
950 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
952 case GL_DEPTH_STENCIL_EXT
:
953 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
954 !att
[BUFFER_STENCIL
].Renderbuffer
) {
958 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
959 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
964 "Unexpected format 0x%x in _mesa_dest_buffer_exists",
974 _mesa_get_color_read_format(GLcontext
*ctx
)
976 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
977 case MESA_FORMAT_ARGB8888
:
979 case MESA_FORMAT_RGB565
:
987 _mesa_get_color_read_type(GLcontext
*ctx
)
989 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
990 case MESA_FORMAT_ARGB8888
:
991 return GL_UNSIGNED_BYTE
;
992 case MESA_FORMAT_RGB565
:
993 return GL_UNSIGNED_SHORT_5_6_5_REV
;
995 return GL_UNSIGNED_BYTE
;