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