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

Side by Side Diff: cc/resources/texture_compressor_etc1.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: Reland: Change ia32 to x86 in Build.gn Created 5 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // See the following specification for details on the ETC1 format: 5 // See the following specification for details on the ETC1 format:
6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8 _texture.txt 6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8 _texture.txt
7 7
8 #include "cc/resources/texture_compressor_etc1.h" 8 #include "cc/resources/texture_compressor_etc1.h"
9 9
10 #include <string.h> 10 #include <string.h>
11 #include <limits> 11 #include <limits>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 14
15 // Defining the following macro will cause the error metric function to weigh 15 // Defining the following macro will cause the error metric function to weigh
16 // each color channel differently depending on how the human eye can perceive 16 // each color channel differently depending on how the human eye can perceive
17 // them. This can give a slight improvement in image quality at the cost of a 17 // them. This can give a slight improvement in image quality at the cost of a
18 // performance hit. 18 // performance hit.
19 // #define USE_PERCEIVED_ERROR_METRIC 19 // #define USE_PERCEIVED_ERROR_METRIC
20 20
21 namespace cc {
22
21 namespace { 23 namespace {
22 24
23 template <typename T> 25 // Constructs a color from a given base color and luminance value.
24 inline T clamp(T val, T min, T max) {
25 return val < min ? min : (val > max ? max : val);
26 }
27
28 inline uint8_t round_to_5_bits(float val) {
29 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31);
30 }
31
32 inline uint8_t round_to_4_bits(float val) {
33 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15);
34 }
35
36 union Color {
37 struct BgraColorType {
38 uint8_t b;
39 uint8_t g;
40 uint8_t r;
41 uint8_t a;
42 } channels;
43 uint8_t components[4];
44 uint32_t bits;
45 };
46
47 /*
48 * Codeword tables.
49 * See: Table 3.17.2
50 */
51 static const int16_t g_codeword_tables[8][4] = {{-8, -2, 2, 8},
52 {-17, -5, 5, 17},
53 {-29, -9, 9, 29},
54 {-42, -13, 13, 42},
55 {-60, -18, 18, 60},
56 {-80, -24, 24, 80},
57 {-106, -33, 33, 106},
58 {-183, -47, 47, 183}};
59
60 /*
61 * Maps modifier indices to pixel index values.
62 * See: Table 3.17.3
63 */
64 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1};
65
66 /*
67 * The ETC1 specification index texels as follows:
68 *
69 * [a][e][i][m] [ 0][ 4][ 8][12]
70 * [b][f][j][n] <-> [ 1][ 5][ 9][13]
71 * [c][g][k][o] [ 2][ 6][10][14]
72 * [d][h][l][p] [ 3][ 7][11][15]
73 *
74 * However, when extracting sub blocks from BGRA data the natural array
75 * indexing order ends up different:
76 *
77 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m]
78 * [c][g][d][h] [b][f][j][n]
79 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o]
80 * [k][o][l][p] [d][h][l][p]
81 *
82 * In order to translate from the natural array indices in a sub block to the
83 * indices (number) used by specification and hardware we use this table.
84 */
85 static const uint8_t g_idx_to_num[4][8] = {
86 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0.
87 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1.
88 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0.
89 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1.
90 };
91
92 inline void WriteColors444(uint8_t* block,
93 const Color& color0,
94 const Color& color1) {
95 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4);
96 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4);
97 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4);
98 }
99
100 inline void WriteColors555(uint8_t* block,
101 const Color& color0,
102 const Color& color1) {
103 // Table for conversion to 3-bit two complement format.
104 static const uint8_t two_compl_trans_table[8] = {
105 4, // -4 (100b)
106 5, // -3 (101b)
107 6, // -2 (110b)
108 7, // -1 (111b)
109 0, // 0 (000b)
110 1, // 1 (001b)
111 2, // 2 (010b)
112 3, // 3 (011b)
113 };
114
115 int16_t delta_r =
116 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3);
117 int16_t delta_g =
118 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3);
119 int16_t delta_b =
120 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3);
121 DCHECK(delta_r >= -4 && delta_r <= 3);
122 DCHECK(delta_g >= -4 && delta_g <= 3);
123 DCHECK(delta_b >= -4 && delta_b <= 3);
124
125 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4];
126 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4];
127 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4];
128 }
129
130 inline void WriteCodewordTable(uint8_t* block,
131 uint8_t sub_block_id,
132 uint8_t table) {
133 DCHECK_LT(sub_block_id, 2);
134 DCHECK_LT(table, 8);
135
136 uint8_t shift = (2 + (3 - sub_block_id * 3));
137 block[3] &= ~(0x07 << shift);
138 block[3] |= table << shift;
139 }
140
141 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) {
142 block[4] |= pixel_data >> 24;
143 block[5] |= (pixel_data >> 16) & 0xff;
144 block[6] |= (pixel_data >> 8) & 0xff;
145 block[7] |= pixel_data & 0xff;
146 }
147
148 inline void WriteFlip(uint8_t* block, bool flip) {
149 block[3] &= ~0x01;
150 block[3] |= static_cast<uint8_t>(flip);
151 }
152
153 inline void WriteDiff(uint8_t* block, bool diff) {
154 block[3] &= ~0x02;
155 block[3] |= static_cast<uint8_t>(diff) << 1;
156 }
157
158 /**
159 * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is
160 * expanded to BGR888 as it would be in hardware after decompression. The
161 * actual 444-bit data is available in the four most significant bits of each
162 * channel.
163 */
164 inline Color MakeColor444(const float* bgr) {
165 uint8_t b4 = round_to_4_bits(bgr[0]);
166 uint8_t g4 = round_to_4_bits(bgr[1]);
167 uint8_t r4 = round_to_4_bits(bgr[2]);
168 Color bgr444;
169 bgr444.channels.b = (b4 << 4) | b4;
170 bgr444.channels.g = (g4 << 4) | g4;
171 bgr444.channels.r = (r4 << 4) | r4;
172 return bgr444;
173 }
174
175 /**
176 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is
177 * expanded to BGR888 as it would be in hardware after decompression. The
178 * actual 555-bit data is available in the five most significant bits of each
179 * channel.
180 */
181 inline Color MakeColor555(const float* bgr) {
182 uint8_t b5 = round_to_5_bits(bgr[0]);
183 uint8_t g5 = round_to_5_bits(bgr[1]);
184 uint8_t r5 = round_to_5_bits(bgr[2]);
185 Color bgr555;
186 bgr555.channels.b = (b5 << 3) | (b5 >> 2);
187 bgr555.channels.g = (g5 << 3) | (g5 >> 2);
188 bgr555.channels.r = (r5 << 3) | (r5 >> 2);
189 return bgr555;
190 }
191
192 /**
193 * Constructs a color from a given base color and luminance value.
194 */
195 inline Color MakeColor(const Color& base, int16_t lum) { 26 inline Color MakeColor(const Color& base, int16_t lum) {
196 int b = static_cast<int>(base.channels.b) + lum; 27 int b = static_cast<int>(base.channels.b) + lum;
197 int g = static_cast<int>(base.channels.g) + lum; 28 int g = static_cast<int>(base.channels.g) + lum;
198 int r = static_cast<int>(base.channels.r) + lum; 29 int r = static_cast<int>(base.channels.r) + lum;
199 Color color; 30 Color color;
200 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); 31 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255));
201 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); 32 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255));
202 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255)); 33 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255));
203 return color; 34 return color;
204 } 35 }
205 36
206 /** 37 // Calculates the error metric for two colors. A small error signals that the
207 * Calculates the error metric for two colors. A small error signals that the 38 // colors are similar to each other, a large error the signals the opposite.
208 * colors are similar to each other, a large error the signals the opposite.
209 */
210 inline uint32_t GetColorError(const Color& u, const Color& v) { 39 inline uint32_t GetColorError(const Color& u, const Color& v) {
211 #ifdef USE_PERCEIVED_ERROR_METRIC 40 #ifdef USE_PERCEIVED_ERROR_METRIC
212 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; 41 float delta_b = static_cast<float>(u.channels.b) - v.channels.b;
213 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; 42 float delta_g = static_cast<float>(u.channels.g) - v.channels.g;
214 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; 43 float delta_r = static_cast<float>(u.channels.r) - v.channels.r;
215 return static_cast<uint32_t>(0.299f * delta_b * delta_b + 44 return static_cast<uint32_t>(0.299f * delta_b * delta_b +
216 0.587f * delta_g * delta_g + 45 0.587f * delta_g * delta_g +
217 0.114f * delta_r * delta_r); 46 0.114f * delta_r * delta_r);
218 #else 47 #else
219 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; 48 int delta_b = static_cast<int>(u.channels.b) - v.channels.b;
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 sub_block_avg[sub_block_off_0], 0, 283 sub_block_avg[sub_block_off_0], 0,
455 g_idx_to_num[sub_block_off_0]); 284 g_idx_to_num[sub_block_off_0]);
456 // Compute luminance for the second sub block. 285 // Compute luminance for the second sub block.
457 ComputeLuminance(dst, sub_block_src[sub_block_off_1], 286 ComputeLuminance(dst, sub_block_src[sub_block_off_1],
458 sub_block_avg[sub_block_off_1], 1, 287 sub_block_avg[sub_block_off_1], 1,
459 g_idx_to_num[sub_block_off_1]); 288 g_idx_to_num[sub_block_off_1]);
460 } 289 }
461 290
462 } // namespace 291 } // namespace
463 292
464 namespace cc {
465
466 void TextureCompressorETC1::Compress(const uint8_t* src, 293 void TextureCompressorETC1::Compress(const uint8_t* src,
467 uint8_t* dst, 294 uint8_t* dst,
468 int width, 295 int width,
469 int height, 296 int height,
470 Quality quality) { 297 Quality quality) {
471 DCHECK(width >= 4 && (width & 3) == 0); 298 DCHECK_GE(width, 4);
472 DCHECK(height >= 4 && (height & 3) == 0); 299 DCHECK_EQ((width & 3), 0);
300 DCHECK_GE(height, 4);
301 DCHECK_EQ((height & 3), 0);
473 302
474 Color ver_blocks[16]; 303 Color ver_blocks[16];
475 Color hor_blocks[16]; 304 Color hor_blocks[16];
476 305
477 for (int y = 0; y < height; y += 4, src += width * 4 * 4) { 306 for (int y = 0; y < height; y += 4, src += width * 4 * 4) {
478 for (int x = 0; x < width; x += 4, dst += 8) { 307 for (int x = 0; x < width; x += 4, dst += 8) {
479 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4); 308 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4);
480 const Color* row1 = row0 + width; 309 const Color* row1 = row0 + width;
481 const Color* row2 = row1 + width; 310 const Color* row2 = row1 + width;
482 const Color* row3 = row2 + width; 311 const Color* row3 = row2 + width;
(...skipping 11 matching lines...) Expand all
494 memcpy(hor_blocks + 4, row1, 16); 323 memcpy(hor_blocks + 4, row1, 16);
495 memcpy(hor_blocks + 8, row2, 16); 324 memcpy(hor_blocks + 8, row2, 16);
496 memcpy(hor_blocks + 12, row3, 16); 325 memcpy(hor_blocks + 12, row3, 16);
497 326
498 CompressBlock(dst, ver_blocks, hor_blocks); 327 CompressBlock(dst, ver_blocks, hor_blocks);
499 } 328 }
500 } 329 }
501 } 330 }
502 331
503 } // namespace cc 332 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698