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 mtx_init(&fb
->Mutex
, mtx_plain
);
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
;
160 fb
->_HasAttachments
= true;
162 compute_depth_max(fb
);
167 * Initialize a user-created gl_framebuffer object.
168 * \sa _mesa_initialize_window_framebuffer
171 _mesa_initialize_user_framebuffer(struct gl_framebuffer
*fb
, GLuint name
)
176 memset(fb
, 0, sizeof(struct gl_framebuffer
));
180 fb
->_NumColorDrawBuffers
= 1;
181 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
182 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
183 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
184 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
185 fb
->Delete
= _mesa_destroy_framebuffer
;
186 mtx_init(&fb
->Mutex
, mtx_plain
);
191 * Deallocate buffer and everything attached to it.
192 * Typically called via the gl_framebuffer->Delete() method.
195 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
198 _mesa_free_framebuffer_data(fb
);
206 * Free all the data hanging off the given gl_framebuffer, but don't free
207 * the gl_framebuffer object itself.
210 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
215 assert(fb
->RefCount
== 0);
217 mtx_destroy(&fb
->Mutex
);
219 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
220 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
221 if (att
->Renderbuffer
) {
222 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
225 _mesa_reference_texobj(&att
->Texture
, NULL
);
227 assert(!att
->Renderbuffer
);
228 assert(!att
->Texture
);
235 * Set *ptr to point to fb, with refcounting and locking.
236 * This is normally only called from the _mesa_reference_framebuffer() macro
237 * when there's a real pointer change.
240 _mesa_reference_framebuffer_(struct gl_framebuffer
**ptr
,
241 struct gl_framebuffer
*fb
)
244 /* unreference old renderbuffer */
245 GLboolean deleteFlag
= GL_FALSE
;
246 struct gl_framebuffer
*oldFb
= *ptr
;
248 mtx_lock(&oldFb
->Mutex
);
249 assert(oldFb
->RefCount
> 0);
251 deleteFlag
= (oldFb
->RefCount
== 0);
252 mtx_unlock(&oldFb
->Mutex
);
255 oldFb
->Delete(oldFb
);
262 mtx_lock(&fb
->Mutex
);
264 mtx_unlock(&fb
->Mutex
);
271 * Resize the given framebuffer's renderbuffers to the new width and height.
272 * This should only be used for window-system framebuffers, not
273 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
274 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
275 * from a device driver.
277 * \note it's possible for ctx to be null since a window can be resized
278 * without a currently bound rendering context.
281 _mesa_resize_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
282 GLuint width
, GLuint height
)
286 /* XXX I think we could check if the size is not changing
290 /* Can only resize win-sys framebuffer objects */
291 assert(_mesa_is_winsys_fbo(fb
));
293 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
294 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
295 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
296 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
297 /* only resize if size is changing */
298 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
299 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
300 assert(rb
->Width
== width
);
301 assert(rb
->Height
== height
);
304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
315 /* update scissor / window bounds */
316 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
317 /* Signal new buffer state so that swrast will update its clipping
318 * info (the CLIP_BIT flag).
320 ctx
->NewState
|= _NEW_BUFFERS
;
325 * Examine all the framebuffer's renderbuffers to update the Width/Height
326 * fields of the framebuffer. If we have renderbuffers with different
327 * sizes, set the framebuffer's width and height to the min size.
328 * Note: this is only intended for user-created framebuffers, not
329 * window-system framebuffes.
332 update_framebuffer_size(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
334 GLuint minWidth
= ~0, minHeight
= ~0;
337 /* user-created framebuffers only */
338 assert(_mesa_is_user_fbo(fb
));
340 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
341 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
342 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
344 minWidth
= MIN2(minWidth
, rb
->Width
);
345 minHeight
= MIN2(minHeight
, rb
->Height
);
349 if (minWidth
!= ~0U) {
350 fb
->Width
= minWidth
;
351 fb
->Height
= minHeight
;
362 * Given a bounding box, intersect the bounding box with the scissor of
363 * a specified vieport.
365 * \param ctx GL context.
366 * \param idx Index of the desired viewport
367 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
371 _mesa_intersect_scissor_bounding_box(const struct gl_context
*ctx
,
372 unsigned idx
, int *bbox
)
374 if (ctx
->Scissor
.EnableFlags
& (1u << idx
)) {
375 if (ctx
->Scissor
.ScissorArray
[idx
].X
> bbox
[0]) {
376 bbox
[0] = ctx
->Scissor
.ScissorArray
[idx
].X
;
378 if (ctx
->Scissor
.ScissorArray
[idx
].Y
> bbox
[2]) {
379 bbox
[2] = ctx
->Scissor
.ScissorArray
[idx
].Y
;
381 if (ctx
->Scissor
.ScissorArray
[idx
].X
+ ctx
->Scissor
.ScissorArray
[idx
].Width
< bbox
[1]) {
382 bbox
[1] = ctx
->Scissor
.ScissorArray
[idx
].X
+ ctx
->Scissor
.ScissorArray
[idx
].Width
;
384 if (ctx
->Scissor
.ScissorArray
[idx
].Y
+ ctx
->Scissor
.ScissorArray
[idx
].Height
< bbox
[3]) {
385 bbox
[3] = ctx
->Scissor
.ScissorArray
[idx
].Y
+ ctx
->Scissor
.ScissorArray
[idx
].Height
;
387 /* finally, check for empty region */
388 if (bbox
[0] > bbox
[1]) {
391 if (bbox
[2] > bbox
[3]) {
398 * Calculate the inclusive bounding box for the scissor of a specific viewport
400 * \param ctx GL context.
401 * \param buffer Framebuffer to be checked against
402 * \param idx Index of the desired viewport
403 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
406 * \warning This function assumes that the framebuffer dimensions are up to
407 * date (e.g., update_framebuffer_size has been recently called on \c buffer).
409 * \sa _mesa_clip_to_region
412 _mesa_scissor_bounding_box(const struct gl_context
*ctx
,
413 const struct gl_framebuffer
*buffer
,
414 unsigned idx
, int *bbox
)
418 bbox
[1] = buffer
->Width
;
419 bbox
[3] = buffer
->Height
;
421 _mesa_intersect_scissor_bounding_box(ctx
, idx
, bbox
);
423 assert(bbox
[0] <= bbox
[1]);
424 assert(bbox
[2] <= bbox
[3]);
428 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
429 * These values are computed from the buffer's width and height and
430 * the scissor box, if it's enabled.
431 * \param ctx the GL context.
434 _mesa_update_draw_buffer_bounds(struct gl_context
*ctx
,
435 struct gl_framebuffer
*buffer
)
442 if (_mesa_is_user_fbo(buffer
)) {
443 /* user-created framebuffer size depends on the renderbuffers */
444 update_framebuffer_size(ctx
, buffer
);
447 /* Default to the first scissor as that's always valid */
448 _mesa_scissor_bounding_box(ctx
, buffer
, 0, bbox
);
449 buffer
->_Xmin
= bbox
[0];
450 buffer
->_Ymin
= bbox
[2];
451 buffer
->_Xmax
= bbox
[1];
452 buffer
->_Ymax
= bbox
[3];
457 * The glGet queries of the framebuffer red/green/blue size, stencil size,
458 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
459 * change depending on the renderbuffer bindings. This function updates
460 * the given framebuffer's Visual from the current renderbuffer bindings.
462 * This may apply to user-created framebuffers or window system framebuffers.
464 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
465 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
466 * The former one is used to convert floating point depth values into
470 _mesa_update_framebuffer_visual(struct gl_context
*ctx
,
471 struct gl_framebuffer
*fb
)
475 memset(&fb
->Visual
, 0, sizeof(fb
->Visual
));
476 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
478 #if 0 /* this _might_ be needed */
479 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
480 /* leave visual fields zero'd */
485 /* find first RGB renderbuffer */
486 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
487 if (fb
->Attachment
[i
].Renderbuffer
) {
488 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
489 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
490 const mesa_format fmt
= rb
->Format
;
492 /* Grab samples and sampleBuffers from any attachment point (assuming
493 * the framebuffer is complete, we'll get the same answer from all
496 fb
->Visual
.samples
= rb
->NumSamples
;
497 fb
->Visual
.sampleBuffers
= rb
->NumSamples
> 0 ? 1 : 0;
499 if (_mesa_is_legal_color_format(ctx
, baseFormat
)) {
500 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
501 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
502 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
503 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
504 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
505 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
506 if (_mesa_get_format_color_encoding(fmt
) == GL_SRGB
)
507 fb
->Visual
.sRGBCapable
= ctx
->Extensions
.EXT_framebuffer_sRGB
;
513 fb
->Visual
.floatMode
= GL_FALSE
;
514 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
515 if (fb
->Attachment
[i
].Renderbuffer
) {
516 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
517 const mesa_format fmt
= rb
->Format
;
519 if (_mesa_get_format_datatype(fmt
) == GL_FLOAT
) {
520 fb
->Visual
.floatMode
= GL_TRUE
;
526 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
527 const struct gl_renderbuffer
*rb
=
528 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
529 const mesa_format fmt
= rb
->Format
;
530 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
531 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
534 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
535 const struct gl_renderbuffer
*rb
=
536 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
537 const mesa_format fmt
= rb
->Format
;
538 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
539 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
542 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
543 const struct gl_renderbuffer
*rb
=
544 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
545 const mesa_format fmt
= rb
->Format
;
546 fb
->Visual
.haveAccumBuffer
= GL_TRUE
;
547 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
548 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
549 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
550 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
553 compute_depth_max(fb
);
558 * Example DrawBuffers scenarios:
560 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
561 * "gl_FragColor" or program writes to the "result.color" register:
563 * fragment color output renderbuffer
564 * --------------------- ---------------
565 * color[0] Front, Back
568 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
569 * gl_FragData[i] or program writes to result.color[i] registers:
571 * fragment color output renderbuffer
572 * --------------------- ---------------
578 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
579 * gl_FragColor, or fixed function:
581 * fragment color output renderbuffer
582 * --------------------- ---------------
583 * color[0] Front, Aux0, Aux1
586 * In either case, the list of renderbuffers is stored in the
587 * framebuffer->_ColorDrawBuffers[] array and
588 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
589 * The renderer (like swrast) has to look at the current fragment shader
590 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
591 * how to map color outputs to renderbuffers.
593 * Note that these two calls are equivalent (for fixed function fragment
595 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
596 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
603 * Update the (derived) list of color drawing renderbuffer pointers.
604 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
608 update_color_draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
612 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
613 fb
->_ColorDrawBuffers
[0] = NULL
;
615 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
616 GLint buf
= fb
->_ColorDrawBufferIndexes
[output
];
618 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
621 fb
->_ColorDrawBuffers
[output
] = NULL
;
628 * Update the (derived) color read renderbuffer pointer.
629 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
632 update_color_read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
635 if (fb
->_ColorReadBufferIndex
== -1 ||
639 fb
->_ColorReadBuffer
= NULL
; /* legal! */
642 assert(fb
->_ColorReadBufferIndex
>= 0);
643 assert(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
645 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
651 * Update a gl_framebuffer's derived state.
653 * Specifically, update these framebuffer fields:
655 * _NumColorDrawBuffers
658 * If the framebuffer is user-created, make sure it's complete.
660 * The following functions (at least) can effect framebuffer state:
661 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
662 * glRenderbufferStorageEXT.
665 update_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
667 if (_mesa_is_winsys_fbo(fb
)) {
668 /* This is a window-system framebuffer */
669 /* Need to update the FB's GL_DRAW_BUFFER state to match the
670 * context state (GL_READ_BUFFER too).
672 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
673 _mesa_drawbuffers(ctx
, fb
, ctx
->Const
.MaxDrawBuffers
,
674 ctx
->Color
.DrawBuffer
, NULL
);
678 /* This is a user-created framebuffer.
679 * Completeness only matters for user-created framebuffers.
681 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
682 _mesa_test_framebuffer_completeness(ctx
, fb
);
686 /* Strictly speaking, we don't need to update the draw-state
687 * if this FB is bound as ctx->ReadBuffer (and conversely, the
688 * read-state if this FB is bound as ctx->DrawBuffer), but no
691 update_color_draw_buffers(ctx
, fb
);
692 update_color_read_buffer(ctx
, fb
);
694 compute_depth_max(fb
);
699 * Update state related to the draw/read framebuffers.
702 _mesa_update_framebuffer(struct gl_context
*ctx
,
703 struct gl_framebuffer
*readFb
,
704 struct gl_framebuffer
*drawFb
)
708 update_framebuffer(ctx
, drawFb
);
709 if (readFb
!= drawFb
)
710 update_framebuffer(ctx
, readFb
);
712 _mesa_update_clamp_vertex_color(ctx
, drawFb
);
713 _mesa_update_clamp_fragment_color(ctx
, drawFb
);
718 * Check if the renderbuffer for a read/draw operation exists.
719 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
720 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
721 * \param reading if TRUE, we're going to read from the buffer,
722 if FALSE, we're going to write to the buffer.
723 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
726 renderbuffer_exists(struct gl_context
*ctx
,
727 struct gl_framebuffer
*fb
,
731 const struct gl_renderbuffer_attachment
*att
= fb
->Attachment
;
733 /* If we don't know the framebuffer status, update it now */
734 if (fb
->_Status
== 0) {
735 _mesa_test_framebuffer_completeness(ctx
, fb
);
738 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
749 case GL_LUMINANCE_ALPHA
:
757 case GL_RED_INTEGER_EXT
:
759 case GL_GREEN_INTEGER_EXT
:
760 case GL_BLUE_INTEGER_EXT
:
761 case GL_ALPHA_INTEGER_EXT
:
762 case GL_RGB_INTEGER_EXT
:
763 case GL_RGBA_INTEGER_EXT
:
764 case GL_BGR_INTEGER_EXT
:
765 case GL_BGRA_INTEGER_EXT
:
766 case GL_LUMINANCE_INTEGER_EXT
:
767 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
769 /* about to read from a color buffer */
770 const struct gl_renderbuffer
*readBuf
= fb
->_ColorReadBuffer
;
774 assert(_mesa_get_format_bits(readBuf
->Format
, GL_RED_BITS
) > 0 ||
775 _mesa_get_format_bits(readBuf
->Format
, GL_ALPHA_BITS
) > 0 ||
776 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_LUMINANCE_SIZE
) > 0 ||
777 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_INTENSITY_SIZE
) > 0 ||
778 _mesa_get_format_bits(readBuf
->Format
, GL_INDEX_BITS
) > 0);
781 /* about to draw to zero or more color buffers (none is OK) */
786 case GL_DEPTH_COMPONENT
:
787 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
) {
792 case GL_STENCIL_INDEX
:
793 if (att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
797 case GL_DEPTH_STENCIL_EXT
:
798 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
||
799 att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
805 "Unexpected format 0x%x in renderbuffer_exists",
816 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
817 * glCopyTex[Sub]Image, etc) exists.
818 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
819 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
820 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
823 _mesa_source_buffer_exists(struct gl_context
*ctx
, GLenum format
)
825 return renderbuffer_exists(ctx
, ctx
->ReadBuffer
, format
, GL_TRUE
);
830 * As above, but for drawing operations.
833 _mesa_dest_buffer_exists(struct gl_context
*ctx
, GLenum format
)
835 return renderbuffer_exists(ctx
, ctx
->DrawBuffer
, format
, GL_FALSE
);
840 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
843 _mesa_get_color_read_format(struct gl_context
*ctx
)
845 if (!ctx
->ReadBuffer
|| !ctx
->ReadBuffer
->_ColorReadBuffer
) {
846 /* The spec is unclear how to handle this case, but NVIDIA's
847 * driver generates GL_INVALID_OPERATION.
849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
850 "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT: "
851 "no GL_READ_BUFFER)");
855 const GLenum format
= ctx
->ReadBuffer
->_ColorReadBuffer
->Format
;
856 const GLenum data_type
= _mesa_get_format_datatype(format
);
858 if (format
== MESA_FORMAT_B8G8R8A8_UNORM
)
860 else if (format
== MESA_FORMAT_B5G6R5_UNORM
)
864 case GL_UNSIGNED_INT
:
866 return GL_RGBA_INTEGER
;
875 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
878 _mesa_get_color_read_type(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_TYPE: "
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_B5G6R5_UNORM
)
894 return GL_UNSIGNED_SHORT_5_6_5_REV
;
897 case GL_SIGNED_NORMALIZED
:
899 case GL_UNSIGNED_INT
:
903 case GL_UNSIGNED_NORMALIZED
:
905 return GL_UNSIGNED_BYTE
;
912 * Returns the read renderbuffer for the specified format.
914 struct gl_renderbuffer
*
915 _mesa_get_read_renderbuffer_for_format(const struct gl_context
*ctx
,
918 const struct gl_framebuffer
*rfb
= ctx
->ReadBuffer
;
920 if (_mesa_is_color_format(format
)) {
921 return rfb
->Attachment
[rfb
->_ColorReadBufferIndex
].Renderbuffer
;
922 } else if (_mesa_is_depth_format(format
) ||
923 _mesa_is_depthstencil_format(format
)) {
924 return rfb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
926 return rfb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
932 * Print framebuffer info to stderr, for debugging.
935 _mesa_print_framebuffer(const struct gl_framebuffer
*fb
)
939 fprintf(stderr
, "Mesa Framebuffer %u at %p\n", fb
->Name
, (void *) fb
);
940 fprintf(stderr
, " Size: %u x %u Status: %s\n", fb
->Width
, fb
->Height
,
941 _mesa_enum_to_string(fb
->_Status
));
942 fprintf(stderr
, " Attachments:\n");
944 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
945 const struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
946 if (att
->Type
== GL_TEXTURE
) {
947 const struct gl_texture_image
*texImage
= att
->Renderbuffer
->TexImage
;
949 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
950 i
, att
->Texture
->Name
, att
->TextureLevel
, att
->CubeMapFace
,
951 att
->Zoffset
, att
->Complete
);
952 fprintf(stderr
, " Size: %u x %u x %u Format %s\n",
953 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
954 _mesa_get_format_name(texImage
->TexFormat
));
956 else if (att
->Type
== GL_RENDERBUFFER
) {
957 fprintf(stderr
, " %2d: Renderbuffer %u, complete %d\n",
958 i
, att
->Renderbuffer
->Name
, att
->Complete
);
959 fprintf(stderr
, " Size: %u x %u Format %s\n",
960 att
->Renderbuffer
->Width
, att
->Renderbuffer
->Height
,
961 _mesa_get_format_name(att
->Renderbuffer
->Format
));
964 fprintf(stderr
, " %2d: none\n", i
);