Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2388)

Unified Diff: cc/resources/texture_compressor_etc1_sse.cc

Issue 1096703002: Reland: Add ETC1 powered SSE encoder for tile texture compression (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: --no-find-copies Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: cc/resources/texture_compressor_etc1_sse.cc
diff --git a/cc/resources/texture_compressor_etc1_sse.cc b/cc/resources/texture_compressor_etc1_sse.cc
new file mode 100644
index 0000000000000000000000000000000000000000..98e4bedf8602158af90ef06dc7bfb1e3fd118cb2
--- /dev/null
+++ b/cc/resources/texture_compressor_etc1_sse.cc
@@ -0,0 +1,1025 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "texture_compressor_etc1_sse.h"
+
+#include <assert.h>
+#include <smmintrin.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <cmath>
+#include <limits>
+#include <sstream>
+
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+
+// Defining the following macro will cause the error metric function to weigh
+// each color channel differently depending on how the human eye can perceive
+// them. This can give a slight improvement in image quality at the cost of a
+// performance hit.
+// #define USE_PERCEIVED_ERROR_METRIC
+
+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]
+ __attribute__((aligned(16))) = {{-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) {
+ /* 0, 1, 2 - for ARM */
adrian.belgun 2015/04/17 14:02:58 Please check image channel order for input. This
+ block[2] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4);
+ block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4);
+ block[0] = (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);
+
+ /* 0, 1, 2 - for ARM */
adrian.belgun 2015/04/17 14:02:58 Same comments as for :103.
+ block[2] = (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[0] = (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;
+ bgr444.channels.a = 0x44; /* added by Radu */
+ 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);
+ bgr555.channels.a = 0x55; /* added by Radu */
+ return bgr555;
+}
+
+/**
+ * Constructs a color from a given base color and luminance value.
+ */
+inline Color MakeColor(const 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));
+ 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) {
+#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;
+ float delta_r = static_cast<float>(u.channels.r) - v.channels.r;
+ return static_cast<uint32_t>(0.299f * delta_b * delta_b +
+ 0.587f * delta_g * delta_g +
+ 0.114f * delta_r * delta_r);
+#else
+ int delta_b = static_cast<int>(u.channels.b) - v.channels.b;
+ int delta_g = static_cast<int>(u.channels.g) - v.channels.g;
+ int delta_r = static_cast<int>(u.channels.r) - v.channels.r;
+ return delta_b * delta_b + delta_g * delta_g + delta_r * delta_r;
+#endif
+}
+
+/**************************************** START OF SSE CODE
adrian.belgun 2015/04/17 14:02:58 Use only one line here. Reduce number of stars.
+ * ***************************************/
+
+struct __sse_data {
+ /* raw data */
+ uint8_t* block;
+ /* 8 bit packed values */
+ __m128i* packed;
+ /* 32 bit zero extended values - 4x4 arrays */
+ __m128i* blue;
+ __m128i* green;
+ __m128i* red;
+ // __m128i *alpha;
+};
+
+/* commonly used registers */
+static const __m128i __sse_zero = _mm_set1_epi32(0);
+static const __m128i __sse_max_int = _mm_set1_epi32(0x7FFFFFFF);
+
+inline __m128i AddAndClamp(const __m128i x, const __m128i y) {
+ static const __m128i color_max = _mm_set1_epi32(0xFF);
+ return _mm_max_epi32(__sse_zero,
+ _mm_min_epi32(_mm_add_epi32(x, y), color_max));
+}
+
+inline __m128i GetColorErrorSSE(const __m128i x, const __m128i y) {
+ __m128i ret = _mm_sub_epi32(x, y);
+ return _mm_mullo_epi32(ret, ret);
+}
+
+inline __m128i AddChannelError(const __m128i x,
+ const __m128i y,
+ const __m128i z) {
+ return _mm_add_epi32(x, _mm_add_epi32(y, z));
+}
+/*
+inline void ShuffleImm(__m128i *src, __m128i *dest, int size, uint8_t notimm) {
+ switch(notimm) {
+ case 0x1B:
+ for (int i = 0; i < size; i++) {
adrian.belgun 2015/04/17 14:02:58 Braces are optional for single-statement loops. Co
+ dest[i] = _mm_shuffle_epi32(src[i], 0x1B);
+ }
+ break;
+ case 0x4E:
+ for (int i = 0; i < size; i++) {
+ dest[i] = _mm_shuffle_epi32(src[i], 0x4E);
+ }
+ break;
+ case 0xB1:
+ for (int i = 0; i < size; i++) {
+ dest[i] = _mm_shuffle_epi32(src[i], 0xB1);
+ }
+ break;
+ case 0xE4:
+ for (int i = 0; i < size; i++) {
+ dest[i] = _mm_shuffle_epi32(src[i], 0xE4);
+ }
+ break;
+ default:
+ for (int i = 0; i < size; i++) {
+ dest[i] = src[i];
+ }
+ };
+}
+*/
+inline uint32_t GetVerticalError(const __sse_data* data,
+ const __m128i* blue_avg,
+ const __m128i* green_avg,
+ const __m128i* red_avg) {
+ __m128i error = __sse_zero;
+
+#pragma unroll
+ for (int i = 0; i < 4; i++) {
+ error = _mm_add_epi32(error, GetColorErrorSSE(data->blue[i], blue_avg[0]));
+ error =
+ _mm_add_epi32(error, GetColorErrorSSE(data->green[i], green_avg[0]));
+ error = _mm_add_epi32(error, GetColorErrorSSE(data->red[i], red_avg[0]));
+ }
+
+ error = _mm_add_epi32(error, _mm_shuffle_epi32(error, 0x4E));
+ error = _mm_add_epi32(error, _mm_shuffle_epi32(error, 0xB1));
+
+ return _mm_cvtsi128_si32(error);
+}
+
+inline uint32_t GetHorizontalError(const __sse_data* data,
+ const __m128i* blue_avg,
+ const __m128i* green_avg,
+ const __m128i* red_avg) {
+ __m128i error = __sse_zero;
+ int first_index, second_index;
+
+#pragma unroll
+ for (int i = 0; i < 2; i++) {
+ first_index = 2 * i;
+ second_index = first_index + 1;
+
+ error = _mm_add_epi32(
+ error, GetColorErrorSSE(data->blue[first_index], blue_avg[i]));
+ error = _mm_add_epi32(
+ error, GetColorErrorSSE(data->blue[second_index], blue_avg[i]));
+ error = _mm_add_epi32(
+ error, GetColorErrorSSE(data->green[first_index], green_avg[i]));
+ error = _mm_add_epi32(
+ error, GetColorErrorSSE(data->green[second_index], green_avg[i]));
+ error = _mm_add_epi32(error,
+ GetColorErrorSSE(data->red[first_index], red_avg[i]));
+ error = _mm_add_epi32(
+ error, GetColorErrorSSE(data->red[second_index], red_avg[i]));
+ }
+
+ error = _mm_add_epi32(error, _mm_shuffle_epi32(error, 0x4E));
+ error = _mm_add_epi32(error, _mm_shuffle_epi32(error, 0xB1));
+ return _mm_cvtsi128_si32(error);
+}
+
+inline void GetAvgColors(const __sse_data* data,
+ float* output,
+ bool* __sse_use_diff) {
+ __m128i sum[2], tmp;
+
+ // TODO(radu.velea): _mm_avg_epu8 on packed data maybe
+
+ /* get avg red */
+ /* [S0 S0 S1 S1] */
+ sum[0] = _mm_add_epi32(data->red[0], data->red[1]);
+ sum[0] = _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0xB1));
+
+ /* [S2 S2 S3 S3] */
+ sum[1] = _mm_add_epi32(data->red[2], data->red[3]);
+ sum[1] = _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0xB1));
+
+ float hred[2], vred[2];
+ hred[0] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0x4E)))) /
+ 8.0f;
+ hred[1] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0x4E)))) /
+ 8.0f;
+
+ tmp = _mm_add_epi32(sum[0], sum[1]);
+ vred[0] = (_mm_cvtsi128_si32(tmp)) / 8.0f;
+ vred[1] = (_mm_extract_epi32(tmp, 2)) / 8.0f;
+
+ /* get avg green */
+ /* [S0 S0 S1 S1] */
+ sum[0] = _mm_add_epi32(data->green[0], data->green[1]);
+ sum[0] = _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0xB1));
+
+ /* [S2 S2 S3 S3] */
+ sum[1] = _mm_add_epi32(data->green[2], data->green[3]);
+ sum[1] = _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0xB1));
+
+ float hgreen[2], vgreen[2];
+ hgreen[0] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0x4E)))) /
+ 8.0f;
+ hgreen[1] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0x4E)))) /
+ 8.0f;
+
+ tmp = _mm_add_epi32(sum[0], sum[1]);
+ vgreen[0] = (_mm_cvtsi128_si32(tmp)) / 8.0f;
+ vgreen[1] = (_mm_extract_epi32(tmp, 2)) / 8.0f;
+
+ /* get avg blue */
+ /* [S0 S0 S1 S1] */
+ sum[0] = _mm_add_epi32(data->blue[0], data->blue[1]);
+ sum[0] = _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0xB1));
+
+ /* [S2 S2 S3 S3] */
+ sum[1] = _mm_add_epi32(data->blue[2], data->blue[3]);
+ sum[1] = _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0xB1));
+
+ float hblue[2], vblue[2];
+ hblue[0] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[0], _mm_shuffle_epi32(sum[0], 0x4E)))) /
+ 8.0f;
+ hblue[1] = (_mm_cvtsi128_si32(
+ _mm_add_epi32(sum[1], _mm_shuffle_epi32(sum[1], 0x4E)))) /
+ 8.0f;
+
+ tmp = _mm_add_epi32(sum[0], sum[1]);
+ vblue[0] = (_mm_cvtsi128_si32(tmp)) / 8.0f;
+ vblue[1] = (_mm_extract_epi32(tmp, 2)) / 8.0f;
+
+ /* TODO(radu.velea): return int's instead of floats */
+ output[0] = vblue[0];
+ output[1] = vgreen[0];
+ output[2] = vred[0];
+
+ output[3] = vblue[1];
+ output[4] = vgreen[1];
+ output[5] = vred[1];
+
+ output[6] = hblue[0];
+ output[7] = hgreen[0];
+ output[8] = hred[0];
+
+ output[9] = hblue[1];
+ output[10] = hgreen[1];
+ output[11] = hred[1];
+
+ __m128i threashhold_upper = _mm_set1_epi32(3);
+ __m128i threashhold_lower = _mm_set1_epi32(-4);
+
+ __m128 factor_v = _mm_set1_ps(31.0f / 255.0f);
+ __m128 rounding_v = _mm_set1_ps(0.5f);
+ __m128 h_avg_0 = _mm_set_ps(hblue[0], hgreen[0], hred[0], 0);
+ __m128 h_avg_1 = _mm_set_ps(hblue[1], hgreen[1], hred[1], 0);
+
+ __m128 v_avg_0 = _mm_set_ps(vblue[0], vgreen[0], vred[0], 0);
+ __m128 v_avg_1 = _mm_set_ps(vblue[1], vgreen[1], vred[1], 0);
+
+ h_avg_0 = _mm_mul_ps(h_avg_0, factor_v);
+ h_avg_1 = _mm_mul_ps(h_avg_1, factor_v);
+ v_avg_0 = _mm_mul_ps(v_avg_0, factor_v);
+ v_avg_1 = _mm_mul_ps(v_avg_1, factor_v);
+
+ h_avg_0 = _mm_add_ps(h_avg_0, rounding_v);
+ h_avg_1 = _mm_add_ps(h_avg_1, rounding_v);
+ v_avg_0 = _mm_add_ps(v_avg_0, rounding_v);
+ v_avg_1 = _mm_add_ps(v_avg_1, rounding_v);
+
+ __m128i h_avg_0i = _mm_cvttps_epi32(h_avg_0);
+ __m128i h_avg_1i = _mm_cvttps_epi32(h_avg_1);
+
+ __m128i v_avg_0i = _mm_cvttps_epi32(v_avg_0);
+ __m128i v_avg_1i = _mm_cvttps_epi32(v_avg_1);
+
+ h_avg_0i = _mm_sub_epi32(h_avg_1i, h_avg_0i);
+ v_avg_0i = _mm_sub_epi32(v_avg_1i, v_avg_0i);
+
+ __sse_use_diff[0] =
+ (0 == _mm_movemask_epi8(_mm_cmplt_epi32(v_avg_0i, threashhold_lower)));
+ __sse_use_diff[0] &=
+ (0 == _mm_movemask_epi8(_mm_cmpgt_epi32(v_avg_0i, threashhold_upper)));
+
+ __sse_use_diff[1] =
+ (0 == _mm_movemask_epi8(_mm_cmplt_epi32(h_avg_0i, threashhold_lower)));
+ __sse_use_diff[1] &=
+ (0 == _mm_movemask_epi8(_mm_cmpgt_epi32(h_avg_0i, threashhold_upper)));
+}
+
+void ComputeLuminanceSSE(uint8_t* block,
+ const Color& base,
+ const int sub_block_id,
+ const uint8_t* idx_to_num_tab,
+ const __sse_data* data) {
+ uint8_t my_best_tbl_idx = 0;
+ uint32_t my_best_error = 0x7FFFFFFF;
+ uint8_t my_best_mod_idx[8][8]; // [table][texel]
+
+ const __m128i base_blue = _mm_set1_epi32(base.channels.b);
+ const __m128i base_green = _mm_set1_epi32(base.channels.g);
+ const __m128i base_red = _mm_set1_epi32(base.channels.r);
+
+ __m128i test_red, test_blue, test_green, tmp, tmp_blue, tmp_green, tmp_red;
+ __m128i block_error;
+
+ /* this will have the minimum errors for each 4 pixels */
+ __m128i first_half_min;
+ __m128i second_half_min;
+
+ /* this will have the matching table index combo for each 4 pixels */
+ __m128i first_half_pattern;
+ __m128i second_half_pattern;
+
+ const __m128i first_blue_data_block = data->blue[2 * sub_block_id];
+ const __m128i first_green_data_block = data->green[2 * sub_block_id];
+ const __m128i first_red_data_block = data->red[2 * sub_block_id];
+
+ const __m128i second_blue_data_block = data->blue[2 * sub_block_id + 1];
+ const __m128i second_green_data_block = data->green[2 * sub_block_id + 1];
+ const __m128i second_red_data_block = data->red[2 * sub_block_id + 1];
+
+ uint32_t min;
+
+#define ELEMENT_1 3, 2, 1, 0
+#define ELEMENT_2 7, 6, 5, 4
+#define ELEMENT_3 11, 10, 9, 8
+#define ELEMENT_4 15, 14, 13, 12
+
+ static const __m128i mask_extended[4] = {
+ _mm_set_epi8(ELEMENT_1, ELEMENT_2, ELEMENT_3, ELEMENT_4),
+ _mm_set_epi8(ELEMENT_2, ELEMENT_1, ELEMENT_4, ELEMENT_3),
+ _mm_set_epi8(ELEMENT_3, ELEMENT_4, ELEMENT_1, ELEMENT_2),
+ _mm_set_epi8(ELEMENT_4, ELEMENT_3, ELEMENT_2, ELEMENT_1)};
+
+ static const __m128i mask_imm[4] = {_mm_set1_epi32(0x1B),
+ _mm_set1_epi32(0x4E),
+ _mm_set1_epi32(0xB1),
+ _mm_set1_epi32(0xE4)};
+
+ for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) {
+ tmp = _mm_set_epi32(
+ g_codeword_tables[tbl_idx][3], g_codeword_tables[tbl_idx][2],
+ g_codeword_tables[tbl_idx][1], g_codeword_tables[tbl_idx][0]);
+
+ test_blue = AddAndClamp(tmp, base_blue);
+ test_green = AddAndClamp(tmp, base_green);
+ test_red = AddAndClamp(tmp, base_red);
+
+ first_half_min = __sse_max_int;
+ second_half_min = __sse_max_int;
+
+ first_half_pattern = __sse_zero;
+ second_half_pattern = __sse_zero;
+
+#pragma unroll
+ for (int i = 0; i < 4; i++) {
+ tmp_blue = _mm_shuffle_epi8(test_blue, mask_extended[i]);
+ tmp_green = _mm_shuffle_epi8(test_green, mask_extended[i]);
+ tmp_red = _mm_shuffle_epi8(test_red, mask_extended[i]);
+
+ block_error =
+ AddChannelError(GetColorErrorSSE(tmp_blue, first_blue_data_block),
+ GetColorErrorSSE(tmp_green, first_green_data_block),
+ GetColorErrorSSE(tmp_red, first_red_data_block));
+
+ /* save winning pattern */
+ first_half_pattern = _mm_max_epi32(
+ first_half_pattern,
+ _mm_and_si128(mask_imm[i],
+ _mm_cmpgt_epi32(first_half_min, block_error)));
+ first_half_min = _mm_min_epi32(first_half_min, block_error);
+
+ /* Second part of the block */
+ block_error =
+ AddChannelError(GetColorErrorSSE(tmp_blue, second_blue_data_block),
+ GetColorErrorSSE(tmp_green, second_green_data_block),
+ GetColorErrorSSE(tmp_red, second_red_data_block));
+
+ /* save winning pattern */
+ second_half_pattern = _mm_max_epi32(
+ second_half_pattern,
+ _mm_and_si128(mask_imm[i],
+ _mm_cmpgt_epi32(second_half_min, block_error)));
+ second_half_min = _mm_min_epi32(second_half_min, block_error);
+ }
+
+ first_half_min = _mm_add_epi32(first_half_min, second_half_min);
+ first_half_min =
+ _mm_add_epi32(first_half_min, _mm_shuffle_epi32(first_half_min, 0x4E));
+ first_half_min =
+ _mm_add_epi32(first_half_min, _mm_shuffle_epi32(first_half_min, 0xB1));
+
+ min = _mm_cvtsi128_si32(first_half_min);
+
+ if (min < my_best_error) {
+ my_best_tbl_idx = tbl_idx;
+ my_best_error = min;
+#if O3_OPTIMIZATION
+#pragma unroll
+ for (int i = 0; i < 4; i++) {
+ my_best_mod_idx[tbl_idx][i] =
+ (_mm_extract_epi32(first_half_pattern, i) >> (2 * i)) & 3;
+ my_best_mod_idx[tbl_idx][i + 4] =
+ (_mm_extract_epi32(second_half_pattern, i) >> (2 * i)) & 3;
+ }
+#endif
+ my_best_mod_idx[tbl_idx][0] =
+ (_mm_extract_epi32(first_half_pattern, 0) >> (0)) & 3;
+ my_best_mod_idx[tbl_idx][4] =
+ (_mm_extract_epi32(second_half_pattern, 0) >> (0)) & 3;
+
+ my_best_mod_idx[tbl_idx][1] =
+ (_mm_extract_epi32(first_half_pattern, 1) >> (2)) & 3;
+ my_best_mod_idx[tbl_idx][5] =
+ (_mm_extract_epi32(second_half_pattern, 1) >> (2)) & 3;
+
+ my_best_mod_idx[tbl_idx][2] =
+ (_mm_extract_epi32(first_half_pattern, 2) >> (4)) & 3;
+ my_best_mod_idx[tbl_idx][6] =
+ (_mm_extract_epi32(second_half_pattern, 2) >> (4)) & 3;
+
+ my_best_mod_idx[tbl_idx][3] =
+ (_mm_extract_epi32(first_half_pattern, 3) >> (6)) & 3;
+ my_best_mod_idx[tbl_idx][7] =
+ (_mm_extract_epi32(second_half_pattern, 3) >> (6)) & 3;
+
+ if (my_best_error == 0) {
+ break;
+ }
+ }
+ }
+
+ WriteCodewordTable(block, sub_block_id, my_best_tbl_idx);
+
+ uint32_t pix_data = 0;
+ uint8_t mod_idx;
+ uint8_t pix_idx;
+ uint32_t lsb;
+ uint32_t msb;
+ int texel_num;
+
+ for (unsigned int i = 0; i < 8; ++i) {
+ mod_idx = my_best_mod_idx[my_best_tbl_idx][i];
+ pix_idx = g_mod_to_pix[mod_idx];
+
+ lsb = pix_idx & 0x1;
+ msb = pix_idx >> 1;
+
+ // Obtain the texel number as specified in the standard.
+ texel_num = idx_to_num_tab[i];
+ pix_data |= msb << (texel_num + 16);
+ pix_data |= lsb << (texel_num);
+ }
+
+ WritePixelData(block, pix_data);
+}
+
+void CompressBlock(uint8_t* dst, __sse_data* data) {
+ /* first 3 vertical 1, seconds 3 vertical 2, third 3 horizontal 1, last 3
+ * horizontal 2 */
+ float __sse_avg_colors[12] = {
+ 0,
+ };
+ bool use_differential[2] = {true, true};
+ GetAvgColors(data, __sse_avg_colors, use_differential);
+ Color sub_block_avg[4];
+
+ /* TODO(radu.velea): remove floating point operations and use only int's +
+ * normal
+ * rounding and shifts */
+ for (int i = 0, j = 1; i < 4; i += 2, j += 2) {
+ if (use_differential[i / 2] == false) {
+ sub_block_avg[i] = MakeColor444(&__sse_avg_colors[i * 3]);
+ sub_block_avg[j] = MakeColor444(&__sse_avg_colors[j * 3]);
+ } else {
+ sub_block_avg[i] = MakeColor555(&__sse_avg_colors[i * 3]);
+ sub_block_avg[j] = MakeColor555(&__sse_avg_colors[j * 3]);
+ }
+ }
+
+ __m128i red_avg[2], green_avg[2], blue_avg[2];
+
+ // TODO(radu.velea): perfect accuracy, maybe skip floating variables
+ blue_avg[0] =
+ _mm_set_epi32((int)__sse_avg_colors[3], (int)__sse_avg_colors[3],
+ (int)__sse_avg_colors[0], (int)__sse_avg_colors[0]);
+
+ green_avg[0] =
+ _mm_set_epi32((int)__sse_avg_colors[4], (int)__sse_avg_colors[4],
+ (int)__sse_avg_colors[1], (int)__sse_avg_colors[1]);
+
+ red_avg[0] =
+ _mm_set_epi32((int)__sse_avg_colors[5], (int)__sse_avg_colors[5],
+ (int)__sse_avg_colors[2], (int)__sse_avg_colors[2]);
+
+ uint32_t vertical_error =
+ GetVerticalError(data, blue_avg, green_avg, red_avg);
+
+ // TODO(radu.velea): perfect accuracy, maybe skip floating variables
+ blue_avg[0] = _mm_set1_epi32((int)__sse_avg_colors[6]);
+ blue_avg[1] = _mm_set1_epi32((int)__sse_avg_colors[9]);
+
+ green_avg[0] = _mm_set1_epi32((int)__sse_avg_colors[7]);
+ green_avg[1] = _mm_set1_epi32((int)__sse_avg_colors[10]);
+
+ red_avg[0] = _mm_set1_epi32((int)__sse_avg_colors[8]);
+ red_avg[1] = _mm_set1_epi32((int)__sse_avg_colors[11]);
+
+ uint32_t horizontal_error =
+ GetHorizontalError(data, blue_avg, green_avg, red_avg);
+
+ bool flip = horizontal_error < vertical_error;
+
+ // Clear destination buffer so that we can "or" in the results.
+ memset(dst, 0, 8);
+
+ WriteDiff(dst, use_differential[!!flip]);
+ 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]);
+ } else {
+ WriteColors444(dst, sub_block_avg[sub_block_off_0],
+ sub_block_avg[sub_block_off_1]);
+ }
+
+ if (flip == false) {
+ /* transpose vertical data into horizontal lines */
+ __m128i tmp;
+#pragma unroll
+ for (int i = 0; i < 4; i += 2) {
+ tmp = data->blue[i];
+ data->blue[i] = _mm_add_epi32(
+ _mm_move_epi64(data->blue[i]),
+ _mm_shuffle_epi32(_mm_move_epi64(data->blue[i + 1]), 0x4E));
+ data->blue[i + 1] = _mm_add_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(tmp, 0x4E)),
+ _mm_shuffle_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(data->blue[i + 1], 0x4E)),
+ 0x4E));
+
+ tmp = data->green[i];
+ data->green[i] = _mm_add_epi32(
+ _mm_move_epi64(data->green[i]),
+ _mm_shuffle_epi32(_mm_move_epi64(data->green[i + 1]), 0x4E));
+ data->green[i + 1] = _mm_add_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(tmp, 0x4E)),
+ _mm_shuffle_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(data->green[i + 1], 0x4E)),
+ 0x4E));
+
+ tmp = data->red[i];
+ data->red[i] = _mm_add_epi32(
+ _mm_move_epi64(data->red[i]),
+ _mm_shuffle_epi32(_mm_move_epi64(data->red[i + 1]), 0x4E));
+ data->red[i + 1] = _mm_add_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(tmp, 0x4E)),
+ _mm_shuffle_epi32(
+ _mm_move_epi64(_mm_shuffle_epi32(data->red[i + 1], 0x4E)), 0x4E));
+ }
+
+ tmp = data->blue[1];
+ data->blue[1] = data->blue[2];
+ data->blue[2] = tmp;
+
+ tmp = data->green[1];
+ data->green[1] = data->green[2];
+ data->green[2] = tmp;
+
+ tmp = data->red[1];
+ data->red[1] = data->red[2];
+ data->red[2] = tmp;
+ }
+
+ // Compute luminance for the first sub block.
+ ComputeLuminanceSSE(dst, sub_block_avg[sub_block_off_0], 0,
+ g_idx_to_num[sub_block_off_0], data);
+ // Compute luminance for the second sub block.
+ ComputeLuminanceSSE(dst, sub_block_avg[sub_block_off_1], 1,
+ g_idx_to_num[sub_block_off_1], data);
+}
+
+static void LegacyExtractBlock(uint8_t* dst, const uint8_t* src, int width) {
+ for (int j = 0; j < 4; ++j) {
+ memcpy(&dst[j * 4 * 4], src, 4 * 4);
+ src += width * 4;
+ }
+}
+
+inline void TransposeBlock(uint8_t* block, __m128i* transposed /* [4] */) {
+ __m128i tmp3, tmp2, tmp1, tmp0;
+
adrian.belgun 2015/04/17 14:02:58 I think something went wrong with 'git cl format'
+ transposed[0] = _mm_loadu_si128((__m128i*)(block)); // a0,a1,a2,...a7, ...a15
+ transposed[1] =
+ _mm_loadu_si128((__m128i*)(block + 16)); // b0, b1,b2,...b7.... b15
+ transposed[2] =
+ _mm_loadu_si128((__m128i*)(block + 32)); // c0, c1,c2,...c7....c15
+ transposed[3] =
+ _mm_loadu_si128((__m128i*)(block + 48)); // d0,d1,d2,...d7....d15
+
+ tmp0 = _mm_unpacklo_epi8(
+ transposed[0], transposed[1]); // a0,b0, a1,b1, a2,b2, a3,b3,....a7,b7
+ tmp1 = _mm_unpacklo_epi8(
+ transposed[2], transposed[3]); // c0,d0, c1,d1, c2,d2, c3,d3,... c7,d7
+ tmp2 = _mm_unpackhi_epi8(
+ transposed[0],
+ transposed[1]); // a8,b8, a9,b9, a10,b10, a11,b11,...a15,b15
+ tmp3 = _mm_unpackhi_epi8(
+ transposed[2],
+ transposed[3]); // c8,d8, c9,d9, c10,d10, c11,d11,...c15,d15
+
+ transposed[0] = _mm_unpacklo_epi8(
+ tmp0, tmp2); // a0,a8, b0,b8, a1,a9, b1,b9, ....a3,a11, b3,b11
+ transposed[1] = _mm_unpackhi_epi8(
+ tmp0, tmp2); // a4,a12, b4,b12, a5,a13, b5,b13,....a7,a15,b7,b15
+ transposed[2] =
+ _mm_unpacklo_epi8(tmp1, tmp3); // c0,c8, d0,d8, c1,c9, d1,d9.....d3,d11
+ transposed[3] = _mm_unpackhi_epi8(
+ tmp1, tmp3); // c4,c12,d4,d12, c5,c13, d5,d13,....d7,d15
+
+ tmp0 = _mm_unpacklo_epi32(transposed[0], transposed[2]); // a0,a8, b0,b8,
+ // c0,c8, d0,d8,
+ // a1,a9, b1,b9,
+ // c1,c9, d1,d9
+ tmp1 = _mm_unpackhi_epi32(transposed[0], transposed[2]); // a2,a10, b2,b10,
+ // c2,c10, d2,d10,
+ // a3,a11, b3,b11,
+ // c3,c11, d3,d11
+ tmp2 = _mm_unpacklo_epi32(transposed[1], transposed[3]); // a4,a12, b4,b12,
+ // c4,c12, d4,d12,
+ // a5,a13, b5,b13,
+ // c5,c13, d5,d13,
+ tmp3 = _mm_unpackhi_epi32(transposed[1],
+ transposed[3]); // a6,a14, b6,b14, c6,c14, d6,d14,
+ // a7,a15,b7,b15,c7,c15,d7,d15
+
+ transposed[0] = _mm_unpacklo_epi8(tmp0, tmp2); // a0,a4, a8, a12, b0,b4,
+ // b8,b12, c0,c4, c8, c12,
+ // d0,d4, d8, d12
+ transposed[1] = _mm_unpackhi_epi8(tmp0, tmp2); // a1,a5, a9, a13, b1,b5,
+ // b9,b13, c1,c5, c9, c13,
+ // d1,d5, d9, d13
+ transposed[2] = _mm_unpacklo_epi8(tmp1, tmp3); // a2,a6, a10,a14, b2,b6,
+ // b10,b14, c2,c6, c10,c14,
+ // d2,d6, d10,d14
+ transposed[3] = _mm_unpackhi_epi8(tmp1, tmp3); // a3,a7, a11,a15, b3,b7,
+ // b11,b15, c3,c7, c11,c15,
+ // d3,d7, d11,d15
+}
+
+inline void UnpackBlock(__m128i* packed,
+ __m128i* red,
+ __m128i* green,
+ __m128i* blue,
+ __m128i* alpha) {
+ const __m128i zero = _mm_set1_epi8(0);
+ __m128i tmp_low, tmp_high;
+
+ /* unpack red */
+ tmp_low = _mm_unpacklo_epi8(packed[0], zero);
+ tmp_high = _mm_unpackhi_epi8(packed[0], zero);
+
+ red[0] = _mm_unpacklo_epi16(tmp_low, zero);
+ red[1] = _mm_unpackhi_epi16(tmp_low, zero);
+
+ red[2] = _mm_unpacklo_epi16(tmp_high, zero);
+ red[3] = _mm_unpackhi_epi16(tmp_high, zero);
+
+ /* unpack green */
+ tmp_low = _mm_unpacklo_epi8(packed[1], zero);
+ tmp_high = _mm_unpackhi_epi8(packed[1], zero);
+
+ green[0] = _mm_unpacklo_epi16(tmp_low, zero);
+ green[1] = _mm_unpackhi_epi16(tmp_low, zero);
+
+ green[2] = _mm_unpacklo_epi16(tmp_high, zero);
+ green[3] = _mm_unpackhi_epi16(tmp_high, zero);
+
+ /* unpack blue */
+ tmp_low = _mm_unpacklo_epi8(packed[2], zero);
+ tmp_high = _mm_unpackhi_epi8(packed[2], zero);
+
+ blue[0] = _mm_unpacklo_epi16(tmp_low, zero);
+ blue[1] = _mm_unpackhi_epi16(tmp_low, zero);
+
+ blue[2] = _mm_unpacklo_epi16(tmp_high, zero);
+ blue[3] = _mm_unpackhi_epi16(tmp_high, zero);
+
+ /* unpack alpha */
+ tmp_low = _mm_unpacklo_epi8(packed[3], zero);
+ tmp_high = _mm_unpackhi_epi8(packed[3], zero);
+
+ alpha[0] = _mm_unpacklo_epi16(tmp_low, zero);
+ alpha[1] = _mm_unpackhi_epi16(tmp_low, zero);
+
+ alpha[2] = _mm_unpacklo_epi16(tmp_high, zero);
+ alpha[3] = _mm_unpackhi_epi16(tmp_high, zero);
+}
+
+inline int BlockIsConstant(const uint8_t* block, const __m128i* transposed) {
+ __m128i first = _mm_set1_epi8(block[0]);
+ first = _mm_cmpeq_epi8(transposed[0], first);
+ if (_mm_movemask_epi8(first) != 0xFFFF) {
+ return 0;
+ }
+
+ first = _mm_set1_epi8(block[1]);
+ first = _mm_cmpeq_epi8(transposed[1], first);
+
+ if (_mm_movemask_epi8(first) != 0xFFFF) {
+ return 0;
+ }
+
+ first = _mm_set1_epi8(block[2]);
+ first = _mm_cmpeq_epi8(transposed[2], first);
+
+ if (_mm_movemask_epi8(first) != 0xFFFF) {
+ return 0;
+ }
+
+ return 1;
+}
+
+inline void CompressSolid(uint8_t* dst, uint8_t* block) {
+ // Clear destination buffer so that we can "or" in the results.
+ memset(dst, 0, 8);
+
+ float src_color_float[3] = {static_cast<float>(block[0]),
+ static_cast<float>(block[1]),
+ static_cast<float>(block[2])};
+ Color base = MakeColor555(src_color_float);
+ Color constant;
+ constant.channels.b = block[0];
+ constant.channels.g = block[1];
+ constant.channels.r = block[2];
+
+ WriteDiff(dst, true);
+ WriteFlip(dst, false);
+ WriteColors555(dst, base, base);
+
+ uint8_t best_tbl_idx = 0;
+ uint8_t best_mod_idx = 0;
+ uint32_t best_mod_err = std::numeric_limits<uint32_t>::max();
+
+ // Try all codeword tables to find the one giving the best results for this
+ // block.
+ for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) {
+ // 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);
+
+ uint32_t mod_err = GetColorError(constant, color);
+ if (mod_err < best_mod_err) {
+ best_tbl_idx = tbl_idx;
+ best_mod_idx = mod_idx;
+ best_mod_err = mod_err;
+
+ if (mod_err == 0)
+ break; // We cannot do any better than this.
+ }
+ }
+
+ if (best_mod_err == 0)
+ break;
+ }
+
+ WriteCodewordTable(dst, 0, best_tbl_idx);
+ WriteCodewordTable(dst, 1, best_tbl_idx);
+
+ uint8_t pix_idx = g_mod_to_pix[best_mod_idx];
+ uint32_t lsb = pix_idx & 0x1;
+ uint32_t msb = pix_idx >> 1;
+
+ uint32_t pix_data = 0;
+ 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];
+ pix_data |= msb << (texel_num + 16);
+ pix_data |= lsb << (texel_num);
+ }
+ }
+
+ WritePixelData(dst, pix_data);
+}
+
+} // namespace
+
+namespace cc {
+
+void TextureCompressorETC1_SSE::Compress(const uint8_t* src,
+ uint8_t* dst,
+ int width,
+ int height,
+ Quality quality) {
+ DCHECK(width >= 4 && (width & 3) == 0);
+ DCHECK(height >= 4 && (height & 3) == 0);
+
+ uint8_t block[64] __attribute__((aligned(16)));
+ __m128i packed[4];
+ __m128i red[4], green[4], blue[4], alpha[4];
+ __sse_data data;
+
+ for (int y = 0; y < height; y += 4, src += width * 4 * 4) {
+ for (int x = 0; x < width; x += 4, dst += 8) {
+ /* SSE */
+ LegacyExtractBlock(block, src + x * 4, width);
+ TransposeBlock(block, packed);
+ if (BlockIsConstant(block, packed) == 1) {
+ /* TODO(radu.velea): handle constant blocks in SSE */
+ CompressSolid(dst, block);
+ } else {
+ UnpackBlock(packed, blue, green, red, alpha);
+
+ data.block = block;
+ data.packed = packed;
+ data.red = red;
+ data.blue = blue;
+ data.green = green;
+
+ CompressBlock(dst, &data);
+ }
+ }
+ }
+}
+
+} // namespace cc
« cc/resources/texture_compressor.cc ('K') | « cc/resources/texture_compressor_etc1_sse.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698