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