[Mesa-dev] [PATCH 05/13] mesa: Add decoding functions for GL_COMPRESSED_RGBA8_ETC2_EAC

Ian Romanick idr at freedesktop.org
Thu Dec 6 17:48:21 PST 2012


From: Anuj Phogat <anuj.phogat at gmail.com>

Data in GL_COMPRESSED_RGBA8_ETC2_EAC format is decoded and stored
in MESA_FORMAT_RGBA8888_REV.

Signed-off-by: Anuj Phogat <anuj.phogat at gmail.com>
Reviewed-by: Brian Paul <brianp at vmware.com>
---
 src/mesa/main/texcompress.c     |   2 +-
 src/mesa/main/texcompress_etc.c | 145 ++++++++++++++++++++++++++++++++++++++--
 src/mesa/main/texcompress_etc.h |   7 ++
 src/mesa/main/texstore.c        |   2 +-
 src/mesa/swrast/s_texfetch.c    |   2 +-
 5 files changed, 151 insertions(+), 7 deletions(-)

diff --git a/src/mesa/main/texcompress.c b/src/mesa/main/texcompress.c
index 3ed0e85..7dc337c 100644
--- a/src/mesa/main/texcompress.c
+++ b/src/mesa/main/texcompress.c
@@ -614,7 +614,7 @@ _mesa_decompress_image(gl_format format, GLuint width, GLuint height,
       fetch = _mesa_fetch_texel_2d_f_etc2_srgb8;
       break;
    case MESA_FORMAT_ETC2_RGBA8_EAC:
-      /* fetch = _mesa_fetch_texel_2d_f_etc2_rgba8_eac; -- not implemented yet */
+      fetch = _mesa_fetch_texel_2d_f_etc2_rgba8_eac;
       break;
    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
       /* fetch = _mesa_fetch_texel_2d_f_etc2_srgb8_alpha8_eac; -- not implemented yet */
diff --git a/src/mesa/main/texcompress_etc.c b/src/mesa/main/texcompress_etc.c
index 2aa8b6a..26375d8 100644
--- a/src/mesa/main/texcompress_etc.c
+++ b/src/mesa/main/texcompress_etc.c
@@ -27,6 +27,7 @@
  * Supported ETC2 texture formats are:
  * GL_COMPRESSED_RGB8_ETC2
  * GL_COMPRESSED_SRGB8_ETC2
+ * GL_COMPRESSED_RGBA8_ETC2_EAC
  */
 
 #include <stdbool.h>
@@ -40,7 +41,7 @@
 
 struct etc2_block {
    int distance;
-   uint32_t pixel_indices;
+   uint64_t pixel_indices[2];
    const int *modifier_tables[2];
    bool flipped;
    bool is_ind_mode;
@@ -50,11 +51,33 @@ struct etc2_block {
    bool is_planar_mode;
    uint8_t base_colors[3][3];
    uint8_t paint_colors[4][3];
+   uint8_t base_codeword;
+   uint8_t multiplier;
+   uint8_t table_index;
 };
 
 static const int etc2_distance_table[8] = {
    3, 6, 11, 16, 23, 32, 41, 64 };
 
+static const int etc2_modifier_tables[16][8] = {
+   {  -3,   -6,   -9,  -15,   2,   5,   8,   14},
+   {  -3,   -7,  -10,  -13,   2,   6,   9,   12},
+   {  -2,   -5,   -8,  -13,   1,   4,   7,   12},
+   {  -2,   -4,   -6,  -13,   1,   3,   5,   12},
+   {  -3,   -6,   -8,  -12,   2,   5,   7,   11},
+   {  -3,   -7,   -9,  -11,   2,   6,   8,   10},
+   {  -4,   -7,   -8,  -11,   3,   6,   7,   10},
+   {  -3,   -5,   -8,  -11,   2,   4,   7,   10},
+   {  -2,   -6,   -8,  -10,   1,   5,   7,    9},
+   {  -2,   -5,   -8,  -10,   1,   4,   7,    9},
+   {  -2,   -4,   -8,  -10,   1,   3,   7,    9},
+   {  -2,   -5,   -7,  -10,   1,   4,   6,    9},
+   {  -3,   -4,   -7,  -10,   2,   3,   6,    9},
+   {  -1,   -2,   -3,  -10,   0,   1,   2,    9},
+   {  -4,   -6,   -8,   -9,   3,   5,   7,    8},
+   {  -3,   -5,   -7,   -9,   2,   4,   6,    8},
+};
+
 /* define etc1_parse_block and etc. */
 #define UINT8_TYPE GLubyte
 #define TAG(x) x
@@ -402,7 +425,7 @@ etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src)
       block->flipped = (src[3] & 0x1);
    }
 
-   block->pixel_indices =
+   block->pixel_indices[0] =
       (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7];
 }
 
@@ -415,8 +438,8 @@ etc2_rgb8_fetch_texel(const struct etc2_block *block,
 
    /* get pixel index */
    bit = y + x * 4;
-   idx = ((block->pixel_indices >> (15 + bit)) & 0x2) |
-         ((block->pixel_indices >>      (bit)) & 0x1);
+   idx = ((block->pixel_indices[0] >> (15 + bit)) & 0x2) |
+         ((block->pixel_indices[0] >>      (bit)) & 0x1);
 
    if (block->is_ind_mode || block->is_diff_mode) {
       /* Use pixel index and subblock to get the modifier */
@@ -459,6 +482,51 @@ etc2_rgb8_fetch_texel(const struct etc2_block *block,
 }
 
 static void
+etc2_alpha8_fetch_texel(const struct etc2_block *block,
+      int x, int y, uint8_t *dst)
+{
+   int modifier, alpha, bit, idx;
+   /* get pixel index */
+   bit = ((3 - y) + (3 - x) * 4) * 3;
+   idx = (block->pixel_indices[1] >> bit) & 0x7;
+   modifier = etc2_modifier_tables[block->table_index][idx];
+   alpha = block->base_codeword + modifier * block->multiplier;
+   dst[3] = etc2_clamp(alpha);
+}
+
+static void
+etc2_alpha8_parse_block(struct etc2_block *block, const uint8_t *src)
+{
+   block->base_codeword = src[0];
+   block->multiplier = (src[1] >> 4) & 0xf;
+   block->table_index = src[1] & 0xf;
+   block->pixel_indices[1] = (((uint64_t)src[2] << 40) |
+                              ((uint64_t)src[3] << 32) |
+                              ((uint64_t)src[4] << 24) |
+                              ((uint64_t)src[5] << 16) |
+                              ((uint64_t)src[6] << 8)  |
+                              ((uint64_t)src[7]));
+}
+
+static void
+etc2_rgba8_parse_block(struct etc2_block *block, const uint8_t *src)
+{
+   /* RGB component is parsed the same way as for MESA_FORMAT_ETC2_RGB8 */
+   etc2_rgb8_parse_block(block, src + 8);
+
+   /* Parse Alpha component */
+   etc2_alpha8_parse_block(block, src);
+}
+
+static void
+etc2_rgba8_fetch_texel(const struct etc2_block *block,
+      int x, int y, uint8_t *dst)
+{
+   etc2_rgb8_fetch_texel(block, x, y, dst);
+   etc2_alpha8_fetch_texel(block, x, y, dst);
+}
+
+static void
 etc2_unpack_rgb8(uint8_t *dst_row,
                  unsigned dst_stride,
                  const uint8_t *src_row,
@@ -531,6 +599,42 @@ etc2_unpack_srgb8(uint8_t *dst_row,
     }
 }
 
+static void
+etc2_unpack_rgba8(uint8_t *dst_row,
+                  unsigned dst_stride,
+                  const uint8_t *src_row,
+                  unsigned src_stride,
+                  unsigned width,
+                  unsigned height)
+{
+   /* If internalformat is COMPRESSED_RGBA8_ETC2_EAC, each 4 × 4 block of
+    * RGBA8888 information is compressed to 128 bits. To decode a block, the
+    * two 64-bit integers int64bitAlpha and int64bitColor are calculated.
+   */
+   const unsigned bw = 4, bh = 4, bs = 16, comps = 4;
+   struct etc2_block block;
+   unsigned x, y, i, j;
+
+   for (y = 0; y < height; y += bh) {
+      const uint8_t *src = src_row;
+
+      for (x = 0; x < width; x+= bw) {
+         etc2_rgba8_parse_block(&block, src);
+
+         for (j = 0; j < bh; j++) {
+            uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps;
+            for (i = 0; i < bw; i++) {
+               etc2_rgba8_fetch_texel(&block, i, j, dst);
+               dst += comps;
+            }
+         }
+         src += bs;
+       }
+
+      src_row += src_stride;
+    }
+}
+
 /* ETC2 texture formats are valid in glCompressedTexImage2D and
  * glCompressedTexSubImage2D functions */
 GLboolean
@@ -549,6 +653,14 @@ _mesa_texstore_etc2_srgb8(TEXSTORE_PARAMS)
    return GL_FALSE;
 }
 
+GLboolean
+_mesa_texstore_etc2_rgba8_eac(TEXSTORE_PARAMS)
+{
+   ASSERT(0);
+
+   return GL_FALSE;
+}
+
 void
 _mesa_fetch_texel_2d_f_etc2_rgb8(const struct swrast_texture_image *texImage,
                                  GLint i, GLint j, GLint k, GLfloat *texel)
@@ -589,10 +701,31 @@ _mesa_fetch_texel_2d_f_etc2_srgb8(const struct swrast_texture_image *texImage,
    texel[ACOMP] = 1.0f;
 }
 
+void
+_mesa_fetch_texel_2d_f_etc2_rgba8_eac(const struct swrast_texture_image *texImage,
+                                      GLint i, GLint j, GLint k, GLfloat *texel)
+{
+   struct etc2_block block;
+   uint8_t dst[4];
+   const uint8_t *src;
+
+   src = texImage->Map +
+      (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;
+
+   etc2_rgba8_parse_block(&block, src);
+   etc2_rgba8_fetch_texel(&block, i % 4, j % 4, dst);
+
+   texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]);
+   texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]);
+   texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]);
+   texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]);
+}
+
 /**
  * Decode texture data in any one of following formats:
  * `MESA_FORMAT_ETC2_RGB8`
  * `MESA_FORMAT_ETC2_SRGB8`
+ * `MESA_FORMAT_ETC2_RGBA8_EAC`
  *
  * The size of the source data must be a multiple of the ETC2 block size
  * even if the texture image's dimensions are not aligned to 4.
@@ -619,4 +752,8 @@ _mesa_unpack_etc2_format(uint8_t *dst_row,
       etc2_unpack_srgb8(dst_row, dst_stride,
                         src_row, src_stride,
                         src_width, src_height);
+   else if (format == MESA_FORMAT_ETC2_RGBA8_EAC)
+      etc2_unpack_rgba8(dst_row, dst_stride,
+                        src_row, src_stride,
+                        src_width, src_height);
 }
diff --git a/src/mesa/main/texcompress_etc.h b/src/mesa/main/texcompress_etc.h
index 1dc1d5a..fd9d2d7 100644
--- a/src/mesa/main/texcompress_etc.h
+++ b/src/mesa/main/texcompress_etc.h
@@ -40,6 +40,9 @@ _mesa_texstore_etc2_rgb8(TEXSTORE_PARAMS);
 GLboolean
 _mesa_texstore_etc2_srgb8(TEXSTORE_PARAMS);
 
+GLboolean
+_mesa_texstore_etc2_rgba8_eac(TEXSTORE_PARAMS);
+
 void
 _mesa_fetch_texel_2d_f_etc1_rgb8(const struct swrast_texture_image *texImage,
                                  GLint i, GLint j, GLint k, GLfloat *texel);
@@ -51,6 +54,10 @@ _mesa_fetch_texel_2d_f_etc2_srgb8(const struct swrast_texture_image *texImage,
                                   GLint i, GLint j, GLint k, GLfloat *texel);
 
 void
+_mesa_fetch_texel_2d_f_etc2_rgba8_eac(const struct swrast_texture_image *texImage,
+                                      GLint i, GLint j, GLint k, GLfloat *texel);
+
+void
 _mesa_etc1_unpack_rgba8888(uint8_t *dst_row,
                            unsigned dst_stride,
                            const uint8_t *src_row,
diff --git a/src/mesa/main/texstore.c b/src/mesa/main/texstore.c
index 3bafd23..b5ef595 100644
--- a/src/mesa/main/texstore.c
+++ b/src/mesa/main/texstore.c
@@ -4126,7 +4126,7 @@ _mesa_get_texstore_func(gl_format format)
       table[MESA_FORMAT_ETC1_RGB8] = _mesa_texstore_etc1_rgb8;
       table[MESA_FORMAT_ETC2_RGB8] = _mesa_texstore_etc2_rgb8;
       table[MESA_FORMAT_ETC2_SRGB8] = _mesa_texstore_etc2_srgb8;
-      /* table[MESA_FORMAT_ETC2_RGBA8_EAC] = _mesa_texstore_etc2_rgba8_eac; -- not implemented yet */
+      table[MESA_FORMAT_ETC2_RGBA8_EAC] = _mesa_texstore_etc2_rgba8_eac;
       /* table[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC] = _mesa_texstore_etc2_srgb8_alpha8_eac; -- not implemented yet */
       /* table[MESA_FORMAT_ETC2_R11_EAC] = _mesa_texstore_etc2_r11_eac; -- not implemented yet */
       /* table[MESA_FORMAT_ETC2_RG11_EAC] = _mesa_texstore_etc2_rg11_eac; -- not implemented yet */
diff --git a/src/mesa/swrast/s_texfetch.c b/src/mesa/swrast/s_texfetch.c
index 66f282c..504d394 100644
--- a/src/mesa/swrast/s_texfetch.c
+++ b/src/mesa/swrast/s_texfetch.c
@@ -1043,7 +1043,7 @@ texfetch_funcs[] =
    {
       MESA_FORMAT_ETC2_RGBA8_EAC,
       NULL,
-      NULL /* _mesa_fetch_texel_2d_f_etc2_rgba8_eac -- not implemented yet */,
+      _mesa_fetch_texel_2d_f_etc2_rgba8_eac,
       NULL
    },
    {
-- 
1.7.11.7



More information about the mesa-dev mailing list