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