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.
42 #include "framebuffer.h"
43 #include "renderbuffer.h"
45 #include "glformats.h"
50 * Compute/set the _DepthMax field for the given framebuffer.
51 * This value depends on the Z buffer resolution.
54 compute_depth_max(struct gl_framebuffer
*fb
)
56 if (fb
->Visual
.depthBits
== 0) {
57 /* Special case. Even if we don't have a depth buffer we need
58 * good values for DepthMax for Z vertex transformation purposes
59 * and for per-fragment fog computation.
61 fb
->_DepthMax
= (1 << 16) - 1;
63 else if (fb
->Visual
.depthBits
< 32) {
64 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
67 /* Special case since shift values greater than or equal to the
68 * number of bits in the left hand expression's type are undefined.
70 fb
->_DepthMax
= 0xffffffff;
72 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
74 /* Minimum resolvable depth value, for polygon offset */
75 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 struct gl_config
*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(struct gl_context
*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 struct gl_config
*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
);
230 * Set *ptr to point to fb, with refcounting and locking.
231 * This is normally only called from the _mesa_reference_framebuffer() macro
232 * when there's a real pointer change.
235 _mesa_reference_framebuffer_(struct gl_framebuffer
**ptr
,
236 struct gl_framebuffer
*fb
)
239 /* unreference old renderbuffer */
240 GLboolean deleteFlag
= GL_FALSE
;
241 struct gl_framebuffer
*oldFb
= *ptr
;
243 _glthread_LOCK_MUTEX(oldFb
->Mutex
);
244 ASSERT(oldFb
->RefCount
> 0);
246 deleteFlag
= (oldFb
->RefCount
== 0);
247 _glthread_UNLOCK_MUTEX(oldFb
->Mutex
);
250 oldFb
->Delete(oldFb
);
257 _glthread_LOCK_MUTEX(fb
->Mutex
);
259 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
266 * Resize the given framebuffer's renderbuffers to the new width and height.
267 * This should only be used for window-system framebuffers, not
268 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
269 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
270 * from a device driver.
272 * \note it's possible for ctx to be null since a window can be resized
273 * without a currently bound rendering context.
276 _mesa_resize_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
277 GLuint width
, GLuint height
)
281 /* XXX I think we could check if the size is not changing
285 /* Can only resize win-sys framebuffer objects */
286 assert(_mesa_is_winsys_fbo(fb
));
288 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
289 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
290 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
291 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
292 /* only resize if size is changing */
293 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
294 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
295 ASSERT(rb
->Width
== width
);
296 ASSERT(rb
->Height
== height
);
299 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
310 /* update scissor / window bounds */
311 _mesa_update_draw_buffer_bounds(ctx
);
312 /* Signal new buffer state so that swrast will update its clipping
313 * info (the CLIP_BIT flag).
315 ctx
->NewState
|= _NEW_BUFFERS
;
322 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
323 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
325 * GL_MESA_resize_buffers extension.
327 * When this function is called, we'll ask the window system how large
328 * the current window is. If it's a new size, we'll call the driver's
329 * ResizeBuffers function. The driver will then resize its color buffers
330 * as needed, and maybe call the swrast's routine for reallocating
331 * swrast-managed depth/stencil/accum/etc buffers.
332 * \note This function should only be called through the GL API, not
333 * from device drivers (as was done in the past).
336 _mesa_resizebuffers( struct gl_context
*ctx
)
338 FLUSH_VERTICES(ctx
, 0);
340 if (MESA_VERBOSE
& VERBOSE_API
)
341 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
343 if (!ctx
->Driver
.GetBufferSize
) {
347 if (ctx
->WinSysDrawBuffer
) {
348 GLuint newWidth
, newHeight
;
349 struct gl_framebuffer
*buffer
= ctx
->WinSysDrawBuffer
;
351 assert(_mesa_is_winsys_fbo(buffer
));
353 /* ask device driver for size of output buffer */
354 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
356 /* see if size of device driver's color buffer (window) has changed */
357 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
358 if (ctx
->Driver
.ResizeBuffers
)
359 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
363 if (ctx
->WinSysReadBuffer
364 && ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
365 GLuint newWidth
, newHeight
;
366 struct gl_framebuffer
*buffer
= ctx
->WinSysReadBuffer
;
368 assert(_mesa_is_winsys_fbo(buffer
));
370 /* ask device driver for size of read buffer */
371 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
373 /* see if size of device driver's color buffer (window) has changed */
374 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
375 if (ctx
->Driver
.ResizeBuffers
)
376 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
380 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
385 * XXX THIS IS OBSOLETE
388 _mesa_ResizeBuffersMESA( void )
390 GET_CURRENT_CONTEXT(ctx
);
392 if (ctx
->Extensions
.MESA_resize_buffers
)
393 _mesa_resizebuffers( ctx
);
399 * Examine all the framebuffer's renderbuffers to update the Width/Height
400 * fields of the framebuffer. If we have renderbuffers with different
401 * sizes, set the framebuffer's width and height to the min size.
402 * Note: this is only intended for user-created framebuffers, not
403 * window-system framebuffes.
406 update_framebuffer_size(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
408 GLuint minWidth
= ~0, minHeight
= ~0;
411 /* user-created framebuffers only */
412 assert(_mesa_is_user_fbo(fb
));
414 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
415 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
416 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
418 minWidth
= MIN2(minWidth
, rb
->Width
);
419 minHeight
= MIN2(minHeight
, rb
->Height
);
423 if (minWidth
!= ~0) {
424 fb
->Width
= minWidth
;
425 fb
->Height
= minHeight
;
435 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
436 * These values are computed from the buffer's width and height and
437 * the scissor box, if it's enabled.
438 * \param ctx the GL context.
441 _mesa_update_draw_buffer_bounds(struct gl_context
*ctx
)
443 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
448 if (_mesa_is_user_fbo(buffer
)) {
449 /* user-created framebuffer size depends on the renderbuffers */
450 update_framebuffer_size(ctx
, buffer
);
455 buffer
->_Xmax
= buffer
->Width
;
456 buffer
->_Ymax
= buffer
->Height
;
458 if (ctx
->Scissor
.Enabled
) {
459 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
460 buffer
->_Xmin
= ctx
->Scissor
.X
;
462 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
463 buffer
->_Ymin
= ctx
->Scissor
.Y
;
465 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
466 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
468 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
469 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
471 /* finally, check for empty region */
472 if (buffer
->_Xmin
> buffer
->_Xmax
) {
473 buffer
->_Xmin
= buffer
->_Xmax
;
475 if (buffer
->_Ymin
> buffer
->_Ymax
) {
476 buffer
->_Ymin
= buffer
->_Ymax
;
480 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
481 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
486 * The glGet queries of the framebuffer red/green/blue size, stencil size,
487 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
488 * change depending on the renderbuffer bindings. This function updates
489 * the given framebuffer's Visual from the current renderbuffer bindings.
491 * This may apply to user-created framebuffers or window system framebuffers.
493 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
494 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
495 * The former one is used to convert floating point depth values into
499 _mesa_update_framebuffer_visual(struct gl_context
*ctx
,
500 struct gl_framebuffer
*fb
)
504 memset(&fb
->Visual
, 0, sizeof(fb
->Visual
));
505 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
507 #if 0 /* this _might_ be needed */
508 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
509 /* leave visual fields zero'd */
514 /* find first RGB renderbuffer */
515 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
516 if (fb
->Attachment
[i
].Renderbuffer
) {
517 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
518 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
519 const gl_format fmt
= rb
->Format
;
521 /* Grab samples and sampleBuffers from any attachment point (assuming
522 * the framebuffer is complete, we'll get the same answer from all
525 fb
->Visual
.samples
= rb
->NumSamples
;
526 fb
->Visual
.sampleBuffers
= rb
->NumSamples
> 0 ? 1 : 0;
528 if (_mesa_is_legal_color_format(ctx
, baseFormat
)) {
529 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
530 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
531 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
532 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
533 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
534 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
535 if (_mesa_get_format_color_encoding(fmt
) == GL_SRGB
)
536 fb
->Visual
.sRGBCapable
= ctx
->Extensions
.EXT_framebuffer_sRGB
;
542 fb
->Visual
.floatMode
= GL_FALSE
;
543 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
544 if (fb
->Attachment
[i
].Renderbuffer
) {
545 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
546 const gl_format fmt
= rb
->Format
;
548 if (_mesa_get_format_datatype(fmt
) == GL_FLOAT
) {
549 fb
->Visual
.floatMode
= GL_TRUE
;
555 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
556 const struct gl_renderbuffer
*rb
=
557 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
558 const gl_format fmt
= rb
->Format
;
559 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
560 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
563 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
564 const struct gl_renderbuffer
*rb
=
565 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
566 const gl_format fmt
= rb
->Format
;
567 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
568 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
571 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
572 const struct gl_renderbuffer
*rb
=
573 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
574 const gl_format fmt
= rb
->Format
;
575 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
576 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
577 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
578 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
579 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
582 compute_depth_max(fb
);
587 * Example DrawBuffers scenarios:
589 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
590 * "gl_FragColor" or program writes to the "result.color" register:
592 * fragment color output renderbuffer
593 * --------------------- ---------------
594 * color[0] Front, Back
597 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
598 * gl_FragData[i] or program writes to result.color[i] registers:
600 * fragment color output renderbuffer
601 * --------------------- ---------------
607 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
608 * gl_FragColor, or fixed function:
610 * fragment color output renderbuffer
611 * --------------------- ---------------
612 * color[0] Front, Aux0, Aux1
615 * In either case, the list of renderbuffers is stored in the
616 * framebuffer->_ColorDrawBuffers[] array and
617 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
618 * The renderer (like swrast) has to look at the current fragment shader
619 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
620 * how to map color outputs to renderbuffers.
622 * Note that these two calls are equivalent (for fixed function fragment
624 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
625 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
632 * Update the (derived) list of color drawing renderbuffer pointers.
633 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
637 update_color_draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
641 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
642 fb
->_ColorDrawBuffers
[0] = NULL
;
644 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
645 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
647 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
650 fb
->_ColorDrawBuffers
[output
] = NULL
;
657 * Update the (derived) color read renderbuffer pointer.
658 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
661 update_color_read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
664 if (fb
->_ColorReadBufferIndex
== -1 ||
668 fb
->_ColorReadBuffer
= NULL
; /* legal! */
671 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
672 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
674 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
680 * Update a gl_framebuffer's derived state.
682 * Specifically, update these framebuffer fields:
684 * _NumColorDrawBuffers
687 * If the framebuffer is user-created, make sure it's complete.
689 * The following functions (at least) can effect framebuffer state:
690 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
691 * glRenderbufferStorageEXT.
694 update_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
696 if (_mesa_is_winsys_fbo(fb
)) {
697 /* This is a window-system framebuffer */
698 /* Need to update the FB's GL_DRAW_BUFFER state to match the
699 * context state (GL_READ_BUFFER too).
701 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
702 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
,
703 ctx
->Color
.DrawBuffer
, NULL
);
707 /* This is a user-created framebuffer.
708 * Completeness only matters for user-created framebuffers.
710 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
711 _mesa_test_framebuffer_completeness(ctx
, fb
);
715 /* Strictly speaking, we don't need to update the draw-state
716 * if this FB is bound as ctx->ReadBuffer (and conversely, the
717 * read-state if this FB is bound as ctx->DrawBuffer), but no
720 update_color_draw_buffers(ctx
, fb
);
721 update_color_read_buffer(ctx
, fb
);
723 compute_depth_max(fb
);
728 * Update state related to the current draw/read framebuffers.
731 _mesa_update_framebuffer(struct gl_context
*ctx
)
733 struct gl_framebuffer
*drawFb
;
734 struct gl_framebuffer
*readFb
;
737 drawFb
= ctx
->DrawBuffer
;
738 readFb
= ctx
->ReadBuffer
;
740 update_framebuffer(ctx
, drawFb
);
741 if (readFb
!= drawFb
)
742 update_framebuffer(ctx
, readFb
);
747 * Check if the renderbuffer for a read/draw operation exists.
748 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
749 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
750 * \param reading if TRUE, we're going to read from the buffer,
751 if FALSE, we're going to write to the buffer.
752 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
755 renderbuffer_exists(struct gl_context
*ctx
,
756 struct gl_framebuffer
*fb
,
760 const struct gl_renderbuffer_attachment
*att
= fb
->Attachment
;
762 /* If we don't know the framebuffer status, update it now */
763 if (fb
->_Status
== 0) {
764 _mesa_test_framebuffer_completeness(ctx
, fb
);
767 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
778 case GL_LUMINANCE_ALPHA
:
786 case GL_RED_INTEGER_EXT
:
788 case GL_GREEN_INTEGER_EXT
:
789 case GL_BLUE_INTEGER_EXT
:
790 case GL_ALPHA_INTEGER_EXT
:
791 case GL_RGB_INTEGER_EXT
:
792 case GL_RGBA_INTEGER_EXT
:
793 case GL_BGR_INTEGER_EXT
:
794 case GL_BGRA_INTEGER_EXT
:
795 case GL_LUMINANCE_INTEGER_EXT
:
796 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
798 /* about to read from a color buffer */
799 const struct gl_renderbuffer
*readBuf
= fb
->_ColorReadBuffer
;
803 ASSERT(_mesa_get_format_bits(readBuf
->Format
, GL_RED_BITS
) > 0 ||
804 _mesa_get_format_bits(readBuf
->Format
, GL_ALPHA_BITS
) > 0 ||
805 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_LUMINANCE_SIZE
) > 0 ||
806 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_INTENSITY_SIZE
) > 0 ||
807 _mesa_get_format_bits(readBuf
->Format
, GL_INDEX_BITS
) > 0);
810 /* about to draw to zero or more color buffers (none is OK) */
815 case GL_DEPTH_COMPONENT
:
816 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
) {
821 case GL_STENCIL_INDEX
:
822 if (att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
826 case GL_DEPTH_STENCIL_EXT
:
827 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
||
828 att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
834 "Unexpected format 0x%x in renderbuffer_exists",
845 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
846 * glCopyTex[Sub]Image, etc) exists.
847 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
848 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
849 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
852 _mesa_source_buffer_exists(struct gl_context
*ctx
, GLenum format
)
854 return renderbuffer_exists(ctx
, ctx
->ReadBuffer
, format
, GL_TRUE
);
859 * As above, but for drawing operations.
862 _mesa_dest_buffer_exists(struct gl_context
*ctx
, GLenum format
)
864 return renderbuffer_exists(ctx
, ctx
->DrawBuffer
, format
, GL_FALSE
);
869 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
872 _mesa_get_color_read_format(struct gl_context
*ctx
)
874 const GLenum data_type
= _mesa_get_format_datatype(
875 ctx
->ReadBuffer
->_ColorReadBuffer
->Format
);
877 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
878 case MESA_FORMAT_ARGB8888
:
880 case MESA_FORMAT_RGB565
:
883 if (data_type
== GL_UNSIGNED_INT
|| data_type
== GL_INT
) {
884 return GL_RGBA_INTEGER
;
893 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
896 _mesa_get_color_read_type(struct gl_context
*ctx
)
898 const GLenum data_type
= _mesa_get_format_datatype(
899 ctx
->ReadBuffer
->_ColorReadBuffer
->Format
);
901 switch (ctx
->ReadBuffer
->_ColorReadBuffer
->Format
) {
902 case MESA_FORMAT_RGB565
:
903 return GL_UNSIGNED_SHORT_5_6_5_REV
;
909 case GL_SIGNED_NORMALIZED
:
911 case GL_UNSIGNED_INT
:
915 case GL_UNSIGNED_NORMALIZED
:
917 return GL_UNSIGNED_BYTE
;
923 * Returns the read renderbuffer for the specified format.
925 struct gl_renderbuffer
*
926 _mesa_get_read_renderbuffer_for_format(struct gl_context
*ctx
,
929 struct gl_framebuffer
*rfb
= ctx
->ReadBuffer
;
931 if (_mesa_is_color_format(format
)) {
932 return rfb
->Attachment
[rfb
->_ColorReadBufferIndex
].Renderbuffer
;
933 } else if (_mesa_is_depth_format(format
) ||
934 _mesa_is_depthstencil_format(format
)) {
935 return rfb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
937 return rfb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
943 * Print framebuffer info to stderr, for debugging.
946 _mesa_print_framebuffer(const struct gl_framebuffer
*fb
)
950 fprintf(stderr
, "Mesa Framebuffer %u at %p\n", fb
->Name
, (void *) fb
);
951 fprintf(stderr
, " Size: %u x %u Status: %s\n", fb
->Width
, fb
->Height
,
952 _mesa_lookup_enum_by_nr(fb
->_Status
));
953 fprintf(stderr
, " Attachments:\n");
955 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
956 const struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
957 if (att
->Type
== GL_TEXTURE
) {
958 const struct gl_texture_image
*texImage
=
959 _mesa_get_attachment_teximage_const(att
);
961 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
962 i
, att
->Texture
->Name
, att
->TextureLevel
, att
->CubeMapFace
,
963 att
->Zoffset
, att
->Complete
);
964 fprintf(stderr
, " Size: %u x %u x %u Format %s\n",
965 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
966 _mesa_get_format_name(texImage
->TexFormat
));
968 else if (att
->Type
== GL_RENDERBUFFER
) {
969 fprintf(stderr
, " %2d: Renderbuffer %u, complete %d\n",
970 i
, att
->Renderbuffer
->Name
, att
->Complete
);
971 fprintf(stderr
, " Size: %u x %u Format %s\n",
972 att
->Renderbuffer
->Width
, att
->Renderbuffer
->Height
,
973 _mesa_get_format_name(att
->Renderbuffer
->Format
));
976 fprintf(stderr
, " %2d: none\n", i
);