OLD | NEW |
---|---|
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 #ifndef CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ | 5 #ifndef CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ |
6 #define CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ | 6 #define CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ |
7 | 7 |
8 #include "cc/resources/texture_compressor.h" | 8 #include "cc/resources/texture_compressor.h" |
9 | 9 |
10 #include "base/compiler_specific.h" | |
11 #include "base/logging.h" | |
12 | |
13 namespace { | |
reveman
2015/05/06 18:44:00
hm, I don't think we ever use anonymous namespace
radu.velea
2015/05/07 11:21:39
Done.
| |
14 | |
15 template <typename T> | |
16 inline T clamp(T val, T min, T max) { | |
17 return val < min ? min : (val > max ? max : val); | |
18 } | |
19 | |
20 inline uint8_t round_to_5_bits(float val) { | |
21 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31); | |
22 } | |
23 | |
24 inline uint8_t round_to_4_bits(float val) { | |
25 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15); | |
26 } | |
27 | |
28 union Color { | |
29 struct BgraColorType { | |
30 uint8_t b; | |
31 uint8_t g; | |
32 uint8_t r; | |
33 uint8_t a; | |
34 } channels; | |
35 uint8_t components[4]; | |
36 uint32_t bits; | |
37 }; | |
38 | |
39 /* | |
40 * Codeword tables. | |
41 * See: Table 3.17.2 | |
42 */ | |
reveman
2015/05/06 18:44:00
This was wrong before your patch but do you mind c
radu.velea
2015/05/07 11:21:39
Done.
| |
43 ALIGNAS(16) static const int16_t g_codeword_tables[8][4] = { | |
44 {-8, -2, 2, 8}, | |
45 {-17, -5, 5, 17}, | |
46 {-29, -9, 9, 29}, | |
47 {-42, -13, 13, 42}, | |
48 {-60, -18, 18, 60}, | |
49 {-80, -24, 24, 80}, | |
50 {-106, -33, 33, 106}, | |
51 {-183, -47, 47, 183}}; | |
52 | |
53 /* | |
54 * Maps modifier indices to pixel index values. | |
55 * See: Table 3.17.3 | |
56 */ | |
57 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1}; | |
58 | |
59 /* | |
60 * The ETC1 specification index texels as follows: | |
61 * | |
62 * [a][e][i][m] [ 0][ 4][ 8][12] | |
63 * [b][f][j][n] <-> [ 1][ 5][ 9][13] | |
64 * [c][g][k][o] [ 2][ 6][10][14] | |
65 * [d][h][l][p] [ 3][ 7][11][15] | |
66 * | |
67 * [ 0][ 1][ 2][ 3] [ 0][ 4][ 4][ 5] | |
68 * [ 4][ 5][ 6][ 7] <-> [ 8][ 9][12][13] | |
69 * [ 8][ 9][10][11] [ 2][ 3][ 6][ 7] | |
70 * [12][13][14][15] [10][11][14][15] | |
71 * | |
72 * However, when extracting sub blocks from BGRA data the natural array | |
73 * indexing order ends up different: | |
74 * | |
75 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m] | |
76 * [c][g][d][h] [b][f][j][n] | |
77 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o] | |
78 * [k][o][l][p] [d][h][l][p] | |
79 * | |
80 * In order to translate from the natural array indices in a sub block to the | |
81 * indices (number) used by specification and hardware we use this table. | |
82 */ | |
83 static const uint8_t g_idx_to_num[4][8] = { | |
84 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0. | |
85 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1. | |
86 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0. | |
87 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1. | |
88 }; | |
89 | |
90 inline void WriteColors444(uint8_t* block, | |
91 const Color& color0, | |
92 const Color& color1) { | |
93 /* for BGRA textures */ | |
94 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4); | |
95 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4); | |
96 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4); | |
97 } | |
98 | |
99 inline void WriteColors555(uint8_t* block, | |
100 const Color& color0, | |
101 const Color& color1) { | |
102 // Table for conversion to 3-bit two complement format. | |
103 static const uint8_t two_compl_trans_table[8] = { | |
104 4, // -4 (100b) | |
105 5, // -3 (101b) | |
106 6, // -2 (110b) | |
107 7, // -1 (111b) | |
108 0, // 0 (000b) | |
109 1, // 1 (001b) | |
110 2, // 2 (010b) | |
111 3, // 3 (011b) | |
112 }; | |
113 | |
114 int16_t delta_r = | |
115 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3); | |
116 int16_t delta_g = | |
117 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3); | |
118 int16_t delta_b = | |
119 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3); | |
120 DCHECK(delta_r >= -4 && delta_r <= 3); | |
121 DCHECK(delta_g >= -4 && delta_g <= 3); | |
122 DCHECK(delta_b >= -4 && delta_b <= 3); | |
123 | |
124 /* For BGRA textures */ | |
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 /* added to distinguish between expanded 555 and 444 colors */ | |
173 bgr444.channels.a = 0x44; | |
174 return bgr444; | |
175 } | |
176 | |
177 /** | |
178 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is | |
179 * expanded to BGR888 as it would be in hardware after decompression. The | |
180 * actual 555-bit data is available in the five most significant bits of each | |
181 * channel. | |
182 */ | |
183 inline Color MakeColor555(const float* bgr) { | |
184 uint8_t b5 = round_to_5_bits(bgr[0]); | |
185 uint8_t g5 = round_to_5_bits(bgr[1]); | |
186 uint8_t r5 = round_to_5_bits(bgr[2]); | |
187 Color bgr555; | |
188 bgr555.channels.b = (b5 << 3) | (b5 >> 2); | |
189 bgr555.channels.g = (g5 << 3) | (g5 >> 2); | |
190 bgr555.channels.r = (r5 << 3) | (r5 >> 2); | |
191 /* added to distinguish between expanded 555 and 444 colors */ | |
192 bgr555.channels.a = 0x55; | |
193 return bgr555; | |
194 } | |
195 | |
196 } // namespace | |
197 | |
10 namespace cc { | 198 namespace cc { |
11 | 199 |
12 class CC_EXPORT TextureCompressorETC1 : public TextureCompressor { | 200 class CC_EXPORT TextureCompressorETC1 : public TextureCompressor { |
13 public: | 201 public: |
14 TextureCompressorETC1() {} | 202 TextureCompressorETC1() {} |
15 | 203 |
16 // Compress a texture using ETC1. Note that the |quality| parameter is | 204 // Compress a texture using ETC1. Note that the |quality| parameter is |
17 // ignored. The current implementation does not support different quality | 205 // ignored. The current implementation does not support different quality |
18 // settings. | 206 // settings. |
19 void Compress(const uint8_t* src, | 207 void Compress(const uint8_t* src, |
20 uint8_t* dst, | 208 uint8_t* dst, |
21 int width, | 209 int width, |
22 int height, | 210 int height, |
23 Quality quality) override; | 211 Quality quality) override; |
24 | 212 |
25 private: | 213 private: |
26 DISALLOW_COPY_AND_ASSIGN(TextureCompressorETC1); | 214 DISALLOW_COPY_AND_ASSIGN(TextureCompressorETC1); |
27 }; | 215 }; |
28 | 216 |
29 } // namespace cc | 217 } // namespace cc |
30 | 218 |
31 #endif // CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ | 219 #endif // CC_RESOURCES_TEXTURE_COMPRESSOR_ETC1_H_ |
OLD | NEW |