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