mesa: updates some comments in buffers.c
[mesa.git] / src / mesa / main / buffers.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
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 "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "mtypes.h"
39
40
41 #define BAD_MASK ~0u
42
43
44 /**
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
52 */
53 static GLbitfield
54 supported_buffer_bitmask(const struct gl_context *ctx,
55 const struct gl_framebuffer *fb)
56 {
57 GLbitfield mask = 0x0;
58
59 if (_mesa_is_user_fbo(fb)) {
60 /* A user-created renderbuffer */
61 GLuint i;
62 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
63 mask |= (BUFFER_BIT_COLOR0 << i);
64 }
65 }
66 else {
67 /* A window system framebuffer */
68 GLint i;
69 mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
70 if (fb->Visual.stereoMode) {
71 mask |= BUFFER_BIT_FRONT_RIGHT;
72 if (fb->Visual.doubleBufferMode) {
73 mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
74 }
75 }
76 else if (fb->Visual.doubleBufferMode) {
77 mask |= BUFFER_BIT_BACK_LEFT;
78 }
79
80 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
81 mask |= (BUFFER_BIT_AUX0 << i);
82 }
83 }
84
85 return mask;
86 }
87
88
89 /**
90 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
91 * Given a GLenum naming one or more color buffers (such as
92 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
93 */
94 static GLbitfield
95 draw_buffer_enum_to_bitmask(const struct gl_context *ctx, GLenum buffer)
96 {
97 switch (buffer) {
98 case GL_NONE:
99 return 0;
100 case GL_FRONT:
101 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
102 case GL_BACK:
103 if (_mesa_is_gles(ctx)) {
104 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
105 * ES 3.0.1 specification says:
106 *
107 * "When draw buffer zero is BACK, color values are written
108 * into the sole buffer for single-buffered contexts, or into
109 * the back buffer for double-buffered contexts."
110 *
111 * Since there is no stereo rendering in ES 3.0, only return the
112 * LEFT bits. This also satisfies the "n must be 1" requirement.
113 *
114 * We also do this for GLES 1 and 2 because those APIs have no
115 * concept of selecting the front and back buffer anyway and it's
116 * convenient to be able to maintain the magic behaviour of
117 * GL_BACK in that case.
118 */
119 if (ctx->DrawBuffer->Visual.doubleBufferMode)
120 return BUFFER_BIT_BACK_LEFT;
121 return BUFFER_BIT_FRONT_LEFT;
122 }
123 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
124 case GL_RIGHT:
125 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
126 case GL_FRONT_RIGHT:
127 return BUFFER_BIT_FRONT_RIGHT;
128 case GL_BACK_RIGHT:
129 return BUFFER_BIT_BACK_RIGHT;
130 case GL_BACK_LEFT:
131 return BUFFER_BIT_BACK_LEFT;
132 case GL_FRONT_AND_BACK:
133 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
134 | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
135 case GL_LEFT:
136 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
137 case GL_FRONT_LEFT:
138 return BUFFER_BIT_FRONT_LEFT;
139 case GL_AUX0:
140 return BUFFER_BIT_AUX0;
141 case GL_AUX1:
142 case GL_AUX2:
143 case GL_AUX3:
144 return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
145 case GL_COLOR_ATTACHMENT0_EXT:
146 return BUFFER_BIT_COLOR0;
147 case GL_COLOR_ATTACHMENT1_EXT:
148 return BUFFER_BIT_COLOR1;
149 case GL_COLOR_ATTACHMENT2_EXT:
150 return BUFFER_BIT_COLOR2;
151 case GL_COLOR_ATTACHMENT3_EXT:
152 return BUFFER_BIT_COLOR3;
153 case GL_COLOR_ATTACHMENT4_EXT:
154 return BUFFER_BIT_COLOR4;
155 case GL_COLOR_ATTACHMENT5_EXT:
156 return BUFFER_BIT_COLOR5;
157 case GL_COLOR_ATTACHMENT6_EXT:
158 return BUFFER_BIT_COLOR6;
159 case GL_COLOR_ATTACHMENT7_EXT:
160 return BUFFER_BIT_COLOR7;
161 default:
162 /* not an error, but also not supported */
163 if (buffer >= GL_COLOR_ATTACHMENT8 && buffer <= GL_COLOR_ATTACHMENT31)
164 return 1 << BUFFER_COUNT;
165 /* error */
166 return BAD_MASK;
167 }
168 }
169
170
171 /**
172 * Helper routine used by glReadBuffer.
173 * Given a GLenum naming a color buffer, return the index of the corresponding
174 * renderbuffer (a BUFFER_* value).
175 * return -1 for an invalid buffer.
176 */
177 static gl_buffer_index
178 read_buffer_enum_to_index(GLenum buffer)
179 {
180 switch (buffer) {
181 case GL_FRONT:
182 return BUFFER_FRONT_LEFT;
183 case GL_BACK:
184 return BUFFER_BACK_LEFT;
185 case GL_RIGHT:
186 return BUFFER_FRONT_RIGHT;
187 case GL_FRONT_RIGHT:
188 return BUFFER_FRONT_RIGHT;
189 case GL_BACK_RIGHT:
190 return BUFFER_BACK_RIGHT;
191 case GL_BACK_LEFT:
192 return BUFFER_BACK_LEFT;
193 case GL_LEFT:
194 return BUFFER_FRONT_LEFT;
195 case GL_FRONT_LEFT:
196 return BUFFER_FRONT_LEFT;
197 case GL_AUX0:
198 return BUFFER_AUX0;
199 case GL_AUX1:
200 case GL_AUX2:
201 case GL_AUX3:
202 return BUFFER_COUNT; /* invalid, but not -1 */
203 case GL_COLOR_ATTACHMENT0_EXT:
204 return BUFFER_COLOR0;
205 case GL_COLOR_ATTACHMENT1_EXT:
206 return BUFFER_COLOR1;
207 case GL_COLOR_ATTACHMENT2_EXT:
208 return BUFFER_COLOR2;
209 case GL_COLOR_ATTACHMENT3_EXT:
210 return BUFFER_COLOR3;
211 case GL_COLOR_ATTACHMENT4_EXT:
212 return BUFFER_COLOR4;
213 case GL_COLOR_ATTACHMENT5_EXT:
214 return BUFFER_COLOR5;
215 case GL_COLOR_ATTACHMENT6_EXT:
216 return BUFFER_COLOR6;
217 case GL_COLOR_ATTACHMENT7_EXT:
218 return BUFFER_COLOR7;
219 default:
220 /* not an error, but also not supported */
221 if (buffer >= GL_COLOR_ATTACHMENT8 && buffer <= GL_COLOR_ATTACHMENT31)
222 return BUFFER_COUNT;
223 /* error */
224 return -1;
225 }
226 }
227
228
229 /**
230 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
231 * Specify which renderbuffer(s) to draw into for the first color output.
232 * <buffer> can name zero, one, two or four renderbuffers!
233 * \sa _mesa_DrawBuffers
234 *
235 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
236 *
237 * Note that the behaviour of this function depends on whether the
238 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
239 * framebuffer object.
240 * In the former case, we update the per-context ctx->Color.DrawBuffer
241 * state var _and_ the FB's ColorDrawBuffer state.
242 * In the later case, we update the FB's ColorDrawBuffer state only.
243 *
244 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
245 * new FB is a window system FB, we need to re-update the FB's
246 * ColorDrawBuffer state to match the context. This is handled in
247 * _mesa_update_framebuffer().
248 *
249 * See the GL_EXT_framebuffer_object spec for more info.
250 */
251 static void
252 draw_buffer(struct gl_context *ctx, struct gl_framebuffer *fb,
253 GLenum buffer, const char *caller)
254 {
255 GLbitfield destMask;
256
257 FLUSH_VERTICES(ctx, 0);
258
259 if (MESA_VERBOSE & VERBOSE_API) {
260 _mesa_debug(ctx, "%s %s\n", caller, _mesa_enum_to_string(buffer));
261 }
262
263 if (buffer == GL_NONE) {
264 destMask = 0x0;
265 }
266 else {
267 const GLbitfield supportedMask
268 = supported_buffer_bitmask(ctx, fb);
269 destMask = draw_buffer_enum_to_bitmask(ctx, buffer);
270 if (destMask == BAD_MASK) {
271 /* totally bogus buffer */
272 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)", caller,
273 _mesa_enum_to_string(buffer));
274 return;
275 }
276 destMask &= supportedMask;
277 if (destMask == 0x0) {
278 /* none of the named color buffers exist! */
279 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid buffer %s)",
280 caller, _mesa_enum_to_string(buffer));
281 return;
282 }
283 }
284
285 /* if we get here, there's no error so set new state */
286 _mesa_drawbuffers(ctx, fb, 1, &buffer, &destMask);
287
288 /* Call device driver function only if fb is the bound draw buffer */
289 if (fb == ctx->DrawBuffer) {
290 if (ctx->Driver.DrawBuffers)
291 ctx->Driver.DrawBuffers(ctx, 1, &buffer);
292 else if (ctx->Driver.DrawBuffer)
293 ctx->Driver.DrawBuffer(ctx, buffer);
294 }
295 }
296
297
298 void GLAPIENTRY
299 _mesa_DrawBuffer(GLenum buffer)
300 {
301 GET_CURRENT_CONTEXT(ctx);
302 draw_buffer(ctx, ctx->DrawBuffer, buffer, "glDrawBuffer");
303 }
304
305
306 void GLAPIENTRY
307 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer, GLenum buf)
308 {
309 GET_CURRENT_CONTEXT(ctx);
310 struct gl_framebuffer *fb;
311
312 if (framebuffer) {
313 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
314 "glNamedFramebufferDrawBuffer");
315 if (!fb)
316 return;
317 }
318 else
319 fb = ctx->WinSysDrawBuffer;
320
321 draw_buffer(ctx, fb, buf, "glNamedFramebufferDrawBuffer");
322 }
323
324
325 /**
326 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
327 * the destination color renderbuffers for N fragment program color outputs.
328 * \sa _mesa_DrawBuffer
329 * \param n number of outputs
330 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
331 * names cannot specify more than one buffer. For example,
332 * GL_FRONT_AND_BACK is illegal.
333 */
334 static void
335 draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb,
336 GLsizei n, const GLenum *buffers, const char *caller)
337 {
338 GLuint output;
339 GLbitfield usedBufferMask, supportedMask;
340 GLbitfield destMask[MAX_DRAW_BUFFERS];
341
342 FLUSH_VERTICES(ctx, 0);
343
344 /* Turns out n==0 is a valid input that should not produce an error.
345 * The remaining code below correctly handles the n==0 case.
346 *
347 * From the OpenGL 3.0 specification, page 258:
348 * "An INVALID_VALUE error is generated if n is greater than
349 * MAX_DRAW_BUFFERS."
350 */
351 if (n < 0) {
352 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", caller);
353 return;
354 }
355
356 if (n > (GLsizei) ctx->Const.MaxDrawBuffers) {
357 _mesa_error(ctx, GL_INVALID_VALUE,
358 "%s(n > maximum number of draw buffers)", caller);
359 return;
360 }
361
362 supportedMask = supported_buffer_bitmask(ctx, fb);
363 usedBufferMask = 0x0;
364
365 /* From the ES 3.0 specification, page 180:
366 * "If the GL is bound to the default framebuffer, then n must be 1
367 * and the constant must be BACK or NONE."
368 * (same restriction applies with GL_EXT_draw_buffers specification)
369 */
370 if (ctx->API == API_OPENGLES2 && _mesa_is_winsys_fbo(fb) &&
371 (n != 1 || (buffers[0] != GL_NONE && buffers[0] != GL_BACK))) {
372 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid buffers)", caller);
373 return;
374 }
375
376 /* complicated error checking... */
377 for (output = 0; output < n; output++) {
378 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL 3.0
379 * specification says:
380 *
381 * "Each buffer listed in bufs must be BACK, NONE, or one of the values
382 * from table 4.3 (NONE, COLOR_ATTACHMENTi)"
383 */
384 if (_mesa_is_gles3(ctx) && buffers[output] != GL_NONE &&
385 buffers[output] != GL_BACK &&
386 (buffers[output] < GL_COLOR_ATTACHMENT0 ||
387 buffers[output] >= GL_COLOR_ATTACHMENT0 + ctx->Const.MaxColorAttachments)) {
388 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffers(buffer)");
389 return;
390 }
391
392 if (buffers[output] == GL_NONE) {
393 destMask[output] = 0x0;
394 }
395 else {
396 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
397 * spec (20080923) says:
398 *
399 * "If the GL is bound to a framebuffer object and DrawBuffers is
400 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
401 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
402 * INVALID_OPERATION results."
403 */
404 if (_mesa_is_user_fbo(fb) && buffers[output] >=
405 GL_COLOR_ATTACHMENT0 + ctx->Const.MaxDrawBuffers) {
406 _mesa_error(ctx, GL_INVALID_OPERATION,
407 "%s(buffers[%d] >= maximum number of draw buffers)",
408 caller, output);
409 return;
410 }
411
412 destMask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
413
414 /* From the OpenGL 3.0 specification, page 258:
415 * "Each buffer listed in bufs must be one of the values from tables
416 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
417 */
418 if (destMask[output] == BAD_MASK) {
419 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)",
420 caller, _mesa_enum_to_string(buffers[output]));
421 return;
422 }
423
424 /* From the OpenGL 4.0 specification, page 256:
425 * "For both the default framebuffer and framebuffer objects, the
426 * constants FRONT, BACK, LEFT, RIGHT, and FRONT_AND_BACK are not
427 * valid in the bufs array passed to DrawBuffers, and will result in
428 * the error INVALID_ENUM. This restriction is because these
429 * constants may themselves refer to multiple buffers, as shown in
430 * table 4.4."
431 * Previous versions of the OpenGL specification say INVALID_OPERATION,
432 * but the Khronos conformance tests expect INVALID_ENUM.
433 */
434 if (_mesa_bitcount(destMask[output]) > 1) {
435 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)",
436 caller, _mesa_enum_to_string(buffers[output]));
437 return;
438 }
439
440 /* From the OpenGL 3.0 specification, page 259:
441 * "If the GL is bound to the default framebuffer and DrawBuffers is
442 * supplied with a constant (other than NONE) that does not indicate
443 * any of the color buffers allocated to the GL context by the window
444 * system, the error INVALID_OPERATION will be generated.
445 *
446 * If the GL is bound to a framebuffer object and DrawBuffers is
447 * supplied with a constant from table 4.6 [...] then the error
448 * INVALID_OPERATION results."
449 */
450 destMask[output] &= supportedMask;
451 if (destMask[output] == 0) {
452 _mesa_error(ctx, GL_INVALID_OPERATION,
453 "%s(unsupported buffer %s)",
454 caller, _mesa_enum_to_string(buffers[output]));
455 return;
456 }
457
458 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
459 * "If the GL is bound to a framebuffer object, the ith buffer listed
460 * in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
461 * (same restriction applies with GL_EXT_draw_buffers specification)
462 */
463 if (ctx->API == API_OPENGLES2 && _mesa_is_user_fbo(fb) &&
464 buffers[output] != GL_NONE &&
465 buffers[output] != GL_COLOR_ATTACHMENT0 + output) {
466 _mesa_error(ctx, GL_INVALID_OPERATION,
467 "%s(unsupported buffer %s)",
468 caller, _mesa_enum_to_string(buffers[output]));
469 return;
470 }
471
472 /* From the OpenGL 3.0 specification, page 258:
473 * "Except for NONE, a buffer may not appear more than once in the
474 * array pointed to by bufs. Specifying a buffer more then once will
475 * result in the error INVALID_OPERATION."
476 */
477 if (destMask[output] & usedBufferMask) {
478 _mesa_error(ctx, GL_INVALID_OPERATION,
479 "%s(duplicated buffer %s)",
480 caller, _mesa_enum_to_string(buffers[output]));
481 return;
482 }
483
484 /* update bitmask */
485 usedBufferMask |= destMask[output];
486 }
487 }
488
489 /* OK, if we get here, there were no errors so set the new state */
490 _mesa_drawbuffers(ctx, fb, n, buffers, destMask);
491
492 /*
493 * Call device driver function if fb is the bound draw buffer.
494 * Note that n can be equal to 0,
495 * in which case we don't want to reference buffers[0], which
496 * may not be valid.
497 */
498 if (fb == ctx->DrawBuffer) {
499 if (ctx->Driver.DrawBuffers)
500 ctx->Driver.DrawBuffers(ctx, n, buffers);
501 else if (ctx->Driver.DrawBuffer)
502 ctx->Driver.DrawBuffer(ctx, n > 0 ? buffers[0] : GL_NONE);
503 }
504 }
505
506
507 void GLAPIENTRY
508 _mesa_DrawBuffers(GLsizei n, const GLenum *buffers)
509 {
510 GET_CURRENT_CONTEXT(ctx);
511 draw_buffers(ctx, ctx->DrawBuffer, n, buffers, "glDrawBuffers");
512 }
513
514
515 void GLAPIENTRY
516 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer, GLsizei n,
517 const GLenum *bufs)
518 {
519 GET_CURRENT_CONTEXT(ctx);
520 struct gl_framebuffer *fb;
521
522 if (framebuffer) {
523 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
524 "glNamedFramebufferDrawBuffers");
525 if (!fb)
526 return;
527 }
528 else
529 fb = ctx->WinSysDrawBuffer;
530
531 draw_buffers(ctx, fb, n, bufs, "glNamedFramebufferDrawBuffers");
532 }
533
534
535 /**
536 * Performs necessary state updates when _mesa_drawbuffers makes an
537 * actual change.
538 */
539 static void
540 updated_drawbuffers(struct gl_context *ctx, struct gl_framebuffer *fb)
541 {
542 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
543
544 if (ctx->API == API_OPENGL_COMPAT && !ctx->Extensions.ARB_ES2_compatibility) {
545 /* Flag the FBO as requiring validation. */
546 if (_mesa_is_user_fbo(fb)) {
547 fb->_Status = 0;
548 }
549 }
550 }
551
552
553 /**
554 * Helper function to set the GL_DRAW_BUFFER state for the given context and
555 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
556 *
557 * All error checking will have been done prior to calling this function
558 * so nothing should go wrong at this point.
559 *
560 * \param ctx current context
561 * \param fb the desired draw buffer
562 * \param n number of color outputs to set
563 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
564 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
565 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
566 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
567 */
568 void
569 _mesa_drawbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
570 GLuint n, const GLenum *buffers, const GLbitfield *destMask)
571 {
572 GLbitfield mask[MAX_DRAW_BUFFERS];
573 GLuint buf;
574
575 if (!destMask) {
576 /* compute destMask values now */
577 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
578 GLuint output;
579 for (output = 0; output < n; output++) {
580 mask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
581 assert(mask[output] != BAD_MASK);
582 mask[output] &= supportedMask;
583 }
584 destMask = mask;
585 }
586
587 /*
588 * destMask[0] may have up to four bits set
589 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
590 * Otherwise, destMask[x] can only have one bit set.
591 */
592 if (n > 0 && _mesa_bitcount(destMask[0]) > 1) {
593 GLuint count = 0, destMask0 = destMask[0];
594 while (destMask0) {
595 GLint bufIndex = ffs(destMask0) - 1;
596 if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
597 updated_drawbuffers(ctx, fb);
598 fb->_ColorDrawBufferIndexes[count] = bufIndex;
599 }
600 count++;
601 destMask0 &= ~(1 << bufIndex);
602 }
603 fb->ColorDrawBuffer[0] = buffers[0];
604 fb->_NumColorDrawBuffers = count;
605 }
606 else {
607 GLuint count = 0;
608 for (buf = 0; buf < n; buf++ ) {
609 if (destMask[buf]) {
610 GLint bufIndex = ffs(destMask[buf]) - 1;
611 /* only one bit should be set in the destMask[buf] field */
612 assert(_mesa_bitcount(destMask[buf]) == 1);
613 if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
614 updated_drawbuffers(ctx, fb);
615 fb->_ColorDrawBufferIndexes[buf] = bufIndex;
616 }
617 count = buf + 1;
618 }
619 else {
620 if (fb->_ColorDrawBufferIndexes[buf] != -1) {
621 updated_drawbuffers(ctx, fb);
622 fb->_ColorDrawBufferIndexes[buf] = -1;
623 }
624 }
625 fb->ColorDrawBuffer[buf] = buffers[buf];
626 }
627 fb->_NumColorDrawBuffers = count;
628 }
629
630 /* set remaining outputs to -1 (GL_NONE) */
631 for (buf = fb->_NumColorDrawBuffers; buf < ctx->Const.MaxDrawBuffers; buf++) {
632 if (fb->_ColorDrawBufferIndexes[buf] != -1) {
633 updated_drawbuffers(ctx, fb);
634 fb->_ColorDrawBufferIndexes[buf] = -1;
635 }
636 }
637 for (buf = n; buf < ctx->Const.MaxDrawBuffers; buf++) {
638 fb->ColorDrawBuffer[buf] = GL_NONE;
639 }
640
641 if (_mesa_is_winsys_fbo(fb)) {
642 /* also set context drawbuffer state */
643 for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
644 if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
645 updated_drawbuffers(ctx, fb);
646 ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
647 }
648 }
649 }
650 }
651
652
653 /**
654 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
655 * from the context's Color.DrawBuffer[] state.
656 * Use when changing contexts.
657 */
658 void
659 _mesa_update_draw_buffers(struct gl_context *ctx)
660 {
661 /* should be a window system FBO */
662 assert(_mesa_is_winsys_fbo(ctx->DrawBuffer));
663
664 _mesa_drawbuffers(ctx, ctx->DrawBuffer, ctx->Const.MaxDrawBuffers,
665 ctx->Color.DrawBuffer, NULL);
666 }
667
668
669 /**
670 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
671 * GL_READ_BUFFER state for the given context and FBO.
672 * Note that all error checking should have been done before calling
673 * this function.
674 * \param ctx the rendering context
675 * \param fb the framebuffer object to update
676 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
677 * \param bufferIndex the numerical index corresponding to 'buffer'
678 */
679 void
680 _mesa_readbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
681 GLenum buffer, gl_buffer_index bufferIndex)
682 {
683 if ((fb == ctx->ReadBuffer) && _mesa_is_winsys_fbo(fb)) {
684 /* Only update the per-context READ_BUFFER state if we're bound to
685 * a window-system framebuffer.
686 */
687 ctx->Pixel.ReadBuffer = buffer;
688 }
689
690 fb->ColorReadBuffer = buffer;
691 fb->_ColorReadBufferIndex = bufferIndex;
692
693 ctx->NewState |= _NEW_BUFFERS;
694 }
695
696
697
698 /**
699 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
700 * renderbuffer for reading pixels.
701 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
702 */
703 static void
704 read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb,
705 GLenum buffer, const char *caller)
706 {
707 GLbitfield supportedMask;
708 gl_buffer_index srcBuffer;
709
710 FLUSH_VERTICES(ctx, 0);
711
712 if (MESA_VERBOSE & VERBOSE_API)
713 _mesa_debug(ctx, "%s %s\n", caller, _mesa_enum_to_string(buffer));
714
715 if (buffer == GL_NONE) {
716 /* This is legal--it means that no buffer should be bound for reading. */
717 srcBuffer = -1;
718 }
719 else {
720 /* general case / window-system framebuffer */
721 srcBuffer = read_buffer_enum_to_index(buffer);
722 if (srcBuffer == -1) {
723 _mesa_error(ctx, GL_INVALID_ENUM,
724 "%s(invalid buffer %s)", caller,
725 _mesa_enum_to_string(buffer));
726 return;
727 }
728 supportedMask = supported_buffer_bitmask(ctx, fb);
729 if (((1 << srcBuffer) & supportedMask) == 0) {
730 _mesa_error(ctx, GL_INVALID_OPERATION,
731 "%s(invalid buffer %s)", caller,
732 _mesa_enum_to_string(buffer));
733 return;
734 }
735 }
736
737 /* OK, all error checking has been completed now */
738
739 _mesa_readbuffer(ctx, fb, buffer, srcBuffer);
740
741 /* Call the device driver function only if fb is the bound read buffer */
742 if (fb == ctx->ReadBuffer) {
743 if (ctx->Driver.ReadBuffer)
744 ctx->Driver.ReadBuffer(ctx, buffer);
745 }
746 }
747
748
749 void GLAPIENTRY
750 _mesa_ReadBuffer(GLenum buffer)
751 {
752 GET_CURRENT_CONTEXT(ctx);
753 read_buffer(ctx, ctx->ReadBuffer, buffer, "glReadBuffer");
754 }
755
756
757 void GLAPIENTRY
758 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer, GLenum src)
759 {
760 GET_CURRENT_CONTEXT(ctx);
761 struct gl_framebuffer *fb;
762
763 if (framebuffer) {
764 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
765 "glNamedFramebufferReadBuffer");
766 if (!fb)
767 return;
768 }
769 else
770 fb = ctx->WinSysReadBuffer;
771
772 read_buffer(ctx, fb, src, "glNamedFramebufferReadBuffer");
773 }