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