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