Signed-off-by: Anuj Phogat <anuj.pho...@gmail.com> --- src/mesa/main/texcompress_etc.c | 145 +++++++++++++++++++++++++++++++++++++- src/mesa/main/texcompress_etc.h | 7 ++ 2 files changed, 148 insertions(+), 4 deletions(-)
diff --git a/src/mesa/main/texcompress_etc.c b/src/mesa/main/texcompress_etc.c index b217b6a..f531416 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 */ @@ -43,7 +44,7 @@ struct etc2_block { int flipped; int distance; - uint32_t pixel_indices; + uint64_t pixel_indices[2]; const int *modifier_tables[2]; GLboolean is_ind_mode; GLboolean is_diff_mode; @@ -52,11 +53,33 @@ struct etc2_block { GLboolean is_planar_mode; GLubyte base_colors[3][3]; GLubyte paint_colors[4][3]; + GLubyte base_codeword; + GLubyte multiplier; + GLubyte 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 @@ -404,7 +427,7 @@ etc2_rgb8_parse_block(struct etc2_block *block, const GLubyte *src) block->flipped = (src[3] & 0x1); } - block->pixel_indices = + block->pixel_indices[0] = (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7]; } @@ -417,8 +440,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 */ @@ -460,6 +483,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, GLubyte *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 GLubyte *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 GLubyte *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, GLubyte *dst) +{ + etc2_rgb8_fetch_texel(block, x, y, dst); + etc2_alpha8_fetch_texel(block, x, y, dst); +} + /** * Convert from sRGB color space to linear color space, using the * formula from the GL 3.0 spec, section 4.1.8 (sRGB Texture Color @@ -548,6 +616,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 @@ -566,6 +670,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) @@ -606,10 +718,31 @@ _mesa_fetch_texel_2d_f_etc2_srgb8(const struct swrast_texture_image *texImage, texel[ACOMP] = decode_srgb(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; + GLubyte dst[4]; + const GLubyte *src; + + src = (const GLubyte *) texImage->Map + + (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + 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. @@ -636,4 +769,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..2191c97 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); @@ -49,6 +52,9 @@ _mesa_fetch_texel_2d_f_etc2_rgb8(const struct swrast_texture_image *texImage, void _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, @@ -65,4 +71,5 @@ _mesa_unpack_etc2_format(uint8_t *dst_row, unsigned src_width, unsigned src_height, gl_format format); + #endif -- 1.7.7.6
_______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev