2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 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.
28 * glReadBuffer, DrawBuffer functions.
45 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
46 * available to the rendering context (for drawing or reading).
47 * This depends on the type of framebuffer. For window system framebuffers
48 * we look at the framebuffer's visual. But for user-create framebuffers we
49 * look at the number of supported color attachments.
50 * \param fb the framebuffer to draw to, or read from
51 * \return bitmask of BUFFER_BIT_* flags
54 supported_buffer_bitmask(const struct gl_context
*ctx
,
55 const struct gl_framebuffer
*fb
)
57 GLbitfield mask
= 0x0;
59 if (_mesa_is_user_fbo(fb
)) {
60 /* A user-created renderbuffer */
61 mask
= ((1 << ctx
->Const
.MaxColorAttachments
) - 1) << BUFFER_COLOR0
;
64 /* A window system framebuffer */
66 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
67 if (fb
->Visual
.stereoMode
) {
68 mask
|= BUFFER_BIT_FRONT_RIGHT
;
69 if (fb
->Visual
.doubleBufferMode
) {
70 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
73 else if (fb
->Visual
.doubleBufferMode
) {
74 mask
|= BUFFER_BIT_BACK_LEFT
;
77 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
78 mask
|= (BUFFER_BIT_AUX0
<< i
);
87 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
88 * Given a GLenum naming one or more color buffers (such as
89 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
92 draw_buffer_enum_to_bitmask(const struct gl_context
*ctx
, GLenum buffer
)
98 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
100 if (_mesa_is_gles(ctx
)) {
101 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
102 * ES 3.0.1 specification says:
104 * "When draw buffer zero is BACK, color values are written
105 * into the sole buffer for single-buffered contexts, or into
106 * the back buffer for double-buffered contexts."
108 * Since there is no stereo rendering in ES 3.0, only return the
109 * LEFT bits. This also satisfies the "n must be 1" requirement.
111 * We also do this for GLES 1 and 2 because those APIs have no
112 * concept of selecting the front and back buffer anyway and it's
113 * convenient to be able to maintain the magic behaviour of
114 * GL_BACK in that case.
116 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
117 return BUFFER_BIT_BACK_LEFT
;
118 return BUFFER_BIT_FRONT_LEFT
;
120 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
122 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
124 return BUFFER_BIT_FRONT_RIGHT
;
126 return BUFFER_BIT_BACK_RIGHT
;
128 return BUFFER_BIT_BACK_LEFT
;
129 case GL_FRONT_AND_BACK
:
130 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
131 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
133 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
135 return BUFFER_BIT_FRONT_LEFT
;
137 return BUFFER_BIT_AUX0
;
141 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
142 case GL_COLOR_ATTACHMENT0_EXT
:
143 return BUFFER_BIT_COLOR0
;
144 case GL_COLOR_ATTACHMENT1_EXT
:
145 return BUFFER_BIT_COLOR1
;
146 case GL_COLOR_ATTACHMENT2_EXT
:
147 return BUFFER_BIT_COLOR2
;
148 case GL_COLOR_ATTACHMENT3_EXT
:
149 return BUFFER_BIT_COLOR3
;
150 case GL_COLOR_ATTACHMENT4_EXT
:
151 return BUFFER_BIT_COLOR4
;
152 case GL_COLOR_ATTACHMENT5_EXT
:
153 return BUFFER_BIT_COLOR5
;
154 case GL_COLOR_ATTACHMENT6_EXT
:
155 return BUFFER_BIT_COLOR6
;
156 case GL_COLOR_ATTACHMENT7_EXT
:
157 return BUFFER_BIT_COLOR7
;
159 /* not an error, but also not supported */
160 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
161 return 1 << BUFFER_COUNT
;
169 * Helper routine used by glReadBuffer.
170 * Given a GLenum naming a color buffer, return the index of the corresponding
171 * renderbuffer (a BUFFER_* value).
172 * return -1 for an invalid buffer.
174 static gl_buffer_index
175 read_buffer_enum_to_index(GLenum buffer
)
179 return BUFFER_FRONT_LEFT
;
181 return BUFFER_BACK_LEFT
;
183 return BUFFER_FRONT_RIGHT
;
185 return BUFFER_FRONT_RIGHT
;
187 return BUFFER_BACK_RIGHT
;
189 return BUFFER_BACK_LEFT
;
191 return BUFFER_FRONT_LEFT
;
193 return BUFFER_FRONT_LEFT
;
199 return BUFFER_COUNT
; /* invalid, but not -1 */
200 case GL_COLOR_ATTACHMENT0_EXT
:
201 return BUFFER_COLOR0
;
202 case GL_COLOR_ATTACHMENT1_EXT
:
203 return BUFFER_COLOR1
;
204 case GL_COLOR_ATTACHMENT2_EXT
:
205 return BUFFER_COLOR2
;
206 case GL_COLOR_ATTACHMENT3_EXT
:
207 return BUFFER_COLOR3
;
208 case GL_COLOR_ATTACHMENT4_EXT
:
209 return BUFFER_COLOR4
;
210 case GL_COLOR_ATTACHMENT5_EXT
:
211 return BUFFER_COLOR5
;
212 case GL_COLOR_ATTACHMENT6_EXT
:
213 return BUFFER_COLOR6
;
214 case GL_COLOR_ATTACHMENT7_EXT
:
215 return BUFFER_COLOR7
;
217 /* not an error, but also not supported */
218 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
226 is_legal_es3_readbuffer_enum(GLenum buf
)
228 return buf
== GL_BACK
|| buf
== GL_NONE
||
229 (buf
>= GL_COLOR_ATTACHMENT0
&& buf
<= GL_COLOR_ATTACHMENT31
);
233 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
234 * Specify which renderbuffer(s) to draw into for the first color output.
235 * <buffer> can name zero, one, two or four renderbuffers!
236 * \sa _mesa_DrawBuffers
238 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
240 * Note that the behaviour of this function depends on whether the
241 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
242 * framebuffer object.
243 * In the former case, we update the per-context ctx->Color.DrawBuffer
244 * state var _and_ the FB's ColorDrawBuffer state.
245 * In the later case, we update the FB's ColorDrawBuffer state only.
247 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
248 * new FB is a window system FB, we need to re-update the FB's
249 * ColorDrawBuffer state to match the context. This is handled in
250 * _mesa_update_framebuffer().
252 * See the GL_EXT_framebuffer_object spec for more info.
255 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
256 GLenum buffer
, const char *caller
)
260 FLUSH_VERTICES(ctx
, 0);
262 if (MESA_VERBOSE
& VERBOSE_API
) {
263 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
266 if (buffer
== GL_NONE
) {
270 const GLbitfield supportedMask
271 = supported_buffer_bitmask(ctx
, fb
);
272 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
273 if (destMask
== BAD_MASK
) {
274 /* totally bogus buffer */
275 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
276 _mesa_enum_to_string(buffer
));
279 destMask
&= supportedMask
;
280 if (destMask
== 0x0) {
281 /* none of the named color buffers exist! */
282 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
283 caller
, _mesa_enum_to_string(buffer
));
288 /* if we get here, there's no error so set new state */
289 _mesa_drawbuffers(ctx
, fb
, 1, &buffer
, &destMask
);
291 /* Call device driver function only if fb is the bound draw buffer */
292 if (fb
== ctx
->DrawBuffer
) {
293 if (ctx
->Driver
.DrawBuffers
)
294 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
295 else if (ctx
->Driver
.DrawBuffer
)
296 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
302 _mesa_DrawBuffer(GLenum buffer
)
304 GET_CURRENT_CONTEXT(ctx
);
305 draw_buffer(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
310 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
312 GET_CURRENT_CONTEXT(ctx
);
313 struct gl_framebuffer
*fb
;
316 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
317 "glNamedFramebufferDrawBuffer");
322 fb
= ctx
->WinSysDrawBuffer
;
324 draw_buffer(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
329 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
330 * the destination color renderbuffers for N fragment program color outputs.
331 * \sa _mesa_DrawBuffer
332 * \param n number of outputs
333 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
334 * names cannot specify more than one buffer. For example,
335 * GL_FRONT_AND_BACK is illegal.
338 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
339 GLsizei n
, const GLenum
*buffers
, const char *caller
)
342 GLbitfield usedBufferMask
, supportedMask
;
343 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
345 FLUSH_VERTICES(ctx
, 0);
347 /* Turns out n==0 is a valid input that should not produce an error.
348 * The remaining code below correctly handles the n==0 case.
350 * From the OpenGL 3.0 specification, page 258:
351 * "An INVALID_VALUE error is generated if n is greater than
355 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
359 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
360 _mesa_error(ctx
, GL_INVALID_VALUE
,
361 "%s(n > maximum number of draw buffers)", caller
);
365 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
366 usedBufferMask
= 0x0;
368 /* From the ES 3.0 specification, page 180:
369 * "If the GL is bound to the default framebuffer, then n must be 1
370 * and the constant must be BACK or NONE."
371 * (same restriction applies with GL_EXT_draw_buffers specification)
373 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
374 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
375 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
379 /* complicated error checking... */
380 for (output
= 0; output
< n
; output
++) {
381 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL 3.0
382 * specification says:
384 * "Each buffer listed in bufs must be BACK, NONE, or one of the values
385 * from table 4.3 (NONE, COLOR_ATTACHMENTi)"
387 if (_mesa_is_gles3(ctx
) && buffers
[output
] != GL_NONE
&&
388 buffers
[output
] != GL_BACK
&&
389 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
390 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
391 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffers(buffer)");
395 if (buffers
[output
] == GL_NONE
) {
396 destMask
[output
] = 0x0;
399 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
400 * spec (20080923) says:
402 * "If the GL is bound to a framebuffer object and DrawBuffers is
403 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
404 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
405 * INVALID_OPERATION results."
407 if (_mesa_is_user_fbo(fb
) && buffers
[output
] >=
408 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
409 _mesa_error(ctx
, GL_INVALID_OPERATION
,
410 "%s(buffers[%d] >= maximum number of draw buffers)",
415 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
417 /* From the OpenGL 3.0 specification, page 258:
418 * "Each buffer listed in bufs must be one of the values from tables
419 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
421 if (destMask
[output
] == BAD_MASK
) {
422 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
423 caller
, _mesa_enum_to_string(buffers
[output
]));
427 /* From the OpenGL 4.0 specification, page 256:
428 * "For both the default framebuffer and framebuffer objects, the
429 * constants FRONT, BACK, LEFT, RIGHT, and FRONT_AND_BACK are not
430 * valid in the bufs array passed to DrawBuffers, and will result in
431 * the error INVALID_ENUM. This restriction is because these
432 * constants may themselves refer to multiple buffers, as shown in
434 * Previous versions of the OpenGL specification say INVALID_OPERATION,
435 * but the Khronos conformance tests expect INVALID_ENUM.
437 if (_mesa_bitcount(destMask
[output
]) > 1) {
438 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
439 caller
, _mesa_enum_to_string(buffers
[output
]));
443 /* From the OpenGL 3.0 specification, page 259:
444 * "If the GL is bound to the default framebuffer and DrawBuffers is
445 * supplied with a constant (other than NONE) that does not indicate
446 * any of the color buffers allocated to the GL context by the window
447 * system, the error INVALID_OPERATION will be generated.
449 * If the GL is bound to a framebuffer object and DrawBuffers is
450 * supplied with a constant from table 4.6 [...] then the error
451 * INVALID_OPERATION results."
453 destMask
[output
] &= supportedMask
;
454 if (destMask
[output
] == 0) {
455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
456 "%s(unsupported buffer %s)",
457 caller
, _mesa_enum_to_string(buffers
[output
]));
461 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
462 * "If the GL is bound to a framebuffer object, the ith buffer listed
463 * in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
464 * (same restriction applies with GL_EXT_draw_buffers specification)
466 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
467 buffers
[output
] != GL_NONE
&&
468 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
470 "%s(unsupported buffer %s)",
471 caller
, _mesa_enum_to_string(buffers
[output
]));
475 /* From the OpenGL 3.0 specification, page 258:
476 * "Except for NONE, a buffer may not appear more than once in the
477 * array pointed to by bufs. Specifying a buffer more then once will
478 * result in the error INVALID_OPERATION."
480 if (destMask
[output
] & usedBufferMask
) {
481 _mesa_error(ctx
, GL_INVALID_OPERATION
,
482 "%s(duplicated buffer %s)",
483 caller
, _mesa_enum_to_string(buffers
[output
]));
488 usedBufferMask
|= destMask
[output
];
492 /* OK, if we get here, there were no errors so set the new state */
493 _mesa_drawbuffers(ctx
, fb
, n
, buffers
, destMask
);
496 * Call device driver function if fb is the bound draw buffer.
497 * Note that n can be equal to 0,
498 * in which case we don't want to reference buffers[0], which
501 if (fb
== ctx
->DrawBuffer
) {
502 if (ctx
->Driver
.DrawBuffers
)
503 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
504 else if (ctx
->Driver
.DrawBuffer
)
505 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
511 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
513 GET_CURRENT_CONTEXT(ctx
);
514 draw_buffers(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
519 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
522 GET_CURRENT_CONTEXT(ctx
);
523 struct gl_framebuffer
*fb
;
526 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
527 "glNamedFramebufferDrawBuffers");
532 fb
= ctx
->WinSysDrawBuffer
;
534 draw_buffers(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
539 * Performs necessary state updates when _mesa_drawbuffers makes an
543 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
545 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
547 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
548 /* Flag the FBO as requiring validation. */
549 if (_mesa_is_user_fbo(fb
)) {
557 * Helper function to set the GL_DRAW_BUFFER state for the given context and
558 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
560 * All error checking will have been done prior to calling this function
561 * so nothing should go wrong at this point.
563 * \param ctx current context
564 * \param fb the desired draw buffer
565 * \param n number of color outputs to set
566 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
567 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
568 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
569 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
572 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
573 GLuint n
, const GLenum
*buffers
, const GLbitfield
*destMask
)
575 GLbitfield mask
[MAX_DRAW_BUFFERS
];
579 /* compute destMask values now */
580 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
582 for (output
= 0; output
< n
; output
++) {
583 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
584 assert(mask
[output
] != BAD_MASK
);
585 mask
[output
] &= supportedMask
;
591 * destMask[0] may have up to four bits set
592 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
593 * Otherwise, destMask[x] can only have one bit set.
595 if (n
> 0 && _mesa_bitcount(destMask
[0]) > 1) {
596 GLuint count
= 0, destMask0
= destMask
[0];
598 GLint bufIndex
= ffs(destMask0
) - 1;
599 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
600 updated_drawbuffers(ctx
, fb
);
601 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
604 destMask0
&= ~(1 << bufIndex
);
606 fb
->ColorDrawBuffer
[0] = buffers
[0];
607 fb
->_NumColorDrawBuffers
= count
;
611 for (buf
= 0; buf
< n
; buf
++ ) {
613 GLint bufIndex
= ffs(destMask
[buf
]) - 1;
614 /* only one bit should be set in the destMask[buf] field */
615 assert(_mesa_bitcount(destMask
[buf
]) == 1);
616 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
617 updated_drawbuffers(ctx
, fb
);
618 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
623 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
624 updated_drawbuffers(ctx
, fb
);
625 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
628 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
630 fb
->_NumColorDrawBuffers
= count
;
633 /* set remaining outputs to -1 (GL_NONE) */
634 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
635 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
636 updated_drawbuffers(ctx
, fb
);
637 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
640 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
641 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
644 if (_mesa_is_winsys_fbo(fb
)) {
645 /* also set context drawbuffer state */
646 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
647 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
648 updated_drawbuffers(ctx
, fb
);
649 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
657 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
658 * from the context's Color.DrawBuffer[] state.
659 * Use when changing contexts.
662 _mesa_update_draw_buffers(struct gl_context
*ctx
)
664 /* should be a window system FBO */
665 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
667 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
668 ctx
->Color
.DrawBuffer
, NULL
);
673 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
674 * GL_READ_BUFFER state for the given context and FBO.
675 * Note that all error checking should have been done before calling
677 * \param ctx the rendering context
678 * \param fb the framebuffer object to update
679 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
680 * \param bufferIndex the numerical index corresponding to 'buffer'
683 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
684 GLenum buffer
, gl_buffer_index bufferIndex
)
686 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
687 /* Only update the per-context READ_BUFFER state if we're bound to
688 * a window-system framebuffer.
690 ctx
->Pixel
.ReadBuffer
= buffer
;
693 fb
->ColorReadBuffer
= buffer
;
694 fb
->_ColorReadBufferIndex
= bufferIndex
;
696 ctx
->NewState
|= _NEW_BUFFERS
;
702 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
703 * renderbuffer for reading pixels.
704 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
707 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
708 GLenum buffer
, const char *caller
)
710 GLbitfield supportedMask
;
711 gl_buffer_index srcBuffer
;
713 FLUSH_VERTICES(ctx
, 0);
715 if (MESA_VERBOSE
& VERBOSE_API
)
716 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
718 if (buffer
== GL_NONE
) {
719 /* This is legal--it means that no buffer should be bound for reading. */
723 /* general case / window-system framebuffer */
724 if (_mesa_is_gles3(ctx
) && !is_legal_es3_readbuffer_enum(buffer
))
727 srcBuffer
= read_buffer_enum_to_index(buffer
);
729 if (srcBuffer
== -1) {
730 _mesa_error(ctx
, GL_INVALID_ENUM
,
731 "%s(invalid buffer %s)", caller
,
732 _mesa_enum_to_string(buffer
));
735 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
736 if (((1 << srcBuffer
) & supportedMask
) == 0) {
737 _mesa_error(ctx
, GL_INVALID_OPERATION
,
738 "%s(invalid buffer %s)", caller
,
739 _mesa_enum_to_string(buffer
));
744 /* OK, all error checking has been completed now */
746 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
748 /* Call the device driver function only if fb is the bound read buffer */
749 if (fb
== ctx
->ReadBuffer
) {
750 if (ctx
->Driver
.ReadBuffer
)
751 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
757 _mesa_ReadBuffer(GLenum buffer
)
759 GET_CURRENT_CONTEXT(ctx
);
760 read_buffer(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
765 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
767 GET_CURRENT_CONTEXT(ctx
);
768 struct gl_framebuffer
*fb
;
771 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
772 "glNamedFramebufferReadBuffer");
777 fb
= ctx
->WinSysReadBuffer
;
779 read_buffer(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");