mesa: rework error handling in glDrawBuffers
[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 #include "util/u_math.h"
41
42
43 #define BAD_MASK ~0u
44
45
46 /**
47 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
48 * available to the rendering context (for drawing or reading).
49 * This depends on the type of framebuffer. For window system framebuffers
50 * we look at the framebuffer's visual. But for user-create framebuffers we
51 * look at the number of supported color attachments.
52 * \param fb the framebuffer to draw to, or read from
53 * \return bitmask of BUFFER_BIT_* flags
54 */
55 static GLbitfield
56 supported_buffer_bitmask(const struct gl_context *ctx,
57 const struct gl_framebuffer *fb)
58 {
59 GLbitfield mask = 0x0;
60
61 if (_mesa_is_user_fbo(fb)) {
62 /* A user-created renderbuffer */
63 mask = ((1 << ctx->Const.MaxColorAttachments) - 1) << BUFFER_COLOR0;
64 }
65 else {
66 /* A window system framebuffer */
67 GLint i;
68 mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
69 if (fb->Visual.stereoMode) {
70 mask |= BUFFER_BIT_FRONT_RIGHT;
71 if (fb->Visual.doubleBufferMode) {
72 mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
73 }
74 }
75 else if (fb->Visual.doubleBufferMode) {
76 mask |= BUFFER_BIT_BACK_LEFT;
77 }
78
79 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
80 mask |= (BUFFER_BIT_AUX0 << i);
81 }
82 }
83
84 return mask;
85 }
86
87
88 /**
89 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
90 * Given a GLenum naming one or more color buffers (such as
91 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
92 */
93 static GLbitfield
94 draw_buffer_enum_to_bitmask(const struct gl_context *ctx, GLenum buffer)
95 {
96 switch (buffer) {
97 case GL_NONE:
98 return 0;
99 case GL_FRONT:
100 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
101 case GL_BACK:
102 if (_mesa_is_gles(ctx)) {
103 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
104 * ES 3.0.1 specification says:
105 *
106 * "When draw buffer zero is BACK, color values are written
107 * into the sole buffer for single-buffered contexts, or into
108 * the back buffer for double-buffered contexts."
109 *
110 * Since there is no stereo rendering in ES 3.0, only return the
111 * LEFT bits. This also satisfies the "n must be 1" requirement.
112 *
113 * We also do this for GLES 1 and 2 because those APIs have no
114 * concept of selecting the front and back buffer anyway and it's
115 * convenient to be able to maintain the magic behaviour of
116 * GL_BACK in that case.
117 */
118 if (ctx->DrawBuffer->Visual.doubleBufferMode)
119 return BUFFER_BIT_BACK_LEFT;
120 return BUFFER_BIT_FRONT_LEFT;
121 }
122 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
123 case GL_RIGHT:
124 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
125 case GL_FRONT_RIGHT:
126 return BUFFER_BIT_FRONT_RIGHT;
127 case GL_BACK_RIGHT:
128 return BUFFER_BIT_BACK_RIGHT;
129 case GL_BACK_LEFT:
130 return BUFFER_BIT_BACK_LEFT;
131 case GL_FRONT_AND_BACK:
132 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
133 | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
134 case GL_LEFT:
135 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
136 case GL_FRONT_LEFT:
137 return BUFFER_BIT_FRONT_LEFT;
138 case GL_AUX0:
139 return BUFFER_BIT_AUX0;
140 case GL_AUX1:
141 case GL_AUX2:
142 case GL_AUX3:
143 return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
144 case GL_COLOR_ATTACHMENT0_EXT:
145 return BUFFER_BIT_COLOR0;
146 case GL_COLOR_ATTACHMENT1_EXT:
147 return BUFFER_BIT_COLOR1;
148 case GL_COLOR_ATTACHMENT2_EXT:
149 return BUFFER_BIT_COLOR2;
150 case GL_COLOR_ATTACHMENT3_EXT:
151 return BUFFER_BIT_COLOR3;
152 case GL_COLOR_ATTACHMENT4_EXT:
153 return BUFFER_BIT_COLOR4;
154 case GL_COLOR_ATTACHMENT5_EXT:
155 return BUFFER_BIT_COLOR5;
156 case GL_COLOR_ATTACHMENT6_EXT:
157 return BUFFER_BIT_COLOR6;
158 case GL_COLOR_ATTACHMENT7_EXT:
159 return BUFFER_BIT_COLOR7;
160 default:
161 /* not an error, but also not supported */
162 if (buffer >= GL_COLOR_ATTACHMENT8 && buffer <= GL_COLOR_ATTACHMENT31)
163 return 1 << BUFFER_COUNT;
164 /* error */
165 return BAD_MASK;
166 }
167 }
168
169
170 /**
171 * Helper routine used by glReadBuffer.
172 * Given a GLenum naming a color buffer, return the index of the corresponding
173 * renderbuffer (a BUFFER_* value).
174 * return BUFFER_NONE for an invalid buffer.
175 */
176 static gl_buffer_index
177 read_buffer_enum_to_index(const struct gl_context *ctx, GLenum buffer)
178 {
179 switch (buffer) {
180 case GL_FRONT:
181 return BUFFER_FRONT_LEFT;
182 case GL_BACK:
183 if (_mesa_is_gles(ctx)) {
184 /* In draw_buffer_enum_to_bitmask, when GLES contexts draw to
185 * GL_BACK with a single-buffered configuration, we actually end
186 * up drawing to the sole front buffer in our internal
187 * representation. For consistency, we must read from that
188 * front left buffer too.
189 */
190 if (!ctx->DrawBuffer->Visual.doubleBufferMode)
191 return BUFFER_FRONT_LEFT;
192 }
193 return BUFFER_BACK_LEFT;
194 case GL_RIGHT:
195 return BUFFER_FRONT_RIGHT;
196 case GL_FRONT_RIGHT:
197 return BUFFER_FRONT_RIGHT;
198 case GL_BACK_RIGHT:
199 return BUFFER_BACK_RIGHT;
200 case GL_BACK_LEFT:
201 return BUFFER_BACK_LEFT;
202 case GL_LEFT:
203 return BUFFER_FRONT_LEFT;
204 case GL_FRONT_LEFT:
205 return BUFFER_FRONT_LEFT;
206 case GL_AUX0:
207 return BUFFER_AUX0;
208 case GL_FRONT_AND_BACK:
209 return BUFFER_FRONT_LEFT;
210 case GL_AUX1:
211 case GL_AUX2:
212 case GL_AUX3:
213 return BUFFER_COUNT; /* invalid, but not -1 */
214 case GL_COLOR_ATTACHMENT0_EXT:
215 return BUFFER_COLOR0;
216 case GL_COLOR_ATTACHMENT1_EXT:
217 return BUFFER_COLOR1;
218 case GL_COLOR_ATTACHMENT2_EXT:
219 return BUFFER_COLOR2;
220 case GL_COLOR_ATTACHMENT3_EXT:
221 return BUFFER_COLOR3;
222 case GL_COLOR_ATTACHMENT4_EXT:
223 return BUFFER_COLOR4;
224 case GL_COLOR_ATTACHMENT5_EXT:
225 return BUFFER_COLOR5;
226 case GL_COLOR_ATTACHMENT6_EXT:
227 return BUFFER_COLOR6;
228 case GL_COLOR_ATTACHMENT7_EXT:
229 return BUFFER_COLOR7;
230 default:
231 /* not an error, but also not supported */
232 if (buffer >= GL_COLOR_ATTACHMENT8 && buffer <= GL_COLOR_ATTACHMENT31)
233 return BUFFER_COUNT;
234 /* error */
235 return BUFFER_NONE;
236 }
237 }
238
239 static bool
240 is_legal_es3_readbuffer_enum(GLenum buf)
241 {
242 return buf == GL_BACK || buf == GL_NONE ||
243 (buf >= GL_COLOR_ATTACHMENT0 && buf <= GL_COLOR_ATTACHMENT31);
244 }
245
246 /**
247 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
248 * Specify which renderbuffer(s) to draw into for the first color output.
249 * <buffer> can name zero, one, two or four renderbuffers!
250 * \sa _mesa_DrawBuffers
251 *
252 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
253 *
254 * Note that the behaviour of this function depends on whether the
255 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
256 * framebuffer object.
257 * In the former case, we update the per-context ctx->Color.DrawBuffer
258 * state var _and_ the FB's ColorDrawBuffer state.
259 * In the later case, we update the FB's ColorDrawBuffer state only.
260 *
261 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
262 * new FB is a window system FB, we need to re-update the FB's
263 * ColorDrawBuffer state to match the context. This is handled in
264 * _mesa_update_framebuffer().
265 *
266 * See the GL_EXT_framebuffer_object spec for more info.
267 */
268 static ALWAYS_INLINE void
269 draw_buffer(struct gl_context *ctx, struct gl_framebuffer *fb,
270 GLenum buffer, const char *caller, bool no_error)
271 {
272 GLbitfield destMask;
273
274 FLUSH_VERTICES(ctx, 0);
275
276 if (MESA_VERBOSE & VERBOSE_API) {
277 _mesa_debug(ctx, "%s %s\n", caller, _mesa_enum_to_string(buffer));
278 }
279
280 if (buffer == GL_NONE) {
281 destMask = 0x0;
282 }
283 else {
284 const GLbitfield supportedMask
285 = supported_buffer_bitmask(ctx, fb);
286 destMask = draw_buffer_enum_to_bitmask(ctx, buffer);
287 if (!no_error && destMask == BAD_MASK) {
288 /* totally bogus buffer */
289 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)", caller,
290 _mesa_enum_to_string(buffer));
291 return;
292 }
293 destMask &= supportedMask;
294 if (!no_error && destMask == 0x0) {
295 /* none of the named color buffers exist! */
296 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid buffer %s)",
297 caller, _mesa_enum_to_string(buffer));
298 return;
299 }
300 }
301
302 /* if we get here, there's no error so set new state */
303 const GLenum16 buffer16 = buffer;
304 _mesa_drawbuffers(ctx, fb, 1, &buffer16, &destMask);
305
306 /* Call device driver function only if fb is the bound draw buffer */
307 if (fb == ctx->DrawBuffer) {
308 if (ctx->Driver.DrawBuffer)
309 ctx->Driver.DrawBuffer(ctx);
310 if (ctx->Driver.DrawBufferAllocate)
311 ctx->Driver.DrawBufferAllocate(ctx);
312 }
313 }
314
315
316 static void
317 draw_buffer_error(struct gl_context *ctx, struct gl_framebuffer *fb,
318 GLenum buffer, const char *caller)
319 {
320 draw_buffer(ctx, fb, buffer, caller, false);
321 }
322
323
324 static void
325 draw_buffer_no_error(struct gl_context *ctx, struct gl_framebuffer *fb,
326 GLenum buffer, const char *caller)
327 {
328 draw_buffer(ctx, fb, buffer, caller, true);
329 }
330
331
332 void GLAPIENTRY
333 _mesa_DrawBuffer_no_error(GLenum buffer)
334 {
335 GET_CURRENT_CONTEXT(ctx);
336 draw_buffer_no_error(ctx, ctx->DrawBuffer, buffer, "glDrawBuffer");
337 }
338
339
340 void GLAPIENTRY
341 _mesa_DrawBuffer(GLenum buffer)
342 {
343 GET_CURRENT_CONTEXT(ctx);
344 draw_buffer_error(ctx, ctx->DrawBuffer, buffer, "glDrawBuffer");
345 }
346
347
348 void GLAPIENTRY
349 _mesa_NamedFramebufferDrawBuffer_no_error(GLuint framebuffer, GLenum buf)
350 {
351 GET_CURRENT_CONTEXT(ctx);
352 struct gl_framebuffer *fb;
353
354 if (framebuffer) {
355 fb = _mesa_lookup_framebuffer(ctx, framebuffer);
356 } else {
357 fb = ctx->WinSysDrawBuffer;
358 }
359
360 draw_buffer_no_error(ctx, fb, buf, "glNamedFramebufferDrawBuffer");
361 }
362
363
364 void GLAPIENTRY
365 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer, GLenum buf)
366 {
367 GET_CURRENT_CONTEXT(ctx);
368 struct gl_framebuffer *fb;
369
370 if (framebuffer) {
371 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
372 "glNamedFramebufferDrawBuffer");
373 if (!fb)
374 return;
375 }
376 else
377 fb = ctx->WinSysDrawBuffer;
378
379 draw_buffer_error(ctx, fb, buf, "glNamedFramebufferDrawBuffer");
380 }
381
382
383 /**
384 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
385 * the destination color renderbuffers for N fragment program color outputs.
386 * \sa _mesa_DrawBuffer
387 * \param n number of outputs
388 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
389 * names cannot specify more than one buffer. For example,
390 * GL_FRONT_AND_BACK is illegal. The only exception is GL_BACK
391 * that is considered special and allowed as far as n is one
392 * since 4.5.
393 */
394 static ALWAYS_INLINE void
395 draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb, GLsizei n,
396 const GLenum *buffers, const char *caller, bool no_error)
397 {
398 GLuint output;
399 GLbitfield usedBufferMask, supportedMask;
400 GLbitfield destMask[MAX_DRAW_BUFFERS];
401
402 FLUSH_VERTICES(ctx, 0);
403
404 if (!no_error) {
405 /* Turns out n==0 is a valid input that should not produce an error.
406 * The remaining code below correctly handles the n==0 case.
407 *
408 * From the OpenGL 3.0 specification, page 258:
409 * "An INVALID_VALUE error is generated if n is greater than
410 * MAX_DRAW_BUFFERS."
411 */
412 if (n < 0) {
413 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", caller);
414 return;
415 }
416
417 if (n > (GLsizei) ctx->Const.MaxDrawBuffers) {
418 _mesa_error(ctx, GL_INVALID_VALUE,
419 "%s(n > maximum number of draw buffers)", caller);
420 return;
421 }
422
423 /* From the ES 3.0 specification, page 180:
424 * "If the GL is bound to the default framebuffer, then n must be 1
425 * and the constant must be BACK or NONE."
426 * (same restriction applies with GL_EXT_draw_buffers specification)
427 */
428 if (ctx->API == API_OPENGLES2 && _mesa_is_winsys_fbo(fb) &&
429 (n != 1 || (buffers[0] != GL_NONE && buffers[0] != GL_BACK))) {
430 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid buffers)", caller);
431 return;
432 }
433 }
434
435 supportedMask = supported_buffer_bitmask(ctx, fb);
436 usedBufferMask = 0x0;
437
438 /* complicated error checking... */
439 for (output = 0; output < n; output++) {
440 if (!no_error) {
441 /* From the OpenGL 4.5 specification, page 493 (page 515 of the PDF)
442 * "An INVALID_ENUM error is generated if any value in bufs is FRONT,
443 * LEFT, RIGHT, or FRONT_AND_BACK . This restriction applies to both
444 * the default framebuffer and framebuffer objects, and exists because
445 * these constants may themselves refer to multiple buffers, as shown
446 * in table 17.4."
447 *
448 * From the OpenGL 4.5 specification, page 492 (page 514 of the PDF):
449 * "If the default framebuffer is affected, then each of the constants
450 * must be one of the values listed in table 17.6 or the special value
451 * BACK. When BACK is used, n must be 1 and color values are written
452 * into the left buffer for single-buffered contexts, or into the back
453 * left buffer for double-buffered contexts."
454 *
455 * Note "special value BACK". GL_BACK also refers to multiple buffers,
456 * but it is consider a special case here. This is a change on 4.5.
457 * For OpenGL 4.x we check that behaviour. For any previous version we
458 * keep considering it wrong (as INVALID_ENUM).
459 */
460 if (buffers[output] == GL_BACK &&
461 _mesa_is_winsys_fbo(fb) &&
462 _mesa_is_desktop_gl(ctx) &&
463 ctx->Version >= 40) {
464 if (n != 1) {
465 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(with GL_BACK n must be 1)",
466 caller);
467 return;
468 }
469 } else if (buffers[output] == GL_FRONT ||
470 buffers[output] == GL_LEFT ||
471 buffers[output] == GL_RIGHT ||
472 buffers[output] == GL_FRONT_AND_BACK ||
473 (buffers[output] == GL_BACK &&
474 _mesa_is_desktop_gl(ctx))) {
475 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)",
476 caller, _mesa_enum_to_string(buffers[output]));
477 return;
478 }
479 }
480
481 destMask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
482
483 if (!no_error) {
484 /* From the OpenGL 3.0 specification, page 258:
485 * "Each buffer listed in bufs must be one of the values from tables
486 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
487 */
488 if (destMask[output] == BAD_MASK) {
489 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid buffer %s)",
490 caller, _mesa_enum_to_string(buffers[output]));
491 return;
492 }
493
494 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL ES 3.0
495 * specification says:
496 *
497 * "If the GL is bound to a draw framebuffer object, the ith
498 * buffer listed in bufs must be COLOR_ATTACHMENTi or NONE .
499 * Specifying a buffer out of order, BACK , or COLOR_ATTACHMENTm
500 * where m is greater than or equal to the value of MAX_-
501 * COLOR_ATTACHMENTS , will generate the error INVALID_OPERATION .
502 */
503 if (_mesa_is_gles3(ctx) && _mesa_is_user_fbo(fb) &&
504 buffers[output] != GL_NONE &&
505 (buffers[output] < GL_COLOR_ATTACHMENT0 ||
506 buffers[output] >= GL_COLOR_ATTACHMENT0 + ctx->Const.MaxColorAttachments)) {
507 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffers(buffer)");
508 return;
509 }
510 }
511
512 if (buffers[output] == GL_NONE) {
513 destMask[output] = 0x0;
514 }
515 else {
516 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
517 * spec (20080923) says:
518 *
519 * "If the GL is bound to a framebuffer object and DrawBuffers is
520 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
521 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
522 * INVALID_OPERATION results."
523 */
524 if (!no_error && _mesa_is_user_fbo(fb) && buffers[output] >=
525 GL_COLOR_ATTACHMENT0 + ctx->Const.MaxDrawBuffers) {
526 _mesa_error(ctx, GL_INVALID_OPERATION,
527 "%s(buffers[%d] >= maximum number of draw buffers)",
528 caller, output);
529 return;
530 }
531
532 /* From the OpenGL 3.0 specification, page 259:
533 * "If the GL is bound to the default framebuffer and DrawBuffers is
534 * supplied with a constant (other than NONE) that does not indicate
535 * any of the color buffers allocated to the GL context by the window
536 * system, the error INVALID_OPERATION will be generated.
537 *
538 * If the GL is bound to a framebuffer object and DrawBuffers is
539 * supplied with a constant from table 4.6 [...] then the error
540 * INVALID_OPERATION results."
541 */
542 destMask[output] &= supportedMask;
543 if (!no_error) {
544 if (destMask[output] == 0) {
545 _mesa_error(ctx, GL_INVALID_OPERATION,
546 "%s(unsupported buffer %s)",
547 caller, _mesa_enum_to_string(buffers[output]));
548 return;
549 }
550
551 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
552 * "If the GL is bound to a framebuffer object, the ith buffer
553 * listed in bufs must be COLOR_ATTACHMENTi or NONE. [...]
554 * INVALID_OPERATION." (same restriction applies with
555 * GL_EXT_draw_buffers specification)
556 */
557 if (ctx->API == API_OPENGLES2 && _mesa_is_user_fbo(fb) &&
558 buffers[output] != GL_NONE &&
559 buffers[output] != GL_COLOR_ATTACHMENT0 + output) {
560 _mesa_error(ctx, GL_INVALID_OPERATION,
561 "%s(unsupported buffer %s)",
562 caller, _mesa_enum_to_string(buffers[output]));
563 return;
564 }
565
566 /* From the OpenGL 3.0 specification, page 258:
567 * "Except for NONE, a buffer may not appear more than once in the
568 * array pointed to by bufs. Specifying a buffer more then once
569 * will result in the error INVALID_OPERATION."
570 */
571 if (destMask[output] & usedBufferMask) {
572 _mesa_error(ctx, GL_INVALID_OPERATION,
573 "%s(duplicated buffer %s)",
574 caller, _mesa_enum_to_string(buffers[output]));
575 return;
576 }
577 }
578
579 /* update bitmask */
580 usedBufferMask |= destMask[output];
581 }
582 }
583
584 /* OK, if we get here, there were no errors so set the new state */
585 GLenum16 buffers16[MAX_DRAW_BUFFERS];
586 for (int i = 0; i < n; i++)
587 buffers16[i] = buffers[i];
588
589 _mesa_drawbuffers(ctx, fb, n, buffers16, destMask);
590
591 /*
592 * Call device driver function if fb is the bound draw buffer.
593 * Note that n can be equal to 0,
594 * in which case we don't want to reference buffers[0], which
595 * may not be valid.
596 */
597 if (fb == ctx->DrawBuffer) {
598 if (ctx->Driver.DrawBuffer)
599 ctx->Driver.DrawBuffer(ctx);
600 if (ctx->Driver.DrawBufferAllocate)
601 ctx->Driver.DrawBufferAllocate(ctx);
602 }
603 }
604
605
606 static void
607 draw_buffers_error(struct gl_context *ctx, struct gl_framebuffer *fb, GLsizei n,
608 const GLenum *buffers, const char *caller)
609 {
610 draw_buffers(ctx, fb, n, buffers, caller, false);
611 }
612
613
614 static void
615 draw_buffers_no_error(struct gl_context *ctx, struct gl_framebuffer *fb,
616 GLsizei n, const GLenum *buffers, const char *caller)
617 {
618 draw_buffers(ctx, fb, n, buffers, caller, true);
619 }
620
621
622 void GLAPIENTRY
623 _mesa_DrawBuffers_no_error(GLsizei n, const GLenum *buffers)
624 {
625 GET_CURRENT_CONTEXT(ctx);
626 draw_buffers_no_error(ctx, ctx->DrawBuffer, n, buffers, "glDrawBuffers");
627 }
628
629
630 void GLAPIENTRY
631 _mesa_DrawBuffers(GLsizei n, const GLenum *buffers)
632 {
633 GET_CURRENT_CONTEXT(ctx);
634 draw_buffers_error(ctx, ctx->DrawBuffer, n, buffers, "glDrawBuffers");
635 }
636
637
638 void GLAPIENTRY
639 _mesa_NamedFramebufferDrawBuffers_no_error(GLuint framebuffer, GLsizei n,
640 const GLenum *bufs)
641 {
642 GET_CURRENT_CONTEXT(ctx);
643 struct gl_framebuffer *fb;
644
645 if (framebuffer) {
646 fb = _mesa_lookup_framebuffer(ctx, framebuffer);
647 } else {
648 fb = ctx->WinSysDrawBuffer;
649 }
650
651 draw_buffers_no_error(ctx, fb, n, bufs, "glNamedFramebufferDrawBuffers");
652 }
653
654
655 void GLAPIENTRY
656 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer, GLsizei n,
657 const GLenum *bufs)
658 {
659 GET_CURRENT_CONTEXT(ctx);
660 struct gl_framebuffer *fb;
661
662 if (framebuffer) {
663 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
664 "glNamedFramebufferDrawBuffers");
665 if (!fb)
666 return;
667 }
668 else
669 fb = ctx->WinSysDrawBuffer;
670
671 draw_buffers_error(ctx, fb, n, bufs, "glNamedFramebufferDrawBuffers");
672 }
673
674
675 /**
676 * Performs necessary state updates when _mesa_drawbuffers makes an
677 * actual change.
678 */
679 static void
680 updated_drawbuffers(struct gl_context *ctx, struct gl_framebuffer *fb)
681 {
682 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
683
684 if (ctx->API == API_OPENGL_COMPAT && !ctx->Extensions.ARB_ES2_compatibility) {
685 /* Flag the FBO as requiring validation. */
686 if (_mesa_is_user_fbo(fb)) {
687 fb->_Status = 0;
688 }
689 }
690 }
691
692
693 /**
694 * Helper function to set the GL_DRAW_BUFFER state for the given context and
695 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
696 *
697 * All error checking will have been done prior to calling this function
698 * so nothing should go wrong at this point.
699 *
700 * \param ctx current context
701 * \param fb the desired draw buffer
702 * \param n number of color outputs to set
703 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
704 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
705 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
706 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
707 */
708 void
709 _mesa_drawbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
710 GLuint n, const GLenum16 *buffers,
711 const GLbitfield *destMask)
712 {
713 GLbitfield mask[MAX_DRAW_BUFFERS];
714 GLuint buf;
715
716 if (!destMask) {
717 /* compute destMask values now */
718 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
719 GLuint output;
720 for (output = 0; output < n; output++) {
721 mask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
722 assert(mask[output] != BAD_MASK);
723 mask[output] &= supportedMask;
724 }
725 destMask = mask;
726 }
727
728 /*
729 * destMask[0] may have up to four bits set
730 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
731 * Otherwise, destMask[x] can only have one bit set.
732 */
733 if (n > 0 && util_bitcount(destMask[0]) > 1) {
734 GLuint count = 0, destMask0 = destMask[0];
735 while (destMask0) {
736 const gl_buffer_index bufIndex = u_bit_scan(&destMask0);
737 if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
738 updated_drawbuffers(ctx, fb);
739 fb->_ColorDrawBufferIndexes[count] = bufIndex;
740 }
741 count++;
742 }
743 fb->ColorDrawBuffer[0] = buffers[0];
744 fb->_NumColorDrawBuffers = count;
745 }
746 else {
747 GLuint count = 0;
748 for (buf = 0; buf < n; buf++ ) {
749 if (destMask[buf]) {
750 gl_buffer_index bufIndex = ffs(destMask[buf]) - 1;
751 /* only one bit should be set in the destMask[buf] field */
752 assert(util_bitcount(destMask[buf]) == 1);
753 if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
754 updated_drawbuffers(ctx, fb);
755 fb->_ColorDrawBufferIndexes[buf] = bufIndex;
756 }
757 count = buf + 1;
758 }
759 else {
760 if (fb->_ColorDrawBufferIndexes[buf] != BUFFER_NONE) {
761 updated_drawbuffers(ctx, fb);
762 fb->_ColorDrawBufferIndexes[buf] = BUFFER_NONE;
763 }
764 }
765 fb->ColorDrawBuffer[buf] = buffers[buf];
766 }
767 fb->_NumColorDrawBuffers = count;
768 }
769
770 /* set remaining outputs to BUFFER_NONE */
771 for (buf = fb->_NumColorDrawBuffers; buf < ctx->Const.MaxDrawBuffers; buf++) {
772 if (fb->_ColorDrawBufferIndexes[buf] != BUFFER_NONE) {
773 updated_drawbuffers(ctx, fb);
774 fb->_ColorDrawBufferIndexes[buf] = BUFFER_NONE;
775 }
776 }
777 for (buf = n; buf < ctx->Const.MaxDrawBuffers; buf++) {
778 fb->ColorDrawBuffer[buf] = GL_NONE;
779 }
780
781 if (_mesa_is_winsys_fbo(fb)) {
782 /* also set context drawbuffer state */
783 for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
784 if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
785 updated_drawbuffers(ctx, fb);
786 ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
787 }
788 }
789 }
790 }
791
792
793 /**
794 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
795 * from the context's Color.DrawBuffer[] state.
796 * Use when changing contexts.
797 */
798 void
799 _mesa_update_draw_buffers(struct gl_context *ctx)
800 {
801 /* should be a window system FBO */
802 assert(_mesa_is_winsys_fbo(ctx->DrawBuffer));
803
804 _mesa_drawbuffers(ctx, ctx->DrawBuffer, ctx->Const.MaxDrawBuffers,
805 ctx->Color.DrawBuffer, NULL);
806 }
807
808
809 /**
810 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
811 * GL_READ_BUFFER state for the given context and FBO.
812 * Note that all error checking should have been done before calling
813 * this function.
814 * \param ctx the rendering context
815 * \param fb the framebuffer object to update
816 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
817 * \param bufferIndex the numerical index corresponding to 'buffer'
818 */
819 void
820 _mesa_readbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
821 GLenum buffer, gl_buffer_index bufferIndex)
822 {
823 if ((fb == ctx->ReadBuffer) && _mesa_is_winsys_fbo(fb)) {
824 /* Only update the per-context READ_BUFFER state if we're bound to
825 * a window-system framebuffer.
826 */
827 ctx->Pixel.ReadBuffer = buffer;
828 }
829
830 fb->ColorReadBuffer = buffer;
831 fb->_ColorReadBufferIndex = bufferIndex;
832
833 ctx->NewState |= _NEW_BUFFERS;
834 }
835
836
837
838 /**
839 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
840 * renderbuffer for reading pixels.
841 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
842 */
843 static ALWAYS_INLINE void
844 read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb,
845 GLenum buffer, const char *caller, bool no_error)
846 {
847 gl_buffer_index srcBuffer;
848
849 FLUSH_VERTICES(ctx, 0);
850
851 if (MESA_VERBOSE & VERBOSE_API)
852 _mesa_debug(ctx, "%s %s\n", caller, _mesa_enum_to_string(buffer));
853
854 if (buffer == GL_NONE) {
855 /* This is legal--it means that no buffer should be bound for reading. */
856 srcBuffer = BUFFER_NONE;
857 }
858 else {
859 /* general case / window-system framebuffer */
860 if (!no_error &&_mesa_is_gles3(ctx) &&
861 !is_legal_es3_readbuffer_enum(buffer))
862 srcBuffer = BUFFER_NONE;
863 else
864 srcBuffer = read_buffer_enum_to_index(ctx, buffer);
865
866 if (!no_error) {
867 GLbitfield supportedMask;
868
869 if (srcBuffer == BUFFER_NONE) {
870 _mesa_error(ctx, GL_INVALID_ENUM,
871 "%s(invalid buffer %s)", caller,
872 _mesa_enum_to_string(buffer));
873 return;
874 }
875
876 supportedMask = supported_buffer_bitmask(ctx, fb);
877 if (((1 << srcBuffer) & supportedMask) == 0) {
878 _mesa_error(ctx, GL_INVALID_OPERATION,
879 "%s(invalid buffer %s)", caller,
880 _mesa_enum_to_string(buffer));
881 return;
882 }
883 }
884 }
885
886 /* OK, all error checking has been completed now */
887
888 _mesa_readbuffer(ctx, fb, buffer, srcBuffer);
889
890 /* Call the device driver function only if fb is the bound read buffer */
891 if (fb == ctx->ReadBuffer) {
892 if (ctx->Driver.ReadBuffer)
893 ctx->Driver.ReadBuffer(ctx, buffer);
894 }
895 }
896
897
898 static void
899 read_buffer_err(struct gl_context *ctx, struct gl_framebuffer *fb,
900 GLenum buffer, const char *caller)
901 {
902 read_buffer(ctx, fb, buffer, caller, false);
903 }
904
905
906 static void
907 read_buffer_no_error(struct gl_context *ctx, struct gl_framebuffer *fb,
908 GLenum buffer, const char *caller)
909 {
910 read_buffer(ctx, fb, buffer, caller, true);
911 }
912
913
914 void GLAPIENTRY
915 _mesa_ReadBuffer_no_error(GLenum buffer)
916 {
917 GET_CURRENT_CONTEXT(ctx);
918 read_buffer_no_error(ctx, ctx->ReadBuffer, buffer, "glReadBuffer");
919 }
920
921
922 void GLAPIENTRY
923 _mesa_ReadBuffer(GLenum buffer)
924 {
925 GET_CURRENT_CONTEXT(ctx);
926 read_buffer_err(ctx, ctx->ReadBuffer, buffer, "glReadBuffer");
927 }
928
929
930 void GLAPIENTRY
931 _mesa_NamedFramebufferReadBuffer_no_error(GLuint framebuffer, GLenum src)
932 {
933 GET_CURRENT_CONTEXT(ctx);
934
935 struct gl_framebuffer *fb;
936
937 if (framebuffer) {
938 fb = _mesa_lookup_framebuffer(ctx, framebuffer);
939 } else {
940 fb = ctx->WinSysReadBuffer;
941 }
942
943 read_buffer_no_error(ctx, fb, src, "glNamedFramebufferReadBuffer");
944 }
945
946
947 void GLAPIENTRY
948 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer, GLenum src)
949 {
950 GET_CURRENT_CONTEXT(ctx);
951 struct gl_framebuffer *fb;
952
953 if (framebuffer) {
954 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
955 "glNamedFramebufferReadBuffer");
956 if (!fb)
957 return;
958 }
959 else
960 fb = ctx->WinSysReadBuffer;
961
962 read_buffer_err(ctx, fb, src, "glNamedFramebufferReadBuffer");
963 }