| Index: cc/resources/texture_compressor_etc1.cc
|
| diff --git a/cc/resources/texture_compressor_etc1.cc b/cc/resources/texture_compressor_etc1.cc
|
| index 61c4438c21862342f8cf0ca10b19a375364a581c..03055437be666d533f9ae99c651d2568fd8baab1 100644
|
| --- a/cc/resources/texture_compressor_etc1.cc
|
| +++ b/cc/resources/texture_compressor_etc1.cc
|
| @@ -20,194 +20,21 @@
|
|
|
| namespace {
|
|
|
| -template <typename T>
|
| -inline T clamp(T val, T min, T max) {
|
| - return val < min ? min : (val > max ? max : val);
|
| -}
|
| -
|
| -inline uint8_t round_to_5_bits(float val) {
|
| - return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31);
|
| -}
|
| -
|
| -inline uint8_t round_to_4_bits(float val) {
|
| - return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15);
|
| -}
|
| -
|
| -union Color {
|
| - struct BgraColorType {
|
| - uint8_t b;
|
| - uint8_t g;
|
| - uint8_t r;
|
| - uint8_t a;
|
| - } channels;
|
| - uint8_t components[4];
|
| - uint32_t bits;
|
| -};
|
| -
|
| -/*
|
| - * Codeword tables.
|
| - * See: Table 3.17.2
|
| - */
|
| -static const int16_t g_codeword_tables[8][4] = {{-8, -2, 2, 8},
|
| - {-17, -5, 5, 17},
|
| - {-29, -9, 9, 29},
|
| - {-42, -13, 13, 42},
|
| - {-60, -18, 18, 60},
|
| - {-80, -24, 24, 80},
|
| - {-106, -33, 33, 106},
|
| - {-183, -47, 47, 183}};
|
| -
|
| -/*
|
| - * Maps modifier indices to pixel index values.
|
| - * See: Table 3.17.3
|
| - */
|
| -static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1};
|
| -
|
| -/*
|
| - * The ETC1 specification index texels as follows:
|
| - *
|
| - * [a][e][i][m] [ 0][ 4][ 8][12]
|
| - * [b][f][j][n] <-> [ 1][ 5][ 9][13]
|
| - * [c][g][k][o] [ 2][ 6][10][14]
|
| - * [d][h][l][p] [ 3][ 7][11][15]
|
| - *
|
| - * However, when extracting sub blocks from BGRA data the natural array
|
| - * indexing order ends up different:
|
| - *
|
| - * vertical0: [a][e][b][f] horizontal0: [a][e][i][m]
|
| - * [c][g][d][h] [b][f][j][n]
|
| - * vertical1: [i][m][j][n] horizontal1: [c][g][k][o]
|
| - * [k][o][l][p] [d][h][l][p]
|
| - *
|
| - * In order to translate from the natural array indices in a sub block to the
|
| - * indices (number) used by specification and hardware we use this table.
|
| - */
|
| -static const uint8_t g_idx_to_num[4][8] = {
|
| - {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0.
|
| - {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1.
|
| - {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0.
|
| - {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1.
|
| -};
|
| -
|
| -inline void WriteColors444(uint8_t* block,
|
| - const Color& color0,
|
| - const Color& color1) {
|
| - block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4);
|
| - block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4);
|
| - block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4);
|
| -}
|
| -
|
| -inline void WriteColors555(uint8_t* block,
|
| - const Color& color0,
|
| - const Color& color1) {
|
| - // Table for conversion to 3-bit two complement format.
|
| - static const uint8_t two_compl_trans_table[8] = {
|
| - 4, // -4 (100b)
|
| - 5, // -3 (101b)
|
| - 6, // -2 (110b)
|
| - 7, // -1 (111b)
|
| - 0, // 0 (000b)
|
| - 1, // 1 (001b)
|
| - 2, // 2 (010b)
|
| - 3, // 3 (011b)
|
| - };
|
| -
|
| - int16_t delta_r =
|
| - static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3);
|
| - int16_t delta_g =
|
| - static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3);
|
| - int16_t delta_b =
|
| - static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3);
|
| - DCHECK(delta_r >= -4 && delta_r <= 3);
|
| - DCHECK(delta_g >= -4 && delta_g <= 3);
|
| - DCHECK(delta_b >= -4 && delta_b <= 3);
|
| -
|
| - block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4];
|
| - block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4];
|
| - block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4];
|
| -}
|
| -
|
| -inline void WriteCodewordTable(uint8_t* block,
|
| - uint8_t sub_block_id,
|
| - uint8_t table) {
|
| - DCHECK_LT(sub_block_id, 2);
|
| - DCHECK_LT(table, 8);
|
| -
|
| - uint8_t shift = (2 + (3 - sub_block_id * 3));
|
| - block[3] &= ~(0x07 << shift);
|
| - block[3] |= table << shift;
|
| -}
|
| -
|
| -inline void WritePixelData(uint8_t* block, uint32_t pixel_data) {
|
| - block[4] |= pixel_data >> 24;
|
| - block[5] |= (pixel_data >> 16) & 0xff;
|
| - block[6] |= (pixel_data >> 8) & 0xff;
|
| - block[7] |= pixel_data & 0xff;
|
| -}
|
| -
|
| -inline void WriteFlip(uint8_t* block, bool flip) {
|
| - block[3] &= ~0x01;
|
| - block[3] |= static_cast<uint8_t>(flip);
|
| -}
|
| -
|
| -inline void WriteDiff(uint8_t* block, bool diff) {
|
| - block[3] &= ~0x02;
|
| - block[3] |= static_cast<uint8_t>(diff) << 1;
|
| -}
|
| -
|
| -/**
|
| - * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is
|
| - * expanded to BGR888 as it would be in hardware after decompression. The
|
| - * actual 444-bit data is available in the four most significant bits of each
|
| - * channel.
|
| - */
|
| -inline Color MakeColor444(const float* bgr) {
|
| - uint8_t b4 = round_to_4_bits(bgr[0]);
|
| - uint8_t g4 = round_to_4_bits(bgr[1]);
|
| - uint8_t r4 = round_to_4_bits(bgr[2]);
|
| - Color bgr444;
|
| - bgr444.channels.b = (b4 << 4) | b4;
|
| - bgr444.channels.g = (g4 << 4) | g4;
|
| - bgr444.channels.r = (r4 << 4) | r4;
|
| - return bgr444;
|
| -}
|
| -
|
| -/**
|
| - * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is
|
| - * expanded to BGR888 as it would be in hardware after decompression. The
|
| - * actual 555-bit data is available in the five most significant bits of each
|
| - * channel.
|
| - */
|
| -inline Color MakeColor555(const float* bgr) {
|
| - uint8_t b5 = round_to_5_bits(bgr[0]);
|
| - uint8_t g5 = round_to_5_bits(bgr[1]);
|
| - uint8_t r5 = round_to_5_bits(bgr[2]);
|
| - Color bgr555;
|
| - bgr555.channels.b = (b5 << 3) | (b5 >> 2);
|
| - bgr555.channels.g = (g5 << 3) | (g5 >> 2);
|
| - bgr555.channels.r = (r5 << 3) | (r5 >> 2);
|
| - return bgr555;
|
| -}
|
| -
|
| -/**
|
| - * Constructs a color from a given base color and luminance value.
|
| - */
|
| -inline Color MakeColor(const Color& base, int16_t lum) {
|
| +// Constructs a color from a given base color and luminance value.
|
| +inline cc::Color MakeColor(const cc::Color& base, int16_t lum) {
|
| int b = static_cast<int>(base.channels.b) + lum;
|
| int g = static_cast<int>(base.channels.g) + lum;
|
| int r = static_cast<int>(base.channels.r) + lum;
|
| - Color color;
|
| - color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255));
|
| - color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255));
|
| - color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255));
|
| + cc::Color color;
|
| + color.channels.b = static_cast<uint8_t>(cc::clamp(b, 0, 255));
|
| + color.channels.g = static_cast<uint8_t>(cc::clamp(g, 0, 255));
|
| + color.channels.r = static_cast<uint8_t>(cc::clamp(r, 0, 255));
|
| return color;
|
| }
|
|
|
| -/**
|
| - * Calculates the error metric for two colors. A small error signals that the
|
| - * colors are similar to each other, a large error the signals the opposite.
|
| - */
|
| -inline uint32_t GetColorError(const Color& u, const Color& v) {
|
| +// Calculates the error metric for two colors. A small error signals that the
|
| +// colors are similar to each other, a large error the signals the opposite.
|
| +inline uint32_t GetColorError(const cc::Color& u, const cc::Color& v) {
|
| #ifdef USE_PERCEIVED_ERROR_METRIC
|
| float delta_b = static_cast<float>(u.channels.b) - v.channels.b;
|
| float delta_g = static_cast<float>(u.channels.g) - v.channels.g;
|
| @@ -223,7 +50,7 @@ inline uint32_t GetColorError(const Color& u, const Color& v) {
|
| #endif
|
| }
|
|
|
| -void GetAverageColor(const Color* src, float* avg_color) {
|
| +void GetAverageColor(const cc::Color* src, float* avg_color) {
|
| uint32_t sum_b = 0, sum_g = 0, sum_r = 0;
|
|
|
| for (unsigned int i = 0; i < 8; ++i) {
|
| @@ -239,8 +66,8 @@ void GetAverageColor(const Color* src, float* avg_color) {
|
| }
|
|
|
| void ComputeLuminance(uint8_t* block,
|
| - const Color* src,
|
| - const Color& base,
|
| + const cc::Color* src,
|
| + const cc::Color& base,
|
| int sub_block_id,
|
| const uint8_t* idx_to_num_tab) {
|
| uint32_t best_tbl_err = std::numeric_limits<uint32_t>::max();
|
| @@ -252,9 +79,9 @@ void ComputeLuminance(uint8_t* block,
|
| for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) {
|
| // Pre-compute all the candidate colors; combinations of the base color and
|
| // all available luminance values.
|
| - Color candidate_color[4]; // [modifier]
|
| + cc::Color candidate_color[4]; // [modifier]
|
| for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
|
| - int16_t lum = g_codeword_tables[tbl_idx][mod_idx];
|
| + int16_t lum = cc::g_codeword_tables[tbl_idx][mod_idx];
|
| candidate_color[mod_idx] = MakeColor(base, lum);
|
| }
|
|
|
| @@ -265,7 +92,7 @@ void ComputeLuminance(uint8_t* block,
|
| // smallest error.
|
| uint32_t best_mod_err = std::numeric_limits<uint32_t>::max();
|
| for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
|
| - const Color& color = candidate_color[mod_idx];
|
| + const cc::Color& color = candidate_color[mod_idx];
|
|
|
| uint32_t mod_err = GetColorError(src[i], color);
|
| if (mod_err < best_mod_err) {
|
| @@ -291,13 +118,13 @@ void ComputeLuminance(uint8_t* block,
|
| }
|
| }
|
|
|
| - WriteCodewordTable(block, sub_block_id, best_tbl_idx);
|
| + cc::WriteCodewordTable(block, sub_block_id, best_tbl_idx);
|
|
|
| uint32_t pix_data = 0;
|
|
|
| for (unsigned int i = 0; i < 8; ++i) {
|
| uint8_t mod_idx = best_mod_idx[best_tbl_idx][i];
|
| - uint8_t pix_idx = g_mod_to_pix[mod_idx];
|
| + uint8_t pix_idx = cc::g_mod_to_pix[mod_idx];
|
|
|
| uint32_t lsb = pix_idx & 0x1;
|
| uint32_t msb = pix_idx >> 1;
|
| @@ -308,7 +135,7 @@ void ComputeLuminance(uint8_t* block,
|
| pix_data |= lsb << (texel_num);
|
| }
|
|
|
| - WritePixelData(block, pix_data);
|
| + cc::WritePixelData(block, pix_data);
|
| }
|
|
|
| /**
|
| @@ -316,7 +143,7 @@ void ComputeLuminance(uint8_t* block,
|
| * block. If it's not the function will bail out without writing anything to
|
| * the destination buffer.
|
| */
|
| -bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
|
| +bool TryCompressSolidBlock(uint8_t* dst, const cc::Color* src) {
|
| for (unsigned int i = 1; i < 16; ++i) {
|
| if (src[i].bits != src[0].bits)
|
| return false;
|
| @@ -328,11 +155,11 @@ bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
|
| float src_color_float[3] = {static_cast<float>(src->channels.b),
|
| static_cast<float>(src->channels.g),
|
| static_cast<float>(src->channels.r)};
|
| - Color base = MakeColor555(src_color_float);
|
| + cc::Color base = cc::MakeColor555(src_color_float);
|
|
|
| - WriteDiff(dst, true);
|
| - WriteFlip(dst, false);
|
| - WriteColors555(dst, base, base);
|
| + cc::WriteDiff(dst, true);
|
| + cc::WriteFlip(dst, false);
|
| + cc::WriteColors555(dst, base, base);
|
|
|
| uint8_t best_tbl_idx = 0;
|
| uint8_t best_mod_idx = 0;
|
| @@ -344,8 +171,8 @@ bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
|
| // Try all modifiers in the current table to find which one gives the
|
| // smallest error.
|
| for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
|
| - int16_t lum = g_codeword_tables[tbl_idx][mod_idx];
|
| - const Color& color = MakeColor(base, lum);
|
| + int16_t lum = cc::g_codeword_tables[tbl_idx][mod_idx];
|
| + const cc::Color& color = MakeColor(base, lum);
|
|
|
| uint32_t mod_err = GetColorError(*src, color);
|
| if (mod_err < best_mod_err) {
|
| @@ -362,10 +189,10 @@ bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
|
| break;
|
| }
|
|
|
| - WriteCodewordTable(dst, 0, best_tbl_idx);
|
| - WriteCodewordTable(dst, 1, best_tbl_idx);
|
| + cc::WriteCodewordTable(dst, 0, best_tbl_idx);
|
| + cc::WriteCodewordTable(dst, 1, best_tbl_idx);
|
|
|
| - uint8_t pix_idx = g_mod_to_pix[best_mod_idx];
|
| + uint8_t pix_idx = cc::g_mod_to_pix[best_mod_idx];
|
| uint32_t lsb = pix_idx & 0x1;
|
| uint32_t msb = pix_idx >> 1;
|
|
|
| @@ -373,23 +200,26 @@ bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
|
| for (unsigned int i = 0; i < 2; ++i) {
|
| for (unsigned int j = 0; j < 8; ++j) {
|
| // Obtain the texel number as specified in the standard.
|
| - int texel_num = g_idx_to_num[i][j];
|
| + int texel_num = cc::g_idx_to_num[i][j];
|
| pix_data |= msb << (texel_num + 16);
|
| pix_data |= lsb << (texel_num);
|
| }
|
| }
|
|
|
| - WritePixelData(dst, pix_data);
|
| + cc::WritePixelData(dst, pix_data);
|
| return true;
|
| }
|
|
|
| -void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) {
|
| +void CompressBlock(uint8_t* dst,
|
| + const cc::Color* ver_src,
|
| + const cc::Color* hor_src) {
|
| if (TryCompressSolidBlock(dst, ver_src))
|
| return;
|
|
|
| - const Color* sub_block_src[4] = {ver_src, ver_src + 8, hor_src, hor_src + 8};
|
| + const cc::Color* sub_block_src[4] = {
|
| + ver_src, ver_src + 8, hor_src, hor_src + 8};
|
|
|
| - Color sub_block_avg[4];
|
| + cc::Color sub_block_avg[4];
|
| bool use_differential[2] = {true, true};
|
|
|
| // Compute the average color for each sub block and determine if differential
|
| @@ -397,11 +227,11 @@ void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) {
|
| for (unsigned int i = 0, j = 1; i < 4; i += 2, j += 2) {
|
| float avg_color_0[3];
|
| GetAverageColor(sub_block_src[i], avg_color_0);
|
| - Color avg_color_555_0 = MakeColor555(avg_color_0);
|
| + cc::Color avg_color_555_0 = cc::MakeColor555(avg_color_0);
|
|
|
| float avg_color_1[3];
|
| GetAverageColor(sub_block_src[j], avg_color_1);
|
| - Color avg_color_555_1 = MakeColor555(avg_color_1);
|
| + cc::Color avg_color_555_1 = cc::MakeColor555(avg_color_1);
|
|
|
| for (unsigned int light_idx = 0; light_idx < 3; ++light_idx) {
|
| int u = avg_color_555_0.components[light_idx] >> 3;
|
| @@ -410,8 +240,8 @@ void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) {
|
| int component_diff = v - u;
|
| if (component_diff < -4 || component_diff > 3) {
|
| use_differential[i / 2] = false;
|
| - sub_block_avg[i] = MakeColor444(avg_color_0);
|
| - sub_block_avg[j] = MakeColor444(avg_color_1);
|
| + sub_block_avg[i] = cc::MakeColor444(avg_color_0);
|
| + sub_block_avg[j] = cc::MakeColor444(avg_color_1);
|
| } else {
|
| sub_block_avg[i] = avg_color_555_0;
|
| sub_block_avg[j] = avg_color_555_1;
|
| @@ -435,28 +265,28 @@ void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) {
|
| // Clear destination buffer so that we can "or" in the results.
|
| memset(dst, 0, 8);
|
|
|
| - WriteDiff(dst, use_differential[!!flip]);
|
| - WriteFlip(dst, flip);
|
| + cc::WriteDiff(dst, use_differential[!!flip]);
|
| + cc::WriteFlip(dst, flip);
|
|
|
| uint8_t sub_block_off_0 = flip ? 2 : 0;
|
| uint8_t sub_block_off_1 = sub_block_off_0 + 1;
|
|
|
| if (use_differential[!!flip]) {
|
| - WriteColors555(dst, sub_block_avg[sub_block_off_0],
|
| - sub_block_avg[sub_block_off_1]);
|
| + cc::WriteColors555(dst, sub_block_avg[sub_block_off_0],
|
| + sub_block_avg[sub_block_off_1]);
|
| } else {
|
| - WriteColors444(dst, sub_block_avg[sub_block_off_0],
|
| - sub_block_avg[sub_block_off_1]);
|
| + cc::WriteColors444(dst, sub_block_avg[sub_block_off_0],
|
| + sub_block_avg[sub_block_off_1]);
|
| }
|
|
|
| // Compute luminance for the first sub block.
|
| ComputeLuminance(dst, sub_block_src[sub_block_off_0],
|
| sub_block_avg[sub_block_off_0], 0,
|
| - g_idx_to_num[sub_block_off_0]);
|
| + cc::g_idx_to_num[sub_block_off_0]);
|
| // Compute luminance for the second sub block.
|
| ComputeLuminance(dst, sub_block_src[sub_block_off_1],
|
| sub_block_avg[sub_block_off_1], 1,
|
| - g_idx_to_num[sub_block_off_1]);
|
| + cc::g_idx_to_num[sub_block_off_1]);
|
| }
|
|
|
| } // namespace
|
| @@ -468,8 +298,10 @@ void TextureCompressorETC1::Compress(const uint8_t* src,
|
| int width,
|
| int height,
|
| Quality quality) {
|
| - DCHECK(width >= 4 && (width & 3) == 0);
|
| - DCHECK(height >= 4 && (height & 3) == 0);
|
| + DCHECK_GE(width, 4);
|
| + DCHECK_EQ((width & 3), 0);
|
| + DCHECK_GE(height, 4);
|
| + DCHECK_EQ((height & 3), 0);
|
|
|
| Color ver_blocks[16];
|
| Color hor_blocks[16];
|
|
|