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

Side by Side Diff: cc/resources/texture_compress/etc1.cc

Issue 793693003: Tile Compression (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
« no previous file with comments | « cc/resources/texture_compress/etc1.h ('k') | cc/resources/texture_compress/texture_compress.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
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
7
8 #include "cc/resources/texture_compress/etc1.h"
9
10 #include <string.h>
11 #include <limits>
12
13 #include "base/logging.h"
14
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
17 // them. This can give a slight improvement in image quality at the cost of a
18 // performance hit.
19 // #define USE_PERCEIVED_ERROR_METRIC
20
21 namespace {
22
23 template <typename T>
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 } // namespace
37
38 namespace etc1 {
39
40 union Color {
41 struct BgraColorType {
42 uint8_t b;
43 uint8_t g;
44 uint8_t r;
45 uint8_t a;
46 } channels;
47 uint8_t components[4];
48 uint32_t bits;
49 };
50
51 /*
52 * Codeword tables.
53 * See: Table 3.17.2
54 */
55 static const int16_t g_codeword_tables[8][4] = {{-8, -2, 2, 8},
56 {-17, -5, 5, 17},
57 {-29, -9, 9, 29},
58 {-42, -13, 13, 42},
59 {-60, -18, 18, 60},
60 {-80, -24, 24, 80},
61 {-106, -33, 33, 106},
62 {-183, -47, 47, 183}};
63
64 /*
65 * Maps modifier indices to pixel index values.
66 * See: Table 3.17.3
67 */
68 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1};
69
70 /*
71 * The ETC1 specification index texels as follows:
72 *
73 * [a][e][i][m] [ 0][ 4][ 8][12]
74 * [b][f][j][n] <-> [ 1][ 5][ 9][13]
75 * [c][g][k][o] [ 2][ 6][10][14]
76 * [d][h][l][p] [ 3][ 7][11][15]
77 *
78 * However, when extracting sub blocks from BGRA data the natural array
79 * indexing order ends up different:
80 *
81 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m]
82 * [c][g][d][h] [b][f][j][n]
83 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o]
84 * [k][o][l][p] [d][h][l][p]
85 *
86 * In order to translate from the natural array indices in a sub block to the
87 * indices (number) used by specification and hardware we use this table.
88 */
89 static const uint8_t g_idx_to_num[4][8] = {
90 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0.
91 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1.
92 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0.
93 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1.
94 };
95
96 inline void WriteColors444(uint8_t* block,
97 const Color& color0,
98 const Color& color1) {
99 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4);
100 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4);
101 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4);
102 }
103
104 inline void WriteColors555(uint8_t* block,
105 const Color& color0,
106 const Color& color1) {
107 // Table for conversion to 3-bit two complement format.
108 static const uint8_t two_compl_trans_table[8] = {
109 4, // -4 (100b)
110 5, // -3 (101b)
111 6, // -2 (110b)
112 7, // -1 (111b)
113 0, // 0 (000b)
114 1, // 1 (001b)
115 2, // 2 (010b)
116 3, // 3 (011b)
117 };
118
119 int16_t delta_r =
120 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3);
121 int16_t delta_g =
122 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3);
123 int16_t delta_b =
124 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3);
125 DCHECK(delta_r >= -4 && delta_r <= 3);
126 DCHECK(delta_g >= -4 && delta_g <= 3);
127 DCHECK(delta_b >= -4 && delta_b <= 3);
128
129 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4];
130 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4];
131 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4];
132 }
133
134 inline void WriteCodewordTable(uint8_t* block,
135 uint8_t sub_block_id,
136 uint8_t table) {
137 DCHECK_LT(sub_block_id, 2);
138 DCHECK_LT(table, 8);
139
140 uint8_t shift = (2 + (3 - sub_block_id * 3));
141 block[3] &= ~(0x07 << shift);
142 block[3] |= table << shift;
143 }
144
145 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) {
146 block[4] |= pixel_data >> 24;
147 block[5] |= (pixel_data >> 16) & 0xff;
148 block[6] |= (pixel_data >> 8) & 0xff;
149 block[7] |= pixel_data & 0xff;
150 }
151
152 inline void WriteFlip(uint8_t* block, bool flip) {
153 block[3] &= ~0x01;
154 block[3] |= static_cast<uint8_t>(flip);
155 }
156
157 inline void WriteDiff(uint8_t* block, bool diff) {
158 block[3] &= ~0x02;
159 block[3] |= static_cast<uint8_t>(diff) << 1;
160 }
161
162 /**
163 * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is
164 * expanded to BGR888 as it would be in hardware after decompression. The
165 * actual 444-bit data is available in the four most significant bits of each
166 * channel.
167 */
168 inline Color MakeColor444(const float* bgr) {
169 uint8_t b4 = round_to_4_bits(bgr[0]);
170 uint8_t g4 = round_to_4_bits(bgr[1]);
171 uint8_t r4 = round_to_4_bits(bgr[2]);
172 Color bgr444;
173 bgr444.channels.b = (b4 << 4) | b4;
174 bgr444.channels.g = (g4 << 4) | g4;
175 bgr444.channels.r = (r4 << 4) | r4;
176 return bgr444;
177 }
178
179 /**
180 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is
181 * expanded to BGR888 as it would be in hardware after decompression. The
182 * actual 555-bit data is available in the five most significant bits of each
183 * channel.
184 */
185 inline Color MakeColor555(const float* bgr) {
186 uint8_t b5 = round_to_5_bits(bgr[0]);
187 uint8_t g5 = round_to_5_bits(bgr[1]);
188 uint8_t r5 = round_to_5_bits(bgr[2]);
189 Color bgr555;
190 bgr555.channels.b = (b5 << 3) | (b5 >> 2);
191 bgr555.channels.g = (g5 << 3) | (g5 >> 2);
192 bgr555.channels.r = (r5 << 3) | (r5 >> 2);
193 return bgr555;
194 }
195
196 /**
197 * Constructs a color from a given base color and luminance value.
198 */
199 inline Color MakeColor(const Color& base, int16_t lum) {
200 int b = static_cast<int>(base.channels.b) + lum;
201 int g = static_cast<int>(base.channels.g) + lum;
202 int r = static_cast<int>(base.channels.r) + lum;
203 Color color;
204 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255));
205 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255));
206 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255));
207 return color;
208 }
209
210 /**
211 * Calculates the error metric for two colors. A small error signals that the
212 * colors are similar to each other, a large error the signals the opposite.
213 */
214 inline uint32_t GetColorError(const Color& u, const Color& v) {
215 #ifdef USE_PERCEIVED_ERROR_METRIC
216 float delta_b = static_cast<float>(u.channels.b) - v.channels.b;
217 float delta_g = static_cast<float>(u.channels.g) - v.channels.g;
218 float delta_r = static_cast<float>(u.channels.r) - v.channels.r;
219 return static_cast<uint32_t>(0.299f * delta_b * delta_b +
220 0.587f * delta_g * delta_g +
221 0.114f * delta_r * delta_r);
222 #else
223 int delta_b = static_cast<int>(u.channels.b) - v.channels.b;
224 int delta_g = static_cast<int>(u.channels.g) - v.channels.g;
225 int delta_r = static_cast<int>(u.channels.r) - v.channels.r;
226 return delta_b * delta_b + delta_g * delta_g + delta_r * delta_r;
227 #endif
228 }
229
230 void GetAverageColor(const Color* src, float* avg_color) {
231 uint32_t sum_b = 0, sum_g = 0, sum_r = 0;
232
233 for (unsigned int i = 0; i < 8; ++i) {
234 sum_b += src[i].channels.b;
235 sum_g += src[i].channels.g;
236 sum_r += src[i].channels.r;
237 }
238
239 const float kInv8 = 1.0f / 8.0f;
240 avg_color[0] = static_cast<float>(sum_b) * kInv8;
241 avg_color[1] = static_cast<float>(sum_g) * kInv8;
242 avg_color[2] = static_cast<float>(sum_r) * kInv8;
243 }
244
245 void ComputeLuminance(uint8_t* block,
246 const Color* src,
247 const Color& base,
248 int sub_block_id,
249 const uint8_t* idx_to_num_tab) {
250 uint32_t best_tbl_err = std::numeric_limits<uint32_t>::max();
251 uint8_t best_tbl_idx = 0;
252 uint8_t best_mod_idx[8][8]; // [table][texel]
253
254 // Try all codeword tables to find the one giving the best results for this
255 // block.
256 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) {
257 // Pre-compute all the candidate colors; combinations of the base color and
258 // all available luminance values.
259 Color candidate_color[4]; // [modifier]
260 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
261 int16_t lum = g_codeword_tables[tbl_idx][mod_idx];
262 candidate_color[mod_idx] = MakeColor(base, lum);
263 }
264
265 uint32_t tbl_err = 0;
266
267 for (unsigned int i = 0; i < 8; ++i) {
268 // Try all modifiers in the current table to find which one gives the
269 // smallest error.
270 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max();
271 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
272 const Color& color = candidate_color[mod_idx];
273
274 uint32_t mod_err = GetColorError(src[i], color);
275 if (mod_err < best_mod_err) {
276 best_mod_idx[tbl_idx][i] = mod_idx;
277 best_mod_err = mod_err;
278
279 if (mod_err == 0)
280 break; // We cannot do any better than this.
281 }
282 }
283
284 tbl_err += best_mod_err;
285 if (tbl_err > best_tbl_err)
286 break; // We're already doing worse than the best table so skip.
287 }
288
289 if (tbl_err < best_tbl_err) {
290 best_tbl_err = tbl_err;
291 best_tbl_idx = tbl_idx;
292
293 if (tbl_err == 0)
294 break; // We cannot do any better than this.
295 }
296 }
297
298 WriteCodewordTable(block, sub_block_id, best_tbl_idx);
299
300 uint32_t pix_data = 0;
301
302 for (unsigned int i = 0; i < 8; ++i) {
303 uint8_t mod_idx = best_mod_idx[best_tbl_idx][i];
304 uint8_t pix_idx = g_mod_to_pix[mod_idx];
305
306 uint32_t lsb = pix_idx & 0x1;
307 uint32_t msb = pix_idx >> 1;
308
309 // Obtain the texel number as specified in the standard.
310 int texel_num = idx_to_num_tab[i];
311 pix_data |= msb << (texel_num + 16);
312 pix_data |= lsb << (texel_num);
313 }
314
315 WritePixelData(block, pix_data);
316 }
317
318 /**
319 * Tries to compress the block under the assumption that it's a single color
320 * block. If it's not the function will bail out without writing anything to
321 * the destination buffer.
322 */
323 bool TryCompressSolidBlock(uint8_t* dst, const Color* src) {
324 for (unsigned int i = 1; i < 16; ++i) {
325 if (src[i].bits != src[0].bits)
326 return false;
327 }
328
329 // Clear destination buffer so that we can "or" in the results.
330 memset(dst, 0, 8);
331
332 float src_color_float[3] = {static_cast<float>(src->channels.b),
333 static_cast<float>(src->channels.g),
334 static_cast<float>(src->channels.r)};
335 Color base = MakeColor555(src_color_float);
336
337 WriteDiff(dst, true);
338 WriteFlip(dst, false);
339 WriteColors555(dst, base, base);
340
341 uint8_t best_tbl_idx = 0;
342 uint8_t best_mod_idx = 0;
343 uint32_t best_mod_err = std::numeric_limits<uint32_t>::max();
344
345 // Try all codeword tables to find the one giving the best results for this
346 // block.
347 for (unsigned int tbl_idx = 0; tbl_idx < 8; ++tbl_idx) {
348 // Try all modifiers in the current table to find which one gives the
349 // smallest error.
350 for (unsigned int mod_idx = 0; mod_idx < 4; ++mod_idx) {
351 int16_t lum = g_codeword_tables[tbl_idx][mod_idx];
352 const Color& color = MakeColor(base, lum);
353
354 uint32_t mod_err = GetColorError(*src, color);
355 if (mod_err < best_mod_err) {
356 best_tbl_idx = tbl_idx;
357 best_mod_idx = mod_idx;
358 best_mod_err = mod_err;
359
360 if (mod_err == 0)
361 break; // We cannot do any better than this.
362 }
363 }
364
365 if (best_mod_err == 0)
366 break;
367 }
368
369 WriteCodewordTable(dst, 0, best_tbl_idx);
370 WriteCodewordTable(dst, 1, best_tbl_idx);
371
372 uint8_t pix_idx = g_mod_to_pix[best_mod_idx];
373 uint32_t lsb = pix_idx & 0x1;
374 uint32_t msb = pix_idx >> 1;
375
376 uint32_t pix_data = 0;
377 for (unsigned int i = 0; i < 2; ++i) {
378 for (unsigned int j = 0; j < 8; ++j) {
379 // Obtain the texel number as specified in the standard.
380 int texel_num = g_idx_to_num[i][j];
381 pix_data |= msb << (texel_num + 16);
382 pix_data |= lsb << (texel_num);
383 }
384 }
385
386 WritePixelData(dst, pix_data);
387 return true;
388 }
389
390 void CompressBlock(uint8_t* dst, const Color* ver_src, const Color* hor_src) {
391 if (TryCompressSolidBlock(dst, ver_src))
392 return;
393
394 const Color* sub_block_src[4] = {ver_src, ver_src + 8, hor_src, hor_src + 8};
395
396 Color sub_block_avg[4];
397 bool use_differential[2] = {true, true};
398
399 // Compute the average color for each sub block and determine if differential
400 // coding can be used.
401 for (unsigned int i = 0, j = 1; i < 4; i += 2, j += 2) {
402 float avg_color_0[3];
403 GetAverageColor(sub_block_src[i], avg_color_0);
404 Color avg_color_555_0 = MakeColor555(avg_color_0);
405
406 float avg_color_1[3];
407 GetAverageColor(sub_block_src[j], avg_color_1);
408 Color avg_color_555_1 = MakeColor555(avg_color_1);
409
410 for (unsigned int light_idx = 0; light_idx < 3; ++light_idx) {
411 int u = avg_color_555_0.components[light_idx] >> 3;
412 int v = avg_color_555_1.components[light_idx] >> 3;
413
414 int component_diff = v - u;
415 if (component_diff < -4 || component_diff > 3) {
416 use_differential[i / 2] = false;
417 sub_block_avg[i] = MakeColor444(avg_color_0);
418 sub_block_avg[j] = MakeColor444(avg_color_1);
419 } else {
420 sub_block_avg[i] = avg_color_555_0;
421 sub_block_avg[j] = avg_color_555_1;
422 }
423 }
424 }
425
426 // Compute the error of each sub block before adjusting for luminance. These
427 // error values are later used for determining if we should flip the sub
428 // block or not.
429 uint32_t sub_block_err[4] = {0};
430 for (unsigned int i = 0; i < 4; ++i) {
431 for (unsigned int j = 0; j < 8; ++j) {
432 sub_block_err[i] += GetColorError(sub_block_avg[i], sub_block_src[i][j]);
433 }
434 }
435
436 bool flip =
437 sub_block_err[2] + sub_block_err[3] < sub_block_err[0] + sub_block_err[1];
438
439 // Clear destination buffer so that we can "or" in the results.
440 memset(dst, 0, 8);
441
442 WriteDiff(dst, use_differential[!!flip]);
443 WriteFlip(dst, flip);
444
445 uint8_t sub_block_off_0 = flip ? 2 : 0;
446 uint8_t sub_block_off_1 = sub_block_off_0 + 1;
447
448 if (use_differential[!!flip]) {
449 WriteColors555(dst, sub_block_avg[sub_block_off_0],
450 sub_block_avg[sub_block_off_1]);
451 } else {
452 WriteColors444(dst, sub_block_avg[sub_block_off_0],
453 sub_block_avg[sub_block_off_1]);
454 }
455
456 // Compute luminance for the first sub block.
457 ComputeLuminance(dst, sub_block_src[sub_block_off_0],
458 sub_block_avg[sub_block_off_0], 0,
459 g_idx_to_num[sub_block_off_0]);
460 // Compute luminance for the second sub block.
461 ComputeLuminance(dst, sub_block_src[sub_block_off_1],
462 sub_block_avg[sub_block_off_1], 1,
463 g_idx_to_num[sub_block_off_1]);
464 }
465
466 void CompressTexture(const uint8_t* src, uint8_t* dst, int width, int height) {
467 DCHECK(width >= 4 && (width & 3) == 0);
468 DCHECK(height >= 4 && (height & 3) == 0);
469
470 Color ver_blocks[16];
471 Color hor_blocks[16];
472
473 for (int y = 0; y < height; y += 4, src += width * 4 * 4) {
474 for (int x = 0; x < width; x += 4, dst += 8) {
475 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4);
476 const Color* row1 = row0 + width;
477 const Color* row2 = row1 + width;
478 const Color* row3 = row2 + width;
479
480 memcpy(ver_blocks, row0, 8);
481 memcpy(ver_blocks + 2, row1, 8);
482 memcpy(ver_blocks + 4, row2, 8);
483 memcpy(ver_blocks + 6, row3, 8);
484 memcpy(ver_blocks + 8, row0 + 2, 8);
485 memcpy(ver_blocks + 10, row1 + 2, 8);
486 memcpy(ver_blocks + 12, row2 + 2, 8);
487 memcpy(ver_blocks + 14, row3 + 2, 8);
488
489 memcpy(hor_blocks, row0, 16);
490 memcpy(hor_blocks + 4, row1, 16);
491 memcpy(hor_blocks + 8, row2, 16);
492 memcpy(hor_blocks + 12, row3, 16);
493
494 CompressBlock(dst, ver_blocks, hor_blocks);
495 }
496 }
497 }
498
499 } // namespace etc1
OLDNEW
« no previous file with comments | « cc/resources/texture_compress/etc1.h ('k') | cc/resources/texture_compress/texture_compress.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698