X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fmain%2Ftexcompress_etc.c;h=f29561cc61ad40df3522214902b885b2644d0e50;hb=34f741b0809a0d7bb5dbc262bbc3ff2eb743090f;hp=2aa8b6ad7232158d28e21d51f6747e643a1662fb;hpb=965a24995d1bfe5c22dfc201de6c2215a3df35a7;p=mesa.git diff --git a/src/mesa/main/texcompress_etc.c b/src/mesa/main/texcompress_etc.c index 2aa8b6ad723..f29561cc61a 100644 --- a/src/mesa/main/texcompress_etc.c +++ b/src/mesa/main/texcompress_etc.c @@ -27,22 +27,31 @@ * Supported ETC2 texture formats are: * GL_COMPRESSED_RGB8_ETC2 * GL_COMPRESSED_SRGB8_ETC2 + * GL_COMPRESSED_RGBA8_ETC2_EAC + * GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC + * GL_COMPRESSED_R11_EAC + * GL_COMPRESSED_RG11_EAC + * GL_COMPRESSED_SIGNED_R11_EAC + * GL_COMPRESSED_SIGNED_RG11_EAC + * MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1 + * MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1 */ #include -#include "mfeatures.h" #include "texcompress.h" #include "texcompress_etc.h" #include "texstore.h" #include "macros.h" -#include "swrast/s_context.h" #include "format_unpack.h" +#include "util/format_srgb.h" + struct etc2_block { int distance; - uint32_t pixel_indices; + uint64_t pixel_indices[2]; const int *modifier_tables[2]; bool flipped; + bool opaque; bool is_ind_mode; bool is_diff_mode; bool is_t_mode; @@ -50,11 +59,44 @@ 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}, +}; + +static const int etc2_modifier_tables_non_opaque[8][4] = { + { 0, 8, 0, -8}, + { 0, 17, 0, -17}, + { 0, 29, 0, -29}, + { 0, 42, 0, -42}, + { 0, 60, 0, -60}, + { 0, 80, 0, -80}, + { 0, 106, 0, -106}, + { 0, 183, 0, -183} +}; + /* define etc1_parse_block and etc. */ #define UINT8_TYPE GLubyte #define TAG(x) x @@ -66,30 +108,11 @@ GLboolean _mesa_texstore_etc1_rgb8(TEXSTORE_PARAMS) { /* GL_ETC1_RGB8_OES is only valid in glCompressedTexImage2D */ - ASSERT(0); + assert(0); return GL_FALSE; } -void -_mesa_fetch_texel_2d_f_etc1_rgb8(const struct swrast_texture_image *texImage, - GLint i, GLint j, GLint k, GLfloat *texel) -{ - struct etc1_block block; - GLubyte dst[3]; - const GLubyte *src; - - src = (const GLubyte *) texImage->Map + - (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; - - etc1_parse_block(&block, src); - etc1_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] = 1.0f; -} /** * Decode texture data in format `MESA_FORMAT_ETC1_RGB8` to @@ -189,7 +212,7 @@ etc2_base_color1_h_mode(const uint8_t *in, GLuint index) break; } return ((x << 4) | (x & 0xf)); - } +} static uint8_t etc2_base_color2_h_mode(const uint8_t *in, GLuint index) @@ -211,7 +234,7 @@ etc2_base_color2_h_mode(const uint8_t *in, GLuint index) break; } return ((x << 4) | (x & 0xf)); - } +} static uint8_t etc2_base_color_o_planar(const uint8_t *in, GLuint index) @@ -280,6 +303,14 @@ etc2_base_color_v_planar(const uint8_t *in, GLuint index) } } +static GLint +etc2_get_pixel_index(const struct etc2_block *block, int x, int y) +{ + int bit = ((3 - y) + (3 - x) * 4) * 3; + int idx = (block->pixel_indices[1] >> bit) & 0x7; + return idx; +} + static uint8_t etc2_clamp(int color) { @@ -287,11 +318,27 @@ etc2_clamp(int color) return (uint8_t) CLAMP(color, 0, 255); } +static GLushort +etc2_clamp2(int color) +{ + /* CLAMP(color, 0, 2047) */ + return (GLushort) CLAMP(color, 0, 2047); +} + +static GLshort +etc2_clamp3(int color) +{ + /* CLAMP(color, -1023, 1023) */ + return (GLshort) CLAMP(color, -1023, 1023); +} + static void -etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src) +etc2_rgb8_parse_block(struct etc2_block *block, + const uint8_t *src, + GLboolean punchthrough_alpha) { unsigned i; - GLboolean diffbit = src[3] & 0x2; + GLboolean diffbit = false; static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 }; const int R_plus_dR = (src[0] >> 3) + lookup[src[0] & 0x7]; @@ -305,7 +352,12 @@ etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src) block->is_h_mode = false; block->is_planar_mode = false; - if (!diffbit) { + if (punchthrough_alpha) + block->opaque = src[3] & 0x2; + else + diffbit = src[3] & 0x2; + + if (!diffbit && !punchthrough_alpha) { /* individual mode */ block->is_ind_mode = true; @@ -340,9 +392,10 @@ etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src) } } else if (G_plus_dG < 0 || G_plus_dG > 31){ + int base_color_1_value, base_color_2_value; + /* H mode */ block->is_h_mode = true; - int base_color_1_value, base_color_2_value; for(i = 0; i < 3; i++) { block->base_colors[0][i] = etc2_base_color1_h_mode(src, i); @@ -372,17 +425,20 @@ etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src) block->distance); } } - else if (B_plus_dB < 0 || B_plus_dB > 31){ + else if (B_plus_dB < 0 || B_plus_dB > 31) { /* Planar mode */ block->is_planar_mode = true; + /* opaque bit must be set in planar mode */ + block->opaque = true; + for (i = 0; i < 3; i++) { block->base_colors[0][i] = etc2_base_color_o_planar(src, i); block->base_colors[1][i] = etc2_base_color_h_planar(src, i); block->base_colors[2][i] = etc2_base_color_v_planar(src, i); } } - else if (diffbit) { + else if (diffbit || punchthrough_alpha) { /* differential mode */ block->is_diff_mode = true; @@ -392,33 +448,54 @@ etc2_rgb8_parse_block(struct etc2_block *block, const uint8_t *src) */ block->base_colors[0][i] = etc1_base_color_diff_hi(src[i]); block->base_colors[1][i] = etc1_base_color_diff_lo(src[i]); - } + } } if (block->is_ind_mode || block->is_diff_mode) { - /* pick modifier tables. same for etc1 & etc2 textures */ - block->modifier_tables[0] = etc1_modifier_tables[(src[3] >> 5) & 0x7]; - block->modifier_tables[1] = etc1_modifier_tables[(src[3] >> 2) & 0x7]; + int table1_idx = (src[3] >> 5) & 0x7; + int table2_idx = (src[3] >> 2) & 0x7; + + /* Use same modifier tables as for etc1 textures if opaque bit is set + * or if non punchthrough texture format + */ + block->modifier_tables[0] = (block->opaque || !punchthrough_alpha) ? + etc1_modifier_tables[table1_idx] : + etc2_modifier_tables_non_opaque[table1_idx]; + block->modifier_tables[1] = (block->opaque || !punchthrough_alpha) ? + etc1_modifier_tables[table2_idx] : + etc2_modifier_tables_non_opaque[table2_idx]; + block->flipped = (src[3] & 0x1); } - block->pixel_indices = + block->pixel_indices[0] = (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7]; } static void etc2_rgb8_fetch_texel(const struct etc2_block *block, - int x, int y, uint8_t *dst) + int x, int y, uint8_t *dst, + GLboolean punchthrough_alpha) { const uint8_t *base_color; int modifier, bit, idx, blk; /* 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) { + /* check for punchthrough_alpha format */ + if (punchthrough_alpha) { + if (!block->opaque && idx == 2) { + dst[0] = dst[1] = dst[2] = dst[3] = 0; + return; + } + else + dst[3] = 255; + } + /* Use pixel index and subblock to get the modifier */ blk = (block->flipped) ? (y >= 2) : (x >= 2); base_color = block->base_colors[blk]; @@ -429,6 +506,16 @@ etc2_rgb8_fetch_texel(const struct etc2_block *block, dst[2] = etc2_clamp(base_color[2] + modifier); } else if (block->is_t_mode || block->is_h_mode) { + /* check for punchthrough_alpha format */ + if (punchthrough_alpha) { + if (!block->opaque && idx == 2) { + dst[0] = dst[1] = dst[2] = dst[3] = 0; + return; + } + else + dst[3] = 255; + } + /* Use pixel index to pick one of the paint colors */ dst[0] = block->paint_colors[idx][0]; dst[1] = block->paint_colors[idx][1]; @@ -455,7 +542,127 @@ etc2_rgb8_fetch_texel(const struct etc2_block *block, dst[0] = etc2_clamp(red); dst[1] = etc2_clamp(green); dst[2] = etc2_clamp(blue); - } + + /* check for punchthrough_alpha format */ + if (punchthrough_alpha) + dst[3] = 255; + } +} + +static void +etc2_alpha8_fetch_texel(const struct etc2_block *block, + int x, int y, uint8_t *dst) +{ + int modifier, alpha, idx; + /* get pixel index */ + idx = etc2_get_pixel_index(block, x, y); + modifier = etc2_modifier_tables[block->table_index][idx]; + alpha = block->base_codeword + modifier * block->multiplier; + dst[3] = etc2_clamp(alpha); +} + +static void +etc2_r11_fetch_texel(const struct etc2_block *block, + int x, int y, uint8_t *dst) +{ + GLint modifier, idx; + GLshort color; + /* Get pixel index */ + idx = etc2_get_pixel_index(block, x, y); + modifier = etc2_modifier_tables[block->table_index][idx]; + + if (block->multiplier != 0) + /* clamp2(base codeword × 8 + 4 + modifier × multiplier × 8) */ + color = etc2_clamp2(((block->base_codeword << 3) | 0x4) + + ((modifier * block->multiplier) << 3)); + else + color = etc2_clamp2(((block->base_codeword << 3) | 0x4) + modifier); + + /* Extend 11 bits color value to 16 bits. OpenGL ES 3.0 specification + * allows extending the color value to any number of bits. But, an + * implementation is not allowed to truncate the 11-bit value to less than + * 11 bits." + */ + color = (color << 5) | (color >> 6); + ((GLushort *)dst)[0] = color; +} + +static void +etc2_signed_r11_fetch_texel(const struct etc2_block *block, + int x, int y, uint8_t *dst) +{ + GLint modifier, idx; + GLshort color; + GLbyte base_codeword = (GLbyte) block->base_codeword; + + if (base_codeword == -128) + base_codeword = -127; + + /* Get pixel index */ + idx = etc2_get_pixel_index(block, x, y); + modifier = etc2_modifier_tables[block->table_index][idx]; + + if (block->multiplier != 0) + /* clamp3(base codeword × 8 + modifier × multiplier × 8) */ + color = etc2_clamp3((base_codeword << 3) + + ((modifier * block->multiplier) << 3)); + else + color = etc2_clamp3((base_codeword << 3) + modifier); + + /* Extend 11 bits color value to 16 bits. OpenGL ES 3.0 specification + * allows extending the color value to any number of bits. But, an + * implementation is not allowed to truncate the 11-bit value to less than + * 11 bits. A negative 11-bit value must first be made positive before bit + * replication, and then made negative again + */ + if (color >= 0) + color = (color << 5) | (color >> 5); + else { + color = -color; + color = (color << 5) | (color >> 5); + color = -color; + } + ((GLshort *)dst)[0] = color; +} + +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_r11_parse_block(struct etc2_block *block, const uint8_t *src) +{ + /* Parsing logic remains same as for etc2_alpha8_parse_block */ + etc2_alpha8_parse_block(block, src); +} + +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, + false /* punchthrough_alpha */); + /* 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, + false /* punchthrough_alpha */); + etc2_alpha8_fetch_texel(block, x, y, dst); } static void @@ -472,14 +679,27 @@ etc2_unpack_rgb8(uint8_t *dst_row, for (y = 0; y < height; y += bh) { const uint8_t *src = src_row; + /* + * Destination texture may not be a multiple of four texels in + * height. Compute a safe height to avoid writing outside the texture. + */ + const unsigned h = MIN2(bh, height - y); for (x = 0; x < width; x+= bw) { - etc2_rgb8_parse_block(&block, src); + /* + * Destination texture may not be a multiple of four texels in + * width. Compute a safe width to avoid writing outside the texture. + */ + const unsigned w = MIN2(bw, width - x); + + etc2_rgb8_parse_block(&block, src, + false /* punchthrough_alpha */); - for (j = 0; j < bh; j++) { + for (j = 0; j < h; j++) { uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; - for (i = 0; i < bw; i++) { - etc2_rgb8_fetch_texel(&block, i, j, dst); + for (i = 0; i < w; i++) { + etc2_rgb8_fetch_texel(&block, i, j, dst, + false /* punchthrough_alpha */); dst[3] = 255; dst += comps; } @@ -507,15 +727,20 @@ etc2_unpack_srgb8(uint8_t *dst_row, for (y = 0; y < height; y += bh) { const uint8_t *src = src_row; + const unsigned h = MIN2(bh, height - y); for (x = 0; x < width; x+= bw) { - etc2_rgb8_parse_block(&block, src); + const unsigned w = MIN2(bw, width - x); + etc2_rgb8_parse_block(&block, src, + false /* punchthrough_alpha */); - for (j = 0; j < bh; j++) { + + for (j = 0; j < h; j++) { uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; - for (i = 0; i < bw; i++) { - etc2_rgb8_fetch_texel(&block, i, j, dst); - /* Convert to MESA_FORMAT_SARGB8 */ + for (i = 0; i < w; i++) { + etc2_rgb8_fetch_texel(&block, i, j, dst, + false /* punchthrough_alpha */); + /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */ tmp = dst[0]; dst[0] = dst[2]; dst[2] = tmp; @@ -525,10 +750,348 @@ etc2_unpack_srgb8(uint8_t *dst_row, } } src += bs; - } + } + + src_row += src_stride; + } +} + +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; + const unsigned h = MIN2(bh, height - y); + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_rgba8_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; + for (i = 0; i < w; i++) { + etc2_rgba8_fetch_texel(&block, i, j, dst); + dst += comps; + } + } + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_srgb8_alpha8(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + /* If internalformat is COMPRESSED_SRGB8_ALPHA8_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; + uint8_t tmp; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_rgba8_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; + for (i = 0; i < w; i++) { + etc2_rgba8_fetch_texel(&block, i, j, dst); + + /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */ + tmp = dst[0]; + dst[0] = dst[2]; + dst[2] = tmp; + dst[3] = dst[3]; + + dst += comps; + } + } + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_r11(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + /* If internalformat is COMPRESSED_R11_EAC, each 4 × 4 block of + color information is compressed to 64 bits. + */ + const unsigned bw = 4, bh = 4, bs = 8, comps = 1, comp_size = 2; + struct etc2_block block; + unsigned x, y, i, j; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_r11_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_r11_fetch_texel(&block, i, j, dst); + dst += comps * comp_size; + } + } + src += bs; + } src_row += src_stride; - } + } +} + +static void +etc2_unpack_rg11(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + /* If internalformat is COMPRESSED_RG11_EAC, each 4 × 4 block of + RG color information is compressed to 128 bits. + */ + const unsigned bw = 4, bh = 4, bs = 16, comps = 2, comp_size = 2; + struct etc2_block block; + unsigned x, y, i, j; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + /* red component */ + etc2_r11_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_r11_fetch_texel(&block, i, j, dst); + dst += comps * comp_size; + } + } + /* green component */ + etc2_r11_parse_block(&block, src + 8); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_r11_fetch_texel(&block, i, j, dst + comp_size); + dst += comps * comp_size; + } + } + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_signed_r11(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + /* If internalformat is COMPRESSED_SIGNED_R11_EAC, each 4 × 4 block of + red color information is compressed to 64 bits. + */ + const unsigned bw = 4, bh = 4, bs = 8, comps = 1, comp_size = 2; + struct etc2_block block; + unsigned x, y, i, j; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_r11_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_signed_r11_fetch_texel(&block, i, j, dst); + dst += comps * comp_size; + } + } + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_signed_rg11(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + /* If internalformat is COMPRESSED_SIGNED_RG11_EAC, each 4 × 4 block of + RG color information is compressed to 128 bits. + */ + const unsigned bw = 4, bh = 4, bs = 16, comps = 2, comp_size = 2; + struct etc2_block block; + unsigned x, y, i, j; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + /* red component */ + etc2_r11_parse_block(&block, src); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_signed_r11_fetch_texel(&block, i, j, dst); + dst += comps * comp_size; + } + } + /* green component */ + etc2_r11_parse_block(&block, src + 8); + + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + + x * comps * comp_size; + for (i = 0; i < w; i++) { + etc2_signed_r11_fetch_texel(&block, i, j, dst + comp_size); + dst += comps * comp_size; + } + } + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_rgb8_punchthrough_alpha1(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + const unsigned bw = 4, bh = 4, bs = 8, comps = 4; + struct etc2_block block; + unsigned x, y, i, j; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_rgb8_parse_block(&block, src, + true /* punchthrough_alpha */); + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; + for (i = 0; i < w; i++) { + etc2_rgb8_fetch_texel(&block, i, j, dst, + true /* punchthrough_alpha */); + dst += comps; + } + } + + src += bs; + } + + src_row += src_stride; + } +} + +static void +etc2_unpack_srgb8_punchthrough_alpha1(uint8_t *dst_row, + unsigned dst_stride, + const uint8_t *src_row, + unsigned src_stride, + unsigned width, + unsigned height) +{ + const unsigned bw = 4, bh = 4, bs = 8, comps = 4; + struct etc2_block block; + unsigned x, y, i, j; + uint8_t tmp; + + for (y = 0; y < height; y += bh) { + const unsigned h = MIN2(bh, height - y); + const uint8_t *src = src_row; + + for (x = 0; x < width; x+= bw) { + const unsigned w = MIN2(bw, width - x); + etc2_rgb8_parse_block(&block, src, + true /* punchthrough_alpha */); + for (j = 0; j < h; j++) { + uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; + for (i = 0; i < w; i++) { + etc2_rgb8_fetch_texel(&block, i, j, dst, + true /* punchthrough_alpha */); + /* Convert to MESA_FORMAT_B8G8R8A8_SRGB */ + tmp = dst[0]; + dst[0] = dst[2]; + dst[2] = tmp; + dst[3] = dst[3]; + + dst += comps; + } + } + + src += bs; + } + + src_row += src_stride; + } } /* ETC2 texture formats are valid in glCompressedTexImage2D and @@ -536,7 +1099,7 @@ etc2_unpack_srgb8(uint8_t *dst_row, GLboolean _mesa_texstore_etc2_rgb8(TEXSTORE_PARAMS) { - ASSERT(0); + assert(0); return GL_FALSE; } @@ -544,55 +1107,88 @@ _mesa_texstore_etc2_rgb8(TEXSTORE_PARAMS) GLboolean _mesa_texstore_etc2_srgb8(TEXSTORE_PARAMS) { - ASSERT(0); + 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) +GLboolean +_mesa_texstore_etc2_rgba8_eac(TEXSTORE_PARAMS) { - struct etc2_block block; - uint8_t dst[3]; - const uint8_t *src; + assert(0); - src = texImage->Map + - (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + return GL_FALSE; +} - etc2_rgb8_parse_block(&block, src); - etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst); +GLboolean +_mesa_texstore_etc2_srgb8_alpha8_eac(TEXSTORE_PARAMS) +{ + assert(0); - texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]); - texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]); - texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]); - texel[ACOMP] = 1.0f; + return GL_FALSE; } -void -_mesa_fetch_texel_2d_f_etc2_srgb8(const struct swrast_texture_image *texImage, - GLint i, GLint j, GLint k, GLfloat *texel) +GLboolean +_mesa_texstore_etc2_r11_eac(TEXSTORE_PARAMS) { - struct etc2_block block; - uint8_t dst[3]; - const uint8_t *src; + assert(0); - src = texImage->Map + - (((texImage->RowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + return GL_FALSE; +} - etc2_rgb8_parse_block(&block, src); - etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst); +GLboolean +_mesa_texstore_etc2_signed_r11_eac(TEXSTORE_PARAMS) +{ + assert(0); - texel[RCOMP] = _mesa_nonlinear_to_linear(dst[0]); - texel[GCOMP] = _mesa_nonlinear_to_linear(dst[1]); - texel[BCOMP] = _mesa_nonlinear_to_linear(dst[2]); - texel[ACOMP] = 1.0f; + return GL_FALSE; } +GLboolean +_mesa_texstore_etc2_rg11_eac(TEXSTORE_PARAMS) +{ + assert(0); + + return GL_FALSE; +} + +GLboolean +_mesa_texstore_etc2_signed_rg11_eac(TEXSTORE_PARAMS) +{ + assert(0); + + return GL_FALSE; +} + +GLboolean +_mesa_texstore_etc2_rgb8_punchthrough_alpha1(TEXSTORE_PARAMS) +{ + assert(0); + + return GL_FALSE; +} + +GLboolean +_mesa_texstore_etc2_srgb8_punchthrough_alpha1(TEXSTORE_PARAMS) +{ + assert(0); + + return GL_FALSE; +} + + /** * Decode texture data in any one of following formats: * `MESA_FORMAT_ETC2_RGB8` * `MESA_FORMAT_ETC2_SRGB8` + * `MESA_FORMAT_ETC2_RGBA8_EAC` + * `MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC` + * `MESA_FORMAT_ETC2_R11_EAC` + * `MESA_FORMAT_ETC2_RG11_EAC` + * `MESA_FORMAT_ETC2_SIGNED_R11_EAC` + * `MESA_FORMAT_ETC2_SIGNED_RG11_EAC` + * `MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1` + * `MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1` * * 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. @@ -609,7 +1205,7 @@ _mesa_unpack_etc2_format(uint8_t *dst_row, unsigned src_stride, unsigned src_width, unsigned src_height, - gl_format format) + mesa_format format) { if (format == MESA_FORMAT_ETC2_RGB8) etc2_unpack_rgb8(dst_row, dst_stride, @@ -619,4 +1215,299 @@ _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); + else if (format == MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC) + etc2_unpack_srgb8_alpha8(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_R11_EAC) + etc2_unpack_r11(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_RG11_EAC) + etc2_unpack_rg11(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_SIGNED_R11_EAC) + etc2_unpack_signed_r11(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_SIGNED_RG11_EAC) + etc2_unpack_signed_rg11(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1) + etc2_unpack_rgb8_punchthrough_alpha1(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); + else if (format == MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1) + etc2_unpack_srgb8_punchthrough_alpha1(dst_row, dst_stride, + src_row, src_stride, + src_width, src_height); +} + + + +static void +fetch_etc1_rgb8(const GLubyte *map, + GLint rowStride, GLint i, GLint j, + GLfloat *texel) +{ + struct etc1_block block; + GLubyte dst[3]; + const GLubyte *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc1_parse_block(&block, src); + etc1_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] = 1.0f; +} + + +static void +fetch_etc2_rgb8(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[3]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_rgb8_parse_block(&block, src, + false /* punchthrough_alpha */); + etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst, + false /* punchthrough_alpha */); + + texel[RCOMP] = UBYTE_TO_FLOAT(dst[0]); + texel[GCOMP] = UBYTE_TO_FLOAT(dst[1]); + texel[BCOMP] = UBYTE_TO_FLOAT(dst[2]); + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_srgb8(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[3]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_rgb8_parse_block(&block, src, + false /* punchthrough_alpha */); + etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst, + false /* punchthrough_alpha */); + + texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(dst[0]); + texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(dst[1]); + texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(dst[2]); + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_rgba8_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[4]; + const uint8_t *src; + + src = map + (((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]); +} + +static void +fetch_etc2_srgb8_alpha8_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[4]; + const uint8_t *src; + + src = map + (((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] = util_format_srgb_8unorm_to_linear_float(dst[0]); + texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(dst[1]); + texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(dst[2]); + texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]); +} + +static void +fetch_etc2_r11_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + GLushort dst; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_r11_parse_block(&block, src); + etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)&dst); + + texel[RCOMP] = USHORT_TO_FLOAT(dst); + texel[GCOMP] = 0.0f; + texel[BCOMP] = 0.0f; + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_rg11_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + GLushort dst[2]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16; + + /* red component */ + etc2_r11_parse_block(&block, src); + etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)dst); + + /* green component */ + etc2_r11_parse_block(&block, src + 8); + etc2_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)(dst + 1)); + + texel[RCOMP] = USHORT_TO_FLOAT(dst[0]); + texel[GCOMP] = USHORT_TO_FLOAT(dst[1]); + texel[BCOMP] = 0.0f; + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_signed_r11_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + GLushort dst; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_r11_parse_block(&block, src); + etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)&dst); + + texel[RCOMP] = SHORT_TO_FLOAT(dst); + texel[GCOMP] = 0.0f; + texel[BCOMP] = 0.0f; + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_signed_rg11_eac(const GLubyte *map, + GLint rowStride, GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + GLushort dst[2]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16; + + /* red component */ + etc2_r11_parse_block(&block, src); + etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)dst); + + /* green component */ + etc2_r11_parse_block(&block, src + 8); + etc2_signed_r11_fetch_texel(&block, i % 4, j % 4, (uint8_t *)(dst + 1)); + + texel[RCOMP] = SHORT_TO_FLOAT(dst[0]); + texel[GCOMP] = SHORT_TO_FLOAT(dst[1]); + texel[BCOMP] = 0.0f; + texel[ACOMP] = 1.0f; +} + +static void +fetch_etc2_rgb8_punchthrough_alpha1(const GLubyte *map, + GLint rowStride, GLint i, GLint j, + GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[4]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_rgb8_parse_block(&block, src, + true /* punchthrough alpha */); + etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst, + true /* punchthrough alpha */); + 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]); +} + +static void +fetch_etc2_srgb8_punchthrough_alpha1(const GLubyte *map, + GLint rowStride, + GLint i, GLint j, GLfloat *texel) +{ + struct etc2_block block; + uint8_t dst[4]; + const uint8_t *src; + + src = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 8; + + etc2_rgb8_parse_block(&block, src, + true /* punchthrough alpha */); + etc2_rgb8_fetch_texel(&block, i % 4, j % 4, dst, + true /* punchthrough alpha */); + texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(dst[0]); + texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(dst[1]); + texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(dst[2]); + texel[ACOMP] = UBYTE_TO_FLOAT(dst[3]); +} + + +compressed_fetch_func +_mesa_get_etc_fetch_func(mesa_format format) +{ + switch (format) { + case MESA_FORMAT_ETC1_RGB8: + return fetch_etc1_rgb8; + case MESA_FORMAT_ETC2_RGB8: + return fetch_etc2_rgb8; + case MESA_FORMAT_ETC2_SRGB8: + return fetch_etc2_srgb8; + case MESA_FORMAT_ETC2_RGBA8_EAC: + return fetch_etc2_rgba8_eac; + case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC: + return fetch_etc2_srgb8_alpha8_eac; + case MESA_FORMAT_ETC2_R11_EAC: + return fetch_etc2_r11_eac; + case MESA_FORMAT_ETC2_RG11_EAC: + return fetch_etc2_rg11_eac; + case MESA_FORMAT_ETC2_SIGNED_R11_EAC: + return fetch_etc2_signed_r11_eac; + case MESA_FORMAT_ETC2_SIGNED_RG11_EAC: + return fetch_etc2_signed_rg11_eac; + case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1: + return fetch_etc2_rgb8_punchthrough_alpha1; + case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: + return fetch_etc2_srgb8_punchthrough_alpha1; + default: + return NULL; + } }