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