Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / util / u_gen_mipmap.c
index 2b675e71b2305ea39cb0b18dbeb3a14d53dbfe46..76023794dcdaecdacb0cea2a509c17bf0b037105 100644 (file)
 #include "pipe/p_shader_tokens.h"
 #include "pipe/p_state.h"
 
+#include "util/u_format.h"
 #include "util/u_memory.h"
 #include "util/u_draw_quad.h"
 #include "util/u_gen_mipmap.h"
 #include "util/u_simple_shaders.h"
-
-#include "tgsi/tgsi_build.h"
-#include "tgsi/tgsi_dump.h"
-#include "tgsi/tgsi_parse.h"
+#include "util/u_math.h"
+#include "util/u_texture.h"
 
 #include "cso_cache/cso_context.h"
 
@@ -62,12 +61,9 @@ struct gen_mipmap_state
    struct pipe_depth_stencil_alpha_state depthstencil;
    struct pipe_rasterizer_state rasterizer;
    struct pipe_sampler_state sampler;
-   struct pipe_viewport_state viewport;
 
-   struct pipe_shader_state vert_shader;
-   struct pipe_shader_state frag_shader;
    void *vs;
-   void *fs;
+   void *fs2d, *fsCube;
 
    struct pipe_buffer *vbuf;  /**< quad vertices */
    unsigned vbuf_slot;
@@ -79,15 +75,15 @@ struct gen_mipmap_state
 
 enum dtype
 {
-   UBYTE,
-   UBYTE_3_3_2,
-   USHORT,
-   USHORT_4_4_4_4,
-   USHORT_5_6_5,
-   USHORT_1_5_5_5_REV,
-   UINT,
-   FLOAT,
-   HALF_FLOAT
+   DTYPE_UBYTE,
+   DTYPE_UBYTE_3_3_2,
+   DTYPE_USHORT,
+   DTYPE_USHORT_4_4_4_4,
+   DTYPE_USHORT_5_6_5,
+   DTYPE_USHORT_1_5_5_5_REV,
+   DTYPE_UINT,
+   DTYPE_FLOAT,
+   DTYPE_HALF_FLOAT
 };
 
 
@@ -195,7 +191,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
    assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
    */
 
-   if (datatype == UBYTE && comps == 4) {
+   if (datatype == DTYPE_UBYTE && comps == 4) {
       uint i, j, k;
       const ubyte(*rowA)[4] = (const ubyte(*)[4]) srcRowA;
       const ubyte(*rowB)[4] = (const ubyte(*)[4]) srcRowB;
@@ -208,7 +204,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
       }
    }
-   else if (datatype == UBYTE && comps == 3) {
+   else if (datatype == DTYPE_UBYTE && comps == 3) {
       uint i, j, k;
       const ubyte(*rowA)[3] = (const ubyte(*)[3]) srcRowA;
       const ubyte(*rowB)[3] = (const ubyte(*)[3]) srcRowB;
@@ -220,7 +216,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
       }
    }
-   else if (datatype == UBYTE && comps == 2) {
+   else if (datatype == DTYPE_UBYTE && comps == 2) {
       uint i, j, k;
       const ubyte(*rowA)[2] = (const ubyte(*)[2]) srcRowA;
       const ubyte(*rowB)[2] = (const ubyte(*)[2]) srcRowB;
@@ -231,7 +227,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
       }
    }
-   else if (datatype == UBYTE && comps == 1) {
+   else if (datatype == DTYPE_UBYTE && comps == 1) {
       uint i, j, k;
       const ubyte *rowA = (const ubyte *) srcRowA;
       const ubyte *rowB = (const ubyte *) srcRowB;
@@ -242,7 +238,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
       }
    }
 
-   else if (datatype == USHORT && comps == 4) {
+   else if (datatype == DTYPE_USHORT && comps == 4) {
       uint i, j, k;
       const ushort(*rowA)[4] = (const ushort(*)[4]) srcRowA;
       const ushort(*rowB)[4] = (const ushort(*)[4]) srcRowB;
@@ -255,7 +251,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
       }
    }
-   else if (datatype == USHORT && comps == 3) {
+   else if (datatype == DTYPE_USHORT && comps == 3) {
       uint i, j, k;
       const ushort(*rowA)[3] = (const ushort(*)[3]) srcRowA;
       const ushort(*rowB)[3] = (const ushort(*)[3]) srcRowB;
@@ -267,7 +263,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
       }
    }
-   else if (datatype == USHORT && comps == 2) {
+   else if (datatype == DTYPE_USHORT && comps == 2) {
       uint i, j, k;
       const ushort(*rowA)[2] = (const ushort(*)[2]) srcRowA;
       const ushort(*rowB)[2] = (const ushort(*)[2]) srcRowB;
@@ -278,7 +274,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
       }
    }
-   else if (datatype == USHORT && comps == 1) {
+   else if (datatype == DTYPE_USHORT && comps == 1) {
       uint i, j, k;
       const ushort *rowA = (const ushort *) srcRowA;
       const ushort *rowB = (const ushort *) srcRowB;
@@ -289,7 +285,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
       }
    }
 
-   else if (datatype == FLOAT && comps == 4) {
+   else if (datatype == DTYPE_FLOAT && comps == 4) {
       uint i, j, k;
       const float(*rowA)[4] = (const float(*)[4]) srcRowA;
       const float(*rowB)[4] = (const float(*)[4]) srcRowB;
@@ -306,7 +302,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
                       rowB[j][3] + rowB[k][3]) * 0.25F;
       }
    }
-   else if (datatype == FLOAT && comps == 3) {
+   else if (datatype == DTYPE_FLOAT && comps == 3) {
       uint i, j, k;
       const float(*rowA)[3] = (const float(*)[3]) srcRowA;
       const float(*rowB)[3] = (const float(*)[3]) srcRowB;
@@ -321,7 +317,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
                       rowB[j][2] + rowB[k][2]) * 0.25F;
       }
    }
-   else if (datatype == FLOAT && comps == 2) {
+   else if (datatype == DTYPE_FLOAT && comps == 2) {
       uint i, j, k;
       const float(*rowA)[2] = (const float(*)[2]) srcRowA;
       const float(*rowB)[2] = (const float(*)[2]) srcRowB;
@@ -334,7 +330,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
                       rowB[j][1] + rowB[k][1]) * 0.25F;
       }
    }
-   else if (datatype == FLOAT && comps == 1) {
+   else if (datatype == DTYPE_FLOAT && comps == 1) {
       uint i, j, k;
       const float *rowA = (const float *) srcRowA;
       const float *rowB = (const float *) srcRowB;
@@ -346,7 +342,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
    }
 
 #if 0
-   else if (datatype == HALF_FLOAT && comps == 4) {
+   else if (datatype == HALF_DTYPE_FLOAT && comps == 4) {
       uint i, j, k, comp;
       const half_float(*rowA)[4] = (const half_float(*)[4]) srcRowA;
       const half_float(*rowB)[4] = (const half_float(*)[4]) srcRowB;
@@ -363,7 +359,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          }
       }
    }
-   else if (datatype == HALF_FLOAT && comps == 3) {
+   else if (datatype == DTYPE_HALF_FLOAT && comps == 3) {
       uint i, j, k, comp;
       const half_float(*rowA)[3] = (const half_float(*)[3]) srcRowA;
       const half_float(*rowB)[3] = (const half_float(*)[3]) srcRowB;
@@ -380,7 +376,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          }
       }
    }
-   else if (datatype == HALF_FLOAT && comps == 2) {
+   else if (datatype == DTYPE_HALF_FLOAT && comps == 2) {
       uint i, j, k, comp;
       const half_float(*rowA)[2] = (const half_float(*)[2]) srcRowA;
       const half_float(*rowB)[2] = (const half_float(*)[2]) srcRowB;
@@ -397,7 +393,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          }
       }
    }
-   else if (datatype == HALF_FLOAT && comps == 1) {
+   else if (datatype == DTYPE_HALF_FLOAT && comps == 1) {
       uint i, j, k;
       const half_float *rowA = (const half_float *) srcRowA;
       const half_float *rowB = (const half_float *) srcRowB;
@@ -414,7 +410,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
    }
 #endif
 
-   else if (datatype == UINT && comps == 1) {
+   else if (datatype == DTYPE_UINT && comps == 1) {
       uint i, j, k;
       const uint *rowA = (const uint *) srcRowA;
       const uint *rowB = (const uint *) srcRowB;
@@ -425,7 +421,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
       }
    }
 
-   else if (datatype == USHORT_5_6_5 && comps == 3) {
+   else if (datatype == DTYPE_USHORT_5_6_5 && comps == 3) {
       uint i, j, k;
       const ushort *rowA = (const ushort *) srcRowA;
       const ushort *rowB = (const ushort *) srcRowB;
@@ -450,7 +446,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (blue << 11) | (green << 5) | red;
       }
    }
-   else if (datatype == USHORT_4_4_4_4 && comps == 4) {
+   else if (datatype == DTYPE_USHORT_4_4_4_4 && comps == 4) {
       uint i, j, k;
       const ushort *rowA = (const ushort *) srcRowA;
       const ushort *rowB = (const ushort *) srcRowB;
@@ -480,7 +476,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
       }
    }
-   else if (datatype == USHORT_1_5_5_5_REV && comps == 4) {
+   else if (datatype == DTYPE_USHORT_1_5_5_5_REV && comps == 4) {
       uint i, j, k;
       const ushort *rowA = (const ushort *) srcRowA;
       const ushort *rowB = (const ushort *) srcRowB;
@@ -510,7 +506,7 @@ do_row(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
       }
    }
-   else if (datatype == UBYTE_3_3_2 && comps == 3) {
+   else if (datatype == DTYPE_UBYTE_3_3_2 && comps == 3) {
       uint i, j, k;
       const ubyte *rowA = (const ubyte *) srcRowA;
       const ubyte *rowB = (const ubyte *) srcRowB;
@@ -571,7 +567,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
    assert(comps >= 1);
    assert(comps <= 4);
 
-   if ((datatype == UBYTE) && (comps == 4)) {
+   if ((datatype == DTYPE_UBYTE) && (comps == 4)) {
       DECLARE_ROW_POINTERS(ubyte, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -582,7 +578,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(3);
       }
    }
-   else if ((datatype == UBYTE) && (comps == 3)) {
+   else if ((datatype == DTYPE_UBYTE) && (comps == 3)) {
       DECLARE_ROW_POINTERS(ubyte, 3);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -592,7 +588,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(2);
       }
    }
-   else if ((datatype == UBYTE) && (comps == 2)) {
+   else if ((datatype == DTYPE_UBYTE) && (comps == 2)) {
       DECLARE_ROW_POINTERS(ubyte, 2);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -601,7 +597,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(1);
       }
    }
-   else if ((datatype == UBYTE) && (comps == 1)) {
+   else if ((datatype == DTYPE_UBYTE) && (comps == 1)) {
       DECLARE_ROW_POINTERS(ubyte, 1);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -609,7 +605,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(0);
       }
    }
-   else if ((datatype == USHORT) && (comps == 4)) {
+   else if ((datatype == DTYPE_USHORT) && (comps == 4)) {
       DECLARE_ROW_POINTERS(ushort, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -620,7 +616,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(3);
       }
    }
-   else if ((datatype == USHORT) && (comps == 3)) {
+   else if ((datatype == DTYPE_USHORT) && (comps == 3)) {
       DECLARE_ROW_POINTERS(ushort, 3);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -630,7 +626,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(2);
       }
    }
-   else if ((datatype == USHORT) && (comps == 2)) {
+   else if ((datatype == DTYPE_USHORT) && (comps == 2)) {
       DECLARE_ROW_POINTERS(ushort, 2);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -639,7 +635,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(1);
       }
    }
-   else if ((datatype == USHORT) && (comps == 1)) {
+   else if ((datatype == DTYPE_USHORT) && (comps == 1)) {
       DECLARE_ROW_POINTERS(ushort, 1);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -647,7 +643,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_3D(0);
       }
    }
-   else if ((datatype == FLOAT) && (comps == 4)) {
+   else if ((datatype == DTYPE_FLOAT) && (comps == 4)) {
       DECLARE_ROW_POINTERS(float, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -658,7 +654,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_F_3D(3);
       }
    }
-   else if ((datatype == FLOAT) && (comps == 3)) {
+   else if ((datatype == DTYPE_FLOAT) && (comps == 3)) {
       DECLARE_ROW_POINTERS(float, 3);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -668,7 +664,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_F_3D(2);
       }
    }
-   else if ((datatype == FLOAT) && (comps == 2)) {
+   else if ((datatype == DTYPE_FLOAT) && (comps == 2)) {
       DECLARE_ROW_POINTERS(float, 2);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -677,7 +673,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_F_3D(1);
       }
    }
-   else if ((datatype == FLOAT) && (comps == 1)) {
+   else if ((datatype == DTYPE_FLOAT) && (comps == 1)) {
       DECLARE_ROW_POINTERS(float, 1);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -685,7 +681,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_F_3D(0);
       }
    }
-   else if ((datatype == HALF_FLOAT) && (comps == 4)) {
+   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 4)) {
       DECLARE_ROW_POINTERS(half_float, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -696,7 +692,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_HF_3D(3);
       }
    }
-   else if ((datatype == HALF_FLOAT) && (comps == 3)) {
+   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 3)) {
       DECLARE_ROW_POINTERS(half_float, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -706,7 +702,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_HF_3D(2);
       }
    }
-   else if ((datatype == HALF_FLOAT) && (comps == 2)) {
+   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 2)) {
       DECLARE_ROW_POINTERS(half_float, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -715,7 +711,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_HF_3D(1);
       }
    }
-   else if ((datatype == HALF_FLOAT) && (comps == 1)) {
+   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 1)) {
       DECLARE_ROW_POINTERS(half_float, 4);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -723,7 +719,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          FILTER_HF_3D(0);
       }
    }
-   else if ((datatype == UINT) && (comps == 1)) {
+   else if ((datatype == DTYPE_UINT) && (comps == 1)) {
       const uint *rowA = (const uint *) srcRowA;
       const uint *rowB = (const uint *) srcRowB;
       const uint *rowC = (const uint *) srcRowC;
@@ -739,7 +735,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (float)((double) tmp * 0.125);
       }
    }
-   else if ((datatype == USHORT_5_6_5) && (comps == 3)) {
+   else if ((datatype == DTYPE_USHORT_5_6_5) && (comps == 3)) {
       DECLARE_ROW_POINTERS0(ushort);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -777,7 +773,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (b << 11) | (g << 5) | r;
       }
    }
-   else if ((datatype == USHORT_4_4_4_4) && (comps == 4)) {
+   else if ((datatype == DTYPE_USHORT_4_4_4_4) && (comps == 4)) {
       DECLARE_ROW_POINTERS0(ushort);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -826,7 +822,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
       }
    }
-   else if ((datatype == USHORT_1_5_5_5_REV) && (comps == 4)) {
+   else if ((datatype == DTYPE_USHORT_1_5_5_5_REV) && (comps == 4)) {
       DECLARE_ROW_POINTERS0(ushort);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -875,7 +871,7 @@ do_row_3D(enum dtype datatype, uint comps, int srcWidth,
          dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
       }
    }
-   else if ((datatype == UBYTE_3_3_2) && (comps == 3)) {
+   else if ((datatype == DTYPE_UBYTE_3_3_2) && (comps == 3)) {
       DECLARE_ROW_POINTERS0(ushort);
 
       for (i = j = 0, k = k0; i < (uint) dstWidth;
@@ -924,39 +920,49 @@ static void
 format_to_type_comps(enum pipe_format pformat,
                      enum dtype *datatype, uint *comps)
 {
+   /* XXX I think this could be implemented in terms of the pf_*() functions */
    switch (pformat) {
    case PIPE_FORMAT_A8R8G8B8_UNORM:
    case PIPE_FORMAT_X8R8G8B8_UNORM:
    case PIPE_FORMAT_B8G8R8A8_UNORM:
    case PIPE_FORMAT_B8G8R8X8_UNORM:
-      *datatype = UBYTE;
+   case PIPE_FORMAT_R8G8B8A8_SRGB:
+   case PIPE_FORMAT_R8G8B8X8_SRGB:
+   case PIPE_FORMAT_A8R8G8B8_SRGB:
+   case PIPE_FORMAT_X8R8G8B8_SRGB:
+   case PIPE_FORMAT_B8G8R8A8_SRGB:
+   case PIPE_FORMAT_B8G8R8X8_SRGB:
+   case PIPE_FORMAT_R8G8B8_SRGB:
+      *datatype = DTYPE_UBYTE;
       *comps = 4;
       return;
    case PIPE_FORMAT_A1R5G5B5_UNORM:
-      *datatype = USHORT_1_5_5_5_REV;
+      *datatype = DTYPE_USHORT_1_5_5_5_REV;
       *comps = 4;
       return;
    case PIPE_FORMAT_A4R4G4B4_UNORM:
-      *datatype = USHORT_4_4_4_4;
+      *datatype = DTYPE_USHORT_4_4_4_4;
       *comps = 4;
       return;
    case PIPE_FORMAT_R5G6B5_UNORM:
-      *datatype = USHORT_5_6_5;
+      *datatype = DTYPE_USHORT_5_6_5;
       *comps = 3;
       return;
    case PIPE_FORMAT_L8_UNORM:
+   case PIPE_FORMAT_L8_SRGB:
    case PIPE_FORMAT_A8_UNORM:
    case PIPE_FORMAT_I8_UNORM:
-      *datatype = UBYTE;
+      *datatype = DTYPE_UBYTE;
       *comps = 1;
       return;
    case PIPE_FORMAT_A8L8_UNORM:
-      *datatype = UBYTE;
+   case PIPE_FORMAT_A8L8_SRGB:
+      *datatype = DTYPE_UBYTE;
       *comps = 2;
       return;
    default:
       assert(0);
-      *datatype = UBYTE;
+      *datatype = DTYPE_UBYTE;
       *comps = 0;
       break;
    }
@@ -992,7 +998,7 @@ reduce_2d(enum pipe_format pformat,
 {
    enum dtype datatype;
    uint comps;
-   const int bpt = pf_get_size(pformat);
+   const int bpt = util_format_get_blocksize(pformat);
    const ubyte *srcA, *srcB;
    ubyte *dst;
    int row;
@@ -1031,7 +1037,7 @@ reduce_3d(enum pipe_format pformat,
           int dstWidth, int dstHeight, int dstDepth,
           int dstRowStride, ubyte *dstPtr)
 {
-   const int bpt = pf_get_size(pformat);
+   const int bpt = util_format_get_blocksize(pformat);
    const int border = 0;
    int img, row;
    int bytesPerSrcImage, bytesPerDstImage;
@@ -1122,12 +1128,12 @@ make_1d_mipmap(struct gen_mipmap_state *ctx,
       
       srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
                                           PIPE_TRANSFER_READ, 0, 0,
-                                          pt->width[srcLevel],
-                                          pt->height[srcLevel]);
+                                          u_minify(pt->width0, srcLevel),
+                                          u_minify(pt->height0, srcLevel));
       dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
                                           PIPE_TRANSFER_WRITE, 0, 0,
-                                          pt->width[dstLevel],
-                                          pt->height[dstLevel]);
+                                          u_minify(pt->width0, dstLevel),
+                                          u_minify(pt->height0, dstLevel));
 
       srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
       dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
@@ -1155,8 +1161,8 @@ make_2d_mipmap(struct gen_mipmap_state *ctx,
    const uint zslice = 0;
    uint dstLevel;
    
-   assert(pt->block.width == 1);
-   assert(pt->block.height == 1);
+   assert(util_format_get_blockwidth(pt->format) == 1);
+   assert(util_format_get_blockheight(pt->format) == 1);
 
    for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
       const uint srcLevel = dstLevel - 1;
@@ -1165,12 +1171,12 @@ make_2d_mipmap(struct gen_mipmap_state *ctx,
       
       srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
                                           PIPE_TRANSFER_READ, 0, 0,
-                                          pt->width[srcLevel],
-                                          pt->height[srcLevel]);
+                                          u_minify(pt->width0, srcLevel),
+                                          u_minify(pt->height0, srcLevel));
       dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
                                           PIPE_TRANSFER_WRITE, 0, 0,
-                                          pt->width[dstLevel],
-                                          pt->height[dstLevel]);
+                                          u_minify(pt->width0, dstLevel),
+                                          u_minify(pt->height0, dstLevel));
 
       srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
       dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
@@ -1200,8 +1206,8 @@ make_3d_mipmap(struct gen_mipmap_state *ctx,
    struct pipe_screen *screen = pipe->screen;
    uint dstLevel, zslice = 0;
 
-   assert(pt->block.width == 1);
-   assert(pt->block.height == 1);
+   assert(util_format_get_blockwidth(pt->format) == 1);
+   assert(util_format_get_blockheight(pt->format) == 1);
 
    for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
       const uint srcLevel = dstLevel - 1;
@@ -1210,12 +1216,12 @@ make_3d_mipmap(struct gen_mipmap_state *ctx,
       
       srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
                                           PIPE_TRANSFER_READ, 0, 0,
-                                          pt->width[srcLevel],
-                                          pt->height[srcLevel]);
+                                          u_minify(pt->width0, srcLevel),
+                                          u_minify(pt->height0, srcLevel));
       dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
                                           PIPE_TRANSFER_WRITE, 0, 0,
-                                          pt->width[dstLevel],
-                                          pt->height[dstLevel]);
+                                          u_minify(pt->width0, dstLevel),
+                                          u_minify(pt->height0, dstLevel));
 
       srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
       dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
@@ -1281,10 +1287,6 @@ util_create_gen_mipmap(struct pipe_context *pipe,
 
    /* disabled blending/masking */
    memset(&ctx->blend, 0, sizeof(ctx->blend));
-   ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
-   ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
-   ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
-   ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
    ctx->blend.colormask = PIPE_MASK_RGBA;
 
    /* no-op depth/stencil/alpha */
@@ -1294,8 +1296,7 @@ util_create_gen_mipmap(struct pipe_context *pipe,
    memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
    ctx->rasterizer.front_winding = PIPE_WINDING_CW;
    ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
-   ctx->rasterizer.bypass_clipping = 1;
-   /*ctx->rasterizer.bypass_vs = 1;*/
+   ctx->rasterizer.bypass_vs_clip_and_viewport = 1;
    ctx->rasterizer.gl_rasterization_rules = 1;
 
    /* sampler state */
@@ -1306,34 +1307,25 @@ util_create_gen_mipmap(struct pipe_context *pipe,
    ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
    ctx->sampler.normalized_coords = 1;
 
-   /* viewport state (identity, verts are in wincoords) */
-   ctx->viewport.scale[0] = 1.0;
-   ctx->viewport.scale[1] = 1.0;
-   ctx->viewport.scale[2] = 1.0;
-   ctx->viewport.scale[3] = 1.0;
-   ctx->viewport.translate[0] = 0.0;
-   ctx->viewport.translate[1] = 0.0;
-   ctx->viewport.translate[2] = 0.0;
-   ctx->viewport.translate[3] = 0.0;
-
-   /* vertex shader */
+   /* vertex shader - still needed to specify mapping from fragment
+    * shader input semantics to vertex elements 
+    */
    {
       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
                                       TGSI_SEMANTIC_GENERIC };
       const uint semantic_indexes[] = { 0, 0 };
       ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
-                                                    semantic_indexes,
-                                                    &ctx->vert_shader);
+                                                    semantic_indexes);
    }
 
    /* fragment shader */
-   ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader);
+   ctx->fs2d = util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
+   ctx->fsCube = util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
 
    /* vertex data that doesn't change */
    for (i = 0; i < 4; i++) {
       ctx->vertices[i][0][2] = 0.0f; /* z */
       ctx->vertices[i][0][3] = 1.0f; /* w */
-      ctx->vertices[i][1][2] = 0.0f; /* r */
       ctx->vertices[i][1][3] = 1.0f; /* q */
    }
 
@@ -1367,29 +1359,55 @@ get_next_slot(struct gen_mipmap_state *ctx)
 
 
 static unsigned
-set_vertex_data(struct gen_mipmap_state *ctx, float width, float height)
+set_vertex_data(struct gen_mipmap_state *ctx,
+                enum pipe_texture_target tex_target,
+                uint face, float width, float height)
 {
    unsigned offset;
 
+   /* vert[0].position */
    ctx->vertices[0][0][0] = 0.0f; /*x*/
    ctx->vertices[0][0][1] = 0.0f; /*y*/
-   ctx->vertices[0][1][0] = 0.0f; /*s*/
-   ctx->vertices[0][1][1] = 0.0f; /*t*/
 
+   /* vert[1].position */
    ctx->vertices[1][0][0] = width;
    ctx->vertices[1][0][1] = 0.0f;
-   ctx->vertices[1][1][0] = 1.0f;
-   ctx->vertices[1][1][1] = 0.0f;
 
+   /* vert[2].position */
    ctx->vertices[2][0][0] = width;
    ctx->vertices[2][0][1] = height;
-   ctx->vertices[2][1][0] = 1.0f;
-   ctx->vertices[2][1][1] = 1.0f;
 
+   /* vert[3].position */
    ctx->vertices[3][0][0] = 0.0f;
    ctx->vertices[3][0][1] = height;
-   ctx->vertices[3][1][0] = 0.0f;
-   ctx->vertices[3][1][1] = 1.0f;
+
+   /* Setup vertex texcoords.  This is a little tricky for cube maps. */
+   if (tex_target == PIPE_TEXTURE_CUBE) {
+      static const float st[4][2] = {
+         {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
+      };
+
+      util_map_texcoords2d_onto_cubemap(face, &st[0][0], 2,
+                                        &ctx->vertices[0][1][0], 8);
+   }
+   else {
+      /* 1D/2D */
+      ctx->vertices[0][1][0] = 0.0f; /*s*/
+      ctx->vertices[0][1][1] = 0.0f; /*t*/
+      ctx->vertices[0][1][2] = 0.0f; /*r*/
+
+      ctx->vertices[1][1][0] = 1.0f;
+      ctx->vertices[1][1][1] = 0.0f;
+      ctx->vertices[1][1][2] = 0.0f;
+
+      ctx->vertices[2][1][0] = 1.0f;
+      ctx->vertices[2][1][1] = 1.0f;
+      ctx->vertices[2][1][2] = 0.0f;
+
+      ctx->vertices[3][1][0] = 0.0f;
+      ctx->vertices[3][1][1] = 1.0f;
+      ctx->vertices[3][1][2] = 0.0f;
+   }
 
    offset = get_next_slot( ctx );
 
@@ -1410,10 +1428,8 @@ util_destroy_gen_mipmap(struct gen_mipmap_state *ctx)
    struct pipe_context *pipe = ctx->pipe;
 
    pipe->delete_vs_state(pipe, ctx->vs);
-   pipe->delete_fs_state(pipe, ctx->fs);
-
-   FREE((void*) ctx->vert_shader.tokens);
-   FREE((void*) ctx->frag_shader.tokens);
+   pipe->delete_fs_state(pipe, ctx->fs2d);
+   pipe->delete_fs_state(pipe, ctx->fsCube);
 
    pipe_buffer_reference(&ctx->vbuf, NULL);
 
@@ -1451,10 +1467,22 @@ util_gen_mipmap(struct gen_mipmap_state *ctx,
    struct pipe_context *pipe = ctx->pipe;
    struct pipe_screen *screen = pipe->screen;
    struct pipe_framebuffer_state fb;
+   void *fs = (pt->target == PIPE_TEXTURE_CUBE) ? ctx->fsCube : ctx->fs2d;
    uint dstLevel;
    uint zslice = 0;
    uint offset;
 
+   /* The texture object should have room for the levels which we're
+    * about to generate.
+    */
+   assert(lastLevel <= pt->last_level);
+
+   /* If this fails, why are we here? */
+   assert(lastLevel > baseLevel);
+
+   assert(filter == PIPE_TEX_FILTER_LINEAR ||
+          filter == PIPE_TEX_FILTER_NEAREST);
+
    /* check if we can render in the texture's format */
    if (!screen->is_format_supported(screen, pt->format, PIPE_TEXTURE_2D,
                                     PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
@@ -1471,15 +1499,13 @@ util_gen_mipmap(struct gen_mipmap_state *ctx,
    cso_save_framebuffer(ctx->cso);
    cso_save_fragment_shader(ctx->cso);
    cso_save_vertex_shader(ctx->cso);
-   cso_save_viewport(ctx->cso);
 
    /* bind our state */
    cso_set_blend(ctx->cso, &ctx->blend);
    cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
    cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
-   cso_set_viewport(ctx->cso, &ctx->viewport);
 
-   cso_set_fragment_shader_handle(ctx->cso, ctx->fs);
+   cso_set_fragment_shader_handle(ctx->cso, fs);
    cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
 
    /* init framebuffer state */
@@ -1505,8 +1531,8 @@ util_gen_mipmap(struct gen_mipmap_state *ctx,
        * Setup framebuffer / dest surface
        */
       fb.cbufs[0] = surf;
-      fb.width = pt->width[dstLevel];
-      fb.height = pt->height[dstLevel];
+      fb.width = u_minify(pt->width0, dstLevel);
+      fb.height = u_minify(pt->height0, dstLevel);
       cso_set_framebuffer(ctx->cso, &fb);
 
       /*
@@ -1523,10 +1549,12 @@ util_gen_mipmap(struct gen_mipmap_state *ctx,
 
       cso_set_sampler_textures(ctx->cso, 1, &pt);
 
-      /* quad coords in window coords (bypassing clipping, viewport mapping) */
+      /* quad coords in window coords (bypassing vs, clip and viewport) */
       offset = set_vertex_data(ctx,
-                               (float) pt->width[dstLevel],
-                               (float) pt->height[dstLevel]);
+                               pt->target,
+                               face,
+                               (float) u_minify(pt->width0, dstLevel),
+                               (float) u_minify(pt->height0, dstLevel));
 
       util_draw_vertex_buffer(ctx->pipe, 
                               ctx->vbuf,
@@ -1550,5 +1578,4 @@ util_gen_mipmap(struct gen_mipmap_state *ctx,
    cso_restore_framebuffer(ctx->cso);
    cso_restore_fragment_shader(ctx->cso);
    cso_restore_vertex_shader(ctx->cso);
-   cso_restore_viewport(ctx->cso);
 }