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_window_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
);
112 _mesa_initialize_user_framebuffer(fb
, name
);
119 * Initialize a gl_framebuffer object. Typically used to initialize
120 * window system-created framebuffers, not user-created framebuffers.
121 * \sa _mesa_initialize_user_framebuffer
124 _mesa_initialize_window_framebuffer(struct gl_framebuffer
*fb
,
125 const GLvisual
*visual
)
130 memset(fb
, 0, sizeof(struct gl_framebuffer
));
132 _glthread_INIT_MUTEX(fb
->Mutex
);
136 /* save the visual */
137 fb
->Visual
= *visual
;
139 /* Init read/draw renderbuffer state */
140 if (visual
->doubleBufferMode
) {
141 fb
->_NumColorDrawBuffers
= 1;
142 fb
->ColorDrawBuffer
[0] = GL_BACK
;
143 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_BACK_LEFT
;
144 fb
->ColorReadBuffer
= GL_BACK
;
145 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
148 fb
->_NumColorDrawBuffers
= 1;
149 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
150 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_FRONT_LEFT
;
151 fb
->ColorReadBuffer
= GL_FRONT
;
152 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
155 fb
->Delete
= _mesa_destroy_framebuffer
;
156 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
158 compute_depth_max(fb
);
163 * Initialize a user-created gl_framebuffer object.
164 * \sa _mesa_initialize_window_framebuffer
167 _mesa_initialize_user_framebuffer(struct gl_framebuffer
*fb
, GLuint name
)
172 memset(fb
, 0, sizeof(struct gl_framebuffer
));
176 fb
->_NumColorDrawBuffers
= 1;
177 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
178 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
179 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
180 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
181 fb
->Delete
= _mesa_destroy_framebuffer
;
182 _glthread_INIT_MUTEX(fb
->Mutex
);
187 * Deallocate buffer and everything attached to it.
188 * Typically called via the gl_framebuffer->Delete() method.
191 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
194 _mesa_free_framebuffer_data(fb
);
201 * Free all the data hanging off the given gl_framebuffer, but don't free
202 * the gl_framebuffer object itself.
205 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
210 assert(fb
->RefCount
== 0);
212 _glthread_DESTROY_MUTEX(fb
->Mutex
);
214 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
215 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
216 if (att
->Renderbuffer
) {
217 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
220 _mesa_reference_texobj(&att
->Texture
, NULL
);
222 ASSERT(!att
->Renderbuffer
);
223 ASSERT(!att
->Texture
);
227 /* unbind _Depth/_StencilBuffer to decr ref counts */
228 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, NULL
);
229 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, NULL
);
234 * Set *ptr to point to fb, with refcounting and locking.
237 _mesa_reference_framebuffer(struct gl_framebuffer
**ptr
,
238 struct gl_framebuffer
*fb
)
247 /* unreference old renderbuffer */
248 GLboolean deleteFlag
= GL_FALSE
;
249 struct gl_framebuffer
*oldFb
= *ptr
;
251 _glthread_LOCK_MUTEX(oldFb
->Mutex
);
252 ASSERT(oldFb
->RefCount
> 0);
254 deleteFlag
= (oldFb
->RefCount
== 0);
255 _glthread_UNLOCK_MUTEX(oldFb
->Mutex
);
258 oldFb
->Delete(oldFb
);
265 _glthread_LOCK_MUTEX(fb
->Mutex
);
267 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
274 * Resize the given framebuffer's renderbuffers to the new width and height.
275 * This should only be used for window-system framebuffers, not
276 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
277 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
278 * from a device driver.
280 * \note it's possible for ctx to be null since a window can be resized
281 * without a currently bound rendering context.
284 _mesa_resize_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
285 GLuint width
, GLuint height
)
289 /* XXX I think we could check if the size is not changing
293 /* For window system framebuffers, Name is zero */
294 assert(fb
->Name
== 0);
296 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
297 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
298 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
299 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
300 /* only resize if size is changing */
301 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
302 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
303 ASSERT(rb
->Width
== width
);
304 ASSERT(rb
->Height
== height
);
307 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
314 if (fb
->_DepthBuffer
) {
315 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
316 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
317 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
318 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
323 if (fb
->_StencilBuffer
) {
324 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
325 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
326 if (!rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
327 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
336 /* update scissor / window bounds */
337 _mesa_update_draw_buffer_bounds(ctx
);
338 /* Signal new buffer state so that swrast will update its clipping
339 * info (the CLIP_BIT flag).
341 ctx
->NewState
|= _NEW_BUFFERS
;
348 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
349 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
351 * GL_MESA_resize_buffers extension.
353 * When this function is called, we'll ask the window system how large
354 * the current window is. If it's a new size, we'll call the driver's
355 * ResizeBuffers function. The driver will then resize its color buffers
356 * as needed, and maybe call the swrast's routine for reallocating
357 * swrast-managed depth/stencil/accum/etc buffers.
358 * \note This function should only be called through the GL API, not
359 * from device drivers (as was done in the past).
362 _mesa_resizebuffers( GLcontext
*ctx
)
364 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
366 if (MESA_VERBOSE
& VERBOSE_API
)
367 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
369 if (!ctx
->Driver
.GetBufferSize
) {
373 if (ctx
->WinSysDrawBuffer
) {
374 GLuint newWidth
, newHeight
;
375 GLframebuffer
*buffer
= ctx
->WinSysDrawBuffer
;
377 assert(buffer
->Name
== 0);
379 /* ask device driver for size of output 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 if (ctx
->WinSysReadBuffer
390 && ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
391 GLuint newWidth
, newHeight
;
392 GLframebuffer
*buffer
= ctx
->WinSysReadBuffer
;
394 assert(buffer
->Name
== 0);
396 /* ask device driver for size of read buffer */
397 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
399 /* see if size of device driver's color buffer (window) has changed */
400 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
401 if (ctx
->Driver
.ResizeBuffers
)
402 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
406 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
411 * XXX THIS IS OBSOLETE
414 _mesa_ResizeBuffersMESA( void )
416 GET_CURRENT_CONTEXT(ctx
);
418 if (ctx
->Extensions
.MESA_resize_buffers
)
419 _mesa_resizebuffers( ctx
);
425 * Examine all the framebuffer's renderbuffers to update the Width/Height
426 * fields of the framebuffer. If we have renderbuffers with different
427 * sizes, set the framebuffer's width and height to the min size.
428 * Note: this is only intended for user-created framebuffers, not
429 * window-system framebuffes.
432 update_framebuffer_size(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
434 GLuint minWidth
= ~0, minHeight
= ~0;
437 /* user-created framebuffers only */
440 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
441 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
442 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
444 minWidth
= MIN2(minWidth
, rb
->Width
);
445 minHeight
= MIN2(minHeight
, rb
->Height
);
449 if (minWidth
!= ~0) {
450 fb
->Width
= minWidth
;
451 fb
->Height
= minHeight
;
461 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
462 * These values are computed from the buffer's width and height and
463 * the scissor box, if it's enabled.
464 * \param ctx the GL context.
467 _mesa_update_draw_buffer_bounds(GLcontext
*ctx
)
469 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
475 /* user-created framebuffer size depends on the renderbuffers */
476 update_framebuffer_size(ctx
, buffer
);
481 buffer
->_Xmax
= buffer
->Width
;
482 buffer
->_Ymax
= buffer
->Height
;
484 if (ctx
->Scissor
.Enabled
) {
485 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
486 buffer
->_Xmin
= ctx
->Scissor
.X
;
488 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
489 buffer
->_Ymin
= ctx
->Scissor
.Y
;
491 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
492 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
494 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
495 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
497 /* finally, check for empty region */
498 if (buffer
->_Xmin
> buffer
->_Xmax
) {
499 buffer
->_Xmin
= buffer
->_Xmax
;
501 if (buffer
->_Ymin
> buffer
->_Ymax
) {
502 buffer
->_Ymin
= buffer
->_Ymax
;
506 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
507 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
512 * The glGet queries of the framebuffer red/green/blue size, stencil size,
513 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
514 * change depending on the renderbuffer bindings. This function updates
515 * the given framebuffer's Visual from the current renderbuffer bindings.
517 * This may apply to user-created framebuffers or window system framebuffers.
519 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
520 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
521 * The former one is used to convert floating point depth values into
525 _mesa_update_framebuffer_visual(struct gl_framebuffer
*fb
)
529 memset(&fb
->Visual
, 0, sizeof(fb
->Visual
));
530 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
532 #if 0 /* this _might_ be needed */
533 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
534 /* leave visual fields zero'd */
539 /* find first RGB renderbuffer */
540 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
541 if (fb
->Attachment
[i
].Renderbuffer
) {
542 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
543 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
544 const gl_format fmt
= rb
->Format
;
546 if (baseFormat
== GL_RGBA
|| baseFormat
== GL_RGB
) {
547 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
548 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
549 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
550 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
551 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
552 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
553 fb
->Visual
.floatMode
= GL_FALSE
;
554 fb
->Visual
.samples
= rb
->NumSamples
;
560 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
561 const struct gl_renderbuffer
*rb
=
562 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
563 const gl_format fmt
= rb
->Format
;
564 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
565 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
568 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
569 const struct gl_renderbuffer
*rb
=
570 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
571 const gl_format fmt
= rb
->Format
;
572 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
573 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
576 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
577 const struct gl_renderbuffer
*rb
=
578 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
579 const gl_format fmt
= rb
->Format
;
580 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
581 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
582 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
583 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
584 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
587 compute_depth_max(fb
);
592 * Update the framebuffer's _DepthBuffer field using the renderbuffer
593 * found at the given attachment index.
595 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
596 * create and install a depth wrapper/adaptor.
598 * \param fb the framebuffer whose _DepthBuffer field to update
599 * \param attIndex indicates the renderbuffer to possibly wrap
602 _mesa_update_depth_buffer(GLcontext
*ctx
,
603 struct gl_framebuffer
*fb
,
606 struct gl_renderbuffer
*depthRb
;
608 /* only one possiblity for now */
609 ASSERT(attIndex
== BUFFER_DEPTH
);
611 depthRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
613 if (depthRb
&& depthRb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
614 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
615 if (!fb
->_DepthBuffer
616 || fb
->_DepthBuffer
->Wrapped
!= depthRb
617 || _mesa_get_format_base_format(fb
->_DepthBuffer
->Format
) != GL_DEPTH_COMPONENT
) {
618 /* need to update wrapper */
619 struct gl_renderbuffer
*wrapper
620 = _mesa_new_z24_renderbuffer_wrapper(ctx
, depthRb
);
621 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, wrapper
);
622 ASSERT(fb
->_DepthBuffer
->Wrapped
== depthRb
);
626 /* depthRb may be null */
627 _mesa_reference_renderbuffer(&fb
->_DepthBuffer
, depthRb
);
633 * Update the framebuffer's _StencilBuffer field using the renderbuffer
634 * found at the given attachment index.
636 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
637 * create and install a stencil wrapper/adaptor.
639 * \param fb the framebuffer whose _StencilBuffer field to update
640 * \param attIndex indicates the renderbuffer to possibly wrap
643 _mesa_update_stencil_buffer(GLcontext
*ctx
,
644 struct gl_framebuffer
*fb
,
647 struct gl_renderbuffer
*stencilRb
;
649 ASSERT(attIndex
== BUFFER_DEPTH
||
650 attIndex
== BUFFER_STENCIL
);
652 stencilRb
= fb
->Attachment
[attIndex
].Renderbuffer
;
654 if (stencilRb
&& stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
655 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
656 if (!fb
->_StencilBuffer
657 || fb
->_StencilBuffer
->Wrapped
!= stencilRb
658 || _mesa_get_format_base_format(fb
->_StencilBuffer
->Format
) != GL_STENCIL_INDEX
) {
659 /* need to update wrapper */
660 struct gl_renderbuffer
*wrapper
661 = _mesa_new_s8_renderbuffer_wrapper(ctx
, stencilRb
);
662 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, wrapper
);
663 ASSERT(fb
->_StencilBuffer
->Wrapped
== stencilRb
);
667 /* stencilRb may be null */
668 _mesa_reference_renderbuffer(&fb
->_StencilBuffer
, stencilRb
);
674 * Example DrawBuffers scenarios:
676 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
677 * "gl_FragColor" or program writes to the "result.color" register:
679 * fragment color output renderbuffer
680 * --------------------- ---------------
681 * color[0] Front, Back
684 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
685 * gl_FragData[i] or program writes to result.color[i] registers:
687 * fragment color output renderbuffer
688 * --------------------- ---------------
694 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
695 * gl_FragColor, or fixed function:
697 * fragment color output renderbuffer
698 * --------------------- ---------------
699 * color[0] Front, Aux0, Aux1
702 * In either case, the list of renderbuffers is stored in the
703 * framebuffer->_ColorDrawBuffers[] array and
704 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
705 * The renderer (like swrast) has to look at the current fragment shader
706 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
707 * how to map color outputs to renderbuffers.
709 * Note that these two calls are equivalent (for fixed function fragment
711 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
712 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
719 * Update the (derived) list of color drawing renderbuffer pointers.
720 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
724 update_color_draw_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
728 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
729 fb
->_ColorDrawBuffers
[0] = NULL
;
731 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
732 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
734 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
737 fb
->_ColorDrawBuffers
[output
] = NULL
;
744 * Update the (derived) color read renderbuffer pointer.
745 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
748 update_color_read_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
751 if (fb
->_ColorReadBufferIndex
== -1 ||
755 fb
->_ColorReadBuffer
= NULL
; /* legal! */
758 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
759 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
761 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
767 * Update a gl_framebuffer's derived state.
769 * Specifically, update these framebuffer fields:
771 * _NumColorDrawBuffers
776 * If the framebuffer is user-created, make sure it's complete.
778 * The following functions (at least) can effect framebuffer state:
779 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
780 * glRenderbufferStorageEXT.
783 update_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
786 /* This is a window-system framebuffer */
787 /* Need to update the FB's GL_DRAW_BUFFER state to match the
788 * context state (GL_READ_BUFFER too).
790 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
791 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
,
792 ctx
->Color
.DrawBuffer
, NULL
);
794 if (fb
->ColorReadBuffer
!= ctx
->Pixel
.ReadBuffer
) {
799 /* This is a user-created framebuffer.
800 * Completeness only matters for user-created framebuffers.
802 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
803 _mesa_test_framebuffer_completeness(ctx
, fb
);
807 /* Strictly speaking, we don't need to update the draw-state
808 * if this FB is bound as ctx->ReadBuffer (and conversely, the
809 * read-state if this FB is bound as ctx->DrawBuffer), but no
812 update_color_draw_buffers(ctx
, fb
);
813 update_color_read_buffer(ctx
, fb
);
814 _mesa_update_depth_buffer(ctx
, fb
, BUFFER_DEPTH
);
815 _mesa_update_stencil_buffer(ctx
, fb
, BUFFER_STENCIL
);
817 compute_depth_max(fb
);
822 * Update state related to the current draw/read framebuffers.
825 _mesa_update_framebuffer(GLcontext
*ctx
)
827 struct gl_framebuffer
*drawFb
;
828 struct gl_framebuffer
*readFb
;
831 drawFb
= ctx
->DrawBuffer
;
832 readFb
= ctx
->ReadBuffer
;
834 update_framebuffer(ctx
, drawFb
);
835 if (readFb
!= drawFb
)
836 update_framebuffer(ctx
, readFb
);
841 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
842 * glCopyTex[Sub]Image, etc) exists.
843 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
844 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
845 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
848 _mesa_source_buffer_exists(GLcontext
*ctx
, GLenum format
)
850 const struct gl_renderbuffer_attachment
*att
= ctx
->ReadBuffer
->Attachment
;
852 /* If we don't know the framebuffer status, update it now */
853 if (ctx
->ReadBuffer
->_Status
== 0) {
854 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
857 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
868 case GL_LUMINANCE_ALPHA
:
876 if (ctx
->ReadBuffer
->_ColorReadBuffer
== NULL
) {
879 ASSERT(_mesa_get_format_bits(ctx
->ReadBuffer
->_ColorReadBuffer
->Format
, GL_RED_BITS
) > 0 ||
880 _mesa_get_format_bits(ctx
->ReadBuffer
->_ColorReadBuffer
->Format
, GL_INDEX_BITS
) > 0);
883 case GL_DEPTH_COMPONENT
:
884 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
887 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
890 case GL_STENCIL_INDEX
:
891 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
894 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
896 case GL_DEPTH_STENCIL_EXT
:
897 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
898 !att
[BUFFER_STENCIL
].Renderbuffer
) {
902 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
903 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
908 "Unexpected format 0x%x in _mesa_source_buffer_exists",
919 * As above, but for drawing operations.
920 * XXX could do some code merging w/ above function.
923 _mesa_dest_buffer_exists(GLcontext
*ctx
, GLenum format
)
925 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
927 /* If we don't know the framebuffer status, update it now */
928 if (ctx
->DrawBuffer
->_Status
== 0) {
929 _mesa_test_framebuffer_completeness(ctx
, ctx
->DrawBuffer
);
932 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
943 case GL_LUMINANCE_ALPHA
:
951 /* Nothing special since GL_DRAW_BUFFER could be GL_NONE. */
952 /* Could assert that colorbuffer has RedBits > 0 */
955 case GL_DEPTH_COMPONENT
:
956 if (!att
[BUFFER_DEPTH
].Renderbuffer
) {
959 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
962 case GL_STENCIL_INDEX
:
963 if (!att
[BUFFER_STENCIL
].Renderbuffer
) {
966 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
968 case GL_DEPTH_STENCIL_EXT
:
969 if (!att
[BUFFER_DEPTH
].Renderbuffer
||
970 !att
[BUFFER_STENCIL
].Renderbuffer
) {
974 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
975 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
980 "Unexpected format 0x%x in _mesa_dest_buffer_exists",
991 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
994 _mesa_get_color_read_format(GLcontext
*ctx
)
996 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
997 case MESA_FORMAT_ARGB8888
:
999 case MESA_FORMAT_RGB565
:
1008 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
1011 _mesa_get_color_read_type(GLcontext
*ctx
)
1013 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
1014 case MESA_FORMAT_ARGB8888
:
1015 return GL_UNSIGNED_BYTE
;
1016 case MESA_FORMAT_RGB565
:
1017 return GL_UNSIGNED_SHORT_5_6_5_REV
;
1019 return GL_UNSIGNED_BYTE
;