Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / main / convolve.c
index 252ef92e3d575ef701bc5ccfe11376efa70ce087..814c6a0a5a6b13d24d8f7b4d7b58bf32cd589dee 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: convolve.c,v 1.5 2000/09/05 20:28:56 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  6.5.2
  *
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  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"),
  */
 
 
-#ifdef PC_HEADER
-#include "all.h"
-#else
 #include "glheader.h"
+#include "bufferobj.h"
+#include "colormac.h"
 #include "convolve.h"
 #include "context.h"
 #include "image.h"
-#include "span.h"
-#include "types.h"
-#endif
+#include "mtypes.h"
+#include "pixel.h"
+#include "state.h"
 
 
 /*
@@ -105,36 +102,40 @@ base_filter_format( GLenum format )
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, GLenum format, GLenum type, const GLvoid *image)
 {
-   GLenum baseFormat;
+   GLint baseFormat;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionFilter1D");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (target != GL_CONVOLUTION_1D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(target)");
       return;
    }
 
    baseFormat = base_filter_format(internalFormat);
    if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(internalFormat)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(internalFormat)");
       return;
    }
 
    if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
-      gl_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter1D(width)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter1D(width)");
+      return;
+   }
+
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter1D(format or type)");
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
-       format == GL_COLOR_INDEX ||
+   if (format == GL_COLOR_INDEX ||
        format == GL_STENCIL_INDEX ||
        format == GL_DEPTH_COMPONENT ||
        format == GL_INTENSITY ||
        type == GL_BITMAP) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(format or type)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(format or type)");
       return;
    }
 
@@ -143,120 +144,164 @@ _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, G
    ctx->Convolution1D.Width = width;
    ctx->Convolution1D.Height = 1;
 
-   /* unpack filter image */
-   _mesa_unpack_float_color_span(ctx, width, GL_RGBA,
+   if (ctx->Unpack.BufferObj->Name) {
+      /* 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 */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glConvolutionFilter1D(PBO is mapped)");
+         return;
+      }
+      image = ADD_POINTERS(buf, image);
+   }
+   else if (!image) {
+      return;
+   }
+
+   _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
                                  ctx->Convolution1D.Filter,
                                  format, type, image, &ctx->Unpack,
-                                 0, GL_FALSE);
-
-   /* apply scale and bias */
-   {
-      const GLfloat *scale = ctx->Pixel.ConvolutionFilterScale[0];
-      const GLfloat *bias = ctx->Pixel.ConvolutionFilterBias[0];
-      GLint i;
-      for (i = 0; i < width; i++) {
-         GLfloat r = ctx->Convolution1D.Filter[i * 4 + 0];
-         GLfloat g = ctx->Convolution1D.Filter[i * 4 + 1];
-         GLfloat b = ctx->Convolution1D.Filter[i * 4 + 2];
-         GLfloat a = ctx->Convolution1D.Filter[i * 4 + 3];
-         r = r * scale[0] + bias[0];
-         g = g * scale[1] + bias[1];
-         b = b * scale[2] + bias[2];
-         a = a * scale[3] + bias[3];
-         ctx->Convolution1D.Filter[i * 4 + 0] = r;
-         ctx->Convolution1D.Filter[i * 4 + 1] = g;
-         ctx->Convolution1D.Filter[i * 4 + 2] = b;
-         ctx->Convolution1D.Filter[i * 4 + 3] = a;
-      }
+                                 0); /* transferOps */
+
+   if (ctx->Unpack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                              ctx->Unpack.BufferObj);
    }
+
+   _mesa_scale_and_bias_rgba(width,
+                             (GLfloat (*)[4]) ctx->Convolution1D.Filter,
+                             ctx->Pixel.ConvolutionFilterScale[0][0],
+                             ctx->Pixel.ConvolutionFilterScale[0][1],
+                             ctx->Pixel.ConvolutionFilterScale[0][2],
+                             ctx->Pixel.ConvolutionFilterScale[0][3],
+                             ctx->Pixel.ConvolutionFilterBias[0][0],
+                             ctx->Pixel.ConvolutionFilterBias[0][1],
+                             ctx->Pixel.ConvolutionFilterBias[0][2],
+                             ctx->Pixel.ConvolutionFilterBias[0][3]);
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *image)
 {
-   GLenum baseFormat;
-   GLint i, components;
+   GLint baseFormat;
+   GLint i;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionFilter2D");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (target != GL_CONVOLUTION_2D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(target)");
       return;
    }
 
    baseFormat = base_filter_format(internalFormat);
    if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(internalFormat)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(internalFormat)");
       return;
    }
 
    if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
-      gl_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(width)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(width)");
       return;
    }
    if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
-      gl_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(height)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(height)");
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
-       format == GL_COLOR_INDEX ||
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter2D(format or type)");
+      return;
+   }
+   if (format == GL_COLOR_INDEX ||
        format == GL_STENCIL_INDEX ||
        format == GL_DEPTH_COMPONENT ||
        format == GL_INTENSITY ||
        type == GL_BITMAP) {
-      gl_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(format or type)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(format or type)");
       return;
    }
 
-   components = _mesa_components_in_format(format);
-   assert(components > 0);  /* this should have been caught earlier */
+   /* this should have been caught earlier */
+   assert(_mesa_components_in_format(format));
 
    ctx->Convolution2D.Format = format;
    ctx->Convolution2D.InternalFormat = internalFormat;
    ctx->Convolution2D.Width = width;
    ctx->Convolution2D.Height = height;
 
+   if (ctx->Unpack.BufferObj->Name) {
+      /* 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 */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glConvolutionFilter2D(PBO is mapped)");
+         return;
+      }
+      image = ADD_POINTERS(buf, image);
+   }
+   else if (!image) {
+      return;
+   }
+
    /* Unpack filter image.  We always store filters in RGBA format. */
    for (i = 0; i < height; i++) {
-      const GLvoid *src = _mesa_image_address(&ctx->Unpack, image, width,
-                                              height, format, type, 0, i, 0);
+      const GLvoid *src = _mesa_image_address2d(&ctx->Unpack, image, width,
+                                                height, format, type, i, 0);
       GLfloat *dst = ctx->Convolution2D.Filter + i * width * 4;
-      _mesa_unpack_float_color_span(ctx, width, GL_RGBA, dst,
+      _mesa_unpack_color_span_float(ctx, width, GL_RGBA, dst,
                                     format, type, src, &ctx->Unpack,
-                                    0, GL_FALSE);
-   }
-
-   /* apply scale and bias */
-   {
-      const GLfloat *scale = ctx->Pixel.ConvolutionFilterScale[1];
-      const GLfloat *bias = ctx->Pixel.ConvolutionFilterBias[1];
-      for (i = 0; i < width * height * 4; i++) {
-         GLfloat r = ctx->Convolution2D.Filter[i * 4 + 0];
-         GLfloat g = ctx->Convolution2D.Filter[i * 4 + 1];
-         GLfloat b = ctx->Convolution2D.Filter[i * 4 + 2];
-         GLfloat a = ctx->Convolution2D.Filter[i * 4 + 3];
-         r = r * scale[0] + bias[0];
-         g = g * scale[1] + bias[1];
-         b = b * scale[2] + bias[2];
-         a = a * scale[3] + bias[3];
-         ctx->Convolution2D.Filter[i * 4 + 0] = r;
-         ctx->Convolution2D.Filter[i * 4 + 1] = g;
-         ctx->Convolution2D.Filter[i * 4 + 2] = b;
-         ctx->Convolution2D.Filter[i * 4 + 3] = a;
-      }
+                                    0); /* transferOps */
+   }
+
+   if (ctx->Unpack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                              ctx->Unpack.BufferObj);
    }
+
+   _mesa_scale_and_bias_rgba(width * height,
+                             (GLfloat (*)[4]) ctx->Convolution2D.Filter,
+                             ctx->Pixel.ConvolutionFilterScale[1][0],
+                             ctx->Pixel.ConvolutionFilterScale[1][1],
+                             ctx->Pixel.ConvolutionFilterScale[1][2],
+                             ctx->Pixel.ConvolutionFilterScale[1][3],
+                             ctx->Pixel.ConvolutionFilterBias[1][0],
+                             ctx->Pixel.ConvolutionFilterBias[1][1],
+                             ctx->Pixel.ConvolutionFilterBias[1][2],
+                             ctx->Pixel.ConvolutionFilterBias[1][3]);
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
 {
    GET_CURRENT_CONTEXT(ctx);
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionParameterf");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
@@ -269,7 +314,7 @@ _mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
          c = 2;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(target)");
          return;
    }
 
@@ -281,41 +326,38 @@ _mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
             ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
          }
          else {
-            gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(params)");
+            _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(params)");
             return;
          }
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(pname)");
          return;
    }
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_convolution_attrib *conv;
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionParameterfv");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
          c = 0;
-         conv = &ctx->Convolution1D;
          break;
       case GL_CONVOLUTION_2D:
          c = 1;
-         conv = &ctx->Convolution2D;
          break;
       case GL_SEPARABLE_2D:
          c = 2;
-         conv = &ctx->Separable2D;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(target)");
          return;
    }
 
@@ -330,7 +372,7 @@ _mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
             ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
          }
          else {
-            gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(params)");
+            _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(params)");
             return;
          }
          break;
@@ -341,19 +383,20 @@ _mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
          COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params);
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(pname)");
          return;
    }
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
 {
    GET_CURRENT_CONTEXT(ctx);
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionParameteri");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
@@ -366,7 +409,7 @@ _mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
          c = 2;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(target)");
          return;
    }
 
@@ -378,41 +421,38 @@ _mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
             ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
          }
          else {
-            gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(params)");
+            _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(params)");
             return;
          }
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(pname)");
          return;
    }
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ConvolutionParameteriv(GLenum target, GLenum pname, const GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_convolution_attrib *conv;
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glConvolutionParameteriv");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
          c = 0;
-         conv = &ctx->Convolution1D;
          break;
       case GL_CONVOLUTION_2D:
          c = 1;
-         conv = &ctx->Convolution2D;
          break;
       case GL_SEPARABLE_2D:
          c = 2;
-         conv = &ctx->Separable2D;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(target)");
          return;
    }
 
@@ -430,128 +470,119 @@ _mesa_ConvolutionParameteriv(GLenum target, GLenum pname, const GLint *params)
             ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
          }
          else {
-            gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(params)");
+            _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(params)");
             return;
          }
          break;
       case GL_CONVOLUTION_FILTER_SCALE:
-         COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params);
+        /* COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params); */
+        /* need cast to prevent compiler warnings */  
+        ctx->Pixel.ConvolutionFilterScale[c][0] = (GLfloat) params[0]; 
+        ctx->Pixel.ConvolutionFilterScale[c][1] = (GLfloat) params[1]; 
+        ctx->Pixel.ConvolutionFilterScale[c][2] = (GLfloat) params[2]; 
+        ctx->Pixel.ConvolutionFilterScale[c][3] = (GLfloat) params[3]; 
          break;
       case GL_CONVOLUTION_FILTER_BIAS:
-         COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params);
+        /* COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params); */
+        /* need cast to prevent compiler warnings */  
+        ctx->Pixel.ConvolutionFilterBias[c][0] = (GLfloat) params[0]; 
+        ctx->Pixel.ConvolutionFilterBias[c][1] = (GLfloat) params[1]; 
+        ctx->Pixel.ConvolutionFilterBias[c][2] = (GLfloat) params[2]; 
+        ctx->Pixel.ConvolutionFilterBias[c][3] = (GLfloat) params[3]; 
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(pname)");
          return;
    }
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_CopyConvolutionFilter1D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width)
 {
-   GLenum baseFormat;
-   GLfloat rgba[MAX_CONVOLUTION_WIDTH][4];
+   GLint baseFormat;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyConvolutionFilter1D");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (target != GL_CONVOLUTION_1D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(target)");
       return;
    }
 
    baseFormat = base_filter_format(internalFormat);
    if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
-      gl_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(internalFormat)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(internalFormat)");
       return;
    }
 
    if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
-      gl_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter1D(width)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter1D(width)");
       return;
    }
 
-   /* read pixels from framebuffer */
-   gl_read_rgba_span(ctx, ctx->ReadBuffer, width, x, y, (GLubyte (*)[4]) rgba);
-
-   /* store as convolution filter */
-   _mesa_ConvolutionFilter1D(target, internalFormat, width,
-                             GL_RGBA, GL_UNSIGNED_BYTE, rgba);
+   ctx->Driver.CopyConvolutionFilter1D( ctx, target, 
+                                       internalFormat, x, y, width);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_CopyConvolutionFilter2D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height)
 {
-   GLenum baseFormat;
-   GLint i;
-   struct gl_pixelstore_attrib packSave;
-   GLfloat rgba[MAX_CONVOLUTION_HEIGHT][MAX_CONVOLUTION_WIDTH][4];
+   GLint baseFormat;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyConvolutionFilter2D");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (target != GL_CONVOLUTION_2D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(target)");
       return;
    }
 
    baseFormat = base_filter_format(internalFormat);
    if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
-      gl_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(internalFormat)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(internalFormat)");
       return;
    }
 
    if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
-      gl_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(width)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(width)");
       return;
    }
    if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
-      gl_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(height)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(height)");
       return;
    }
 
-   /* read pixels from framebuffer */
-   for (i = 0; i < height; i++) {
-      gl_read_rgba_span(ctx, ctx->ReadBuffer, width, x, y + i,
-                        (GLubyte (*)[4]) rgba[i]);
-   }
-
-   /*
-    * store as convolution filter
-    */
-   packSave = ctx->Unpack;  /* save pixel packing params */
-
-   ctx->Unpack.Alignment = 1;
-   ctx->Unpack.RowLength = MAX_CONVOLUTION_WIDTH;
-   ctx->Unpack.SkipPixels = 0;
-   ctx->Unpack.SkipRows = 0;
-   ctx->Unpack.ImageHeight = 0;
-   ctx->Unpack.SkipImages = 0;
-   ctx->Unpack.SwapBytes = GL_FALSE;
-   ctx->Unpack.LsbFirst = GL_FALSE;
-
-   _mesa_ConvolutionFilter2D(target, internalFormat, width, height,
-                             GL_RGBA, GL_UNSIGNED_BYTE, rgba);
-
-   ctx->Unpack = packSave;  /* restore pixel packing params */
+   ctx->Driver.CopyConvolutionFilter2D( ctx, target, internalFormat, x, y, 
+                                       width, height );
 }
 
 
-void
-_mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type, GLvoid *image)
+void GLAPIENTRY
+_mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type,
+                           GLvoid *image)
 {
-   const struct gl_convolution_attrib *filter;
-   GLint row;
+   struct gl_convolution_attrib *filter;
+   GLuint row;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetConvolutionFilter");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (ctx->NewState) {
+      _mesa_update_state(ctx);
+   }
+
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
+      return;
+   }
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
-       format == GL_COLOR_INDEX ||
+   if (format == GL_COLOR_INDEX ||
        format == GL_STENCIL_INDEX ||
        format == GL_DEPTH_COMPONENT ||
        format == GL_INTENSITY ||
        type == GL_BITMAP) {
-      gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
       return;
    }
 
@@ -563,31 +594,55 @@ _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type, GLvoid *im
          filter = &(ctx->Convolution2D);
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(target)");
+         return;
+   }
+
+   if (ctx->Pack.BufferObj->Name) {
+      /* 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 */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetConvolutionFilter(PBO is mapped)");
          return;
+      }
+      image = ADD_POINTERS(image, buf);
    }
 
    for (row = 0; row < filter->Height; row++) {
-      GLvoid *dst = _mesa_image_address( &ctx->Pack, image, filter->Width,
-                                         filter->Height, format, type,
-                                         0, row, 0);
-      const GLfloat *src = filter->Filter + row * filter->Width * 4;
-      /* XXX apply transfer ops or not? */
-      _mesa_pack_float_rgba_span(ctx, filter->Width,
-                                 (const GLfloat (*)[4]) src,
-                                 format, type, dst, &ctx->Pack, 0);
+      GLvoid *dst = _mesa_image_address2d(&ctx->Pack, image, filter->Width,
+                                          filter->Height, format, type,
+                                          row, 0);
+      GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + row * filter->Width * 4);
+      _mesa_pack_rgba_span_float(ctx, filter->Width, src,
+                                 format, type, dst, &ctx->Pack, 0x0);
+   }
+
+   if (ctx->Pack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                              ctx->Pack.BufferObj);
    }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
    const struct gl_convolution_attrib *conv;
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetConvolutionParameterfv");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
@@ -603,7 +658,7 @@ _mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
          conv = &ctx->Separable2D;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(target)");
          return;
    }
 
@@ -636,20 +691,19 @@ _mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
          *params = (GLfloat) ctx->Const.MaxConvolutionHeight;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(pname)");
          return;
    }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
    const struct gl_convolution_attrib *conv;
    GLuint c;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetConvolutionParameteriv");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    switch (target) {
       case GL_CONVOLUTION_1D:
@@ -665,7 +719,7 @@ _mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
          conv = &ctx->Separable2D;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(target)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(target)");
          return;
    }
 
@@ -707,97 +761,142 @@ _mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
          *params = (GLint) ctx->Const.MaxConvolutionHeight;
          break;
       default:
-         gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(pname)");
+         _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(pname)");
          return;
    }
 }
 
 
-void
-_mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type, GLvoid *row, GLvoid *column, GLvoid *span)
+void GLAPIENTRY
+_mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type,
+                         GLvoid *row, GLvoid *column, GLvoid *span)
 {
    const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
-   const struct gl_convolution_attrib *filter;
+   struct gl_convolution_attrib *filter;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetSeparableFilter");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (ctx->NewState) {
+      _mesa_update_state(ctx);
+   }
 
    if (target != GL_SEPARABLE_2D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glGetSeparableFilter(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetSeparableFilter(target)");
+      return;
+   }
+
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetConvolutionFilter(format or type)");
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
-       format == GL_COLOR_INDEX ||
+   if (format == GL_COLOR_INDEX ||
        format == GL_STENCIL_INDEX ||
        format == GL_DEPTH_COMPONENT ||
        format == GL_INTENSITY ||
        type == GL_BITMAP) {
-      gl_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
       return;
    }
 
    filter = &ctx->Separable2D;
 
+   if (ctx->Pack.BufferObj->Name) {
+      /* Pack filter into PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Pack, filter->Width, 1, 1,
+                                     format, type, row)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetSeparableFilter(invalid PBO access, width)");
+         return;
+      }
+      if (!_mesa_validate_pbo_access(1, &ctx->Pack, filter->Height, 1, 1,
+                                     format, type, column)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetSeparableFilter(invalid PBO access, height)");
+         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 */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetSeparableFilter(PBO is mapped)");
+         return;
+      }
+      row = ADD_POINTERS(buf, row);
+      column = ADD_POINTERS(buf, column);
+   }
+
    /* Row filter */
-   {
-      GLvoid *dst = _mesa_image_address( &ctx->Pack, row, filter->Width,
-                                         filter->Height, format, type,
-                                         0, 0, 0);
-      _mesa_pack_float_rgba_span(ctx, filter->Width,
-                                 (const GLfloat (*)[4]) filter->Filter,
-                                 format, type, dst, &ctx->Pack, 0);
+   if (row) {
+      GLvoid *dst = _mesa_image_address1d(&ctx->Pack, row, filter->Width,
+                                          format, type, 0);
+      _mesa_pack_rgba_span_float(ctx, filter->Width,
+                                 (GLfloat (*)[4]) filter->Filter,
+                                 format, type, dst, &ctx->Pack, 0x0);
    }
 
    /* Column filter */
-   {
-      GLvoid *dst = _mesa_image_address( &ctx->Pack, column, filter->Width,
-                                         1, format, type,
-                                         0, 0, 0);
-      const GLfloat *src = filter->Filter + colStart;
-      _mesa_pack_float_rgba_span(ctx, filter->Height,
-                                 (const GLfloat (*)[4]) src,
-                                 format, type, dst, &ctx->Pack, 0);
+   if (column) {
+      GLvoid *dst = _mesa_image_address1d(&ctx->Pack, column, filter->Height,
+                                          format, type, 0);
+      GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + colStart);
+      _mesa_pack_rgba_span_float(ctx, filter->Height, src,
+                                 format, type, dst, &ctx->Pack, 0x0);
    }
 
    (void) span;  /* unused at this time */
+
+   if (ctx->Pack.BufferObj->Name) {
+      /* Pack filter into PBO */
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                              ctx->Unpack.BufferObj);
+   }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *row, const GLvoid *column)
 {
    const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
-   GLenum baseFormat;
+   GLint baseFormat;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glSeparableFilter2D");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (target != GL_SEPARABLE_2D) {
-      gl_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
       return;
    }
 
    baseFormat = base_filter_format(internalFormat);
    if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
-      gl_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
       return;
    }
 
    if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
-      gl_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
       return;
    }
    if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
-      gl_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
+      _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
-       format == GL_COLOR_INDEX ||
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
+      return;
+   }
+
+   if (format == GL_COLOR_INDEX ||
        format == GL_STENCIL_INDEX ||
        format == GL_DEPTH_COMPONENT ||
        format == GL_INTENSITY ||
        type == GL_BITMAP) {
-      gl_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
       return;
    }
 
@@ -806,59 +905,78 @@ _mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLs
    ctx->Separable2D.Width = width;
    ctx->Separable2D.Height = height;
 
-   /* unpack row filter */
-   _mesa_unpack_float_color_span(ctx, width, GL_RGBA,
-                                 ctx->Separable2D.Filter,
-                                 format, type, row, &ctx->Unpack,
-                                 0, GL_FALSE);
-
-   /* apply scale and bias */
-   {
-      const GLfloat *scale = ctx->Pixel.ConvolutionFilterScale[2];
-      const GLfloat *bias = ctx->Pixel.ConvolutionFilterBias[2];
-      GLint i;
-      for (i = 0; i < width; i++) {
-         GLfloat r = ctx->Separable2D.Filter[i * 4 + 0];
-         GLfloat g = ctx->Separable2D.Filter[i * 4 + 1];
-         GLfloat b = ctx->Separable2D.Filter[i * 4 + 2];
-         GLfloat a = ctx->Separable2D.Filter[i * 4 + 3];
-         r = r * scale[0] + bias[0];
-         g = g * scale[1] + bias[1];
-         b = b * scale[2] + bias[2];
-         a = a * scale[3] + bias[3];
-         ctx->Separable2D.Filter[i * 4 + 0] = r;
-         ctx->Separable2D.Filter[i * 4 + 1] = g;
-         ctx->Separable2D.Filter[i * 4 + 2] = b;
-         ctx->Separable2D.Filter[i * 4 + 3] = a;
+   if (ctx->Unpack.BufferObj->Name) {
+      /* unpack filter from PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Unpack, width, 1, 1,
+                                     format, type, row)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glSeparableFilter2D(invalid PBO access, width)");
+         return;
+      }
+      if (!_mesa_validate_pbo_access(1, &ctx->Unpack, height, 1, 1,
+                                     format, type, column)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glSeparableFilter2D(invalid PBO access, height)");
+         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 */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glSeparableFilter2D(PBO is mapped)");
+         return;
       }
+      row = ADD_POINTERS(buf, row);
+      column = ADD_POINTERS(buf, column);
+   }
+
+   /* unpack row filter */
+   if (row) {
+      _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
+                                    ctx->Separable2D.Filter,
+                                    format, type, row, &ctx->Unpack,
+                                    0);  /* transferOps */
+
+      _mesa_scale_and_bias_rgba(width,
+                             (GLfloat (*)[4]) ctx->Separable2D.Filter,
+                             ctx->Pixel.ConvolutionFilterScale[2][0],
+                             ctx->Pixel.ConvolutionFilterScale[2][1],
+                             ctx->Pixel.ConvolutionFilterScale[2][2],
+                             ctx->Pixel.ConvolutionFilterScale[2][3],
+                             ctx->Pixel.ConvolutionFilterBias[2][0],
+                             ctx->Pixel.ConvolutionFilterBias[2][1],
+                             ctx->Pixel.ConvolutionFilterBias[2][2],
+                             ctx->Pixel.ConvolutionFilterBias[2][3]);
    }
 
    /* unpack column filter */
-   _mesa_unpack_float_color_span(ctx, width, GL_RGBA,
-                                 &ctx->Separable2D.Filter[colStart],
-                                 format, type, column, &ctx->Unpack,
-                                 0, GL_FALSE);
-
-   /* apply scale and bias */
-   {
-      const GLfloat *scale = ctx->Pixel.ConvolutionFilterScale[2];
-      const GLfloat *bias = ctx->Pixel.ConvolutionFilterBias[2];
-      GLint i;
-      for (i = 0; i < width; i++) {
-         GLfloat r = ctx->Separable2D.Filter[i * 4 + 0 + colStart];
-         GLfloat g = ctx->Separable2D.Filter[i * 4 + 1 + colStart];
-         GLfloat b = ctx->Separable2D.Filter[i * 4 + 2 + colStart];
-         GLfloat a = ctx->Separable2D.Filter[i * 4 + 3 + colStart];
-         r = r * scale[0] + bias[0];
-         g = g * scale[1] + bias[1];
-         b = b * scale[2] + bias[2];
-         a = a * scale[3] + bias[3];
-         ctx->Separable2D.Filter[i * 4 + 0 + colStart] = r;
-         ctx->Separable2D.Filter[i * 4 + 1 + colStart] = g;
-         ctx->Separable2D.Filter[i * 4 + 2 + colStart] = b;
-         ctx->Separable2D.Filter[i * 4 + 3 + colStart] = a;
-      }
+   if (column) {
+      _mesa_unpack_color_span_float(ctx, height, GL_RGBA,
+                                    &ctx->Separable2D.Filter[colStart],
+                                    format, type, column, &ctx->Unpack,
+                                    0); /* transferOps */
+
+      _mesa_scale_and_bias_rgba(height,
+                       (GLfloat (*)[4]) (ctx->Separable2D.Filter + colStart),
+                       ctx->Pixel.ConvolutionFilterScale[2][0],
+                       ctx->Pixel.ConvolutionFilterScale[2][1],
+                       ctx->Pixel.ConvolutionFilterScale[2][2],
+                       ctx->Pixel.ConvolutionFilterScale[2][3],
+                       ctx->Pixel.ConvolutionFilterBias[2][0],
+                       ctx->Pixel.ConvolutionFilterBias[2][1],
+                       ctx->Pixel.ConvolutionFilterBias[2][2],
+                       ctx->Pixel.ConvolutionFilterBias[2][3]);
+   }
+
+   if (ctx->Unpack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                              ctx->Unpack.BufferObj);
    }
+
+   ctx->NewState |= _NEW_PIXEL;
 }
 
 
@@ -1378,3 +1496,33 @@ _mesa_convolve_sep_image(const GLcontext *ctx,
          ;
    }
 }
+
+
+
+/*
+ * This function computes an image's size after convolution.
+ * If the convolution border mode is GL_REDUCE, the post-convolution
+ * image will be smaller than the original.
+ */
+void
+_mesa_adjust_image_for_convolution(const GLcontext *ctx, GLuint dimensions,
+                                   GLsizei *width, GLsizei *height)
+{
+   if (ctx->Pixel.Convolution1DEnabled
+       && dimensions == 1
+       && ctx->Pixel.ConvolutionBorderMode[0] == GL_REDUCE) {
+      *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
+   }
+   else if (ctx->Pixel.Convolution2DEnabled
+            && dimensions > 1
+            && ctx->Pixel.ConvolutionBorderMode[1] == GL_REDUCE) {
+      *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
+      *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
+   }
+   else if (ctx->Pixel.Separable2DEnabled
+            && dimensions > 1
+            && ctx->Pixel.ConvolutionBorderMode[2] == GL_REDUCE) {
+      *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
+      *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
+   }
+}