mesa: Allow extensions in MESA_EXTENSION_OVERRIDE to be prefixed with '+'
[mesa.git] / src / mesa / main / buffers.c
index c767e622865801d189c23a62d13f2be3c5cae75d..5c37f3d1a86874d8e85d5fbd86cb0bd039cfec12 100644 (file)
@@ -35,8 +35,7 @@
 #include "colormac.h"
 #include "context.h"
 #include "enums.h"
-#include "fbobject.h"
-#include "state.h"
+#include "mtypes.h"
 
 
 #define BAD_MASK ~0u
@@ -52,7 +51,7 @@
  * \return  bitmask of BUFFER_BIT_* flags
  */
 static GLbitfield
-supported_buffer_bitmask(const GLcontext *ctx, const struct gl_framebuffer *fb)
+supported_buffer_bitmask(const struct gl_context *ctx, const struct gl_framebuffer *fb)
 {
    GLbitfield mask = 0x0;
 
@@ -120,11 +119,9 @@ draw_buffer_enum_to_bitmask(GLenum buffer)
       case GL_AUX0:
          return BUFFER_BIT_AUX0;
       case GL_AUX1:
-         return BUFFER_BIT_AUX1;
       case GL_AUX2:
-         return BUFFER_BIT_AUX2;
       case GL_AUX3:
-         return BUFFER_BIT_AUX3;
+         return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
       case GL_COLOR_ATTACHMENT0_EXT:
          return BUFFER_BIT_COLOR0;
       case GL_COLOR_ATTACHMENT1_EXT:
@@ -133,6 +130,14 @@ draw_buffer_enum_to_bitmask(GLenum buffer)
          return BUFFER_BIT_COLOR2;
       case GL_COLOR_ATTACHMENT3_EXT:
          return BUFFER_BIT_COLOR3;
+      case GL_COLOR_ATTACHMENT4_EXT:
+         return BUFFER_BIT_COLOR4;
+      case GL_COLOR_ATTACHMENT5_EXT:
+         return BUFFER_BIT_COLOR5;
+      case GL_COLOR_ATTACHMENT6_EXT:
+         return BUFFER_BIT_COLOR6;
+      case GL_COLOR_ATTACHMENT7_EXT:
+         return BUFFER_BIT_COLOR7;
       default:
          /* error */
          return BAD_MASK;
@@ -169,11 +174,9 @@ read_buffer_enum_to_index(GLenum buffer)
       case GL_AUX0:
          return BUFFER_AUX0;
       case GL_AUX1:
-         return BUFFER_AUX1;
       case GL_AUX2:
-         return BUFFER_AUX2;
       case GL_AUX3:
-         return BUFFER_AUX3;
+         return BUFFER_COUNT; /* invalid, but not -1 */
       case GL_COLOR_ATTACHMENT0_EXT:
          return BUFFER_COLOR0;
       case GL_COLOR_ATTACHMENT1_EXT:
@@ -182,6 +185,14 @@ read_buffer_enum_to_index(GLenum buffer)
          return BUFFER_COLOR2;
       case GL_COLOR_ATTACHMENT3_EXT:
          return BUFFER_COLOR3;
+      case GL_COLOR_ATTACHMENT4_EXT:
+         return BUFFER_COLOR4;
+      case GL_COLOR_ATTACHMENT5_EXT:
+         return BUFFER_COLOR5;
+      case GL_COLOR_ATTACHMENT6_EXT:
+         return BUFFER_COLOR6;
+      case GL_COLOR_ATTACHMENT7_EXT:
+         return BUFFER_COLOR7;
       default:
          /* error */
          return -1;
@@ -196,6 +207,20 @@ read_buffer_enum_to_index(GLenum buffer)
  * \sa _mesa_DrawBuffersARB
  *
  * \param buffer  buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
+ *
+ * Note that the behaviour of this function depends on whether the
+ * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
+ * a user-created framebuffer object (Name!=0).
+ *   In the former case, we update the per-context ctx->Color.DrawBuffer
+ *   state var _and_ the FB's ColorDrawBuffer state.
+ *   In the later case, we update the FB's ColorDrawBuffer state only.
+ *
+ * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
+ * new FB is a window system FB, we need to re-update the FB's
+ * ColorDrawBuffer state to match the context.  This is handled in
+ * _mesa_update_framebuffer().
+ *
+ * See the GL_EXT_framebuffer_object spec for more info.
  */
 void GLAPIENTRY
 _mesa_DrawBuffer(GLenum buffer)
@@ -217,13 +242,14 @@ _mesa_DrawBuffer(GLenum buffer)
       destMask = draw_buffer_enum_to_bitmask(buffer);
       if (destMask == BAD_MASK) {
          /* totally bogus buffer */
-         _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer=0x%x)", buffer);
          return;
       }
       destMask &= supportedMask;
       if (destMask == 0x0) {
          /* none of the named color buffers exist! */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffer(buffer)");
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glDrawBuffer(buffer=0x%x)", buffer);
          return;
       }
    }
@@ -259,11 +285,10 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (!ctx->Extensions.ARB_draw_buffers) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffersARB");
-      return;
-   }
-   if (n < 1 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
+   /* Turns out n==0 is a valid input that should not produce an error.
+    * The remaining code below correctly handles the n==0 case.
+    */
+   if (n < 0 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
       return;
    }
@@ -305,69 +330,43 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
    _mesa_drawbuffers(ctx, n, buffers, destMask);
 
    /*
-    * Call device driver function.
+    * Call device driver function.  Note that n can be equal to 0,
+    * in which case we don't want to reference buffers[0], which
+    * may not be valid.
     */
    if (ctx->Driver.DrawBuffers)
       ctx->Driver.DrawBuffers(ctx, n, buffers);
    else if (ctx->Driver.DrawBuffer)
-      ctx->Driver.DrawBuffer(ctx, buffers[0]);
-}
-
-
-/**
- * Set color output state.  Traditionally, there was only one color
- * output, but fragment programs can now have several distinct color
- * outputs (see GL_ARB_draw_buffers).  This function sets the state
- * for one such color output.
- * \param ctx  current context
- * \param output  which fragment program output
- * \param buffer  buffer to write to (like GL_LEFT)
- * \param destMask  BUFFER_* bitmask
- *                  (like BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
- */
-static void
-set_color_output(GLcontext *ctx, GLuint output, GLenum buffer,
-                 GLbitfield destMask)
-{
-   struct gl_framebuffer *fb = ctx->DrawBuffer;
-
-   ASSERT(output < ctx->Const.MaxDrawBuffers);
-
-   /* Set per-FBO state */
-   fb->ColorDrawBuffer[output] = buffer;
-   fb->_ColorDrawBufferMask[output] = destMask;
-   /* not really needed, will be set later */
-   fb->_NumColorDrawBuffers[output] = 0;
-
-   if (fb->Name == 0)
-   /* Set traditional state var */
-      ctx->Color.DrawBuffer[output] = buffer;
+      ctx->Driver.DrawBuffer(ctx, n > 0 ? buffers[0] : GL_NONE);
 }
 
 
 /**
- * Helper routine used by _mesa_DrawBuffer, _mesa_DrawBuffersARB and
- * other places (window fbo fixup) to set fbo (and the old ctx) fields.
+ * Helper function to set the GL_DRAW_BUFFER state in the context and
+ * current FBO.  Called via glDrawBuffer(), glDrawBuffersARB()
+ *
  * All error checking will have been done prior to calling this function
  * so nothing should go wrong at this point.
+ *
  * \param ctx  current context
  * \param n    number of color outputs to set
  * \param buffers  array[n] of colorbuffer names, like GL_LEFT.
- * \param destMask  array[n] of BUFFER_* bitmasks which correspond to the
+ * \param destMask  array[n] of BUFFER_BIT_* bitmasks which correspond to the
  *                  colorbuffer names.  (i.e. GL_FRONT_AND_BACK =>
  *                  BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
  */
 void
-_mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
+_mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
                   const GLbitfield *destMask)
 {
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
    GLbitfield mask[MAX_DRAW_BUFFERS];
-   GLuint output;
+   GLboolean newState = GL_FALSE;
 
    if (!destMask) {
       /* compute destMask values now */
-      const GLbitfield supportedMask
-         = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
+      const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
+      GLuint output;
       for (output = 0; output < n; output++) {
          mask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
          ASSERT(mask[output] != BAD_MASK);
@@ -376,25 +375,118 @@ _mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
       destMask = mask;
    }
 
-   for (output = 0; output < n; output++) {
-      set_color_output(ctx, output, buffers[output], destMask[output]);
+   /*
+    * If n==1, destMask[0] may have up to four bits set.
+    * Otherwise, destMask[x] can only have one bit set.
+    */
+   if (n == 1) {
+      GLuint count = 0, destMask0 = destMask[0];
+      while (destMask0) {
+         GLint bufIndex = _mesa_ffs(destMask0) - 1;
+         if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
+            fb->_ColorDrawBufferIndexes[count] = bufIndex;
+            newState = GL_TRUE;
+         }
+         count++;
+         destMask0 &= ~(1 << bufIndex);
+      }
+      fb->ColorDrawBuffer[0] = buffers[0];
+      if (fb->_NumColorDrawBuffers != count) {
+         fb->_NumColorDrawBuffers = count;
+         newState = GL_TRUE;
+      }
+   }
+   else {
+      GLuint buf, count = 0;
+      for (buf = 0; buf < n; buf++ ) {
+         if (destMask[buf]) {
+            GLint bufIndex = _mesa_ffs(destMask[buf]) - 1;
+            /* only one bit should be set in the destMask[buf] field */
+            ASSERT(_mesa_bitcount(destMask[buf]) == 1);
+            if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
+               fb->_ColorDrawBufferIndexes[buf] = bufIndex;
+               newState = GL_TRUE;
+            }
+            fb->ColorDrawBuffer[buf] = buffers[buf];
+            count = buf + 1;
+         }
+         else {
+            if (fb->_ColorDrawBufferIndexes[buf] != -1) {
+               fb->_ColorDrawBufferIndexes[buf] = -1;
+               newState = GL_TRUE;
+            }
+         }
+      }
+      /* set remaining outputs to -1 (GL_NONE) */
+      while (buf < ctx->Const.MaxDrawBuffers) {
+         if (fb->_ColorDrawBufferIndexes[buf] != -1) {
+            fb->_ColorDrawBufferIndexes[buf] = -1;
+            newState = GL_TRUE;
+         }
+         fb->ColorDrawBuffer[buf] = GL_NONE;
+         buf++;
+      }
+      fb->_NumColorDrawBuffers = count;
    }
 
-   /* set remaining color outputs to NONE */
-   for (output = n; output < ctx->Const.MaxDrawBuffers; output++) {
-      set_color_output(ctx, output, GL_NONE, 0x0);
+   if (fb->Name == 0) {
+      /* also set context drawbuffer state */
+      GLuint buf;
+      for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
+         if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
+            ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
+            newState = GL_TRUE;
+         }
+      }
+   }
+
+   if (newState)
+      FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+}
+
+
+/**
+ * Like \sa _mesa_drawbuffers(), this is a helper function for setting
+ * GL_READ_BUFFER state in the context and current FBO.
+ * \param ctx  the rendering context
+ * \param buffer  GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
+ * \param bufferIndex  the numerical index corresponding to 'buffer'
+ */
+void
+_mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex)
+{
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+
+   if (fb->Name == 0) {
+      /* Only update the per-context READ_BUFFER state if we're bound to
+       * a window-system framebuffer.
+       */
+      ctx->Pixel.ReadBuffer = buffer;
    }
 
+   fb->ColorReadBuffer = buffer;
+   fb->_ColorReadBufferIndex = bufferIndex;
+
    ctx->NewState |= _NEW_BUFFERS;
 }
 
 
-GLboolean
-_mesa_readbuffer_update_fields(GLcontext *ctx, GLenum buffer)
+
+/**
+ * Called by glReadBuffer to set the source renderbuffer for reading pixels.
+ * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
+ */
+void GLAPIENTRY
+_mesa_ReadBuffer(GLenum buffer)
 {
    struct gl_framebuffer *fb;
    GLbitfield supportedMask;
    GLint srcBuffer;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
 
    fb = ctx->ReadBuffer;
 
@@ -409,43 +501,21 @@ _mesa_readbuffer_update_fields(GLcontext *ctx, GLenum buffer)
       /* general case / window-system framebuffer */
       srcBuffer = read_buffer_enum_to_index(buffer);
       if (srcBuffer == -1) {
-         _mesa_error(ctx, GL_INVALID_ENUM, "glReadBuffer(buffer=0x%x)", buffer);
-         return GL_FALSE;
+         _mesa_error(ctx, GL_INVALID_ENUM,
+                     "glReadBuffer(buffer=0x%x)", buffer);
+         return;
       }
       supportedMask = supported_buffer_bitmask(ctx, fb);
       if (((1 << srcBuffer) & supportedMask) == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glReadBuffer(buffer=0x%x)", buffer);
-         return GL_FALSE;
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glReadBuffer(buffer=0x%x)", buffer);
+         return;
       }
    }
 
-   if (fb->Name == 0) {
-      ctx->Pixel.ReadBuffer = buffer;
-   }
-   fb->ColorReadBuffer = buffer;
-   fb->_ColorReadBufferIndex = srcBuffer;
-
-   return GL_TRUE;
-}
-
-
-
-/**
- * Called by glReadBuffer to set the source renderbuffer for reading pixels.
- * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
- */
-void GLAPIENTRY
-_mesa_ReadBuffer(GLenum buffer)
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
-
-   if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
-
-   if (!_mesa_readbuffer_update_fields(ctx, buffer))
-      return;
+   /* OK, all error checking has been completed now */
 
+   _mesa_readbuffer(ctx, buffer, srcBuffer);
    ctx->NewState |= _NEW_BUFFERS;
 
    /*
@@ -454,41 +524,3 @@ _mesa_ReadBuffer(GLenum buffer)
    if (ctx->Driver.ReadBuffer)
       (*ctx->Driver.ReadBuffer)(ctx, buffer);
 }
-
-
-
-/*
- * XXX move somewhere else someday?
- */
-void GLAPIENTRY
-_mesa_SampleCoverageARB(GLclampf value, GLboolean invert)
-{
-   GET_CURRENT_CONTEXT(ctx);
-
-   if (!ctx->Extensions.ARB_multisample) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleCoverageARB");
-      return;
-   }
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
-   ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
-   ctx->Multisample.SampleCoverageInvert = invert;
-   ctx->NewState |= _NEW_MULTISAMPLE;
-}
-
-
-
-/**
- * Initialize the context's multisample state.
- * \param ctx  the GL context.
- */
-void
-_mesa_init_multisample(GLcontext *ctx)
-{
-   ctx->Multisample.Enabled = GL_FALSE;
-   ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
-   ctx->Multisample.SampleAlphaToOne = GL_FALSE;
-   ctx->Multisample.SampleCoverage = GL_FALSE;
-   ctx->Multisample.SampleCoverageValue = 1.0;
-   ctx->Multisample.SampleCoverageInvert = GL_FALSE;
-}