mesa: Restore 78-column wrapping of license text in C-style comments.
[mesa.git] / src / mesa / main / buffers.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file buffers.c
29 * glReadBuffer, DrawBuffer functions.
30 */
31
32
33
34 #include "glheader.h"
35 #include "buffers.h"
36 #include "colormac.h"
37 #include "context.h"
38 #include "enums.h"
39 #include "fbobject.h"
40 #include "mtypes.h"
41
42
43 #define BAD_MASK ~0u
44
45
46 /**
47 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
48 * available to the rendering context (for drawing or reading).
49 * This depends on the type of framebuffer. For window system framebuffers
50 * we look at the framebuffer's visual. But for user-create framebuffers we
51 * look at the number of supported color attachments.
52 * \param fb the framebuffer to draw to, or read from
53 * \return bitmask of BUFFER_BIT_* flags
54 */
55 static GLbitfield
56 supported_buffer_bitmask(const struct gl_context *ctx,
57 const struct gl_framebuffer *fb)
58 {
59 GLbitfield mask = 0x0;
60
61 if (_mesa_is_user_fbo(fb)) {
62 /* A user-created renderbuffer */
63 GLuint i;
64 ASSERT(ctx->Extensions.EXT_framebuffer_object);
65 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
66 mask |= (BUFFER_BIT_COLOR0 << i);
67 }
68 }
69 else {
70 /* A window system framebuffer */
71 GLint i;
72 mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
73 if (fb->Visual.stereoMode) {
74 mask |= BUFFER_BIT_FRONT_RIGHT;
75 if (fb->Visual.doubleBufferMode) {
76 mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
77 }
78 }
79 else if (fb->Visual.doubleBufferMode) {
80 mask |= BUFFER_BIT_BACK_LEFT;
81 }
82
83 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
84 mask |= (BUFFER_BIT_AUX0 << i);
85 }
86 }
87
88 return mask;
89 }
90
91
92 /**
93 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
94 * Given a GLenum naming one or more color buffers (such as
95 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
96 */
97 static GLbitfield
98 draw_buffer_enum_to_bitmask(const struct gl_context *ctx, GLenum buffer)
99 {
100 switch (buffer) {
101 case GL_NONE:
102 return 0;
103 case GL_FRONT:
104 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
105 case GL_BACK:
106 if (_mesa_is_gles3(ctx)) {
107 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
108 * ES 3.0.1 specification says:
109 *
110 * "When draw buffer zero is BACK, color values are written
111 * into the sole buffer for single-buffered contexts, or into
112 * the back buffer for double-buffered contexts."
113 *
114 * Since there is no stereo rendering in ES 3.0, only return the
115 * LEFT bits. This also satisfies the "n must be 1" requirement.
116 */
117 if (ctx->DrawBuffer->Visual.doubleBufferMode)
118 return BUFFER_BIT_BACK_LEFT;
119 return BUFFER_BIT_FRONT_LEFT;
120 }
121 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
122 case GL_RIGHT:
123 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
124 case GL_FRONT_RIGHT:
125 return BUFFER_BIT_FRONT_RIGHT;
126 case GL_BACK_RIGHT:
127 return BUFFER_BIT_BACK_RIGHT;
128 case GL_BACK_LEFT:
129 return BUFFER_BIT_BACK_LEFT;
130 case GL_FRONT_AND_BACK:
131 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
132 | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
133 case GL_LEFT:
134 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
135 case GL_FRONT_LEFT:
136 return BUFFER_BIT_FRONT_LEFT;
137 case GL_AUX0:
138 return BUFFER_BIT_AUX0;
139 case GL_AUX1:
140 case GL_AUX2:
141 case GL_AUX3:
142 return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
143 case GL_COLOR_ATTACHMENT0_EXT:
144 return BUFFER_BIT_COLOR0;
145 case GL_COLOR_ATTACHMENT1_EXT:
146 return BUFFER_BIT_COLOR1;
147 case GL_COLOR_ATTACHMENT2_EXT:
148 return BUFFER_BIT_COLOR2;
149 case GL_COLOR_ATTACHMENT3_EXT:
150 return BUFFER_BIT_COLOR3;
151 case GL_COLOR_ATTACHMENT4_EXT:
152 return BUFFER_BIT_COLOR4;
153 case GL_COLOR_ATTACHMENT5_EXT:
154 return BUFFER_BIT_COLOR5;
155 case GL_COLOR_ATTACHMENT6_EXT:
156 return BUFFER_BIT_COLOR6;
157 case GL_COLOR_ATTACHMENT7_EXT:
158 return BUFFER_BIT_COLOR7;
159 default:
160 /* error */
161 return BAD_MASK;
162 }
163 }
164
165
166 /**
167 * Helper routine used by glReadBuffer.
168 * Given a GLenum naming a color buffer, return the index of the corresponding
169 * renderbuffer (a BUFFER_* value).
170 * return -1 for an invalid buffer.
171 */
172 static GLint
173 read_buffer_enum_to_index(GLenum buffer)
174 {
175 switch (buffer) {
176 case GL_FRONT:
177 return BUFFER_FRONT_LEFT;
178 case GL_BACK:
179 return BUFFER_BACK_LEFT;
180 case GL_RIGHT:
181 return BUFFER_FRONT_RIGHT;
182 case GL_FRONT_RIGHT:
183 return BUFFER_FRONT_RIGHT;
184 case GL_BACK_RIGHT:
185 return BUFFER_BACK_RIGHT;
186 case GL_BACK_LEFT:
187 return BUFFER_BACK_LEFT;
188 case GL_LEFT:
189 return BUFFER_FRONT_LEFT;
190 case GL_FRONT_LEFT:
191 return BUFFER_FRONT_LEFT;
192 case GL_AUX0:
193 return BUFFER_AUX0;
194 case GL_AUX1:
195 case GL_AUX2:
196 case GL_AUX3:
197 return BUFFER_COUNT; /* invalid, but not -1 */
198 case GL_COLOR_ATTACHMENT0_EXT:
199 return BUFFER_COLOR0;
200 case GL_COLOR_ATTACHMENT1_EXT:
201 return BUFFER_COLOR1;
202 case GL_COLOR_ATTACHMENT2_EXT:
203 return BUFFER_COLOR2;
204 case GL_COLOR_ATTACHMENT3_EXT:
205 return BUFFER_COLOR3;
206 case GL_COLOR_ATTACHMENT4_EXT:
207 return BUFFER_COLOR4;
208 case GL_COLOR_ATTACHMENT5_EXT:
209 return BUFFER_COLOR5;
210 case GL_COLOR_ATTACHMENT6_EXT:
211 return BUFFER_COLOR6;
212 case GL_COLOR_ATTACHMENT7_EXT:
213 return BUFFER_COLOR7;
214 default:
215 /* error */
216 return -1;
217 }
218 }
219
220
221 /**
222 * Called by glDrawBuffer().
223 * Specify which renderbuffer(s) to draw into for the first color output.
224 * <buffer> can name zero, one, two or four renderbuffers!
225 * \sa _mesa_DrawBuffers
226 *
227 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
228 *
229 * Note that the behaviour of this function depends on whether the
230 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
231 * a user-created framebuffer object (Name!=0).
232 * In the former case, we update the per-context ctx->Color.DrawBuffer
233 * state var _and_ the FB's ColorDrawBuffer state.
234 * In the later case, we update the FB's ColorDrawBuffer state only.
235 *
236 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
237 * new FB is a window system FB, we need to re-update the FB's
238 * ColorDrawBuffer state to match the context. This is handled in
239 * _mesa_update_framebuffer().
240 *
241 * See the GL_EXT_framebuffer_object spec for more info.
242 */
243 void GLAPIENTRY
244 _mesa_DrawBuffer(GLenum buffer)
245 {
246 GLbitfield destMask;
247 GET_CURRENT_CONTEXT(ctx);
248
249 FLUSH_VERTICES(ctx, 0);
250
251 if (MESA_VERBOSE & VERBOSE_API) {
252 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
253 }
254
255 if (buffer == GL_NONE) {
256 destMask = 0x0;
257 }
258 else {
259 const GLbitfield supportedMask
260 = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
261 destMask = draw_buffer_enum_to_bitmask(ctx, buffer);
262 if (destMask == BAD_MASK) {
263 /* totally bogus buffer */
264 _mesa_error(ctx, GL_INVALID_ENUM,
265 "glDrawBuffer(buffer=0x%x)", buffer);
266 return;
267 }
268 destMask &= supportedMask;
269 if (destMask == 0x0) {
270 /* none of the named color buffers exist! */
271 _mesa_error(ctx, GL_INVALID_OPERATION,
272 "glDrawBuffer(buffer=0x%x)", buffer);
273 return;
274 }
275 }
276
277 /* if we get here, there's no error so set new state */
278 _mesa_drawbuffers(ctx, 1, &buffer, &destMask);
279
280 /*
281 * Call device driver function.
282 */
283 if (ctx->Driver.DrawBuffers)
284 ctx->Driver.DrawBuffers(ctx, 1, &buffer);
285 else if (ctx->Driver.DrawBuffer)
286 ctx->Driver.DrawBuffer(ctx, buffer);
287 }
288
289
290 /**
291 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
292 * for N fragment program color outputs.
293 * \sa _mesa_DrawBuffer
294 * \param n number of outputs
295 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
296 * names cannot specify more than one buffer. For example,
297 * GL_FRONT_AND_BACK is illegal.
298 */
299 void GLAPIENTRY
300 _mesa_DrawBuffers(GLsizei n, const GLenum *buffers)
301 {
302 GLint output;
303 GLbitfield usedBufferMask, supportedMask;
304 GLbitfield destMask[MAX_DRAW_BUFFERS];
305 GET_CURRENT_CONTEXT(ctx);
306
307 FLUSH_VERTICES(ctx, 0);
308
309 /* Turns out n==0 is a valid input that should not produce an error.
310 * The remaining code below correctly handles the n==0 case.
311 *
312 * From the OpenGL 3.0 specification, page 258:
313 * "An INVALID_VALUE error is generated if n is greater than
314 * MAX_DRAW_BUFFERS."
315 */
316 if (n < 0 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
317 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
318 return;
319 }
320
321 supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
322 usedBufferMask = 0x0;
323
324 /* From the ES 3.0 specification, page 180:
325 * "If the GL is bound to the default framebuffer, then n must be 1
326 * and the constant must be BACK or NONE."
327 */
328 if (_mesa_is_gles3(ctx) && _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
329 (n != 1 || (buffers[0] != GL_NONE && buffers[0] != GL_BACK))) {
330 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffers(buffer)");
331 return;
332 }
333
334 /* complicated error checking... */
335 for (output = 0; output < n; output++) {
336 if (buffers[output] == GL_NONE) {
337 destMask[output] = 0x0;
338 }
339 else {
340 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
341 * spec (20080923) says:
342 *
343 * "If the GL is bound to a framebuffer object and DrawBuffers is
344 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
345 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
346 * INVALID_OPERATION results."
347 */
348 if (_mesa_is_user_fbo(ctx->DrawBuffer) && buffers[output] >=
349 GL_COLOR_ATTACHMENT0 + ctx->Const.MaxDrawBuffers) {
350 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffersARB(buffer)");
351 return;
352 }
353
354 destMask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
355
356 /* From the OpenGL 3.0 specification, page 258:
357 * "Each buffer listed in bufs must be one of the values from tables
358 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
359 */
360 if (destMask[output] == BAD_MASK) {
361 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
362 return;
363 }
364
365 /* From the OpenGL 3.0 specification, page 259:
366 * "For both the default framebuffer and framebuffer objects, the
367 * constants FRONT, BACK, LEFT, RIGHT, and FRONT_AND_BACK are not
368 * valid in the bufs array passed to DrawBuffers, and will result in
369 * the error INVALID_OPERATION. This restriction is because these
370 * constants may themselves refer to multiple buffers, as shown in
371 * table 4.4."
372 */
373 if (_mesa_bitcount(destMask[output]) > 1) {
374 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffersARB(buffer)");
375 return;
376 }
377
378 /* From the OpenGL 3.0 specification, page 259:
379 * "If the GL is bound to the default framebuffer and DrawBuffers is
380 * supplied with a constant (other than NONE) that does not indicate
381 * any of the color buffers allocated to the GL context by the window
382 * system, the error INVALID_OPERATION will be generated.
383 *
384 * If the GL is bound to a framebuffer object and DrawBuffers is
385 * supplied with a constant from table 4.6 [...] then the error
386 * INVALID_OPERATION results."
387 */
388 destMask[output] &= supportedMask;
389 if (destMask[output] == 0) {
390 _mesa_error(ctx, GL_INVALID_OPERATION,
391 "glDrawBuffersARB(unsupported buffer)");
392 return;
393 }
394
395 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
396 * "If the GL is bound to a framebuffer object, the ith buffer listed
397 * in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
398 */
399 if (_mesa_is_gles3(ctx) && _mesa_is_user_fbo(ctx->DrawBuffer) &&
400 buffers[output] != GL_NONE &&
401 buffers[output] != GL_COLOR_ATTACHMENT0 + output) {
402 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffers(buffer)");
403 return;
404 }
405
406 /* From the OpenGL 3.0 specification, page 258:
407 * "Except for NONE, a buffer may not appear more than once in the
408 * array pointed to by bufs. Specifying a buffer more then once will
409 * result in the error INVALID_OPERATION."
410 */
411 if (destMask[output] & usedBufferMask) {
412 _mesa_error(ctx, GL_INVALID_OPERATION,
413 "glDrawBuffersARB(duplicated buffer)");
414 return;
415 }
416
417 /* update bitmask */
418 usedBufferMask |= destMask[output];
419 }
420 }
421
422 /* OK, if we get here, there were no errors so set the new state */
423 _mesa_drawbuffers(ctx, n, buffers, destMask);
424
425 /*
426 * Call device driver function. Note that n can be equal to 0,
427 * in which case we don't want to reference buffers[0], which
428 * may not be valid.
429 */
430 if (ctx->Driver.DrawBuffers)
431 ctx->Driver.DrawBuffers(ctx, n, buffers);
432 else if (ctx->Driver.DrawBuffer)
433 ctx->Driver.DrawBuffer(ctx, n > 0 ? buffers[0] : GL_NONE);
434 }
435
436
437 /**
438 * Performs necessary state updates when _mesa_drawbuffers makes an
439 * actual change.
440 */
441 static void
442 updated_drawbuffers(struct gl_context *ctx)
443 {
444 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
445
446 if (ctx->API == API_OPENGL_COMPAT && !ctx->Extensions.ARB_ES2_compatibility) {
447 struct gl_framebuffer *fb = ctx->DrawBuffer;
448
449 /* Flag the FBO as requiring validation. */
450 if (_mesa_is_user_fbo(fb)) {
451 fb->_Status = 0;
452 }
453 }
454 }
455
456
457 /**
458 * Helper function to set the GL_DRAW_BUFFER state in the context and
459 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
460 *
461 * All error checking will have been done prior to calling this function
462 * so nothing should go wrong at this point.
463 *
464 * \param ctx current context
465 * \param n number of color outputs to set
466 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
467 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
468 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
469 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
470 */
471 void
472 _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
473 const GLbitfield *destMask)
474 {
475 struct gl_framebuffer *fb = ctx->DrawBuffer;
476 GLbitfield mask[MAX_DRAW_BUFFERS];
477 GLuint buf;
478
479 if (!destMask) {
480 /* compute destMask values now */
481 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
482 GLuint output;
483 for (output = 0; output < n; output++) {
484 mask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
485 ASSERT(mask[output] != BAD_MASK);
486 mask[output] &= supportedMask;
487 }
488 destMask = mask;
489 }
490
491 /*
492 * If n==1, destMask[0] may have up to four bits set.
493 * Otherwise, destMask[x] can only have one bit set.
494 */
495 if (n == 1) {
496 GLuint count = 0, destMask0 = destMask[0];
497 while (destMask0) {
498 GLint bufIndex = ffs(destMask0) - 1;
499 if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
500 updated_drawbuffers(ctx);
501 fb->_ColorDrawBufferIndexes[count] = bufIndex;
502 }
503 count++;
504 destMask0 &= ~(1 << bufIndex);
505 }
506 fb->ColorDrawBuffer[0] = buffers[0];
507 fb->_NumColorDrawBuffers = count;
508 }
509 else {
510 GLuint count = 0;
511 for (buf = 0; buf < n; buf++ ) {
512 if (destMask[buf]) {
513 GLint bufIndex = ffs(destMask[buf]) - 1;
514 /* only one bit should be set in the destMask[buf] field */
515 ASSERT(_mesa_bitcount(destMask[buf]) == 1);
516 if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
517 updated_drawbuffers(ctx);
518 fb->_ColorDrawBufferIndexes[buf] = bufIndex;
519 }
520 count = buf + 1;
521 }
522 else {
523 if (fb->_ColorDrawBufferIndexes[buf] != -1) {
524 updated_drawbuffers(ctx);
525 fb->_ColorDrawBufferIndexes[buf] = -1;
526 }
527 }
528 fb->ColorDrawBuffer[buf] = buffers[buf];
529 }
530 fb->_NumColorDrawBuffers = count;
531 }
532
533 /* set remaining outputs to -1 (GL_NONE) */
534 for (buf = fb->_NumColorDrawBuffers; buf < ctx->Const.MaxDrawBuffers; buf++) {
535 if (fb->_ColorDrawBufferIndexes[buf] != -1) {
536 updated_drawbuffers(ctx);
537 fb->_ColorDrawBufferIndexes[buf] = -1;
538 }
539 }
540 for (buf = n; buf < ctx->Const.MaxDrawBuffers; buf++) {
541 fb->ColorDrawBuffer[buf] = GL_NONE;
542 }
543
544 if (_mesa_is_winsys_fbo(fb)) {
545 /* also set context drawbuffer state */
546 for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
547 if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
548 updated_drawbuffers(ctx);
549 ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
550 }
551 }
552 }
553 }
554
555
556 /**
557 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
558 * from the context's Color.DrawBuffer[] state.
559 * Use when changing contexts.
560 */
561 void
562 _mesa_update_draw_buffers(struct gl_context *ctx)
563 {
564 GLenum buffers[MAX_DRAW_BUFFERS];
565 GLuint i;
566
567 /* should be a window system FBO */
568 assert(_mesa_is_winsys_fbo(ctx->DrawBuffer));
569
570 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++)
571 buffers[i] = ctx->Color.DrawBuffer[i];
572
573 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
574 }
575
576
577 /**
578 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
579 * GL_READ_BUFFER state in the context and current FBO.
580 * \param ctx the rendering context
581 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
582 * \param bufferIndex the numerical index corresponding to 'buffer'
583 */
584 void
585 _mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex)
586 {
587 struct gl_framebuffer *fb = ctx->ReadBuffer;
588
589 if (_mesa_is_winsys_fbo(fb)) {
590 /* Only update the per-context READ_BUFFER state if we're bound to
591 * a window-system framebuffer.
592 */
593 ctx->Pixel.ReadBuffer = buffer;
594 }
595
596 fb->ColorReadBuffer = buffer;
597 fb->_ColorReadBufferIndex = bufferIndex;
598
599 ctx->NewState |= _NEW_BUFFERS;
600 }
601
602
603
604 /**
605 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
606 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
607 */
608 void GLAPIENTRY
609 _mesa_ReadBuffer(GLenum buffer)
610 {
611 struct gl_framebuffer *fb;
612 GLbitfield supportedMask;
613 GLint srcBuffer;
614 GET_CURRENT_CONTEXT(ctx);
615
616 FLUSH_VERTICES(ctx, 0);
617
618 if (MESA_VERBOSE & VERBOSE_API)
619 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
620
621 fb = ctx->ReadBuffer;
622
623 if (MESA_VERBOSE & VERBOSE_API)
624 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
625
626 if (buffer == GL_NONE) {
627 /* This is legal--it means that no buffer should be bound for reading. */
628 srcBuffer = -1;
629 }
630 else {
631 /* general case / window-system framebuffer */
632 srcBuffer = read_buffer_enum_to_index(buffer);
633 if (srcBuffer == -1) {
634 _mesa_error(ctx, GL_INVALID_ENUM,
635 "glReadBuffer(buffer=0x%x)", buffer);
636 return;
637 }
638 supportedMask = supported_buffer_bitmask(ctx, fb);
639 if (((1 << srcBuffer) & supportedMask) == 0) {
640 _mesa_error(ctx, GL_INVALID_OPERATION,
641 "glReadBuffer(buffer=0x%x)", buffer);
642 return;
643 }
644 }
645
646 /* OK, all error checking has been completed now */
647
648 _mesa_readbuffer(ctx, buffer, srcBuffer);
649
650 /*
651 * Call device driver function.
652 */
653 if (ctx->Driver.ReadBuffer)
654 (*ctx->Driver.ReadBuffer)(ctx, buffer);
655 }