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