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