Mesa (master): mesa: use new _mesa_map_pbo_source/dest() functions in more places

Brian Paul brianp at kemper.freedesktop.org
Thu Sep 3 17:48:36 UTC 2009


Module: Mesa
Branch: master
Commit: 203f395aaf717a6faf21a76979cc24d544ae148b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=203f395aaf717a6faf21a76979cc24d544ae148b

Author: Brian Paul <brianp at vmware.com>
Date:   Thu Sep  3 10:41:14 2009 -0600

mesa: use new _mesa_map_pbo_source/dest() functions in more places

This trims down the code a bit.  The next step would be to combine
the validate and map operations into one helper...

---

 src/mesa/main/colortab.c  |   63 +++++--------
 src/mesa/main/convolve.c  |  100 +++++++------------
 src/mesa/main/histogram.c |   67 ++++---------
 src/mesa/main/pixel.c     |  239 +++++++++++++++------------------------------
 src/mesa/main/polygon.c   |   77 ++++++---------
 5 files changed, 192 insertions(+), 354 deletions(-)

diff --git a/src/mesa/main/colortab.c b/src/mesa/main/colortab.c
index 3630406..91c2988 100644
--- a/src/mesa/main/colortab.c
+++ b/src/mesa/main/colortab.c
@@ -179,27 +179,22 @@ store_colortable_entries(GLcontext *ctx, struct gl_color_table *table,
 			 GLfloat bScale, GLfloat bBias,
 			 GLfloat aScale, GLfloat aBias)
 {
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* Get/unpack the color table data from a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(1, &ctx->Unpack, count, 1, 1,
-                                     format, type, data)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glColor[Sub]Table(bad PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
+   if (!_mesa_validate_pbo_access(1, &ctx->Unpack, count, 1, 1,
+                                  format, type, data)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glColor[Sub]Table(bad PBO access)");
+      return;
+   }
+
+   data = _mesa_map_pbo_source(ctx, &ctx->Unpack, data);
+   if (!data) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glColor[Sub]Table(PBO mapped)");
-         return;
       }
-      data = ADD_POINTERS(buf, data);
+      return;
    }
 
-
    {
       /* convert user-provided data to GLfloat values */
       GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
@@ -279,10 +274,7 @@ store_colortable_entries(GLcontext *ctx, struct gl_color_table *table,
       }
    }
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 }
 
 
@@ -696,34 +688,27 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       return;
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack color table into PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(1, &ctx->Pack, table->Size, 1, 1,
-                                     format, type, data)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetColorTable(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
+   if (!_mesa_validate_pbo_access(1, &ctx->Pack, table->Size, 1, 1,
+                                  format, type, data)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetColorTable(invalid PBO access)");
+      return;
+   }
+
+   data = _mesa_map_pbo_dest(ctx, &ctx->Pack, data);
+   if (!data) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          /* buffer is already mapped - that's an error */
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetColorTable(PBO is mapped)");
-         return;
       }
-      data = ADD_POINTERS(buf, data);
+      return;
    }
 
    _mesa_pack_rgba_span_float(ctx, table->Size, rgba,
                               format, type, data, &ctx->Pack, 0x0);
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 
diff --git a/src/mesa/main/convolve.c b/src/mesa/main/convolve.c
index 69dba72..7e0761e 100644
--- a/src/mesa/main/convolve.c
+++ b/src/mesa/main/convolve.c
@@ -144,27 +144,19 @@ _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, G
    ctx->Convolution1D.Width = width;
    ctx->Convolution1D.Height = 1;
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* unpack filter from PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(1, &ctx->Unpack, width, 1, 1,
-                                     format, type, image)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glConvolutionFilter1D(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!_mesa_validate_pbo_access(1, &ctx->Unpack, width, 1, 1,
+                                  format, type, image)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glConvolutionFilter1D(invalid PBO access)");
+      return;
+   }
+
+   image = _mesa_map_pbo_source(ctx, &ctx->Unpack, image);
+   if (!image) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glConvolutionFilter1D(PBO is mapped)");
-         return;
       }
-      image = ADD_POINTERS(buf, image);
-   }
-   else if (!image) {
       return;
    }
 
@@ -173,10 +165,7 @@ _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, G
                                  format, type, image, &ctx->Unpack,
                                  0); /* transferOps */
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 
    _mesa_scale_and_bias_rgba(width,
                              (GLfloat (*)[4]) ctx->Convolution1D.Filter,
@@ -242,27 +231,19 @@ _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, G
    ctx->Convolution2D.Width = width;
    ctx->Convolution2D.Height = height;
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* unpack filter from PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
-                                     format, type, image)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glConvolutionFilter2D(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
+                                  format, type, image)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glConvolutionFilter2D(invalid PBO access)");
+      return;
+   }
+
+   image = _mesa_map_pbo_source(ctx, &ctx->Unpack, image);
+   if (!image) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glConvolutionFilter2D(PBO is mapped)");
-         return;
       }
-      image = ADD_POINTERS(buf, image);
-   }
-   else if (!image) {
       return;
    }
 
@@ -276,10 +257,7 @@ _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, G
                                     0); /* transferOps */
    }
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 
    _mesa_scale_and_bias_rgba(width * height,
                              (GLfloat (*)[4]) ctx->Convolution2D.Filter,
@@ -598,26 +576,21 @@ _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type,
          return;
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* Pack the filter into a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(2, &ctx->Pack,
-                                     filter->Width, filter->Height,
-                                     1, format, type, image)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetConvolutionFilter(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!_mesa_validate_pbo_access(2, &ctx->Pack,
+                                  filter->Width, filter->Height,
+                                  1, format, type, image)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetConvolutionFilter(invalid PBO access)");
+      return;
+   }
+
+   image = _mesa_map_pbo_dest(ctx, &ctx->Pack, image);
+   if (!image) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetConvolutionFilter(PBO is mapped)");
-         return;
       }
-      image = ADD_POINTERS(image, buf);
+      return;
    }
 
    for (row = 0; row < filter->Height; row++) {
@@ -629,10 +602,7 @@ _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type,
                                  format, type, dst, &ctx->Pack, 0x0);
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 
diff --git a/src/mesa/main/histogram.c b/src/mesa/main/histogram.c
index 726a50d..f1d01cd 100644
--- a/src/mesa/main/histogram.c
+++ b/src/mesa/main/histogram.c
@@ -649,27 +649,19 @@ _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvo
       return;
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack min/max values into a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(1, &ctx->Pack, 2, 1, 1,
-                                     format, type, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetMinMax(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
+   if (!_mesa_validate_pbo_access(1, &ctx->Pack, 2, 1, 1,
+                                  format, type, values)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetMinMax(invalid PBO access)");
+      return;
+   }
+
+   values = _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          /* buffer is already mapped - that's an error */
          _mesa_error(ctx, GL_INVALID_OPERATION,"glGetMinMax(PBO is mapped)");
-         return;
       }
-      values = ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
-      /* not an error */
       return;
    }
 
@@ -687,10 +679,7 @@ _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvo
                                  format, type, values, &ctx->Pack, 0x0);
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 
    if (reset) {
       _mesa_ResetMinmax(GL_MINMAX);
@@ -733,27 +722,18 @@ _mesa_GetHistogram(GLenum target, GLboolean reset, GLenum format, GLenum type, G
       return;
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack min/max values into a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(1, &ctx->Pack, ctx->Histogram.Width, 1, 1,
-                                     format, type, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetHistogram(invalid PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!_mesa_validate_pbo_access(1, &ctx->Pack, ctx->Histogram.Width, 1, 1,
+                                  format, type, values)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetHistogram(invalid PBO access)");
+      return;
+   }
+
+   values = _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx,GL_INVALID_OPERATION,"glGetHistogram(PBO is mapped)");
-         return;
       }
-      values = ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
-      /* not an error */
       return;
    }
 
@@ -761,10 +741,7 @@ _mesa_GetHistogram(GLenum target, GLboolean reset, GLenum format, GLenum type, G
                   (CONST GLuint (*)[4]) ctx->Histogram.Count,
                   format, type, values, &ctx->Pack);
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 
    if (reset) {
       GLuint i;
diff --git a/src/mesa/main/pixel.c b/src/mesa/main/pixel.c
index 25f55a4..fcef6df 100644
--- a/src/mesa/main/pixel.c
+++ b/src/mesa/main/pixel.c
@@ -136,6 +136,33 @@ store_pixelmap(GLcontext *ctx, GLenum map, GLsizei mapsize,
 }
 
 
+/**
+ * Convenience wrapper for _mesa_validate_pbo_access() for gl[Get]PixelMap().
+ */
+static GLboolean
+validate_pbo_access(GLcontext *ctx, struct gl_pixelstore_attrib *pack,
+                    GLsizei mapsize, GLenum format, GLenum type,
+                    const GLvoid *ptr)
+{
+   GLboolean ok;
+
+   /* Note, need to use DefaultPacking and Unpack's buffer object */
+   ctx->DefaultPacking.BufferObj = pack->BufferObj;
+
+   ok = _mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
+                                  format, type, ptr);
+
+   /* restore */
+   ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
+
+   if (!ok) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glPixelMap(invalid PBO access)");
+   }
+   return ok;
+}
+
+
 void GLAPIENTRY
 _mesa_PixelMapfv( GLenum map, GLsizei mapsize, const GLfloat *values )
 {
@@ -158,40 +185,23 @@ _mesa_PixelMapfv( GLenum map, GLsizei mapsize, const GLfloat *values )
 
    FLUSH_VERTICES(ctx, _NEW_PIXEL);
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* unpack pixelmap from PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Unpack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Unpack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_FLOAT, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glPixelMapfv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize,
+                            GL_INTENSITY, GL_FLOAT, values)) {
+      return;
+   }
+
+   values = (const GLfloat *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glPixelMapfv(PBO is mapped)");
-         return;
       }
-      values = (const GLfloat *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
    store_pixelmap(ctx, map, mapsize, values);
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 }
 
 
@@ -217,31 +227,17 @@ _mesa_PixelMapuiv(GLenum map, GLsizei mapsize, const GLuint *values )
 
    FLUSH_VERTICES(ctx, _NEW_PIXEL);
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* unpack pixelmap from PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Unpack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Unpack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_UNSIGNED_INT, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glPixelMapuiv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize,
+                            GL_INTENSITY, GL_UNSIGNED_INT, values)) {
+      return;
+   }
+
+   values = (const GLuint *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glPixelMapuiv(PBO is mapped)");
-         return;
       }
-      values = (const GLuint *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
@@ -259,10 +255,7 @@ _mesa_PixelMapuiv(GLenum map, GLsizei mapsize, const GLuint *values )
       }
    }
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 
    store_pixelmap(ctx, map, mapsize, fvalues);
 }
@@ -290,32 +283,17 @@ _mesa_PixelMapusv(GLenum map, GLsizei mapsize, const GLushort *values )
 
    FLUSH_VERTICES(ctx, _NEW_PIXEL);
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* unpack pixelmap from PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Unpack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Unpack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_UNSIGNED_SHORT,
-                                     values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glPixelMapusv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize,
+                            GL_INTENSITY, GL_UNSIGNED_SHORT, values)) {
+      return;
+   }
+
+   values = (const GLushort *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glPixelMapusv(PBO is mapped)");
-         return;
       }
-      values = (const GLushort *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
@@ -333,10 +311,7 @@ _mesa_PixelMapusv(GLenum map, GLsizei mapsize, const GLushort *values )
       }
    }
 
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 
    store_pixelmap(ctx, map, mapsize, fvalues);
 }
@@ -359,31 +334,17 @@ _mesa_GetPixelMapfv( GLenum map, GLfloat *values )
 
    mapsize = pm->Size;
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack pixelmap into PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Pack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Pack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_FLOAT, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetPixelMapfv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Pack, mapsize,
+                            GL_INTENSITY, GL_FLOAT, values)) {
+      return;
+   }
+
+   values = (GLfloat *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetPixelMapfv(PBO is mapped)");
-         return;
       }
-      values = (GLfloat *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
@@ -397,10 +358,7 @@ _mesa_GetPixelMapfv( GLenum map, GLfloat *values )
       MEMCPY(values, pm->Map, mapsize * sizeof(GLfloat));
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 
@@ -420,31 +378,17 @@ _mesa_GetPixelMapuiv( GLenum map, GLuint *values )
    }
    mapsize = pm->Size;
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack pixelmap into PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Pack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Pack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_UNSIGNED_INT, values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetPixelMapuiv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Pack, mapsize,
+                            GL_INTENSITY, GL_UNSIGNED_INT, values)) {
+      return;
+   }
+
+   values = (GLuint *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetPixelMapuiv(PBO is mapped)");
-         return;
       }
-      values = (GLuint *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
@@ -458,10 +402,7 @@ _mesa_GetPixelMapuiv( GLenum map, GLuint *values )
       }
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 
@@ -481,32 +422,17 @@ _mesa_GetPixelMapusv( GLenum map, GLushort *values )
    }
    mapsize = pm ? pm->Size : 0;
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* pack pixelmap into PBO */
-      GLubyte *buf;
-      /* Note, need to use DefaultPacking and Pack's buffer object */
-      ctx->DefaultPacking.BufferObj = ctx->Pack.BufferObj;
-      if (!_mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
-                                     GL_INTENSITY, GL_UNSIGNED_SHORT,
-                                     values)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetPixelMapusv(invalid PBO access)");
-         return;
-      }
-      /* restore */
-      ctx->DefaultPacking.BufferObj = ctx->Shared->NullBufferObj;
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
+   if (!validate_pbo_access(ctx, &ctx->Pack, mapsize,
+                            GL_INTENSITY, GL_UNSIGNED_SHORT, values)) {
+      return;
+   }
+
+   values = (GLushort *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
+   if (!values) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetPixelMapusv(PBO is mapped)");
-         return;
       }
-      values = (GLushort *) ADD_POINTERS(buf, values);
-   }
-   else if (!values) {
       return;
    }
 
@@ -528,10 +454,7 @@ _mesa_GetPixelMapusv( GLenum map, GLushort *values )
       }
    }
 
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 
diff --git a/src/mesa/main/polygon.c b/src/mesa/main/polygon.c
index d11c942..e327a52 100644
--- a/src/mesa/main/polygon.c
+++ b/src/mesa/main/polygon.c
@@ -193,32 +193,25 @@ _mesa_PolygonMode( GLenum face, GLenum mode )
 void
 _mesa_polygon_stipple(GLcontext *ctx, const GLubyte *pattern)
 {
-   if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
-      /* Get/unpack the stipple pattern from a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(2, &ctx->Unpack, 32, 32, 1,
-                                     GL_COLOR_INDEX, GL_BITMAP, pattern)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glPolygonStipple(bad PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              ctx->Unpack.BufferObj);
-      if (!buf) {
+   if (!_mesa_validate_pbo_access(2, &ctx->Unpack, 32, 32, 1,
+                                  GL_COLOR_INDEX, GL_BITMAP, pattern)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glPolygonStipple(bad PBO access)");
+      return;
+   }
+
+   pattern = _mesa_map_pbo_source(ctx, &ctx->Unpack, pattern);
+   if (!pattern) {
+      if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glPolygonStipple(PBO mapped)");
-         return;
       }
-      buf = ADD_POINTERS(buf, pattern);
-      _mesa_unpack_polygon_stipple(buf, ctx->PolygonStipple, &ctx->Unpack);
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              ctx->Unpack.BufferObj);
-   }
-   else {
-      /* Get/unpack the stipple pattern from user memory */
-      _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
+      return;
    }
+
+   _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
+
+   _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
 }
 
 
@@ -255,35 +248,25 @@ _mesa_GetPolygonStipple( GLubyte *dest )
    if (MESA_VERBOSE&VERBOSE_API)
       _mesa_debug(ctx, "glGetPolygonStipple\n");
 
-   /* XXX someday we may put this code into a separate function and call
-    * it with ctx->Driver.GetPolygonStipple().
-    */
-   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* Put/pack the stipple pattern into a PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(2, &ctx->Pack, 32, 32, 1,
-                                     GL_COLOR_INDEX, GL_BITMAP, dest)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetPolygonStipple(bad PBO access)");
-         return;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                                              GL_WRITE_ONLY_ARB,
-                                              ctx->Pack.BufferObj);
-      if (!buf) {
+   if (!_mesa_validate_pbo_access(2, &ctx->Pack, 32, 32, 1,
+                                  GL_COLOR_INDEX, GL_BITMAP, dest)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetPolygonStipple(bad PBO access)");
+      return;
+   }
+
+   dest = _mesa_map_pbo_dest(ctx, &ctx->Pack, dest);
+   if (!dest) {
+      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glGetPolygonStipple(PBO mapped)");
-         return;
       }
-      buf = ADD_POINTERS(buf, dest);
-      _mesa_pack_polygon_stipple(ctx->PolygonStipple, buf, &ctx->Pack);
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
-   }
-   else {
-      /* Put/pack the stipple pattern into user memory */
-      _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
+      return;
    }
+
+   _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
+
+   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
 }
 
 




More information about the mesa-commit mailing list