check for framebuffer completeness, code clean-up
[mesa.git] / src / mesa / main / convolve.c
index edb02482b399526d644678fb98254d97d2f12727..6b97c7f56cdbd6a4f506a5d83653348c5c93d0df 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.3
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  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"),
 
 
 #include "glheader.h"
+#include "bufferobj.h"
 #include "colormac.h"
 #include "convolve.h"
 #include "context.h"
 #include "image.h"
 #include "mtypes.h"
+#include "pixel.h"
 #include "state.h"
 
 
@@ -123,7 +125,7 @@ _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, G
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter1D(format or type)");
       return;
    }
@@ -142,33 +144,51 @@ _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, G
    ctx->Convolution1D.Width = width;
    ctx->Convolution1D.Height = 1;
 
-   /* unpack filter image */
+   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;
 }
 
@@ -201,7 +221,7 @@ _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, G
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter2D(format or type)");
       return;
    }
@@ -222,36 +242,56 @@ _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, G
    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_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; 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;
 }
 
@@ -516,7 +556,6 @@ _mesa_CopyConvolutionFilter2D(GLenum target, GLenum internalFormat, GLint x, GLi
 
    ctx->Driver.CopyConvolutionFilter2D( ctx, target, internalFormat, x, y, 
                                        width, height );
-
 }
 
 
@@ -532,7 +571,7 @@ _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type, GLvoid *im
       _mesa_update_state(ctx);
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
       return;
    }
@@ -558,15 +597,42 @@ _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type, GLvoid *im
          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);
+      GLvoid *dst = _mesa_image_address2d(&ctx->Pack, image, filter->Width,
+                                          filter->Height, format, type,
+                                          row, 0);
       const GLfloat *src = filter->Filter + row * filter->Width * 4;
       _mesa_pack_rgba_span_float(ctx, filter->Width,
                                  (const GLfloat (*)[4]) src,
                                  format, type, dst, &ctx->Pack, 0);
    }
+
+   if (ctx->Pack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                              ctx->Pack.BufferObj);
+   }
 }
 
 
@@ -718,7 +784,7 @@ _mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type, GLvoid *row,
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
       return;
    }
@@ -734,21 +800,47 @@ _mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type, GLvoid *row,
 
    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);
+   if (row) {
+      GLvoid *dst = _mesa_image_address1d(&ctx->Pack, row, filter->Width,
+                                          format, type, 0);
       _mesa_pack_rgba_span_float(ctx, filter->Width,
                                  (const GLfloat (*)[4]) filter->Filter,
                                  format, type, dst, &ctx->Pack, 0);
    }
 
    /* Column filter */
-   {
-      GLvoid *dst = _mesa_image_address( &ctx->Pack, column, filter->Width,
-                                         1, format, type,
-                                         0, 0, 0);
+   if (column) {
+      GLvoid *dst = _mesa_image_address1d(&ctx->Pack, column, filter->Height,
+                                          format, type, 0);
       const GLfloat *src = filter->Filter + colStart;
       _mesa_pack_rgba_span_float(ctx, filter->Height,
                                  (const GLfloat (*)[4]) src,
@@ -756,6 +848,12 @@ _mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type, GLvoid *row,
    }
 
    (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);
+   }
 }
 
 
@@ -787,7 +885,7 @@ _mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLs
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
       return;
    }
@@ -806,58 +904,75 @@ _mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLs
    ctx->Separable2D.Width = width;
    ctx->Separable2D.Height = height;
 
-   /* unpack row filter */
-   _mesa_unpack_color_span_float(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_color_span_float(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;