mesa: add ARB_texture_buffer_range glTextureBufferRangeEXT function
[mesa.git] / src / mesa / main / texcompress_fxt1.c
index 85becb80d2a30b3239f5950d76eb477887b5a432..bd284b36113c4182a13cab8649cb899fe66fbb70 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
 /**
  * \file texcompress_fxt1.c
- * GL_EXT_texture_compression_fxt1 support.
+ * GL_3DFX_texture_compression_FXT1 support.
  */
 
 
+#include "errors.h"
 #include "glheader.h"
 #include "imports.h"
-#include "colormac.h"
-#include "context.h"
-#include "convolve.h"
 #include "image.h"
+#include "macros.h"
 #include "mipmap.h"
 #include "texcompress.h"
 #include "texcompress_fxt1.h"
 #include "texstore.h"
-
-
-#if FEATURE_texture_fxt1
+#include "mtypes.h"
 
 
 static void
@@ -49,9 +46,9 @@ fxt1_encode (GLuint width, GLuint height, GLint comps,
              const void *source, GLint srcRowStride,
              void *dest, GLint destRowStride);
 
-void
+static void
 fxt1_decode_1 (const void *texture, GLint stride,
-               GLint i, GLint j, GLchan *rgba);
+               GLint i, GLint j, GLubyte *rgba);
 
 
 /**
@@ -60,52 +57,50 @@ fxt1_decode_1 (const void *texture, GLint stride,
 GLboolean
 _mesa_texstore_rgb_fxt1(TEXSTORE_PARAMS)
 {
-   const GLchan *pixels;
+   const GLubyte *pixels;
    GLint srcRowStride;
    GLubyte *dst;
-   const GLint texWidth = dstRowStride * 8 / 16; /* a bit of a hack */
-   const GLchan *tempImage = NULL;
+   const GLubyte *tempImage = NULL;
 
-   ASSERT(dstFormat == MESA_FORMAT_RGB_FXT1);
-   ASSERT(dstXoffset % 8 == 0);
-   ASSERT(dstYoffset % 4 == 0);
-   ASSERT(dstZoffset     == 0);
-   (void) dstZoffset;
-   (void) dstImageOffsets;
+   assert(dstFormat == MESA_FORMAT_RGB_FXT1);
 
    if (srcFormat != GL_RGB ||
-       srcType != CHAN_TYPE ||
+       srcType != GL_UNSIGNED_BYTE ||
        ctx->_ImageTransferState ||
+       ALIGN(srcPacking->RowLength, srcPacking->Alignment) != srcWidth ||
        srcPacking->SwapBytes) {
-      /* convert image to RGB/GLchan */
-      tempImage = _mesa_make_temp_chan_image(ctx, dims,
-                                             baseInternalFormat,
-                                             _mesa_get_format_base_format(dstFormat),
-                                             srcWidth, srcHeight, srcDepth,
-                                             srcFormat, srcType, srcAddr,
-                                             srcPacking);
+      /* convert image to RGB/GLubyte */
+      GLubyte *tempImageSlices[1];
+      int rgbRowStride = 3 * srcWidth * sizeof(GLubyte);
+      tempImage = malloc(srcWidth * srcHeight * 3 * sizeof(GLubyte));
       if (!tempImage)
          return GL_FALSE; /* out of memory */
-      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
+      tempImageSlices[0] = (GLubyte *) tempImage;
+      _mesa_texstore(ctx, dims,
+                     baseInternalFormat,
+                     MESA_FORMAT_RGB_UNORM8,
+                     rgbRowStride, tempImageSlices,
+                     srcWidth, srcHeight, srcDepth,
+                     srcFormat, srcType, srcAddr,
+                     srcPacking);
       pixels = tempImage;
       srcRowStride = 3 * srcWidth;
       srcFormat = GL_RGB;
    }
    else {
-      pixels = (const GLchan *) srcAddr;
+      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
+                                     srcFormat, srcType, 0, 0);
+
       srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
-                                            srcType) / sizeof(GLchan);
+                                            srcType) / sizeof(GLubyte);
    }
 
-   dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
-                                        dstFormat,
-                                        texWidth, (GLubyte *) dstAddr);
+   dst = dstSlices[0];
 
    fxt1_encode(srcWidth, srcHeight, 3, pixels, srcRowStride,
                dst, dstRowStride);
 
-   if (tempImage)
-      _mesa_free((void*) tempImage);
+   free((void*) tempImage);
 
    return GL_TRUE;
 }
@@ -117,88 +112,58 @@ _mesa_texstore_rgb_fxt1(TEXSTORE_PARAMS)
 GLboolean
 _mesa_texstore_rgba_fxt1(TEXSTORE_PARAMS)
 {
-   const GLchan *pixels;
+   const GLubyte *pixels;
    GLint srcRowStride;
    GLubyte *dst;
-   GLint texWidth = dstRowStride * 8 / 16; /* a bit of a hack */
-   const GLchan *tempImage = NULL;
+   const GLubyte *tempImage = NULL;
 
-   ASSERT(dstFormat == MESA_FORMAT_RGBA_FXT1);
-   ASSERT(dstXoffset % 8 == 0);
-   ASSERT(dstYoffset % 4 == 0);
-   ASSERT(dstZoffset     == 0);
-   (void) dstZoffset;
-   (void) dstImageOffsets;
+   assert(dstFormat == MESA_FORMAT_RGBA_FXT1);
 
    if (srcFormat != GL_RGBA ||
-       srcType != CHAN_TYPE ||
+       srcType != GL_UNSIGNED_BYTE ||
        ctx->_ImageTransferState ||
        srcPacking->SwapBytes) {
-      /* convert image to RGBA/GLchan */
-      tempImage = _mesa_make_temp_chan_image(ctx, dims,
-                                             baseInternalFormat,
-                                             _mesa_get_format_base_format(dstFormat),
-                                             srcWidth, srcHeight, srcDepth,
-                                             srcFormat, srcType, srcAddr,
-                                             srcPacking);
+      /* convert image to RGBA/GLubyte */
+      GLubyte *tempImageSlices[1];
+      int rgbaRowStride = 4 * srcWidth * sizeof(GLubyte);
+      tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(GLubyte));
       if (!tempImage)
          return GL_FALSE; /* out of memory */
-      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
+      tempImageSlices[0] = (GLubyte *) tempImage;
+      _mesa_texstore(ctx, dims,
+                     baseInternalFormat,
+#if UTIL_ARCH_LITTLE_ENDIAN
+                     MESA_FORMAT_R8G8B8A8_UNORM,
+#else
+                     MESA_FORMAT_A8B8G8R8_UNORM,
+#endif
+                     rgbaRowStride, tempImageSlices,
+                     srcWidth, srcHeight, srcDepth,
+                     srcFormat, srcType, srcAddr,
+                     srcPacking);
       pixels = tempImage;
       srcRowStride = 4 * srcWidth;
       srcFormat = GL_RGBA;
    }
    else {
-      pixels = (const GLchan *) srcAddr;
+      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
+                                     srcFormat, srcType, 0, 0);
+
       srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
-                                            srcType) / sizeof(GLchan);
+                                            srcType) / sizeof(GLubyte);
    }
 
-   dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
-                                        dstFormat,
-                                        texWidth, (GLubyte *) dstAddr);
+   dst = dstSlices[0];
 
    fxt1_encode(srcWidth, srcHeight, 4, pixels, srcRowStride,
                dst, dstRowStride);
 
-   if (tempImage)
-      _mesa_free((void*) tempImage);
+   free((void*) tempImage);
 
    return GL_TRUE;
 }
 
 
-void
-_mesa_fetch_texel_2d_f_rgba_fxt1( const struct gl_texture_image *texImage,
-                                  GLint i, GLint j, GLint k, GLfloat *texel )
-{
-   /* just sample as GLchan and convert to float here */
-   GLchan rgba[4];
-   (void) k;
-   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, rgba);
-   texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
-   texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
-   texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
-   texel[ACOMP] = CHAN_TO_FLOAT(rgba[ACOMP]);
-}
-
-
-void
-_mesa_fetch_texel_2d_f_rgb_fxt1( const struct gl_texture_image *texImage,
-                                 GLint i, GLint j, GLint k, GLfloat *texel )
-{
-   /* just sample as GLchan and convert to float here */
-   GLchan rgba[4];
-   (void) k;
-   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, rgba);
-   texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
-   texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
-   texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
-   texel[ACOMP] = 1.0F;
-}
-
-
-
 /***************************************************************************\
  * FXT1 encoder
  *
@@ -217,8 +182,8 @@ _mesa_fetch_texel_2d_f_rgb_fxt1( const struct gl_texture_image *texImage,
 #define LL_RMS_D 10 /* fault tolerance (maximum delta) */
 #define LL_RMS_E 255 /* fault tolerance (maximum error) */
 #define ALPHA_TS 2 /* alpha threshold: (255 - ALPHA_TS) deemed opaque */
-#define ISTBLACK(v) (*((GLuint *)(v)) == 0)
-
+static const GLuint zero = 0;
+#define ISTBLACK(v) (memcmp(&(v), &zero, sizeof(zero)) == 0)
 
 /*
  * Define a 64-bit unsigned integer type and macros
@@ -406,7 +371,7 @@ fxt1_choose (GLfloat vec[][MAX_COMP], GLint nv,
    } hist[N_TEXELS];
    GLint lenh = 0;
 
-   _mesa_memset(hist, 0, sizeof(hist));
+   memset(hist, 0, sizeof(hist));
 
    for (k = 0; k < n; k++) {
       GLint l;
@@ -476,7 +441,7 @@ fxt1_lloyd (GLfloat vec[][MAX_COMP], GLint nv,
     *     for each sample color
     *         sort to nearest vector.
     *
-    *     replace each vector with the centroid of it's matching colors.
+    *     replace each vector with the centroid of its matching colors.
     *
     *     repeat until RMS doesn't improve.
     *
@@ -528,6 +493,7 @@ fxt1_lloyd (GLfloat vec[][MAX_COMP], GLint nv,
 #else
          GLint best = fxt1_bestcol(vec, nv, input[k], nc, &err);
 #endif
+         assert(best >= 0);
          /* add in closest color */
          for (i = 0; i < nc; i++) {
             sum[best][i] += input[k][i];
@@ -1211,7 +1177,7 @@ fxt1_quantize (GLuint *cc, const GLubyte *lines[], GLint comps)
 
    if (comps == 3) {
       /* make the whole block opaque */
-      _mesa_memset(input, -1, sizeof(input));
+      memset(input, -1, sizeof(input));
    }
 
    /* 8 texels each line */
@@ -1289,6 +1255,41 @@ fxt1_quantize (GLuint *cc, const GLubyte *lines[], GLint comps)
 }
 
 
+
+/**
+ * Upscale an image by replication, not (typical) stretching.
+ * We use this when the image width or height is less than a
+ * certain size (4, 8) and we need to upscale an image.
+ */
+static void
+upscale_teximage2d(GLsizei inWidth, GLsizei inHeight,
+                   GLsizei outWidth, GLsizei outHeight,
+                   GLint comps, const GLubyte *src, GLint srcRowStride,
+                   GLubyte *dest )
+{
+   GLint i, j, k;
+
+   assert(outWidth >= inWidth);
+   assert(outHeight >= inHeight);
+#if 0
+   assert(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
+   assert((outWidth & 3) == 0);
+   assert((outHeight & 3) == 0);
+#endif
+
+   for (i = 0; i < outHeight; i++) {
+      const GLint ii = i % inHeight;
+      for (j = 0; j < outWidth; j++) {
+         const GLint jj = j % inWidth;
+         for (k = 0; k < comps; k++) {
+            dest[(i * outWidth + j) * comps + k]
+               = src[ii * srcRowStride + jj * comps + k];
+         }
+      }
+   }
+}
+
+
 static void
 fxt1_encode (GLuint width, GLuint height, GLint comps,
              const void *source, GLint srcRowStride,
@@ -1305,42 +1306,21 @@ fxt1_encode (GLuint width, GLuint height, GLint comps,
    if ((width & 7) | (height & 3)) {
       GLint newWidth = (width + 7) & ~7;
       GLint newHeight = (height + 3) & ~3;
-      newSource = _mesa_malloc(comps * newWidth * newHeight * sizeof(GLchan));
+      newSource = malloc(comps * newWidth * newHeight * sizeof(GLubyte));
       if (!newSource) {
          GET_CURRENT_CONTEXT(ctx);
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture compression");
          goto cleanUp;
       }
-      _mesa_upscale_teximage2d(width, height, newWidth, newHeight,
-                               comps, (const GLchan *) source,
-                               srcRowStride, (GLchan *) newSource);
+      upscale_teximage2d(width, height, newWidth, newHeight,
+                         comps, (const GLubyte *) source,
+                         srcRowStride, (GLubyte *) newSource);
       source = newSource;
       width = newWidth;
       height = newHeight;
       srcRowStride = comps * newWidth;
    }
 
-   /* convert from 16/32-bit channels to GLubyte if needed */
-   if (CHAN_TYPE != GL_UNSIGNED_BYTE) {
-      const GLuint n = width * height * comps;
-      const GLchan *src = (const GLchan *) source;
-      GLubyte *dest = (GLubyte *) _mesa_malloc(n * sizeof(GLubyte));
-      GLuint i;
-      if (!dest) {
-         GET_CURRENT_CONTEXT(ctx);
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture compression");
-         goto cleanUp;
-      }
-      for (i = 0; i < n; i++) {
-         dest[i] = CHAN_TO_UBYTE(src[i]);
-      }
-      if (newSource != NULL) {
-         _mesa_free(newSource);
-      }
-      newSource = dest;  /* we'll free this buffer before returning */
-      source = dest;  /* the new, GLubyte incoming image */
-   }
-
    data = (const GLubyte *) source;
    destRowStride = (destRowStride - width * 2) / 4;
    for (y = 0; y < height; y += 4) {
@@ -1360,9 +1340,7 @@ fxt1_encode (GLuint width, GLuint height, GLint comps,
    }
 
  cleanUp:
-   if (newSource != NULL) {
-      _mesa_free(newSource);
-   }
+   free(newSource);
 }
 
 
@@ -1402,7 +1380,7 @@ static const GLubyte _rgb_scale_6[] = {
 
 
 static void
-fxt1_decode_1HI (const GLubyte *code, GLint t, GLchan *rgba)
+fxt1_decode_1HI (const GLubyte *code, GLint t, GLubyte *rgba)
 {
    const GLuint *cc;
 
@@ -1428,16 +1406,16 @@ fxt1_decode_1HI (const GLubyte *code, GLint t, GLchan *rgba)
          g = LERP(6, t, UP5(CC_SEL(cc, 5)), UP5(CC_SEL(cc, 20)));
          r = LERP(6, t, UP5(CC_SEL(cc, 10)), UP5(CC_SEL(cc, 25)));
       }
-      rgba[RCOMP] = UBYTE_TO_CHAN(r);
-      rgba[GCOMP] = UBYTE_TO_CHAN(g);
-      rgba[BCOMP] = UBYTE_TO_CHAN(b);
-      rgba[ACOMP] = CHAN_MAX;
+      rgba[RCOMP] = r;
+      rgba[GCOMP] = g;
+      rgba[BCOMP] = b;
+      rgba[ACOMP] = 255;
    }
 }
 
 
 static void
-fxt1_decode_1CHROMA (const GLubyte *code, GLint t, GLchan *rgba)
+fxt1_decode_1CHROMA (const GLubyte *code, GLint t, GLubyte *rgba)
 {
    const GLuint *cc;
    GLuint kk;
@@ -1452,15 +1430,15 @@ fxt1_decode_1CHROMA (const GLubyte *code, GLint t, GLchan *rgba)
    t *= 15;
    cc = (const GLuint *)(code + 8 + t / 8);
    kk = cc[0] >> (t & 7);
-   rgba[BCOMP] = UBYTE_TO_CHAN( UP5(kk) );
-   rgba[GCOMP] = UBYTE_TO_CHAN( UP5(kk >> 5) );
-   rgba[RCOMP] = UBYTE_TO_CHAN( UP5(kk >> 10) );
-   rgba[ACOMP] = CHAN_MAX;
+   rgba[BCOMP] = UP5(kk);
+   rgba[GCOMP] = UP5(kk >> 5);
+   rgba[RCOMP] = UP5(kk >> 10);
+   rgba[ACOMP] = 255;
 }
 
 
 static void
-fxt1_decode_1MIXED (const GLubyte *code, GLint t, GLchan *rgba)
+fxt1_decode_1MIXED (const GLubyte *code, GLint t, GLubyte *rgba)
 {
    const GLuint *cc;
    GLuint col[2][3];
@@ -1515,10 +1493,10 @@ fxt1_decode_1MIXED (const GLubyte *code, GLint t, GLchan *rgba)
             g = (UP5(col[0][GCOMP]) + UP6(col[1][GCOMP], glsb)) / 2;
             r = (UP5(col[0][RCOMP]) + UP5(col[1][RCOMP])) / 2;
          }
-         rgba[RCOMP] = UBYTE_TO_CHAN(r);
-         rgba[GCOMP] = UBYTE_TO_CHAN(g);
-         rgba[BCOMP] = UBYTE_TO_CHAN(b);
-         rgba[ACOMP] = CHAN_MAX;
+         rgba[RCOMP] = r;
+         rgba[GCOMP] = g;
+         rgba[BCOMP] = b;
+         rgba[ACOMP] = 255;
       }
    } else {
       /* alpha[0] == 0 */
@@ -1537,16 +1515,16 @@ fxt1_decode_1MIXED (const GLubyte *code, GLint t, GLchan *rgba)
                         UP6(col[1][GCOMP], glsb));
          r = LERP(3, t, UP5(col[0][RCOMP]), UP5(col[1][RCOMP]));
       }
-      rgba[RCOMP] = UBYTE_TO_CHAN(r);
-      rgba[GCOMP] = UBYTE_TO_CHAN(g);
-      rgba[BCOMP] = UBYTE_TO_CHAN(b);
-      rgba[ACOMP] = CHAN_MAX;
+      rgba[RCOMP] = r;
+      rgba[GCOMP] = g;
+      rgba[BCOMP] = b;
+      rgba[ACOMP] = 255;
    }
 }
 
 
 static void
-fxt1_decode_1ALPHA (const GLubyte *code, GLint t, GLchan *rgba)
+fxt1_decode_1ALPHA (const GLubyte *code, GLint t, GLubyte *rgba)
 {
    const GLuint *cc;
    GLubyte r, g, b, a;
@@ -1613,18 +1591,18 @@ fxt1_decode_1ALPHA (const GLubyte *code, GLint t, GLchan *rgba)
          r = UP5(kk >> 10);
       }
    }
-   rgba[RCOMP] = UBYTE_TO_CHAN(r);
-   rgba[GCOMP] = UBYTE_TO_CHAN(g);
-   rgba[BCOMP] = UBYTE_TO_CHAN(b);
-   rgba[ACOMP] = UBYTE_TO_CHAN(a);
+   rgba[RCOMP] = r;
+   rgba[GCOMP] = g;
+   rgba[BCOMP] = b;
+   rgba[ACOMP] = a;
 }
 
 
-void
+static void
 fxt1_decode_1 (const void *texture, GLint stride, /* in pixels */
-               GLint i, GLint j, GLchan *rgba)
+               GLint i, GLint j, GLubyte *rgba)
 {
-   static void (*decode_1[]) (const GLubyte *, GLint, GLchan *) = {
+   static void (*decode_1[]) (const GLubyte *, GLint, GLubyte *) = {
       fxt1_decode_1HI,     /* cc-high   = "00?" */
       fxt1_decode_1HI,     /* cc-high   = "00?" */
       fxt1_decode_1CHROMA, /* cc-chroma = "010" */
@@ -1649,4 +1627,43 @@ fxt1_decode_1 (const void *texture, GLint stride, /* in pixels */
 }
 
 
-#endif /* FEATURE_texture_fxt1 */
+
+
+static void
+fetch_rgb_fxt1(const GLubyte *map,
+               GLint rowStride, GLint i, GLint j, GLfloat *texel)
+{
+   GLubyte rgba[4];
+   fxt1_decode_1(map, rowStride, i, j, rgba);
+   texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
+   texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
+   texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
+   texel[ACOMP] = 1.0F;
+}
+
+
+static void
+fetch_rgba_fxt1(const GLubyte *map,
+                GLint rowStride, GLint i, GLint j, GLfloat *texel)
+{
+   GLubyte rgba[4];
+   fxt1_decode_1(map, rowStride, i, j, rgba);
+   texel[RCOMP] = UBYTE_TO_FLOAT(rgba[RCOMP]);
+   texel[GCOMP] = UBYTE_TO_FLOAT(rgba[GCOMP]);
+   texel[BCOMP] = UBYTE_TO_FLOAT(rgba[BCOMP]);
+   texel[ACOMP] = UBYTE_TO_FLOAT(rgba[ACOMP]);
+}
+
+
+compressed_fetch_func
+_mesa_get_fxt_fetch_func(mesa_format format)
+{
+   switch (format) {
+   case MESA_FORMAT_RGB_FXT1:
+      return fetch_rgb_fxt1;
+   case MESA_FORMAT_RGBA_FXT1:
+      return fetch_rgba_fxt1;
+   default:
+      return NULL;
+   }
+}