mesa: remove _mesa_ffs(), implement ffs() for non-GNU platforms
[mesa.git] / src / mesa / main / buffers.c
index d8b5f3b1f4aafe02bbd5f53e27b8644210901682..46c785fc4dc28f076a90fc0efb80f8f37cd2d6c5 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;
 
@@ -338,13 +337,33 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
    if (ctx->Driver.DrawBuffers)
       ctx->Driver.DrawBuffers(ctx, n, buffers);
    else if (ctx->Driver.DrawBuffer)
-      ctx->Driver.DrawBuffer(ctx, n>0? buffers[0]:GL_NONE);
+      ctx->Driver.DrawBuffer(ctx, n > 0 ? buffers[0] : GL_NONE);
 }
 
+/**
+ * Performs necessary state updates when _mesa_drawbuffers makes an
+ * actual change.
+ */
+static void
+updated_drawbuffers(struct gl_context *ctx)
+{
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
+#if FEATURE_GL
+   if (ctx->API == API_OPENGL && !ctx->Extensions.ARB_ES2_compatibility) {
+      struct gl_framebuffer *fb = ctx->DrawBuffer;
+
+      /* Flag the FBO as requiring validation. */
+      if (fb->Name != 0) {
+        fb->_Status = 0;
+      }
+   }
+#endif
+}
 
 /**
  * Helper function to set the GL_DRAW_BUFFER state in the context and
- * current FBO.
+ * 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.
@@ -357,11 +376,12 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
  *                  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 buf;
 
    if (!destMask) {
       /* compute destMask values now */
@@ -375,49 +395,89 @@ _mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
       destMask = mask;
    }
 
+   /*
+    * 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 buf, count = 0;
-      /* init to -1 to help catch errors */
-      fb->_ColorDrawBufferIndexes[0] = -1;
-      for (buf = 0; buf < BUFFER_COUNT; buf++) {
-         if (destMask[0] & (1 << buf)) {
-            fb->_ColorDrawBufferIndexes[count] = buf;
-            count++;
+      GLuint count = 0, destMask0 = destMask[0];
+      while (destMask0) {
+         GLint bufIndex = ffs(destMask0) - 1;
+         if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
+            updated_drawbuffers(ctx);
+            fb->_ColorDrawBufferIndexes[count] = bufIndex;
          }
+         count++;
+         destMask0 &= ~(1 << bufIndex);
       }
       fb->ColorDrawBuffer[0] = buffers[0];
       fb->_NumColorDrawBuffers = count;
    }
    else {
-      GLuint buf, count = 0;
+      GLuint count = 0;
       for (buf = 0; buf < n; buf++ ) {
          if (destMask[buf]) {
-            fb->_ColorDrawBufferIndexes[buf] = _mesa_ffs(destMask[buf]) - 1;
-            fb->ColorDrawBuffer[buf] = buffers[buf];
+            GLint bufIndex = 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) {
+              updated_drawbuffers(ctx);
+               fb->_ColorDrawBufferIndexes[buf] = bufIndex;
+            }
             count = buf + 1;
          }
          else {
-            fb->_ColorDrawBufferIndexes[buf] = -1;
+            if (fb->_ColorDrawBufferIndexes[buf] != -1) {
+              updated_drawbuffers(ctx);
+               fb->_ColorDrawBufferIndexes[buf] = -1;
+            }
          }
+         fb->ColorDrawBuffer[buf] = buffers[buf];
       }
-      /* set remaining outputs to -1 (GL_NONE) */
-      while (buf < ctx->Const.MaxDrawBuffers) {
+      fb->_NumColorDrawBuffers = count;
+   }
+
+   /* set remaining outputs to -1 (GL_NONE) */
+   for (buf = fb->_NumColorDrawBuffers; buf < ctx->Const.MaxDrawBuffers; buf++) {
+      if (fb->_ColorDrawBufferIndexes[buf] != -1) {
+         updated_drawbuffers(ctx);
          fb->_ColorDrawBufferIndexes[buf] = -1;
-         fb->ColorDrawBuffer[buf] = GL_NONE;
-         buf++;
       }
-      fb->_NumColorDrawBuffers = count;
+   }
+   for (buf = n; buf < ctx->Const.MaxDrawBuffers; buf++) {
+      fb->ColorDrawBuffer[buf] = GL_NONE;
    }
 
    if (fb->Name == 0) {
       /* also set context drawbuffer state */
-      GLuint buf;
       for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
-         ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
+         if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
+           updated_drawbuffers(ctx);
+            ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
+         }
       }
    }
+}
 
-   ctx->NewState |= _NEW_BUFFERS;
+
+/**
+ * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
+ * from the context's Color.DrawBuffer[] state.
+ * Use when changing contexts.
+ */
+void
+_mesa_update_draw_buffers(struct gl_context *ctx)
+{
+   GLenum buffers[MAX_DRAW_BUFFERS];
+   GLuint i;
+
+   /* should be a window system FBO */
+   assert(ctx->DrawBuffer->Name == 0);
+
+   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++)
+      buffers[i] = ctx->Color.DrawBuffer[i];
+
+   _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
 }
 
 
@@ -429,7 +489,7 @@ _mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
  * \param bufferIndex  the numerical index corresponding to 'buffer'
  */
 void
-_mesa_readbuffer(GLcontext *ctx, GLenum buffer, GLint bufferIndex)
+_mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex)
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;