mesa: remove unneeded #include of colormac.h
[mesa.git] / src / mesa / main / buffers.c
index cc268b67cbca57bca3f48443a6eb96df4fe18ba2..37a9790923b1f6f169ac22e21795eac595713692 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
@@ -32,7 +32,6 @@
 
 #include "glheader.h"
 #include "buffers.h"
-#include "colormac.h"
 #include "context.h"
 #include "enums.h"
 #include "fbobject.h"
@@ -60,7 +59,6 @@ supported_buffer_bitmask(const struct gl_context *ctx,
    if (_mesa_is_user_fbo(fb)) {
       /* A user-created renderbuffer */
       GLuint i;
-      ASSERT(ctx->Extensions.EXT_framebuffer_object);
       for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
          mask |= (BUFFER_BIT_COLOR0 << i);
       }
@@ -94,7 +92,7 @@ supported_buffer_bitmask(const struct gl_context *ctx,
  * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
  */
 static GLbitfield
-draw_buffer_enum_to_bitmask(GLenum buffer)
+draw_buffer_enum_to_bitmask(const struct gl_context *ctx, GLenum buffer)
 {
    switch (buffer) {
       case GL_NONE:
@@ -102,6 +100,26 @@ draw_buffer_enum_to_bitmask(GLenum buffer)
       case GL_FRONT:
          return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
       case GL_BACK:
+         if (_mesa_is_gles(ctx)) {
+            /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
+             * ES 3.0.1 specification says:
+             *
+             *     "When draw buffer zero is BACK, color values are written
+             *     into the sole buffer for single-buffered contexts, or into
+             *     the back buffer for double-buffered contexts."
+             *
+             * Since there is no stereo rendering in ES 3.0, only return the
+             * LEFT bits.  This also satisfies the "n must be 1" requirement.
+             *
+             * We also do this for GLES 1 and 2 because those APIs have no
+             * concept of selecting the front and back buffer anyway and it's
+             * convenient to be able to maintain the magic behaviour of
+             * GL_BACK in that case.
+             */
+            if (ctx->DrawBuffer->Visual.doubleBufferMode)
+               return BUFFER_BIT_BACK_LEFT;
+            return BUFFER_BIT_FRONT_LEFT;
+         }
          return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
       case GL_RIGHT:
          return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
@@ -206,13 +224,13 @@ read_buffer_enum_to_index(GLenum buffer)
  * Called by glDrawBuffer().
  * Specify which renderbuffer(s) to draw into for the first color output.
  * <buffer> can name zero, one, two or four renderbuffers!
- * \sa _mesa_DrawBuffersARB
+ * \sa _mesa_DrawBuffers
  *
  * \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).
+ * current ctx->DrawBuffer is a window-system framebuffer or a user-created
+ * framebuffer object.
  *   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.
@@ -229,7 +247,8 @@ _mesa_DrawBuffer(GLenum buffer)
 {
    GLbitfield destMask;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
+
+   FLUSH_VERTICES(ctx, 0);
 
    if (MESA_VERBOSE & VERBOSE_API) {
       _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
@@ -241,7 +260,7 @@ _mesa_DrawBuffer(GLenum buffer)
    else {
       const GLbitfield supportedMask
          = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
-      destMask = draw_buffer_enum_to_bitmask(buffer);
+      destMask = draw_buffer_enum_to_bitmask(ctx, buffer);
       if (destMask == BAD_MASK) {
          /* totally bogus buffer */
          _mesa_error(ctx, GL_INVALID_ENUM,
@@ -280,16 +299,21 @@ _mesa_DrawBuffer(GLenum buffer)
  *                 GL_FRONT_AND_BACK is illegal.
  */
 void GLAPIENTRY
-_mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
+_mesa_DrawBuffers(GLsizei n, const GLenum *buffers)
 {
-   GLint output;
+   GLuint output;
    GLbitfield usedBufferMask, supportedMask;
    GLbitfield destMask[MAX_DRAW_BUFFERS];
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+
+   FLUSH_VERTICES(ctx, 0);
 
    /* Turns out n==0 is a valid input that should not produce an error.
     * The remaining code below correctly handles the n==0 case.
+    *
+    * From the OpenGL 3.0 specification, page 258:
+    * "An INVALID_VALUE error is generated if n is greater than
+    *  MAX_DRAW_BUFFERS."
     */
    if (n < 0 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
@@ -299,26 +323,112 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
    supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
    usedBufferMask = 0x0;
 
+   /* From the ES 3.0 specification, page 180:
+    * "If the GL is bound to the default framebuffer, then n must be 1
+    *  and the constant must be BACK or NONE."
+    * (same restriction applies with GL_EXT_draw_buffers specification)
+    */
+   if (ctx->API == API_OPENGLES2 && _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
+       (n != 1 || (buffers[0] != GL_NONE && buffers[0] != GL_BACK))) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffers(buffer)");
+      return;
+   }
+
    /* complicated error checking... */
    for (output = 0; output < n; output++) {
+      /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL 3.0
+       * specification says:
+       *
+       *     "Each buffer listed in bufs must be BACK, NONE, or one of the values
+       *      from table 4.3 (NONE, COLOR_ATTACHMENTi)"
+       */
+      if (_mesa_is_gles3(ctx) && buffers[output] != GL_NONE &&
+          buffers[output] != GL_BACK &&
+          (buffers[output] < GL_COLOR_ATTACHMENT0 ||
+           buffers[output] >= GL_COLOR_ATTACHMENT0 + ctx->Const.MaxColorAttachments)) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffers(buffer)");
+         return;
+      }
+
       if (buffers[output] == GL_NONE) {
          destMask[output] = 0x0;
       }
       else {
-         destMask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
-         if (destMask[output] == BAD_MASK
-             || _mesa_bitcount(destMask[output]) > 1) {
+         /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
+          * spec (20080923) says:
+          *
+          *     "If the GL is bound to a framebuffer object and DrawBuffers is
+          *     supplied with [...] COLOR_ATTACHMENTm where m is greater than
+          *     or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
+          *     INVALID_OPERATION results."
+          */
+         if (_mesa_is_user_fbo(ctx->DrawBuffer) && buffers[output] >=
+             GL_COLOR_ATTACHMENT0 + ctx->Const.MaxDrawBuffers) {
+            _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffersARB(buffer)");
+            return;
+         }
+
+         destMask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
+
+         /* From the OpenGL 3.0 specification, page 258:
+          * "Each buffer listed in bufs must be one of the values from tables
+          *  4.5 or 4.6.  Otherwise, an INVALID_ENUM error is generated.
+          */
+         if (destMask[output] == BAD_MASK) {
             _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
             return;
          }         
+
+         /* From the OpenGL 4.0 specification, page 256:
+          * "For both the default framebuffer and framebuffer objects, the
+          *  constants FRONT, BACK, LEFT, RIGHT, and FRONT_AND_BACK are not
+          *  valid in the bufs array passed to DrawBuffers, and will result in
+          *  the error INVALID_ENUM. This restriction is because these
+          *  constants may themselves refer to multiple buffers, as shown in
+          *  table 4.4."
+          *  Previous versions of the OpenGL specification say INVALID_OPERATION,
+          *  but the Khronos conformance tests expect INVALID_ENUM.
+          */
+         if (_mesa_bitcount(destMask[output]) > 1) {
+            _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
+            return;
+         }
+
+         /* From the OpenGL 3.0 specification, page 259:
+          * "If the GL is bound to the default framebuffer and DrawBuffers is
+          *  supplied with a constant (other than NONE) that does not indicate
+          *  any of the color buffers allocated to the GL context by the window
+          *  system, the error INVALID_OPERATION will be generated.
+          *
+          *  If the GL is bound to a framebuffer object and DrawBuffers is
+          *  supplied with a constant from table 4.6 [...] then the error
+          *  INVALID_OPERATION results."
+          */
          destMask[output] &= supportedMask;
          if (destMask[output] == 0) {
             _mesa_error(ctx, GL_INVALID_OPERATION,
                         "glDrawBuffersARB(unsupported buffer)");
             return;
          }
+
+         /* ES 3.0 is even more restrictive.  From the ES 3.0 spec, page 180:
+          * "If the GL is bound to a framebuffer object, the ith buffer listed
+          *  in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
+          * (same restriction applies with GL_EXT_draw_buffers specification)
+          */
+         if (ctx->API == API_OPENGLES2 && _mesa_is_user_fbo(ctx->DrawBuffer) &&
+             buffers[output] != GL_NONE &&
+             buffers[output] != GL_COLOR_ATTACHMENT0 + output) {
+            _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffers(buffer)");
+            return;
+         }
+
+         /* From the OpenGL 3.0 specification, page 258:
+          * "Except for NONE, a buffer may not appear more than once in the
+          *  array pointed to by bufs.  Specifying a buffer more then once will
+          *  result in the error INVALID_OPERATION."
+          */
          if (destMask[output] & usedBufferMask) {
-            /* can't specify a dest buffer more than once! */
             _mesa_error(ctx, GL_INVALID_OPERATION,
                         "glDrawBuffersARB(duplicated buffer)");
             return;
@@ -353,7 +463,7 @@ updated_drawbuffers(struct gl_context *ctx)
 {
    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
 
-   if (ctx->API == API_OPENGL && !ctx->Extensions.ARB_ES2_compatibility) {
+   if (ctx->API == API_OPENGL_COMPAT && !ctx->Extensions.ARB_ES2_compatibility) {
       struct gl_framebuffer *fb = ctx->DrawBuffer;
 
       /* Flag the FBO as requiring validation. */
@@ -391,18 +501,19 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
       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);
+         mask[output] = draw_buffer_enum_to_bitmask(ctx, buffers[output]);
+         assert(mask[output] != BAD_MASK);
          mask[output] &= supportedMask;
       }
       destMask = mask;
    }
 
    /*
-    * If n==1, destMask[0] may have up to four bits set.
+    * destMask[0] may have up to four bits set
+    * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
     * Otherwise, destMask[x] can only have one bit set.
     */
-   if (n == 1) {
+   if (n > 0 && _mesa_bitcount(destMask[0]) > 1) {
       GLuint count = 0, destMask0 = destMask[0];
       while (destMask0) {
          GLint bufIndex = ffs(destMask0) - 1;
@@ -422,7 +533,7 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
          if (destMask[buf]) {
             GLint bufIndex = ffs(destMask[buf]) - 1;
             /* only one bit should be set in the destMask[buf] field */
-            ASSERT(_mesa_bitcount(destMask[buf]) == 1);
+            assert(_mesa_bitcount(destMask[buf]) == 1);
             if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
               updated_drawbuffers(ctx);
                fb->_ColorDrawBufferIndexes[buf] = bufIndex;
@@ -471,16 +582,11 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
 void
 _mesa_update_draw_buffers(struct gl_context *ctx)
 {
-   GLenum buffers[MAX_DRAW_BUFFERS];
-   GLuint i;
-
    /* should be a window system FBO */
    assert(_mesa_is_winsys_fbo(ctx->DrawBuffer));
 
-   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++)
-      buffers[i] = ctx->Color.DrawBuffer[i];
-
-   _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
+   _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers,
+                     ctx->Color.DrawBuffer, NULL);
 }
 
 
@@ -522,7 +628,8 @@ _mesa_ReadBuffer(GLenum buffer)
    GLbitfield supportedMask;
    GLint srcBuffer;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+
+   FLUSH_VERTICES(ctx, 0);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
@@ -532,8 +639,8 @@ _mesa_ReadBuffer(GLenum buffer)
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
 
-   if (_mesa_is_user_fbo(fb) && buffer == GL_NONE) {
-      /* This is legal for user-created framebuffer objects */
+   if (buffer == GL_NONE) {
+      /* This is legal--it means that no buffer should be bound for reading. */
       srcBuffer = -1;
    }
    else {
@@ -555,7 +662,6 @@ _mesa_ReadBuffer(GLenum buffer)
    /* OK, all error checking has been completed now */
 
    _mesa_readbuffer(ctx, buffer, srcBuffer);
-   ctx->NewState |= _NEW_BUFFERS;
 
    /*
     * Call device driver function.