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