Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / main / texcompress_fxt1.c
index aa56d9c91a312970bb7081ced6035f14deafd398..fc151605c9e32319e9a05eba6f1dbeb206536969 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  7.1
  *
- * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2008  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"),
@@ -35,6 +35,7 @@
 #include "context.h"
 #include "convolve.h"
 #include "image.h"
+#include "mipmap.h"
 #include "texcompress.h"
 #include "texformat.h"
 #include "texstore.h"
@@ -45,7 +46,7 @@ fxt1_encode (GLuint width, GLuint height, GLint comps,
              const void *source, GLint srcRowStride,
              void *dest, GLint destRowStride);
 
-static void
+void
 fxt1_decode_1 (const void *texture, GLint stride,
                GLint i, GLint j, GLchan *rgba);
 
@@ -64,7 +65,7 @@ _mesa_init_texture_fxt1( GLcontext *ctx )
  * Called via TexFormat->StoreImage to store an RGB_FXT1 texture.
  */
 static GLboolean
-texstore_rgb_fxt1(STORE_PARAMS)
+texstore_rgb_fxt1(TEXSTORE_PARAMS)
 {
    const GLchan *pixels;
    GLint srcRowStride;
@@ -76,7 +77,8 @@ texstore_rgb_fxt1(STORE_PARAMS)
    ASSERT(dstXoffset % 8 == 0);
    ASSERT(dstYoffset % 4 == 0);
    ASSERT(dstZoffset     == 0);
-   (void) dstZoffset; (void) dstImageStride;
+   (void) dstZoffset;
+   (void) dstImageOffsets;
 
    if (srcFormat != GL_RGB ||
        srcType != CHAN_TYPE ||
@@ -103,7 +105,7 @@ texstore_rgb_fxt1(STORE_PARAMS)
    }
 
    dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
-                                        GL_COMPRESSED_RGB_FXT1_3DFX,
+                                        dstFormat->MesaFormat,
                                         texWidth, (GLubyte *) dstAddr);
 
    fxt1_encode(srcWidth, srcHeight, 3, pixels, srcRowStride,
@@ -120,7 +122,7 @@ texstore_rgb_fxt1(STORE_PARAMS)
  * Called via TexFormat->StoreImage to store an RGBA_FXT1 texture.
  */
 static GLboolean
-texstore_rgba_fxt1(STORE_PARAMS)
+texstore_rgba_fxt1(TEXSTORE_PARAMS)
 {
    const GLchan *pixels;
    GLint srcRowStride;
@@ -132,7 +134,8 @@ texstore_rgba_fxt1(STORE_PARAMS)
    ASSERT(dstXoffset % 8 == 0);
    ASSERT(dstYoffset % 4 == 0);
    ASSERT(dstZoffset     == 0);
-   (void) dstZoffset; (void) dstImageStride;
+   (void) dstZoffset;
+   (void) dstImageOffsets;
 
    if (srcFormat != GL_RGBA ||
        srcType != CHAN_TYPE ||
@@ -159,7 +162,7 @@ texstore_rgba_fxt1(STORE_PARAMS)
    }
 
    dst = _mesa_compressed_image_address(dstXoffset, dstYoffset, 0,
-                                        GL_COMPRESSED_RGBA_FXT1_3DFX,
+                                        dstFormat->MesaFormat,
                                         texWidth, (GLubyte *) dstAddr);
 
    fxt1_encode(srcWidth, srcHeight, 4, pixels, srcRowStride,
@@ -234,6 +237,7 @@ const struct gl_texture_format _mesa_texformat_rgb_fxt1 = {
    0,                                  /* IntensityBits */
    0,                                  /* IndexBits */
    0,                                  /* DepthBits */
+   0,                                  /* StencilBits */
    0,                                  /* TexelBytes */
    texstore_rgb_fxt1,                  /* StoreTexImageFunc */
    NULL, /*impossible*/                /* FetchTexel1D */
@@ -242,6 +246,7 @@ const struct gl_texture_format _mesa_texformat_rgb_fxt1 = {
    NULL, /*impossible*/                /* FetchTexel1Df */
    fetch_texel_2d_f_rgb_fxt1,          /* FetchTexel2Df */
    NULL, /*impossible*/                /* FetchTexel3Df */
+   NULL                                        /* StoreTexel */
 };
 
 const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
@@ -256,6 +261,7 @@ const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
    0,                                  /* IntensityBits */
    0,                                  /* IndexBits */
    0,                                  /* DepthBits */
+   0,                                  /* StencilBits */
    0,                                  /* TexelBytes */
    texstore_rgba_fxt1,                 /* StoreTexImageFunc */
    NULL, /*impossible*/                /* FetchTexel1D */
@@ -264,6 +270,7 @@ const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
    NULL, /*impossible*/                /* FetchTexel1Df */
    fetch_texel_2d_f_rgba_fxt1,                 /* FetchTexel2Df */
    NULL, /*impossible*/                /* FetchTexel3Df */
+   NULL                                        /* StoreTexel */
 };
 
 
@@ -291,17 +298,17 @@ const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
 /*
  * Define a 64-bit unsigned integer type and macros
  */
-#if defined(__GNUC__) && !defined(__cplusplus)
+#if 1
 
 #define FX64_NATIVE 1
 
-typedef unsigned long long Fx64;
+typedef uint64_t Fx64;
 
 #define FX64_MOV32(a, b) a = b
 #define FX64_OR32(a, b)  a |= b
 #define FX64_SHL(a, c)   a <<= c
 
-#else  /* !__GNUC__ */
+#else
 
 #define FX64_NATIVE 0
 
@@ -323,7 +330,7 @@ typedef struct {
        }                                               \
    } while (0)
 
-#endif /* !__GNUC__ */
+#endif
 
 
 #define F(i) (GLfloat)1 /* can be used to obtain an oblong metric: 0.30 / 0.59 / 0.11 */
@@ -745,44 +752,55 @@ fxt1_quantize_ALPHA1 (GLuint *cc,
    GLint minColL = 0, maxColL = 0;
    GLint minColR = 0, maxColR = 0;
    GLint sumL = 0, sumR = 0;
-
+   GLint nn_comp;
    /* Our solution here is to find the darkest and brightest colors in
     * the 4x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
-   minSum = 2000; /* big enough */
-   maxSum = -1; /* small enough */
-   for (k = 0; k < N_TEXELS / 2; k++) {
-      GLint sum = 0;
-      for (i = 0; i < n_comp; i++) {
-         sum += input[k][i];
-      }
-      if (minSum > sum) {
-         minSum = sum;
-         minColL = k;
-      }
-      if (maxSum < sum) {
-         maxSum = sum;
-         maxColL = k;
-      }
-      sumL += sum;
+   nn_comp = n_comp;
+   while ((minColL == maxColL) && nn_comp) {
+       minSum = 2000; /* big enough */
+       maxSum = -1; /* small enough */
+       for (k = 0; k < N_TEXELS / 2; k++) {
+           GLint sum = 0;
+           for (i = 0; i < nn_comp; i++) {
+               sum += input[k][i];
+           }
+           if (minSum > sum) {
+               minSum = sum;
+               minColL = k;
+           }
+           if (maxSum < sum) {
+               maxSum = sum;
+               maxColL = k;
+           }
+           sumL += sum;
+       }
+       
+       nn_comp--;
    }
-   minSum = 2000; /* big enough */
-   maxSum = -1; /* small enough */
-   for (; k < N_TEXELS; k++) {
-      GLint sum = 0;
-      for (i = 0; i < n_comp; i++) {
-         sum += input[k][i];
-      }
-      if (minSum > sum) {
-         minSum = sum;
-         minColR = k;
-      }
-      if (maxSum < sum) {
-         maxSum = sum;
-         maxColR = k;
-      }
-      sumR += sum;
+
+   nn_comp = n_comp;
+   while ((minColR == maxColR) && nn_comp) {
+       minSum = 2000; /* big enough */
+       maxSum = -1; /* small enough */
+       for (k = N_TEXELS / 2; k < N_TEXELS; k++) {
+           GLint sum = 0;
+           for (i = 0; i < nn_comp; i++) {
+               sum += input[k][i];
+           }
+           if (minSum > sum) {
+               minSum = sum;
+               minColR = k;
+           }
+           if (maxSum < sum) {
+               maxSum = sum;
+               maxColR = k;
+           }
+           sumR += sum;
+       }
+
+       nn_comp--;
    }
 
    /* choose the common vector (yuck!) */
@@ -1677,7 +1695,7 @@ fxt1_decode_1ALPHA (const GLubyte *code, GLint t, GLchan *rgba)
 }
 
 
-static void
+void
 fxt1_decode_1 (const void *texture, GLint stride, /* in pixels */
                GLint i, GLint j, GLchan *rgba)
 {