2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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.
43 #include "framebuffer.h"
44 #include "renderbuffer.h"
46 #include "glformats.h"
51 * Compute/set the _DepthMax field for the given framebuffer.
52 * This value depends on the Z buffer resolution.
55 compute_depth_max(struct gl_framebuffer
*fb
)
57 if (fb
->Visual
.depthBits
== 0) {
58 /* Special case. Even if we don't have a depth buffer we need
59 * good values for DepthMax for Z vertex transformation purposes
60 * and for per-fragment fog computation.
62 fb
->_DepthMax
= (1 << 16) - 1;
64 else if (fb
->Visual
.depthBits
< 32) {
65 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
68 /* Special case since shift values greater than or equal to the
69 * number of bits in the left hand expression's type are undefined.
71 fb
->_DepthMax
= 0xffffffff;
73 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
75 /* Minimum resolvable depth value, for polygon offset */
76 fb
->_MRD
= (GLfloat
)1.0 / fb
->_DepthMaxF
;
80 * Create and initialize a gl_framebuffer object.
81 * This is intended for creating _window_system_ framebuffers, not generic
82 * framebuffer objects ala GL_EXT_framebuffer_object.
84 * \sa _mesa_new_framebuffer
86 struct gl_framebuffer
*
87 _mesa_create_framebuffer(const struct gl_config
*visual
)
89 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
92 _mesa_initialize_window_framebuffer(fb
, visual
);
99 * Allocate a new gl_framebuffer object.
100 * This is the default function for ctx->Driver.NewFramebuffer().
101 * This is for allocating user-created framebuffers, not window-system
103 * \sa _mesa_create_framebuffer
105 struct gl_framebuffer
*
106 _mesa_new_framebuffer(struct gl_context
*ctx
, GLuint name
)
108 struct gl_framebuffer
*fb
;
111 fb
= CALLOC_STRUCT(gl_framebuffer
);
113 _mesa_initialize_user_framebuffer(fb
, name
);
120 * Initialize a gl_framebuffer object. Typically used to initialize
121 * window system-created framebuffers, not user-created framebuffers.
122 * \sa _mesa_initialize_user_framebuffer
125 _mesa_initialize_window_framebuffer(struct gl_framebuffer
*fb
,
126 const struct gl_config
*visual
)
131 memset(fb
, 0, sizeof(struct gl_framebuffer
));
133 _glthread_INIT_MUTEX(fb
->Mutex
);
137 /* save the visual */
138 fb
->Visual
= *visual
;
140 /* Init read/draw renderbuffer state */
141 if (visual
->doubleBufferMode
) {
142 fb
->_NumColorDrawBuffers
= 1;
143 fb
->ColorDrawBuffer
[0] = GL_BACK
;
144 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_BACK_LEFT
;
145 fb
->ColorReadBuffer
= GL_BACK
;
146 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
149 fb
->_NumColorDrawBuffers
= 1;
150 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
151 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_FRONT_LEFT
;
152 fb
->ColorReadBuffer
= GL_FRONT
;
153 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
156 fb
->Delete
= _mesa_destroy_framebuffer
;
157 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
158 fb
->_AllColorBuffersFixedPoint
= !visual
->floatMode
;
159 fb
->_HasSNormOrFloatColorBuffer
= visual
->floatMode
;
161 compute_depth_max(fb
);
166 * Initialize a user-created gl_framebuffer object.
167 * \sa _mesa_initialize_window_framebuffer
170 _mesa_initialize_user_framebuffer(struct gl_framebuffer
*fb
, GLuint name
)
175 memset(fb
, 0, sizeof(struct gl_framebuffer
));
179 fb
->_NumColorDrawBuffers
= 1;
180 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
181 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
182 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
183 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
184 fb
->Delete
= _mesa_destroy_framebuffer
;
185 _glthread_INIT_MUTEX(fb
->Mutex
);
190 * Deallocate buffer and everything attached to it.
191 * Typically called via the gl_framebuffer->Delete() method.
194 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
197 _mesa_free_framebuffer_data(fb
);
204 * Free all the data hanging off the given gl_framebuffer, but don't free
205 * the gl_framebuffer object itself.
208 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
213 assert(fb
->RefCount
== 0);
215 _glthread_DESTROY_MUTEX(fb
->Mutex
);
217 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
218 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
219 if (att
->Renderbuffer
) {
220 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
223 _mesa_reference_texobj(&att
->Texture
, NULL
);
225 ASSERT(!att
->Renderbuffer
);
226 ASSERT(!att
->Texture
);
233 * Set *ptr to point to fb, with refcounting and locking.
234 * This is normally only called from the _mesa_reference_framebuffer() macro
235 * when there's a real pointer change.
238 _mesa_reference_framebuffer_(struct gl_framebuffer
**ptr
,
239 struct gl_framebuffer
*fb
)
242 /* unreference old renderbuffer */
243 GLboolean deleteFlag
= GL_FALSE
;
244 struct gl_framebuffer
*oldFb
= *ptr
;
246 _glthread_LOCK_MUTEX(oldFb
->Mutex
);
247 ASSERT(oldFb
->RefCount
> 0);
249 deleteFlag
= (oldFb
->RefCount
== 0);
250 _glthread_UNLOCK_MUTEX(oldFb
->Mutex
);
253 oldFb
->Delete(oldFb
);
260 _glthread_LOCK_MUTEX(fb
->Mutex
);
262 _glthread_UNLOCK_MUTEX(fb
->Mutex
);
269 * Resize the given framebuffer's renderbuffers to the new width and height.
270 * This should only be used for window-system framebuffers, not
271 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
272 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
273 * from a device driver.
275 * \note it's possible for ctx to be null since a window can be resized
276 * without a currently bound rendering context.
279 _mesa_resize_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
280 GLuint width
, GLuint height
)
284 /* XXX I think we could check if the size is not changing
288 /* Can only resize win-sys framebuffer objects */
289 assert(_mesa_is_winsys_fbo(fb
));
291 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
292 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
293 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
294 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
295 /* only resize if size is changing */
296 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
297 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
298 ASSERT(rb
->Width
== width
);
299 ASSERT(rb
->Height
== height
);
302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
313 /* update scissor / window bounds */
314 _mesa_update_draw_buffer_bounds(ctx
);
315 /* Signal new buffer state so that swrast will update its clipping
316 * info (the CLIP_BIT flag).
318 ctx
->NewState
|= _NEW_BUFFERS
;
325 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
326 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
328 * GL_MESA_resize_buffers extension.
330 * When this function is called, we'll ask the window system how large
331 * the current window is. If it's a new size, we'll call the driver's
332 * ResizeBuffers function. The driver will then resize its color buffers
333 * as needed, and maybe call the swrast's routine for reallocating
334 * swrast-managed depth/stencil/accum/etc buffers.
335 * \note This function should only be called through the GL API, not
336 * from device drivers (as was done in the past).
339 _mesa_resizebuffers( struct gl_context
*ctx
)
341 FLUSH_VERTICES(ctx
, 0);
343 if (MESA_VERBOSE
& VERBOSE_API
)
344 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
346 if (!ctx
->Driver
.GetBufferSize
) {
350 if (ctx
->WinSysDrawBuffer
) {
351 GLuint newWidth
, newHeight
;
352 struct gl_framebuffer
*buffer
= ctx
->WinSysDrawBuffer
;
354 assert(_mesa_is_winsys_fbo(buffer
));
356 /* ask device driver for size of output buffer */
357 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
359 /* see if size of device driver's color buffer (window) has changed */
360 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
361 if (ctx
->Driver
.ResizeBuffers
)
362 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
366 if (ctx
->WinSysReadBuffer
367 && ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
368 GLuint newWidth
, newHeight
;
369 struct gl_framebuffer
*buffer
= ctx
->WinSysReadBuffer
;
371 assert(_mesa_is_winsys_fbo(buffer
));
373 /* ask device driver for size of read buffer */
374 ctx
->Driver
.GetBufferSize( buffer
, &newWidth
, &newHeight
);
376 /* see if size of device driver's color buffer (window) has changed */
377 if (buffer
->Width
!= newWidth
|| buffer
->Height
!= newHeight
) {
378 if (ctx
->Driver
.ResizeBuffers
)
379 ctx
->Driver
.ResizeBuffers(ctx
, buffer
, newWidth
, newHeight
);
383 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
388 * XXX THIS IS OBSOLETE
391 _mesa_ResizeBuffersMESA( void )
393 GET_CURRENT_CONTEXT(ctx
);
395 if (ctx
->Extensions
.MESA_resize_buffers
)
396 _mesa_resizebuffers( ctx
);
402 * Examine all the framebuffer's renderbuffers to update the Width/Height
403 * fields of the framebuffer. If we have renderbuffers with different
404 * sizes, set the framebuffer's width and height to the min size.
405 * Note: this is only intended for user-created framebuffers, not
406 * window-system framebuffes.
409 update_framebuffer_size(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
411 GLuint minWidth
= ~0, minHeight
= ~0;
414 /* user-created framebuffers only */
415 assert(_mesa_is_user_fbo(fb
));
417 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
418 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
419 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
421 minWidth
= MIN2(minWidth
, rb
->Width
);
422 minHeight
= MIN2(minHeight
, rb
->Height
);
426 if (minWidth
!= ~0) {
427 fb
->Width
= minWidth
;
428 fb
->Height
= minHeight
;
438 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
439 * These values are computed from the buffer's width and height and
440 * the scissor box, if it's enabled.
441 * \param ctx the GL context.
444 _mesa_update_draw_buffer_bounds(struct gl_context
*ctx
)
446 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
451 if (_mesa_is_user_fbo(buffer
)) {
452 /* user-created framebuffer size depends on the renderbuffers */
453 update_framebuffer_size(ctx
, buffer
);
458 buffer
->_Xmax
= buffer
->Width
;
459 buffer
->_Ymax
= buffer
->Height
;
461 if (ctx
->Scissor
.Enabled
) {
462 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
463 buffer
->_Xmin
= ctx
->Scissor
.X
;
465 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
466 buffer
->_Ymin
= ctx
->Scissor
.Y
;
468 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
469 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
471 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
472 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
474 /* finally, check for empty region */
475 if (buffer
->_Xmin
> buffer
->_Xmax
) {
476 buffer
->_Xmin
= buffer
->_Xmax
;
478 if (buffer
->_Ymin
> buffer
->_Ymax
) {
479 buffer
->_Ymin
= buffer
->_Ymax
;
483 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
484 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
489 * The glGet queries of the framebuffer red/green/blue size, stencil size,
490 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
491 * change depending on the renderbuffer bindings. This function updates
492 * the given framebuffer's Visual from the current renderbuffer bindings.
494 * This may apply to user-created framebuffers or window system framebuffers.
496 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
497 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
498 * The former one is used to convert floating point depth values into
502 _mesa_update_framebuffer_visual(struct gl_context
*ctx
,
503 struct gl_framebuffer
*fb
)
507 memset(&fb
->Visual
, 0, sizeof(fb
->Visual
));
508 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
510 #if 0 /* this _might_ be needed */
511 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
512 /* leave visual fields zero'd */
517 /* find first RGB renderbuffer */
518 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
519 if (fb
->Attachment
[i
].Renderbuffer
) {
520 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
521 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
522 const gl_format fmt
= rb
->Format
;
524 /* Grab samples and sampleBuffers from any attachment point (assuming
525 * the framebuffer is complete, we'll get the same answer from all
528 fb
->Visual
.samples
= rb
->NumSamples
;
529 fb
->Visual
.sampleBuffers
= rb
->NumSamples
> 0 ? 1 : 0;
531 if (_mesa_is_legal_color_format(ctx
, baseFormat
)) {
532 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
533 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
534 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
535 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
536 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
537 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
538 if (_mesa_get_format_color_encoding(fmt
) == GL_SRGB
)
539 fb
->Visual
.sRGBCapable
= ctx
->Extensions
.EXT_framebuffer_sRGB
;
545 fb
->Visual
.floatMode
= GL_FALSE
;
546 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
547 if (fb
->Attachment
[i
].Renderbuffer
) {
548 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
549 const gl_format fmt
= rb
->Format
;
551 if (_mesa_get_format_datatype(fmt
) == GL_FLOAT
) {
552 fb
->Visual
.floatMode
= GL_TRUE
;
558 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
559 const struct gl_renderbuffer
*rb
=
560 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
561 const gl_format fmt
= rb
->Format
;
562 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
563 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
566 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
567 const struct gl_renderbuffer
*rb
=
568 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
569 const gl_format fmt
= rb
->Format
;
570 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
571 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
574 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
575 const struct gl_renderbuffer
*rb
=
576 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
577 const gl_format fmt
= rb
->Format
;
578 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
579 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
580 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
581 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
582 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
585 compute_depth_max(fb
);
590 * Example DrawBuffers scenarios:
592 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
593 * "gl_FragColor" or program writes to the "result.color" register:
595 * fragment color output renderbuffer
596 * --------------------- ---------------
597 * color[0] Front, Back
600 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
601 * gl_FragData[i] or program writes to result.color[i] registers:
603 * fragment color output renderbuffer
604 * --------------------- ---------------
610 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
611 * gl_FragColor, or fixed function:
613 * fragment color output renderbuffer
614 * --------------------- ---------------
615 * color[0] Front, Aux0, Aux1
618 * In either case, the list of renderbuffers is stored in the
619 * framebuffer->_ColorDrawBuffers[] array and
620 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
621 * The renderer (like swrast) has to look at the current fragment shader
622 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
623 * how to map color outputs to renderbuffers.
625 * Note that these two calls are equivalent (for fixed function fragment
627 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
628 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
635 * Update the (derived) list of color drawing renderbuffer pointers.
636 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
640 update_color_draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
644 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
645 fb
->_ColorDrawBuffers
[0] = NULL
;
647 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
648 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
650 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
653 fb
->_ColorDrawBuffers
[output
] = NULL
;
660 * Update the (derived) color read renderbuffer pointer.
661 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
664 update_color_read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
667 if (fb
->_ColorReadBufferIndex
== -1 ||
671 fb
->_ColorReadBuffer
= NULL
; /* legal! */
674 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
675 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
677 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
683 * Update a gl_framebuffer's derived state.
685 * Specifically, update these framebuffer fields:
687 * _NumColorDrawBuffers
690 * If the framebuffer is user-created, make sure it's complete.
692 * The following functions (at least) can effect framebuffer state:
693 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
694 * glRenderbufferStorageEXT.
697 update_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
699 if (_mesa_is_winsys_fbo(fb
)) {
700 /* This is a window-system framebuffer */
701 /* Need to update the FB's GL_DRAW_BUFFER state to match the
702 * context state (GL_READ_BUFFER too).
704 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
705 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
,
706 ctx
->Color
.DrawBuffer
, NULL
);
710 /* This is a user-created framebuffer.
711 * Completeness only matters for user-created framebuffers.
713 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
714 _mesa_test_framebuffer_completeness(ctx
, fb
);
718 /* Strictly speaking, we don't need to update the draw-state
719 * if this FB is bound as ctx->ReadBuffer (and conversely, the
720 * read-state if this FB is bound as ctx->DrawBuffer), but no
723 update_color_draw_buffers(ctx
, fb
);
724 update_color_read_buffer(ctx
, fb
);
726 compute_depth_max(fb
);
731 * Update state related to the current draw/read framebuffers.
734 _mesa_update_framebuffer(struct gl_context
*ctx
)
736 struct gl_framebuffer
*drawFb
;
737 struct gl_framebuffer
*readFb
;
740 drawFb
= ctx
->DrawBuffer
;
741 readFb
= ctx
->ReadBuffer
;
743 update_framebuffer(ctx
, drawFb
);
744 if (readFb
!= drawFb
)
745 update_framebuffer(ctx
, readFb
);
747 _mesa_update_clamp_vertex_color(ctx
);
748 _mesa_update_clamp_fragment_color(ctx
);
753 * Check if the renderbuffer for a read/draw operation exists.
754 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
755 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
756 * \param reading if TRUE, we're going to read from the buffer,
757 if FALSE, we're going to write to the buffer.
758 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
761 renderbuffer_exists(struct gl_context
*ctx
,
762 struct gl_framebuffer
*fb
,
766 const struct gl_renderbuffer_attachment
*att
= fb
->Attachment
;
768 /* If we don't know the framebuffer status, update it now */
769 if (fb
->_Status
== 0) {
770 _mesa_test_framebuffer_completeness(ctx
, fb
);
773 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
784 case GL_LUMINANCE_ALPHA
:
792 case GL_RED_INTEGER_EXT
:
794 case GL_GREEN_INTEGER_EXT
:
795 case GL_BLUE_INTEGER_EXT
:
796 case GL_ALPHA_INTEGER_EXT
:
797 case GL_RGB_INTEGER_EXT
:
798 case GL_RGBA_INTEGER_EXT
:
799 case GL_BGR_INTEGER_EXT
:
800 case GL_BGRA_INTEGER_EXT
:
801 case GL_LUMINANCE_INTEGER_EXT
:
802 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
804 /* about to read from a color buffer */
805 const struct gl_renderbuffer
*readBuf
= fb
->_ColorReadBuffer
;
809 ASSERT(_mesa_get_format_bits(readBuf
->Format
, GL_RED_BITS
) > 0 ||
810 _mesa_get_format_bits(readBuf
->Format
, GL_ALPHA_BITS
) > 0 ||
811 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_LUMINANCE_SIZE
) > 0 ||
812 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_INTENSITY_SIZE
) > 0 ||
813 _mesa_get_format_bits(readBuf
->Format
, GL_INDEX_BITS
) > 0);
816 /* about to draw to zero or more color buffers (none is OK) */
821 case GL_DEPTH_COMPONENT
:
822 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
) {
827 case GL_STENCIL_INDEX
:
828 if (att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
832 case GL_DEPTH_STENCIL_EXT
:
833 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
||
834 att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
840 "Unexpected format 0x%x in renderbuffer_exists",
851 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
852 * glCopyTex[Sub]Image, etc) exists.
853 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
854 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
855 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
858 _mesa_source_buffer_exists(struct gl_context
*ctx
, GLenum format
)
860 return renderbuffer_exists(ctx
, ctx
->ReadBuffer
, format
, GL_TRUE
);
865 * As above, but for drawing operations.
868 _mesa_dest_buffer_exists(struct gl_context
*ctx
, GLenum format
)
870 return renderbuffer_exists(ctx
, ctx
->DrawBuffer
, format
, GL_FALSE
);
875 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
878 _mesa_get_color_read_format(struct gl_context
*ctx
)
880 if (!ctx
->ReadBuffer
|| !ctx
->ReadBuffer
->_ColorReadBuffer
) {
881 /* The spec is unclear how to handle this case, but NVIDIA's
882 * driver generates GL_INVALID_OPERATION.
884 _mesa_error(ctx
, GL_INVALID_OPERATION
,
885 "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT: "
886 "no GL_READ_BUFFER)");
890 const GLenum format
= ctx
->ReadBuffer
->_ColorReadBuffer
->Format
;
891 const GLenum data_type
= _mesa_get_format_datatype(format
);
893 if (format
== MESA_FORMAT_ARGB8888
)
895 else if (format
== MESA_FORMAT_RGB565
)
899 case GL_UNSIGNED_INT
:
901 return GL_RGBA_INTEGER
;
910 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
913 _mesa_get_color_read_type(struct gl_context
*ctx
)
915 if (!ctx
->ReadBuffer
|| !ctx
->ReadBuffer
->_ColorReadBuffer
) {
916 /* The spec is unclear how to handle this case, but NVIDIA's
917 * driver generates GL_INVALID_OPERATION.
919 _mesa_error(ctx
, GL_INVALID_OPERATION
,
920 "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE: "
921 "no GL_READ_BUFFER)");
925 const GLenum format
= ctx
->ReadBuffer
->_ColorReadBuffer
->Format
;
926 const GLenum data_type
= _mesa_get_format_datatype(format
);
928 if (format
== MESA_FORMAT_RGB565
)
929 return GL_UNSIGNED_SHORT_5_6_5_REV
;
932 case GL_SIGNED_NORMALIZED
:
934 case GL_UNSIGNED_INT
:
938 case GL_UNSIGNED_NORMALIZED
:
940 return GL_UNSIGNED_BYTE
;
947 * Returns the read renderbuffer for the specified format.
949 struct gl_renderbuffer
*
950 _mesa_get_read_renderbuffer_for_format(const struct gl_context
*ctx
,
953 const struct gl_framebuffer
*rfb
= ctx
->ReadBuffer
;
955 if (_mesa_is_color_format(format
)) {
956 return rfb
->Attachment
[rfb
->_ColorReadBufferIndex
].Renderbuffer
;
957 } else if (_mesa_is_depth_format(format
) ||
958 _mesa_is_depthstencil_format(format
)) {
959 return rfb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
961 return rfb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
967 * Print framebuffer info to stderr, for debugging.
970 _mesa_print_framebuffer(const struct gl_framebuffer
*fb
)
974 fprintf(stderr
, "Mesa Framebuffer %u at %p\n", fb
->Name
, (void *) fb
);
975 fprintf(stderr
, " Size: %u x %u Status: %s\n", fb
->Width
, fb
->Height
,
976 _mesa_lookup_enum_by_nr(fb
->_Status
));
977 fprintf(stderr
, " Attachments:\n");
979 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
980 const struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
981 if (att
->Type
== GL_TEXTURE
) {
982 const struct gl_texture_image
*texImage
= att
->Renderbuffer
->TexImage
;
984 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
985 i
, att
->Texture
->Name
, att
->TextureLevel
, att
->CubeMapFace
,
986 att
->Zoffset
, att
->Complete
);
987 fprintf(stderr
, " Size: %u x %u x %u Format %s\n",
988 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
989 _mesa_get_format_name(texImage
->TexFormat
));
991 else if (att
->Type
== GL_RENDERBUFFER
) {
992 fprintf(stderr
, " %2d: Renderbuffer %u, complete %d\n",
993 i
, att
->Renderbuffer
->Name
, att
->Complete
);
994 fprintf(stderr
, " Size: %u x %u Format %s\n",
995 att
->Renderbuffer
->Width
, att
->Renderbuffer
->Height
,
996 _mesa_get_format_name(att
->Renderbuffer
->Format
));
999 fprintf(stderr
, " %2d: none\n", i
);