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