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.
34 #include "util/imports.h"
43 #include "framebuffer.h"
44 #include "renderbuffer.h"
46 #include "glformats.h"
52 * Compute/set the _DepthMax field for the given framebuffer.
53 * This value depends on the Z buffer resolution.
56 compute_depth_max(struct gl_framebuffer
*fb
)
58 if (fb
->Visual
.depthBits
== 0) {
59 /* Special case. Even if we don't have a depth buffer we need
60 * good values for DepthMax for Z vertex transformation purposes
61 * and for per-fragment fog computation.
63 fb
->_DepthMax
= (1 << 16) - 1;
65 else if (fb
->Visual
.depthBits
< 32) {
66 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
69 /* Special case since shift values greater than or equal to the
70 * number of bits in the left hand expression's type are undefined.
72 fb
->_DepthMax
= 0xffffffff;
74 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
76 /* Minimum resolvable depth value, for polygon offset */
77 fb
->_MRD
= (GLfloat
)1.0 / fb
->_DepthMaxF
;
81 * Create and initialize a gl_framebuffer object.
82 * This is intended for creating _window_system_ framebuffers, not generic
83 * framebuffer objects ala GL_EXT_framebuffer_object.
85 * \sa _mesa_new_framebuffer
87 struct gl_framebuffer
*
88 _mesa_create_framebuffer(const struct gl_config
*visual
)
90 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
93 _mesa_initialize_window_framebuffer(fb
, visual
);
100 * Allocate a new gl_framebuffer object.
101 * This is the default function for ctx->Driver.NewFramebuffer().
102 * This is for allocating user-created framebuffers, not window-system
104 * \sa _mesa_create_framebuffer
106 struct gl_framebuffer
*
107 _mesa_new_framebuffer(struct gl_context
*ctx
, GLuint name
)
109 struct gl_framebuffer
*fb
;
112 fb
= CALLOC_STRUCT(gl_framebuffer
);
114 _mesa_initialize_user_framebuffer(fb
, name
);
121 * Initialize a gl_framebuffer object. Typically used to initialize
122 * window system-created framebuffers, not user-created framebuffers.
123 * \sa _mesa_initialize_user_framebuffer
126 _mesa_initialize_window_framebuffer(struct gl_framebuffer
*fb
,
127 const struct gl_config
*visual
)
132 memset(fb
, 0, sizeof(struct gl_framebuffer
));
134 simple_mtx_init(&fb
->Mutex
, mtx_plain
);
138 /* save the visual */
139 fb
->Visual
= *visual
;
141 /* Init read/draw renderbuffer state */
142 if (visual
->doubleBufferMode
) {
143 fb
->_NumColorDrawBuffers
= 1;
144 fb
->ColorDrawBuffer
[0] = GL_BACK
;
145 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_BACK_LEFT
;
146 fb
->ColorReadBuffer
= GL_BACK
;
147 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
150 fb
->_NumColorDrawBuffers
= 1;
151 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
152 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_FRONT_LEFT
;
153 fb
->ColorReadBuffer
= GL_FRONT
;
154 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
157 fb
->Delete
= _mesa_destroy_framebuffer
;
158 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
159 fb
->_AllColorBuffersFixedPoint
= !visual
->floatMode
;
160 fb
->_HasSNormOrFloatColorBuffer
= visual
->floatMode
;
161 fb
->_HasAttachments
= true;
164 fb
->SampleLocationTable
= NULL
;
165 fb
->ProgrammableSampleLocations
= 0;
166 fb
->SampleLocationPixelGrid
= 0;
168 compute_depth_max(fb
);
173 * Initialize a user-created gl_framebuffer object.
174 * \sa _mesa_initialize_window_framebuffer
177 _mesa_initialize_user_framebuffer(struct gl_framebuffer
*fb
, GLuint name
)
182 memset(fb
, 0, sizeof(struct gl_framebuffer
));
186 fb
->_NumColorDrawBuffers
= 1;
187 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
188 fb
->_ColorDrawBufferIndexes
[0] = BUFFER_COLOR0
;
189 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
190 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
191 fb
->SampleLocationTable
= NULL
;
192 fb
->ProgrammableSampleLocations
= 0;
193 fb
->SampleLocationPixelGrid
= 0;
194 fb
->Delete
= _mesa_destroy_framebuffer
;
195 simple_mtx_init(&fb
->Mutex
, mtx_plain
);
200 * Deallocate buffer and everything attached to it.
201 * Typically called via the gl_framebuffer->Delete() method.
204 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
207 _mesa_free_framebuffer_data(fb
);
215 * Free all the data hanging off the given gl_framebuffer, but don't free
216 * the gl_framebuffer object itself.
219 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
222 assert(fb
->RefCount
== 0);
224 simple_mtx_destroy(&fb
->Mutex
);
226 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
227 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
228 if (att
->Renderbuffer
) {
229 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
232 _mesa_reference_texobj(&att
->Texture
, NULL
);
234 assert(!att
->Renderbuffer
);
235 assert(!att
->Texture
);
239 free(fb
->SampleLocationTable
);
240 fb
->SampleLocationTable
= NULL
;
245 * Set *ptr to point to fb, with refcounting and locking.
246 * This is normally only called from the _mesa_reference_framebuffer() macro
247 * when there's a real pointer change.
250 _mesa_reference_framebuffer_(struct gl_framebuffer
**ptr
,
251 struct gl_framebuffer
*fb
)
254 /* unreference old renderbuffer */
255 GLboolean deleteFlag
= GL_FALSE
;
256 struct gl_framebuffer
*oldFb
= *ptr
;
258 simple_mtx_lock(&oldFb
->Mutex
);
259 assert(oldFb
->RefCount
> 0);
261 deleteFlag
= (oldFb
->RefCount
== 0);
262 simple_mtx_unlock(&oldFb
->Mutex
);
265 oldFb
->Delete(oldFb
);
271 simple_mtx_lock(&fb
->Mutex
);
273 simple_mtx_unlock(&fb
->Mutex
);
280 * Resize the given framebuffer's renderbuffers to the new width and height.
281 * This should only be used for window-system framebuffers, not
282 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
283 * This will typically be called directly from a device driver.
285 * \note it's possible for ctx to be null since a window can be resized
286 * without a currently bound rendering context.
289 _mesa_resize_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
290 GLuint width
, GLuint height
)
292 /* XXX I think we could check if the size is not changing
296 /* Can only resize win-sys framebuffer objects */
297 assert(_mesa_is_winsys_fbo(fb
));
299 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
300 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
301 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
302 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
303 /* only resize if size is changing */
304 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
305 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
306 assert(rb
->Width
== width
);
307 assert(rb
->Height
== height
);
310 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
321 /* update scissor / window bounds */
322 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
323 /* Signal new buffer state so that swrast will update its clipping
324 * info (the CLIP_BIT flag).
326 ctx
->NewState
|= _NEW_BUFFERS
;
331 * Given a bounding box, intersect the bounding box with the scissor of
332 * a specified vieport.
334 * \param ctx GL context.
335 * \param idx Index of the desired viewport
336 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
340 _mesa_intersect_scissor_bounding_box(const struct gl_context
*ctx
,
341 unsigned idx
, int *bbox
)
343 if (ctx
->Scissor
.EnableFlags
& (1u << idx
)) {
344 if (ctx
->Scissor
.ScissorArray
[idx
].X
> bbox
[0]) {
345 bbox
[0] = ctx
->Scissor
.ScissorArray
[idx
].X
;
347 if (ctx
->Scissor
.ScissorArray
[idx
].Y
> bbox
[2]) {
348 bbox
[2] = ctx
->Scissor
.ScissorArray
[idx
].Y
;
350 if (ctx
->Scissor
.ScissorArray
[idx
].X
+ ctx
->Scissor
.ScissorArray
[idx
].Width
< bbox
[1]) {
351 bbox
[1] = ctx
->Scissor
.ScissorArray
[idx
].X
+ ctx
->Scissor
.ScissorArray
[idx
].Width
;
353 if (ctx
->Scissor
.ScissorArray
[idx
].Y
+ ctx
->Scissor
.ScissorArray
[idx
].Height
< bbox
[3]) {
354 bbox
[3] = ctx
->Scissor
.ScissorArray
[idx
].Y
+ ctx
->Scissor
.ScissorArray
[idx
].Height
;
356 /* finally, check for empty region */
357 if (bbox
[0] > bbox
[1]) {
360 if (bbox
[2] > bbox
[3]) {
367 * Calculate the inclusive bounding box for the scissor of a specific viewport
369 * \param ctx GL context.
370 * \param buffer Framebuffer to be checked against
371 * \param idx Index of the desired viewport
372 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
375 * \warning This function assumes that the framebuffer dimensions are up to
378 * \sa _mesa_clip_to_region
381 scissor_bounding_box(const struct gl_context
*ctx
,
382 const struct gl_framebuffer
*buffer
,
383 unsigned idx
, int *bbox
)
387 bbox
[1] = buffer
->Width
;
388 bbox
[3] = buffer
->Height
;
390 _mesa_intersect_scissor_bounding_box(ctx
, idx
, bbox
);
392 assert(bbox
[0] <= bbox
[1]);
393 assert(bbox
[2] <= bbox
[3]);
397 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
398 * These values are computed from the buffer's width and height and
399 * the scissor box, if it's enabled.
400 * \param ctx the GL context.
403 _mesa_update_draw_buffer_bounds(struct gl_context
*ctx
,
404 struct gl_framebuffer
*buffer
)
411 /* Default to the first scissor as that's always valid */
412 scissor_bounding_box(ctx
, buffer
, 0, bbox
);
413 buffer
->_Xmin
= bbox
[0];
414 buffer
->_Ymin
= bbox
[2];
415 buffer
->_Xmax
= bbox
[1];
416 buffer
->_Ymax
= bbox
[3];
421 * The glGet queries of the framebuffer red/green/blue size, stencil size,
422 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
423 * change depending on the renderbuffer bindings. This function updates
424 * the given framebuffer's Visual from the current renderbuffer bindings.
426 * This may apply to user-created framebuffers or window system framebuffers.
428 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
429 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
430 * The former one is used to convert floating point depth values into
434 _mesa_update_framebuffer_visual(struct gl_context
*ctx
,
435 struct gl_framebuffer
*fb
)
437 memset(&fb
->Visual
, 0, sizeof(fb
->Visual
));
439 /* find first RGB renderbuffer */
440 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
441 if (fb
->Attachment
[i
].Renderbuffer
) {
442 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
443 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
444 const mesa_format fmt
= rb
->Format
;
446 /* Grab samples and sampleBuffers from any attachment point (assuming
447 * the framebuffer is complete, we'll get the same answer from all
450 fb
->Visual
.samples
= rb
->NumSamples
;
451 fb
->Visual
.sampleBuffers
= rb
->NumSamples
> 0 ? 1 : 0;
453 if (_mesa_is_legal_color_format(ctx
, baseFormat
)) {
454 fb
->Visual
.redBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
455 fb
->Visual
.greenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
456 fb
->Visual
.blueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
457 fb
->Visual
.alphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
458 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
459 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
460 if (_mesa_is_format_srgb(fmt
))
461 fb
->Visual
.sRGBCapable
= ctx
->Extensions
.EXT_sRGB
;
467 fb
->Visual
.floatMode
= GL_FALSE
;
468 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
469 if (fb
->Attachment
[i
].Renderbuffer
) {
470 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
471 const mesa_format fmt
= rb
->Format
;
473 if (_mesa_get_format_datatype(fmt
) == GL_FLOAT
) {
474 fb
->Visual
.floatMode
= GL_TRUE
;
480 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
481 const struct gl_renderbuffer
*rb
=
482 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
483 const mesa_format fmt
= rb
->Format
;
484 fb
->Visual
.depthBits
= _mesa_get_format_bits(fmt
, GL_DEPTH_BITS
);
487 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
488 const struct gl_renderbuffer
*rb
=
489 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
490 const mesa_format fmt
= rb
->Format
;
491 fb
->Visual
.stencilBits
= _mesa_get_format_bits(fmt
, GL_STENCIL_BITS
);
494 if (fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
495 const struct gl_renderbuffer
*rb
=
496 fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
497 const mesa_format fmt
= rb
->Format
;
498 fb
->Visual
.accumRedBits
= _mesa_get_format_bits(fmt
, GL_RED_BITS
);
499 fb
->Visual
.accumGreenBits
= _mesa_get_format_bits(fmt
, GL_GREEN_BITS
);
500 fb
->Visual
.accumBlueBits
= _mesa_get_format_bits(fmt
, GL_BLUE_BITS
);
501 fb
->Visual
.accumAlphaBits
= _mesa_get_format_bits(fmt
, GL_ALPHA_BITS
);
504 compute_depth_max(fb
);
505 _mesa_update_allow_draw_out_of_order(ctx
);
510 * Example DrawBuffers scenarios:
512 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
513 * "gl_FragColor" or program writes to the "result.color" register:
515 * fragment color output renderbuffer
516 * --------------------- ---------------
517 * color[0] Front, Back
520 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
521 * gl_FragData[i] or program writes to result.color[i] registers:
523 * fragment color output renderbuffer
524 * --------------------- ---------------
530 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
531 * gl_FragColor, or fixed function:
533 * fragment color output renderbuffer
534 * --------------------- ---------------
535 * color[0] Front, Aux0, Aux1
538 * In either case, the list of renderbuffers is stored in the
539 * framebuffer->_ColorDrawBuffers[] array and
540 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
541 * The renderer (like swrast) has to look at the current fragment shader
542 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
543 * how to map color outputs to renderbuffers.
545 * Note that these two calls are equivalent (for fixed function fragment
547 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
548 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
555 * Update the (derived) list of color drawing renderbuffer pointers.
556 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
560 update_color_draw_buffers(struct gl_framebuffer
*fb
)
564 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
565 fb
->_ColorDrawBuffers
[0] = NULL
;
567 for (output
= 0; output
< fb
->_NumColorDrawBuffers
; output
++) {
568 gl_buffer_index buf
= fb
->_ColorDrawBufferIndexes
[output
];
569 if (buf
!= BUFFER_NONE
) {
570 fb
->_ColorDrawBuffers
[output
] = fb
->Attachment
[buf
].Renderbuffer
;
573 fb
->_ColorDrawBuffers
[output
] = NULL
;
580 * Update the (derived) color read renderbuffer pointer.
581 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
584 update_color_read_buffer(struct gl_framebuffer
*fb
)
586 if (fb
->_ColorReadBufferIndex
== BUFFER_NONE
||
590 fb
->_ColorReadBuffer
= NULL
; /* legal! */
593 assert(fb
->_ColorReadBufferIndex
>= 0);
594 assert(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
596 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
602 * Update a gl_framebuffer's derived state.
604 * Specifically, update these framebuffer fields:
606 * _NumColorDrawBuffers
609 * If the framebuffer is user-created, make sure it's complete.
611 * The following functions (at least) can effect framebuffer state:
612 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
613 * glRenderbufferStorageEXT.
616 update_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
618 if (_mesa_is_winsys_fbo(fb
)) {
619 /* This is a window-system framebuffer */
620 /* Need to update the FB's GL_DRAW_BUFFER state to match the
621 * context state (GL_READ_BUFFER too).
623 if (fb
->ColorDrawBuffer
[0] != ctx
->Color
.DrawBuffer
[0]) {
624 _mesa_drawbuffers(ctx
, fb
, ctx
->Const
.MaxDrawBuffers
,
625 ctx
->Color
.DrawBuffer
, NULL
);
628 /* Call device driver function if fb is the bound draw buffer. */
629 if (fb
== ctx
->DrawBuffer
) {
630 if (ctx
->Driver
.DrawBufferAllocate
)
631 ctx
->Driver
.DrawBufferAllocate(ctx
);
635 /* This is a user-created framebuffer.
636 * Completeness only matters for user-created framebuffers.
638 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
639 _mesa_test_framebuffer_completeness(ctx
, fb
);
643 /* Strictly speaking, we don't need to update the draw-state
644 * if this FB is bound as ctx->ReadBuffer (and conversely, the
645 * read-state if this FB is bound as ctx->DrawBuffer), but no
648 update_color_draw_buffers(fb
);
649 update_color_read_buffer(fb
);
651 compute_depth_max(fb
);
656 * Update state related to the draw/read framebuffers.
659 _mesa_update_framebuffer(struct gl_context
*ctx
,
660 struct gl_framebuffer
*readFb
,
661 struct gl_framebuffer
*drawFb
)
665 update_framebuffer(ctx
, drawFb
);
666 if (readFb
!= drawFb
)
667 update_framebuffer(ctx
, readFb
);
669 _mesa_update_clamp_vertex_color(ctx
, drawFb
);
670 _mesa_update_clamp_fragment_color(ctx
, drawFb
);
675 * Check if the renderbuffer for a read/draw operation exists.
676 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
677 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
678 * \param reading if TRUE, we're going to read from the buffer,
679 if FALSE, we're going to write to the buffer.
680 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
683 renderbuffer_exists(struct gl_context
*ctx
,
684 struct gl_framebuffer
*fb
,
688 const struct gl_renderbuffer_attachment
*att
= fb
->Attachment
;
690 /* If we don't know the framebuffer status, update it now */
691 if (fb
->_Status
== 0) {
692 _mesa_test_framebuffer_completeness(ctx
, fb
);
695 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
706 case GL_LUMINANCE_ALPHA
:
714 case GL_RED_INTEGER_EXT
:
716 case GL_GREEN_INTEGER_EXT
:
717 case GL_BLUE_INTEGER_EXT
:
718 case GL_ALPHA_INTEGER_EXT
:
719 case GL_RGB_INTEGER_EXT
:
720 case GL_RGBA_INTEGER_EXT
:
721 case GL_BGR_INTEGER_EXT
:
722 case GL_BGRA_INTEGER_EXT
:
723 case GL_LUMINANCE_INTEGER_EXT
:
724 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
726 /* about to read from a color buffer */
727 const struct gl_renderbuffer
*readBuf
= fb
->_ColorReadBuffer
;
731 assert(_mesa_get_format_bits(readBuf
->Format
, GL_RED_BITS
) > 0 ||
732 _mesa_get_format_bits(readBuf
->Format
, GL_ALPHA_BITS
) > 0 ||
733 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_LUMINANCE_SIZE
) > 0 ||
734 _mesa_get_format_bits(readBuf
->Format
, GL_TEXTURE_INTENSITY_SIZE
) > 0 ||
735 _mesa_get_format_bits(readBuf
->Format
, GL_INDEX_BITS
) > 0);
738 /* about to draw to zero or more color buffers (none is OK) */
743 case GL_DEPTH_COMPONENT
:
744 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
) {
749 case GL_STENCIL_INDEX
:
750 if (att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
754 case GL_DEPTH_STENCIL_EXT
:
755 if (att
[BUFFER_DEPTH
].Type
== GL_NONE
||
756 att
[BUFFER_STENCIL
].Type
== GL_NONE
) {
762 "Unexpected format 0x%x in renderbuffer_exists",
773 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
774 * glCopyTex[Sub]Image, etc) exists.
775 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
776 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
777 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
780 _mesa_source_buffer_exists(struct gl_context
*ctx
, GLenum format
)
782 return renderbuffer_exists(ctx
, ctx
->ReadBuffer
, format
, GL_TRUE
);
787 * As above, but for drawing operations.
790 _mesa_dest_buffer_exists(struct gl_context
*ctx
, GLenum format
)
792 return renderbuffer_exists(ctx
, ctx
->DrawBuffer
, format
, GL_FALSE
);
797 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES queries (using
798 * GetIntegerv, GetFramebufferParameteriv, etc)
800 * If @fb is NULL, the method returns the value for the current bound
804 _mesa_get_color_read_format(struct gl_context
*ctx
,
805 struct gl_framebuffer
*fb
,
809 _mesa_update_state(ctx
);
812 fb
= ctx
->ReadBuffer
;
814 if (!fb
|| !fb
->_ColorReadBuffer
) {
816 * From OpenGL 4.5 spec, section 18.2.2 "ReadPixels":
818 * "An INVALID_OPERATION error is generated by GetIntegerv if pname
819 * is IMPLEMENTATION_COLOR_READ_FORMAT or IMPLEMENTATION_COLOR_-
820 * READ_TYPE and any of:
821 * * the read framebuffer is not framebuffer complete.
822 * * the read framebuffer is a framebuffer object, and the selected
823 * read buffer (see section 18.2.1) has no image attached.
824 * * the selected read buffer is NONE."
826 * There is not equivalent quote for GetFramebufferParameteriv or
827 * GetNamedFramebufferParameteriv, but from section 9.2.3 "Framebuffer
830 * "Values of framebuffer-dependent state are identical to those that
831 * would be obtained were the framebuffer object bound and queried
832 * using the simple state queries in that table."
834 * Where "using the simple state queries" refer to use GetIntegerv. So
835 * we will assume that on that situation the same error should be
838 _mesa_error(ctx
, GL_INVALID_OPERATION
,
839 "%s(GL_IMPLEMENTATION_COLOR_READ_FORMAT: no GL_READ_BUFFER)",
844 const mesa_format format
= fb
->_ColorReadBuffer
->Format
;
847 case MESA_FORMAT_RGBA_UINT8
:
848 return GL_RGBA_INTEGER
;
849 case MESA_FORMAT_B8G8R8A8_UNORM
:
851 case MESA_FORMAT_B5G6R5_UNORM
:
852 case MESA_FORMAT_R11G11B10_FLOAT
:
854 case MESA_FORMAT_RG_FLOAT32
:
855 case MESA_FORMAT_RG_FLOAT16
:
856 case MESA_FORMAT_RG_UNORM8
:
858 case MESA_FORMAT_RG_SINT32
:
859 case MESA_FORMAT_RG_UINT32
:
860 case MESA_FORMAT_RG_SINT16
:
861 case MESA_FORMAT_RG_UINT16
:
862 case MESA_FORMAT_RG_SINT8
:
863 case MESA_FORMAT_RG_UINT8
:
864 return GL_RG_INTEGER
;
865 case MESA_FORMAT_R_FLOAT32
:
866 case MESA_FORMAT_R_FLOAT16
:
867 case MESA_FORMAT_R_UNORM16
:
868 case MESA_FORMAT_R_UNORM8
:
869 case MESA_FORMAT_R_SNORM16
:
870 case MESA_FORMAT_R_SNORM8
:
872 case MESA_FORMAT_R_SINT32
:
873 case MESA_FORMAT_R_UINT32
:
874 case MESA_FORMAT_R_SINT16
:
875 case MESA_FORMAT_R_UINT16
:
876 case MESA_FORMAT_R_SINT8
:
877 case MESA_FORMAT_R_UINT8
:
878 return GL_RED_INTEGER
;
883 if (_mesa_is_format_integer(format
))
884 return GL_RGBA_INTEGER
;
892 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES queries (using
893 * GetIntegerv, GetFramebufferParameteriv, etc)
895 * If @fb is NULL, the method returns the value for the current bound
899 _mesa_get_color_read_type(struct gl_context
*ctx
,
900 struct gl_framebuffer
*fb
,
904 _mesa_update_state(ctx
);
907 fb
= ctx
->ReadBuffer
;
909 if (!fb
|| !fb
->_ColorReadBuffer
) {
911 * See comment on _mesa_get_color_read_format
913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
914 "%s(GL_IMPLEMENTATION_COLOR_READ_TYPE: no GL_READ_BUFFER)",
919 const mesa_format format
= fb
->_ColorReadBuffer
->Format
;
923 _mesa_uncompressed_format_to_type_and_comps(format
, &data_type
, &comps
);
931 * Returns the read renderbuffer for the specified format.
933 struct gl_renderbuffer
*
934 _mesa_get_read_renderbuffer_for_format(const struct gl_context
*ctx
,
937 const struct gl_framebuffer
*rfb
= ctx
->ReadBuffer
;
939 if (_mesa_is_color_format(format
)) {
940 return rfb
->Attachment
[rfb
->_ColorReadBufferIndex
].Renderbuffer
;
941 } else if (_mesa_is_depth_format(format
) ||
942 _mesa_is_depthstencil_format(format
)) {
943 return rfb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
945 return rfb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
951 * Print framebuffer info to stderr, for debugging.
954 _mesa_print_framebuffer(const struct gl_framebuffer
*fb
)
956 fprintf(stderr
, "Mesa Framebuffer %u at %p\n", fb
->Name
, (void *) fb
);
957 fprintf(stderr
, " Size: %u x %u Status: %s\n", fb
->Width
, fb
->Height
,
958 _mesa_enum_to_string(fb
->_Status
));
959 fprintf(stderr
, " Attachments:\n");
961 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
962 const struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
963 if (att
->Type
== GL_TEXTURE
) {
964 const struct gl_texture_image
*texImage
= att
->Renderbuffer
->TexImage
;
966 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
967 i
, att
->Texture
->Name
, att
->TextureLevel
, att
->CubeMapFace
,
968 att
->Zoffset
, att
->Complete
);
969 fprintf(stderr
, " Size: %u x %u x %u Format %s\n",
970 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
971 _mesa_get_format_name(texImage
->TexFormat
));
973 else if (att
->Type
== GL_RENDERBUFFER
) {
974 fprintf(stderr
, " %2d: Renderbuffer %u, complete %d\n",
975 i
, att
->Renderbuffer
->Name
, att
->Complete
);
976 fprintf(stderr
, " Size: %u x %u Format %s\n",
977 att
->Renderbuffer
->Width
, att
->Renderbuffer
->Height
,
978 _mesa_get_format_name(att
->Renderbuffer
->Format
));
981 fprintf(stderr
, " %2d: none\n", i
);
987 _mesa_is_front_buffer_reading(const struct gl_framebuffer
*fb
)
989 if (!fb
|| _mesa_is_user_fbo(fb
))
992 return fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
;
996 _mesa_is_front_buffer_drawing(const struct gl_framebuffer
*fb
)
998 if (!fb
|| _mesa_is_user_fbo(fb
))
1001 return (fb
->_NumColorDrawBuffers
>= 1 &&
1002 fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
);
1005 static inline GLuint
1006 _mesa_geometric_nonvalidated_samples(const struct gl_framebuffer
*buffer
)
1008 return buffer
->_HasAttachments
?
1009 buffer
->Visual
.samples
:
1010 buffer
->DefaultGeometry
.NumSamples
;
1014 _mesa_is_multisample_enabled(const struct gl_context
*ctx
)
1016 /* The sample count may not be validated by the driver, but when it is set,
1017 * we know that is in a valid range and no driver should ever validate a
1018 * multisampled framebuffer to non-multisampled and vice-versa.
1020 return ctx
->Multisample
.Enabled
&&
1022 _mesa_geometric_nonvalidated_samples(ctx
->DrawBuffer
) >= 1;
1026 * Is alpha testing enabled and applicable to the currently bound
1030 _mesa_is_alpha_test_enabled(const struct gl_context
*ctx
)
1032 bool buffer0_is_integer
= ctx
->DrawBuffer
->_IntegerBuffers
& 0x1;
1033 return (ctx
->Color
.AlphaEnabled
&& !buffer0_is_integer
);
1037 * Is alpha to coverage enabled and applicable to the currently bound
1041 _mesa_is_alpha_to_coverage_enabled(const struct gl_context
*ctx
)
1043 bool buffer0_is_integer
= ctx
->DrawBuffer
->_IntegerBuffers
& 0x1;
1044 return (ctx
->Multisample
.SampleAlphaToCoverage
&&
1045 _mesa_is_multisample_enabled(ctx
) &&
1046 !buffer0_is_integer
);