buffers.c revision a4d72189b271664501338cc93107845f3d40ae54
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/**
27 * \file buffers.c
28 * glReadBuffer, DrawBuffer functions.
29 */
30
31
32
33#include "glheader.h"
34#include "buffers.h"
35#include "colormac.h"
36#include "context.h"
37#include "enums.h"
38#include "mtypes.h"
39
40
41#define BAD_MASK ~0u
42
43
44/**
45 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
46 * available to the rendering context (for drawing or reading).
47 * This depends on the type of framebuffer.  For window system framebuffers
48 * we look at the framebuffer's visual.  But for user-create framebuffers we
49 * look at the number of supported color attachments.
50 * \param fb  the framebuffer to draw to, or read from
51 * \return  bitmask of BUFFER_BIT_* flags
52 */
53static GLbitfield
54supported_buffer_bitmask(const struct gl_context *ctx, const struct gl_framebuffer *fb)
55{
56   GLbitfield mask = 0x0;
57
58   if (fb->Name > 0) {
59      /* A user-created renderbuffer */
60      GLuint i;
61      ASSERT(ctx->Extensions.EXT_framebuffer_object);
62      for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
63         mask |= (BUFFER_BIT_COLOR0 << i);
64      }
65   }
66   else {
67      /* A window system framebuffer */
68      GLint i;
69      mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
70      if (fb->Visual.stereoMode) {
71         mask |= BUFFER_BIT_FRONT_RIGHT;
72         if (fb->Visual.doubleBufferMode) {
73            mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
74         }
75      }
76      else if (fb->Visual.doubleBufferMode) {
77         mask |= BUFFER_BIT_BACK_LEFT;
78      }
79
80      for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
81         mask |= (BUFFER_BIT_AUX0 << i);
82      }
83   }
84
85   return mask;
86}
87
88
89/**
90 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
91 * Given a GLenum naming one or more color buffers (such as
92 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
93 */
94static GLbitfield
95draw_buffer_enum_to_bitmask(GLenum buffer)
96{
97   switch (buffer) {
98      case GL_NONE:
99         return 0;
100      case GL_FRONT:
101         return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
102      case GL_BACK:
103         return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
104      case GL_RIGHT:
105         return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
106      case GL_FRONT_RIGHT:
107         return BUFFER_BIT_FRONT_RIGHT;
108      case GL_BACK_RIGHT:
109         return BUFFER_BIT_BACK_RIGHT;
110      case GL_BACK_LEFT:
111         return BUFFER_BIT_BACK_LEFT;
112      case GL_FRONT_AND_BACK:
113         return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
114              | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
115      case GL_LEFT:
116         return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
117      case GL_FRONT_LEFT:
118         return BUFFER_BIT_FRONT_LEFT;
119      case GL_AUX0:
120         return BUFFER_BIT_AUX0;
121      case GL_AUX1:
122      case GL_AUX2:
123      case GL_AUX3:
124         return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
125      case GL_COLOR_ATTACHMENT0_EXT:
126         return BUFFER_BIT_COLOR0;
127      case GL_COLOR_ATTACHMENT1_EXT:
128         return BUFFER_BIT_COLOR1;
129      case GL_COLOR_ATTACHMENT2_EXT:
130         return BUFFER_BIT_COLOR2;
131      case GL_COLOR_ATTACHMENT3_EXT:
132         return BUFFER_BIT_COLOR3;
133      case GL_COLOR_ATTACHMENT4_EXT:
134         return BUFFER_BIT_COLOR4;
135      case GL_COLOR_ATTACHMENT5_EXT:
136         return BUFFER_BIT_COLOR5;
137      case GL_COLOR_ATTACHMENT6_EXT:
138         return BUFFER_BIT_COLOR6;
139      case GL_COLOR_ATTACHMENT7_EXT:
140         return BUFFER_BIT_COLOR7;
141      default:
142         /* error */
143         return BAD_MASK;
144   }
145}
146
147
148/**
149 * Helper routine used by glReadBuffer.
150 * Given a GLenum naming a color buffer, return the index of the corresponding
151 * renderbuffer (a BUFFER_* value).
152 * return -1 for an invalid buffer.
153 */
154static GLint
155read_buffer_enum_to_index(GLenum buffer)
156{
157   switch (buffer) {
158      case GL_FRONT:
159         return BUFFER_FRONT_LEFT;
160      case GL_BACK:
161         return BUFFER_BACK_LEFT;
162      case GL_RIGHT:
163         return BUFFER_FRONT_RIGHT;
164      case GL_FRONT_RIGHT:
165         return BUFFER_FRONT_RIGHT;
166      case GL_BACK_RIGHT:
167         return BUFFER_BACK_RIGHT;
168      case GL_BACK_LEFT:
169         return BUFFER_BACK_LEFT;
170      case GL_LEFT:
171         return BUFFER_FRONT_LEFT;
172      case GL_FRONT_LEFT:
173         return BUFFER_FRONT_LEFT;
174      case GL_AUX0:
175         return BUFFER_AUX0;
176      case GL_AUX1:
177      case GL_AUX2:
178      case GL_AUX3:
179         return BUFFER_COUNT; /* invalid, but not -1 */
180      case GL_COLOR_ATTACHMENT0_EXT:
181         return BUFFER_COLOR0;
182      case GL_COLOR_ATTACHMENT1_EXT:
183         return BUFFER_COLOR1;
184      case GL_COLOR_ATTACHMENT2_EXT:
185         return BUFFER_COLOR2;
186      case GL_COLOR_ATTACHMENT3_EXT:
187         return BUFFER_COLOR3;
188      case GL_COLOR_ATTACHMENT4_EXT:
189         return BUFFER_COLOR4;
190      case GL_COLOR_ATTACHMENT5_EXT:
191         return BUFFER_COLOR5;
192      case GL_COLOR_ATTACHMENT6_EXT:
193         return BUFFER_COLOR6;
194      case GL_COLOR_ATTACHMENT7_EXT:
195         return BUFFER_COLOR7;
196      default:
197         /* error */
198         return -1;
199   }
200}
201
202
203/**
204 * Called by glDrawBuffer().
205 * Specify which renderbuffer(s) to draw into for the first color output.
206 * <buffer> can name zero, one, two or four renderbuffers!
207 * \sa _mesa_DrawBuffersARB
208 *
209 * \param buffer  buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
210 *
211 * Note that the behaviour of this function depends on whether the
212 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
213 * a user-created framebuffer object (Name!=0).
214 *   In the former case, we update the per-context ctx->Color.DrawBuffer
215 *   state var _and_ the FB's ColorDrawBuffer state.
216 *   In the later case, we update the FB's ColorDrawBuffer state only.
217 *
218 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
219 * new FB is a window system FB, we need to re-update the FB's
220 * ColorDrawBuffer state to match the context.  This is handled in
221 * _mesa_update_framebuffer().
222 *
223 * See the GL_EXT_framebuffer_object spec for more info.
224 */
225void GLAPIENTRY
226_mesa_DrawBuffer(GLenum buffer)
227{
228   GLbitfield destMask;
229   GET_CURRENT_CONTEXT(ctx);
230   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
231
232   if (MESA_VERBOSE & VERBOSE_API) {
233      _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
234   }
235
236   if (buffer == GL_NONE) {
237      destMask = 0x0;
238   }
239   else {
240      const GLbitfield supportedMask
241         = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
242      destMask = draw_buffer_enum_to_bitmask(buffer);
243      if (destMask == BAD_MASK) {
244         /* totally bogus buffer */
245         _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer=0x%x)", buffer);
246         return;
247      }
248      destMask &= supportedMask;
249      if (destMask == 0x0) {
250         /* none of the named color buffers exist! */
251         _mesa_error(ctx, GL_INVALID_OPERATION,
252                     "glDrawBuffer(buffer=0x%x)", buffer);
253         return;
254      }
255   }
256
257   /* if we get here, there's no error so set new state */
258   _mesa_drawbuffers(ctx, 1, &buffer, &destMask);
259
260   /*
261    * Call device driver function.
262    */
263   if (ctx->Driver.DrawBuffers)
264      ctx->Driver.DrawBuffers(ctx, 1, &buffer);
265   else if (ctx->Driver.DrawBuffer)
266      ctx->Driver.DrawBuffer(ctx, buffer);
267}
268
269
270/**
271 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
272 * for N fragment program color outputs.
273 * \sa _mesa_DrawBuffer
274 * \param n  number of outputs
275 * \param buffers  array [n] of renderbuffer names.  Unlike glDrawBuffer, the
276 *                 names cannot specify more than one buffer.  For example,
277 *                 GL_FRONT_AND_BACK is illegal.
278 */
279void GLAPIENTRY
280_mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
281{
282   GLint output;
283   GLbitfield usedBufferMask, supportedMask;
284   GLbitfield destMask[MAX_DRAW_BUFFERS];
285   GET_CURRENT_CONTEXT(ctx);
286   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
287
288   /* Turns out n==0 is a valid input that should not produce an error.
289    * The remaining code below correctly handles the n==0 case.
290    */
291   if (n < 0 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
292      _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
293      return;
294   }
295
296   supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
297   usedBufferMask = 0x0;
298
299   /* complicated error checking... */
300   for (output = 0; output < n; output++) {
301      if (buffers[output] == GL_NONE) {
302         destMask[output] = 0x0;
303      }
304      else {
305         destMask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
306         if (destMask[output] == BAD_MASK
307             || _mesa_bitcount(destMask[output]) > 1) {
308            _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
309            return;
310         }
311         destMask[output] &= supportedMask;
312         if (destMask[output] == 0) {
313            _mesa_error(ctx, GL_INVALID_OPERATION,
314                        "glDrawBuffersARB(unsupported buffer)");
315            return;
316         }
317         if (destMask[output] & usedBufferMask) {
318            /* can't specify a dest buffer more than once! */
319            _mesa_error(ctx, GL_INVALID_OPERATION,
320                        "glDrawBuffersARB(duplicated buffer)");
321            return;
322         }
323
324         /* update bitmask */
325         usedBufferMask |= destMask[output];
326      }
327   }
328
329   /* OK, if we get here, there were no errors so set the new state */
330   _mesa_drawbuffers(ctx, n, buffers, destMask);
331
332   /*
333    * Call device driver function.  Note that n can be equal to 0,
334    * in which case we don't want to reference buffers[0], which
335    * may not be valid.
336    */
337   if (ctx->Driver.DrawBuffers)
338      ctx->Driver.DrawBuffers(ctx, n, buffers);
339   else if (ctx->Driver.DrawBuffer)
340      ctx->Driver.DrawBuffer(ctx, n > 0 ? buffers[0] : GL_NONE);
341}
342
343/**
344 * Performs necessary state updates when _mesa_drawbuffers makes an
345 * actual change.
346 */
347static void
348updated_drawbuffers(struct gl_context *ctx)
349{
350   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
351
352#if FEATURE_GL
353   if (ctx->API == API_OPENGL && !ctx->Extensions.ARB_ES2_compatibility) {
354      struct gl_framebuffer *fb = ctx->DrawBuffer;
355
356      /* Flag the FBO as requiring validation. */
357      if (fb->Name != 0) {
358	 fb->_Status = 0;
359      }
360   }
361#endif
362}
363
364/**
365 * Helper function to set the GL_DRAW_BUFFER state in the context and
366 * current FBO.  Called via glDrawBuffer(), glDrawBuffersARB()
367 *
368 * All error checking will have been done prior to calling this function
369 * so nothing should go wrong at this point.
370 *
371 * \param ctx  current context
372 * \param n    number of color outputs to set
373 * \param buffers  array[n] of colorbuffer names, like GL_LEFT.
374 * \param destMask  array[n] of BUFFER_BIT_* bitmasks which correspond to the
375 *                  colorbuffer names.  (i.e. GL_FRONT_AND_BACK =>
376 *                  BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
377 */
378void
379_mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
380                  const GLbitfield *destMask)
381{
382   struct gl_framebuffer *fb = ctx->DrawBuffer;
383   GLbitfield mask[MAX_DRAW_BUFFERS];
384   GLuint buf;
385
386   if (!destMask) {
387      /* compute destMask values now */
388      const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
389      GLuint output;
390      for (output = 0; output < n; output++) {
391         mask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
392         ASSERT(mask[output] != BAD_MASK);
393         mask[output] &= supportedMask;
394      }
395      destMask = mask;
396   }
397
398   /*
399    * If n==1, destMask[0] may have up to four bits set.
400    * Otherwise, destMask[x] can only have one bit set.
401    */
402   if (n == 1) {
403      GLuint count = 0, destMask0 = destMask[0];
404      while (destMask0) {
405         GLint bufIndex = _mesa_ffs(destMask0) - 1;
406         if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
407            updated_drawbuffers(ctx);
408            fb->_ColorDrawBufferIndexes[count] = bufIndex;
409         }
410         count++;
411         destMask0 &= ~(1 << bufIndex);
412      }
413      fb->ColorDrawBuffer[0] = buffers[0];
414      fb->_NumColorDrawBuffers = count;
415   }
416   else {
417      GLuint count = 0;
418      for (buf = 0; buf < n; buf++ ) {
419         if (destMask[buf]) {
420            GLint bufIndex = _mesa_ffs(destMask[buf]) - 1;
421            /* only one bit should be set in the destMask[buf] field */
422            ASSERT(_mesa_bitcount(destMask[buf]) == 1);
423            if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
424	       updated_drawbuffers(ctx);
425               fb->_ColorDrawBufferIndexes[buf] = bufIndex;
426            }
427            count = buf + 1;
428         }
429         else {
430            if (fb->_ColorDrawBufferIndexes[buf] != -1) {
431	       updated_drawbuffers(ctx);
432               fb->_ColorDrawBufferIndexes[buf] = -1;
433            }
434         }
435         fb->ColorDrawBuffer[buf] = buffers[buf];
436      }
437      fb->_NumColorDrawBuffers = count;
438   }
439
440   /* set remaining outputs to -1 (GL_NONE) */
441   for (buf = fb->_NumColorDrawBuffers; buf < ctx->Const.MaxDrawBuffers; buf++) {
442      if (fb->_ColorDrawBufferIndexes[buf] != -1) {
443         updated_drawbuffers(ctx);
444         fb->_ColorDrawBufferIndexes[buf] = -1;
445      }
446   }
447   for (buf = n; buf < ctx->Const.MaxDrawBuffers; buf++) {
448      fb->ColorDrawBuffer[buf] = GL_NONE;
449   }
450
451   if (fb->Name == 0) {
452      /* also set context drawbuffer state */
453      for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
454         if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
455	    updated_drawbuffers(ctx);
456            ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
457         }
458      }
459   }
460}
461
462
463/**
464 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
465 * from the context's Color.DrawBuffer[] state.
466 * Use when changing contexts.
467 */
468void
469_mesa_update_draw_buffers(struct gl_context *ctx)
470{
471   GLenum buffers[MAX_DRAW_BUFFERS];
472   GLuint i;
473
474   /* should be a window system FBO */
475   assert(ctx->DrawBuffer->Name == 0);
476
477   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++)
478      buffers[i] = ctx->Color.DrawBuffer[i];
479
480   _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
481}
482
483
484/**
485 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
486 * GL_READ_BUFFER state in the context and current FBO.
487 * \param ctx  the rendering context
488 * \param buffer  GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
489 * \param bufferIndex  the numerical index corresponding to 'buffer'
490 */
491void
492_mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex)
493{
494   struct gl_framebuffer *fb = ctx->ReadBuffer;
495
496   if (fb->Name == 0) {
497      /* Only update the per-context READ_BUFFER state if we're bound to
498       * a window-system framebuffer.
499       */
500      ctx->Pixel.ReadBuffer = buffer;
501   }
502
503   fb->ColorReadBuffer = buffer;
504   fb->_ColorReadBufferIndex = bufferIndex;
505
506   ctx->NewState |= _NEW_BUFFERS;
507}
508
509
510
511/**
512 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
513 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
514 */
515void GLAPIENTRY
516_mesa_ReadBuffer(GLenum buffer)
517{
518   struct gl_framebuffer *fb;
519   GLbitfield supportedMask;
520   GLint srcBuffer;
521   GET_CURRENT_CONTEXT(ctx);
522   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
523
524   if (MESA_VERBOSE & VERBOSE_API)
525      _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
526
527   fb = ctx->ReadBuffer;
528
529   if (MESA_VERBOSE & VERBOSE_API)
530      _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
531
532   if (fb->Name > 0 && buffer == GL_NONE) {
533      /* This is legal for user-created framebuffer objects */
534      srcBuffer = -1;
535   }
536   else {
537      /* general case / window-system framebuffer */
538      srcBuffer = read_buffer_enum_to_index(buffer);
539      if (srcBuffer == -1) {
540         _mesa_error(ctx, GL_INVALID_ENUM,
541                     "glReadBuffer(buffer=0x%x)", buffer);
542         return;
543      }
544      supportedMask = supported_buffer_bitmask(ctx, fb);
545      if (((1 << srcBuffer) & supportedMask) == 0) {
546         _mesa_error(ctx, GL_INVALID_OPERATION,
547                     "glReadBuffer(buffer=0x%x)", buffer);
548         return;
549      }
550   }
551
552   /* OK, all error checking has been completed now */
553
554   _mesa_readbuffer(ctx, buffer, srcBuffer);
555   ctx->NewState |= _NEW_BUFFERS;
556
557   /*
558    * Call device driver function.
559    */
560   if (ctx->Driver.ReadBuffer)
561      (*ctx->Driver.ReadBuffer)(ctx, buffer);
562}
563