mesa: new BYTE/SHORT_TO_FLOATZ() macros
[mesa.git] / src / mesa / main / buffers.c
index 86446311fe3fa0a66bfd64931558e4821e0e9db7..adea0f5f78ee83f9f9a53cae6ce6a806d621ce34 100644 (file)
@@ -35,6 +35,7 @@
 #include "colormac.h"
 #include "context.h"
 #include "enums.h"
+#include "mtypes.h"
 
 
 #define BAD_MASK ~0u
@@ -339,6 +340,26 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
       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
@@ -360,7 +381,7 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    GLbitfield mask[MAX_DRAW_BUFFERS];
-   GLboolean newState = GL_FALSE;
+   GLuint buf;
 
    if (!destMask) {
       /* compute destMask values now */
@@ -383,64 +404,80 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
       while (destMask0) {
          GLint bufIndex = _mesa_ffs(destMask0) - 1;
          if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
+            updated_drawbuffers(ctx);
             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;
-      }
+      fb->_NumColorDrawBuffers = count;
    }
    else {
-      GLuint buf, count = 0;
+      GLuint 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) {
+              updated_drawbuffers(ctx);
                fb->_ColorDrawBufferIndexes[buf] = bufIndex;
-               newState = GL_TRUE;
             }
-            fb->ColorDrawBuffer[buf] = buffers[buf];
             count = buf + 1;
          }
          else {
             if (fb->_ColorDrawBufferIndexes[buf] != -1) {
+              updated_drawbuffers(ctx);
                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->ColorDrawBuffer[buf] = buffers[buf];
       }
       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;
+      }
+   }
+   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++) {
          if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) {
+           updated_drawbuffers(ctx);
             ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
-            newState = GL_TRUE;
          }
       }
    }
+}
+
+
+/**
+ * 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];
 
-   if (newState)
-      FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+   _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
 }