add a number of PBO validate/map/unmap functions
[mesa.git] / src / mesa / main / buffers.c
index e930531bf108670002c378ee155990690ce22e57..3cbd671baba794e9b9fe7c7814a3b82dadcf2edd 100644 (file)
@@ -1,13 +1,8 @@
-/**
- * \file buffers.c
- * Frame buffer management.
- */
-
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  7.1
  *
- * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 
 
+/**
+ * \file buffers.c
+ * General framebuffer-related functions, like glClear, glScissor, etc.
+ */
+
+
+
 #include "glheader.h"
 #include "buffers.h"
 #include "colormac.h"
@@ -116,6 +118,8 @@ _mesa_Clear( GLbitfield mask )
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
+   FLUSH_CURRENT(ctx, 0);
+
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glClear 0x%x\n", mask);
 
@@ -132,11 +136,16 @@ _mesa_Clear( GLbitfield mask )
       _mesa_update_state( ctx );       /* update _Xmin, etc */
    }
 
+   if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
+                  "glClear(incomplete framebuffer)");
+      return;
+   }
+
+   if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0)
+      return;
+
    if (ctx->RenderMode == GL_RENDER) {
-      const GLint x = ctx->DrawBuffer->_Xmin;
-      const GLint y = ctx->DrawBuffer->_Ymin;
-      const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
-      const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
       GLbitfield bufferMask;
 
       /* don't clear depth buffer if depth writing disabled */
@@ -169,8 +178,7 @@ _mesa_Clear( GLbitfield mask )
       }
 
       ASSERT(ctx->Driver.Clear);
-      ctx->Driver.Clear( ctx, bufferMask, (GLboolean) !ctx->Scissor.Enabled,
-                        x, y, width, height );
+      ctx->Driver.Clear(ctx, bufferMask);
    }
 }
 
@@ -178,38 +186,41 @@ _mesa_Clear( GLbitfield mask )
 
 /**
  * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
- * available to the rendering context.
- * This depends on the framebuffer we're writing to.  For window system
- * framebuffers we look at the framebuffer's visual.  But for user-
- * create framebuffers we look at the number of supported color attachments.
+ * available to the rendering context (for drawing or reading).
+ * This depends on the type of framebuffer.  For window system framebuffers
+ * we look at the framebuffer's visual.  But for user-create framebuffers we
+ * look at the number of supported color attachments.
+ * \param fb  the framebuffer to draw to, or read from
+ * \return  bitmask of BUFFER_BIT_* flags
  */
-static GLuint
-supported_buffer_bitmask(const GLcontext *ctx, GLuint framebufferID)
+static GLbitfield
+supported_buffer_bitmask(const GLcontext *ctx, const struct gl_framebuffer *fb)
 {
-   GLuint mask = 0x0;
-   GLint i;
+   GLbitfield mask = 0x0;
 
-   if (framebufferID > 0) {
+   if (fb->Name > 0) {
       /* 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);
       }
    }
    else {
-      /* A window system renderbuffer */
+      /* A window system framebuffer */
+      GLint i;
       mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
-      if (ctx->Visual.stereoMode) {
+      if (fb->Visual.stereoMode) {
          mask |= BUFFER_BIT_FRONT_RIGHT;
-         if (ctx->Visual.doubleBufferMode) {
+         if (fb->Visual.doubleBufferMode) {
             mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
          }
       }
-      else if (ctx->Visual.doubleBufferMode) {
+      else if (fb->Visual.doubleBufferMode) {
          mask |= BUFFER_BIT_BACK_LEFT;
       }
 
-      for (i = 0; i < ctx->Visual.numAuxBuffers; i++) {
+      for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
          mask |= (BUFFER_BIT_AUX0 << i);
       }
    }
@@ -223,7 +234,7 @@ supported_buffer_bitmask(const GLcontext *ctx, GLuint framebufferID)
  * Given a GLenum naming one or more color buffers (such as
  * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
  */
-static GLuint
+static GLbitfield
 draw_buffer_enum_to_bitmask(GLenum buffer)
 {
    switch (buffer) {
@@ -273,48 +284,49 @@ draw_buffer_enum_to_bitmask(GLenum buffer)
 
 /**
  * Helper routine used by glReadBuffer.
- * Given a GLenum naming (a) color buffer(s), return the corresponding
- * bitmask of DD_* flags.
+ * Given a GLenum naming a color buffer, return the index of the corresponding
+ * renderbuffer (a BUFFER_* value).
+ * return -1 for an invalid buffer.
  */
-static GLuint
-read_buffer_enum_to_bitmask(GLenum buffer)
+static GLint
+read_buffer_enum_to_index(GLenum buffer)
 {
    switch (buffer) {
       case GL_FRONT:
-         return BUFFER_BIT_FRONT_LEFT;
+         return BUFFER_FRONT_LEFT;
       case GL_BACK:
-         return BUFFER_BIT_BACK_LEFT;
+         return BUFFER_BACK_LEFT;
       case GL_RIGHT:
-         return BUFFER_BIT_FRONT_RIGHT;
+         return BUFFER_FRONT_RIGHT;
       case GL_FRONT_RIGHT:
-         return BUFFER_BIT_FRONT_RIGHT;
+         return BUFFER_FRONT_RIGHT;
       case GL_BACK_RIGHT:
-         return BUFFER_BIT_BACK_RIGHT;
+         return BUFFER_BACK_RIGHT;
       case GL_BACK_LEFT:
-         return BUFFER_BIT_BACK_LEFT;
+         return BUFFER_BACK_LEFT;
       case GL_LEFT:
-         return BUFFER_BIT_FRONT_LEFT;
+         return BUFFER_FRONT_LEFT;
       case GL_FRONT_LEFT:
-         return BUFFER_BIT_FRONT_LEFT;
+         return BUFFER_FRONT_LEFT;
       case GL_AUX0:
-         return BUFFER_BIT_AUX0;
+         return BUFFER_AUX0;
       case GL_AUX1:
-         return BUFFER_BIT_AUX1;
+         return BUFFER_AUX1;
       case GL_AUX2:
-         return BUFFER_BIT_AUX2;
+         return BUFFER_AUX2;
       case GL_AUX3:
-         return BUFFER_BIT_AUX3;
+         return BUFFER_AUX3;
       case GL_COLOR_ATTACHMENT0_EXT:
-         return BUFFER_BIT_COLOR0;
+         return BUFFER_COLOR0;
       case GL_COLOR_ATTACHMENT1_EXT:
-         return BUFFER_BIT_COLOR1;
+         return BUFFER_COLOR1;
       case GL_COLOR_ATTACHMENT2_EXT:
-         return BUFFER_BIT_COLOR2;
+         return BUFFER_COLOR2;
       case GL_COLOR_ATTACHMENT3_EXT:
-         return BUFFER_BIT_COLOR3;
+         return BUFFER_COLOR3;
       default:
          /* error */
-         return BAD_MASK;
+         return -1;
    }
 }
 
@@ -330,8 +342,7 @@ read_buffer_enum_to_bitmask(GLenum buffer)
 void GLAPIENTRY
 _mesa_DrawBuffer(GLenum buffer)
 {
-   GLuint bufferID;
-   GLuint destMask;
+   GLbitfield destMask;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
 
@@ -339,13 +350,12 @@ _mesa_DrawBuffer(GLenum buffer)
       _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
    }
 
-   bufferID = ctx->DrawBuffer->Name;
-
    if (buffer == GL_NONE) {
       destMask = 0x0;
    }
    else {
-      const GLuint supportedMask = supported_buffer_bitmask(ctx, bufferID);
+      const GLbitfield supportedMask
+         = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
       destMask = draw_buffer_enum_to_bitmask(buffer);
       if (destMask == BAD_MASK) {
          /* totally bogus buffer */
@@ -362,6 +372,14 @@ _mesa_DrawBuffer(GLenum buffer)
 
    /* if we get here, there's no error so set new state */
    _mesa_drawbuffers(ctx, 1, &buffer, &destMask);
+
+   /*
+    * Call device driver function.
+    */
+   if (ctx->Driver.DrawBuffers)
+      ctx->Driver.DrawBuffers(ctx, 1, &buffer);
+   else if (ctx->Driver.DrawBuffer)
+      ctx->Driver.DrawBuffer(ctx, buffer);
 }
 
 
@@ -378,9 +396,8 @@ void GLAPIENTRY
 _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
 {
    GLint output;
-   GLuint usedBufferMask, supportedMask;
-   GLuint bufferID;
-   GLuint destMask[MAX_DRAW_BUFFERS];
+   GLbitfield usedBufferMask, supportedMask;
+   GLbitfield destMask[MAX_DRAW_BUFFERS];
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
@@ -393,9 +410,7 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
       return;
    }
 
-   bufferID = ctx->DrawBuffer->Name;
-
-   supportedMask = supported_buffer_bitmask(ctx, bufferID);
+   supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
    usedBufferMask = 0x0;
 
    /* complicated error checking... */
@@ -430,6 +445,14 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
 
    /* OK, if we get here, there were no errors so set the new state */
    _mesa_drawbuffers(ctx, n, buffers, destMask);
+
+   /*
+    * Call device driver function.
+    */
+   if (ctx->Driver.DrawBuffers)
+      ctx->Driver.DrawBuffers(ctx, n, buffers);
+   else if (ctx->Driver.DrawBuffer)
+      ctx->Driver.DrawBuffer(ctx, buffers[0]);
 }
 
 
@@ -445,28 +468,28 @@ _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
  *                  (like BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
  */
 static void
-set_color_output(GLcontext *ctx, GLuint output, GLenum buffer, GLuint destMask)
+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;
-
-   if (fb->Name == 0) {
-      /* Set traditional state var */
-      ctx->Color.DrawBuffer[output] = buffer;
-   }
-
    /* not really needed, will be set later */
    fb->_NumColorDrawBuffers[output] = 0;
+
+   if (fb->Name == 0)
+   /* Set traditional state var */
+      ctx->Color.DrawBuffer[output] = buffer;
 }
 
 
 /**
  * Helper routine used by _mesa_DrawBuffer, _mesa_DrawBuffersARB and
- * _mesa_PopAttrib to set drawbuffer state.
+ * other places (window fbo fixup) to set fbo (and the old ctx) fields.
  * All error checking will have been done prior to calling this function
  * so nothing should go wrong at this point.
  * \param ctx  current context
@@ -477,16 +500,16 @@ set_color_output(GLcontext *ctx, GLuint output, GLenum buffer, GLuint destMask)
  *                  BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
  */
 void
-_mesa_drawbuffers(GLcontext *ctx, GLsizei n, const GLenum *buffers,
-                  const GLuint *destMask)
+_mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
+                  const GLbitfield *destMask)
 {
-   GLuint mask[MAX_DRAW_BUFFERS];
-   GLint output;
+   GLbitfield mask[MAX_DRAW_BUFFERS];
+   GLuint output;
 
    if (!destMask) {
       /* compute destMask values now */
-      const GLuint bufferID = ctx->DrawBuffer->Name;
-      const GLuint supportedMask = supported_buffer_bitmask(ctx, bufferID);
+      const GLbitfield supportedMask
+         = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
       for (output = 0; output < n; output++) {
          mask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
          ASSERT(mask[output] != BAD_MASK);
@@ -504,63 +527,68 @@ _mesa_drawbuffers(GLcontext *ctx, GLsizei n, const GLenum *buffers,
       set_color_output(ctx, output, GL_NONE, 0x0);
    }
 
-   ctx->NewState |= _NEW_COLOR;
-
-   /*
-    * Call device driver function.
-    */
-   if (ctx->Driver.DrawBuffers)
-      ctx->Driver.DrawBuffers(ctx, n, buffers);
-   else if (ctx->Driver.DrawBuffer)
-      ctx->Driver.DrawBuffer(ctx, buffers[0]);
+   ctx->NewState |= _NEW_BUFFERS;
 }
 
 
-
-/**
- * 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)
+GLboolean
+_mesa_readbuffer_update_fields(GLcontext *ctx, GLenum buffer)
 {
    struct gl_framebuffer *fb;
-   GLuint srcMask, supportedMask;
-   GLuint bufferID;
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   GLbitfield supportedMask;
+   GLint srcBuffer;
 
    fb = ctx->ReadBuffer;
-   bufferID = fb->Name;
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
 
-   if (bufferID > 0 && buffer == GL_NONE) {
-      /* legal! */
-      srcMask = 0x0;
+   if (fb->Name > 0 && buffer == GL_NONE) {
+      /* This is legal for user-created framebuffer objects */
+      srcBuffer = -1;
    }
    else {
-      /* general case */
-      srcMask = read_buffer_enum_to_bitmask(buffer);
-      if (srcMask == BAD_MASK) {
-         _mesa_error(ctx, GL_INVALID_ENUM, "glReadBuffer(buffer)");
-         return;
+      /* 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;
       }
-      supportedMask = supported_buffer_bitmask(ctx, bufferID);
-      if ((srcMask & supportedMask) == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glReadBuffer(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;
       }
    }
 
-   if (bufferID == 0) {
+   if (fb->Name == 0) {
       ctx->Pixel.ReadBuffer = buffer;
    }
    fb->ColorReadBuffer = buffer;
-   fb->_ColorReadBufferMask = srcMask;
+   fb->_ColorReadBufferIndex = srcBuffer;
+
+   return GL_TRUE;
+}
 
-   ctx->NewState |= _NEW_PIXEL;
+
+
+/**
+ * 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;
+
+   ctx->NewState |= _NEW_BUFFERS;
 
    /*
     * Call device driver function.
@@ -573,6 +601,9 @@ _mesa_ReadBuffer(GLenum buffer)
 #if _HAVE_FULL_GL
 
 /**
+ * XXX THIS IS OBSOLETE - drivers should take care of detecting window
+ * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
+ *
  * GL_MESA_resize_buffers extension.
  *
  * When this function is called, we'll ask the window system how large
@@ -580,22 +611,26 @@ _mesa_ReadBuffer(GLenum buffer)
  * ResizeBuffers function.  The driver will then resize its color buffers
  * as needed, and maybe call the swrast's routine for reallocating
  * swrast-managed depth/stencil/accum/etc buffers.
- * \note This function may be called from within Mesa or called by the
- * user directly (see the GL_MESA_resize_buffers extension).
+ * \note This function should only be called through the GL API, not
+ * from device drivers (as was done in the past).
  */
-void GLAPIENTRY
-_mesa_ResizeBuffersMESA( void )
-{
-   GET_CURRENT_CONTEXT(ctx);
 
+void _mesa_resizebuffers( GLcontext *ctx )
+{
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glResizeBuffersMESA\n");
 
-   if (ctx->DrawBuffer && ctx->DrawBuffer->Name == 0) {
+   if (!ctx->Driver.GetBufferSize) {
+      return;
+   }
+
+   if (ctx->WinSysDrawBuffer) {
       GLuint newWidth, newHeight;
-      GLframebuffer *buffer = ctx->DrawBuffer;
+      GLframebuffer *buffer = ctx->WinSysDrawBuffer;
+
+      assert(buffer->Name == 0);
 
       /* ask device driver for size of output buffer */
       ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
@@ -607,10 +642,12 @@ _mesa_ResizeBuffersMESA( void )
       }
    }
 
-   if (ctx->ReadBuffer && ctx->ReadBuffer != ctx->DrawBuffer
-       && ctx->ReadBuffer->Name == 0) {
+   if (ctx->WinSysReadBuffer
+       && ctx->WinSysReadBuffer != ctx->WinSysDrawBuffer) {
       GLuint newWidth, newHeight;
-      GLframebuffer *buffer = ctx->ReadBuffer;
+      GLframebuffer *buffer = ctx->WinSysReadBuffer;
+
+      assert(buffer->Name == 0);
 
       /* ask device driver for size of read buffer */
       ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
@@ -626,6 +663,19 @@ _mesa_ResizeBuffersMESA( void )
 }
 
 
+/*
+ * XXX THIS IS OBSOLETE
+ */
+void GLAPIENTRY
+_mesa_ResizeBuffersMESA( void )
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   if (ctx->Extensions.MESA_resize_buffers)
+      _mesa_resizebuffers( ctx );
+}
+
+
 /*
  * XXX move somewhere else someday?
  */
@@ -662,20 +712,10 @@ _mesa_SampleCoverageARB(GLclampf value, GLboolean invert)
  * change flushes the vertices and notifies the driver via
  * the dd_function_table::Scissor callback.
  */
-void GLAPIENTRY
-_mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
+void
+_mesa_set_scissor(GLcontext *ctx, 
+                  GLint x, GLint y, GLsizei width, GLsizei height)
 {
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (width < 0 || height < 0) {
-      _mesa_error( ctx, GL_INVALID_VALUE, "glScissor" );
-      return;
-   }
-
-   if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glScissor %d %d %d %d\n", x, y, width, height);
-
    if (x == ctx->Scissor.X &&
        y == ctx->Scissor.Y &&
        width == ctx->Scissor.Width &&
@@ -693,6 +733,24 @@ _mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
 }
 
 
+void GLAPIENTRY
+_mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (width < 0 || height < 0) {
+      _mesa_error( ctx, GL_INVALID_VALUE, "glScissor" );
+      return;
+   }
+
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glScissor %d %d %d %d\n", x, y, width, height);
+
+   _mesa_set_scissor(ctx, x, y, width, height);
+}
+
+
 
 /**********************************************************************/
 /** \name Initialization */