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

Side by Side Diff: src/utils/SkTextureCompressor_ASTC.cpp

Issue 417933005: Add preliminary ASTC encoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix comments Created 6 years, 4 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
(Empty)
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkTextureCompressor_ASTC.h"
9
10 #include "SkBlitter.h"
11 #include "SkEndian.h"
12
13 // This table contains the weight values for each texel. This is used in determi ning
14 // how to convert a 12x12 grid of alpha values into a 6x5 grid of index values. Since
15 // we have a 6x5 grid, that gives 30 values that we have to compute. For each in dex,
16 // we store up to 20 different triplets of values. In order the triplets are:
17 // weight, texel-x, texel-y
18 // The weight value corresponds to the amount that this index contributes to the final
19 // index value of the given texel. Hence, we need to reconstruct the 6x5 index g rid
20 // from their relative contribution to the 12x12 texel grid.
21 //
22 // The algorithm is something like this:
23 // foreach index i:
24 // total-weight = 0;
25 // total-alpha = 0;
26 // for w = 1 to 20:
27 // weight = table[i][w*3];
28 // texel-x = table[i][w*3 + 1];
29 // texel-y = table[i][w*3 + 2];
30 // if weight >= 0:
31 // total-weight += weight;
32 // total-alpha += weight * alphas[texel-x][texel-y];
33 //
34 // total-alpha /= total-weight;
35 // index = top three bits of total-alpha
36 //
37 // If the associated index does not contribute to 20 different texels (e.g. it's in
38 // a corner), then the extra texels are stored with -1's in the table.
39
40 static const int k6x5To12x12Table[30][60] = {
41 { 16, 0, 0, 9, 1, 0, 1, 2, 0, 10, 0, 1, 6, 1, 1, 1, 2, 1, 4, 0, 2, 2,
42 1, 2, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
43 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
44 { 7, 1, 0, 15, 2, 0, 10, 3, 0, 3, 4, 0, 4, 1, 1, 9, 2, 1, 6, 3, 1, 2,
45 4, 1, 2, 1, 2, 4, 2, 2, 3, 3, 2, 1, 4, 2, -1, 0, 0, -1, 0, 0, -1, 0,
46 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
47 { 6, 3, 0, 13, 4, 0, 12, 5, 0, 4, 6, 0, 4, 3, 1, 8, 4, 1, 8, 5, 1, 3,
48 6, 1, 1, 3, 2, 3, 4, 2, 3, 5, 2, 1, 6, 2, -1, 0, 0, -1, 0, 0, -1, 0,
49 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
50 { 4, 5, 0, 12, 6, 0, 13, 7, 0, 6, 8, 0, 2, 5, 1, 7, 6, 1, 8, 7, 1, 4,
51 8, 1, 1, 5, 2, 3, 6, 2, 3, 7, 2, 2, 8, 2, -1, 0, 0, -1, 0, 0, -1, 0,
52 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
53 { 3, 7, 0, 10, 8, 0, 15, 9, 0, 7, 10, 0, 2, 7, 1, 6, 8, 1, 9, 9, 1, 4,
54 10, 1, 1, 7, 2, 2, 8, 2, 4, 9, 2, 2, 10, 2, -1, 0, 0, -1, 0, 0, -1, 0,
55 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
56 { 1, 9, 0, 9, 10, 0, 16, 11, 0, 1, 9, 1, 6, 10, 1, 10, 11, 1, 2, 10, 2, 4,
57 11, 2, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
58 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1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 9, 5, 8, 9, 1, 5, 10, 4, 6, 10, 5, 7, 10, 2, 8, 10}, // n = 20
107 { 1, 7, 6, 2, 8, 6, 3, 9, 6, 1, 10, 6, 2, 7, 7, 6, 8, 7, 8, 9, 7, 4,
108 10, 7, 3, 7, 8, 9, 8, 8, 14, 9, 8, 7, 10, 8, 2, 7, 9, 7, 8, 9, 11, 9,
109 9, 5, 10, 9, 1, 7, 10, 4, 8, 10, 6, 9, 10, 3, 10, 10}, // n = 20
110 { 2, 10, 6, 3, 11, 6, 1, 9, 7, 5, 10, 7, 9, 11, 7, 1, 9, 8, 8, 10, 8, 15,
111 11, 8, 1, 9, 9, 7, 10, 9, 12, 11, 9, 3, 10, 10, 6, 11, 10, -1, 0, 0, -1, 0,
112 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
113 { 4, 0, 9, 2, 1, 9, 10, 0, 10, 6, 1, 10, 1, 2, 10, 16, 0, 11, 9, 1, 11, 1,
114 2, 11, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
115 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
116 { 2, 1, 9, 4, 2, 9, 2, 3, 9, 1, 4, 9, 4, 1, 10, 9, 2, 10, 6, 3, 10, 2,
117 4, 10, 7, 1, 11, 15, 2, 11, 10, 3, 11, 3, 4, 11, -1, 0, 0, -1, 0, 0, -1, 0,
118 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
119 { 2, 3, 9, 3, 4, 9, 3, 5, 9, 1, 6, 9, 4, 3, 10, 8, 4, 10, 7, 5, 10, 2,
120 6, 10, 6, 3, 11, 13, 4, 11, 12, 5, 11, 4, 6, 11, -1, 0, 0, -1, 0, 0, -1, 0,
121 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
122 { 1, 5, 9, 3, 6, 9, 3, 7, 9, 1, 8, 9, 3, 5, 10, 8, 6, 10, 8, 7, 10, 4,
123 8, 10, 4, 5, 11, 12, 6, 11, 13, 7, 11, 6, 8, 11, -1, 0, 0, -1, 0, 0, -1, 0,
124 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
125 { 1, 7, 9, 3, 8, 9, 4, 9, 9, 2, 10, 9, 2, 7, 10, 6, 8, 10, 9, 9, 10, 4,
126 10, 10, 3, 7, 11, 10, 8, 11, 15, 9, 11, 7, 10, 11, -1, 0, 0, -1, 0, 0, -1, 0,
127 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20
128 { 2, 10, 9, 4, 11, 9, 1, 9, 10, 6, 10, 10, 10, 11, 10, 1, 9, 11, 9, 10, 11, 16,
129 11, 11, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
130 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0} // n = 20
131 };
132
133 // Returns the alpha value of a texel at position (x, y) from src.
134 // (x, y) are assumed to be in the range [0, 12).
135 uint8_t get_alpha(const uint8_t *src, int rowBytes, int x, int y) {
robertphillips 2014/07/24 17:47:39 SkASSERT the x & y assumptions here ?
krajcevski 2014/07/24 19:59:08 Done.
136 return *(src + y*rowBytes + x);
137 }
138
139 // Compresses an ASTC block, by looking up the proper contributions from
140 // k6x5To12x12Table and computing an index from the associated values.
141 void compress_a8_astc_block(uint8_t** dst, const uint8_t* src, int rowBytes) {
142 uint8_t indices[5][6];
robertphillips 2014/07/24 17:47:39 Any reason you have the outer loop variable as 'j'
krajcevski 2014/07/24 19:59:08 Actually, we can just loop from 0 to 30, since we'
143 for (int j = 0; j < 5; ++j) {
144 for (int i = 0; i < 6; ++i) {
145 const int idx = j*6+i;
robertphillips 2014/07/24 17:47:39 weightTot, alphaTot ?
krajcevski 2014/07/24 19:59:08 Done.
146 int wtotal = 0;
147 int atotal = 0;
148 for (int w = 0; w < 20; ++w) {
149 const int weight = k6x5To12x12Table[idx][w*3];
150 const int x = k6x5To12x12Table[idx][w*3 + 1];
151 const int y = k6x5To12x12Table[idx][w*3 + 2];
152 if (weight > 0) {
153 wtotal += weight;
154 atotal += weight * get_alpha(src, rowBytes, x, y);
155 }
156 }
157
158 atotal = (atotal + (wtotal/2)) / wtotal;
159 indices[j][i] = atotal;
160 }
161 }
162
163 // Pack indices... The ASTC block layout is fairly complicated. An extensive
164 // description can be found here:
165 // https://www.opengl.org/registry/specs/KHR/texture_compression_astc_hdr.tx t
166 //
167 // Here is a summary of the options that we've chosen:
168 // 1. Block mode: 0b00101110011
169 // - 6x5 texel grid
170 // - Single plane
171 // - Low-precision index values
172 // - Index range 0-7 (three bits per index)
173 // 2. Partitions: 0b00
174 // - One partition
175 // 3. Color Endpoint Mode: 0b0000
176 // - Direct luminance -- e0=(v0,v0,v0,0xFF); e1=(v1,v1,v1,0xFF);
177 // 4. 8-bit endpoints:
178 // v0 = 0, v1 = 255
179 //
180 // The rest of the block contains the 30 index values from before, which
robertphillips 2014/07/24 17:47:39 typo
krajcevski 2014/07/24 19:59:07 Done.
181 // is currently stored in the indices[5][6] varaible.
182 uint64_t top = 0x0000000001FE000173ULL;
183 uint64_t bottom = 0;
184 int idx = 0;
185 for (int j = 0; j < 5; ++j) {
186 for (int i = 0; i < 6; ++i) {
187 // This is our index.
188 uint8_t index = (indices[j][i] & 0xE0) >> 5;
189
190 // Since we read from the end of the block in reverse, the three-bit values
191 // need to have their bits reversed
robertphillips 2014/07/24 17:47:39 I don't know if this can land without a bit twiddl
krajcevski 2014/07/24 19:59:08 Yea, that's kind of ugly. I changed it so that we
192 switch(index) {
193 case 0:
194 case 2:
195 case 5:
196 case 7:
197 // Do nothing
198 break;
199
200 case 1:
201 index = 4;
202 break;
203 case 4:
204 index = 1;
205 break;
206 case 3:
207 index = 6;
208 break;
209 case 6:
210 index = 3;
211 break;
212 }
213
214 // Finally, place the index in the proper place in the block based
215 // on raster order.
216 if (idx <= 20) {
217 bottom |= static_cast<uint64_t>(index) << (61-idx*3);
218 } else if (idx > 21) {
219 top |= static_cast<uint64_t>(index) << (59-(idx-22)*3);
220 } else {
221 SkASSERT(21 == idx);
222 bottom |= index >> 2;
223 top |= static_cast<uint64_t>(index) << 62;
224 }
225
226 idx++;
227 }
228 }
229
230 uint64_t* dst64 = reinterpret_cast<uint64_t*>(*dst);
231 dst64[0] = SkEndian_SwapLE64(top);
232 dst64[1] = SkEndian_SwapLE64(bottom);
233
234 *dst += 16;
235 }
236
237 ////////////////////////////////////////////////////////////////////////////////
238
239 namespace SkTextureCompressor {
240
241 bool CompressA8To12x12ASTC(uint8_t* dst, const uint8_t* src, int width, int heig ht, int rowBytes) {
242 if (width < 0 || ((width % 12) != 0) || height < 0 || ((height % 12) != 0)) {
243 return false;
244 }
245
246 uint8_t** dstPtr = &dst;
247 for (int j = 0; j < height; j+=12) {
248 for (int i = 0; i < width; i+=12) {
249 compress_a8_astc_block(dstPtr, src + j*rowBytes + i, rowBytes);
250 }
251 }
252
253 return true;
254 }
255
256 SkBlitter* CreateASTCBlitter(int width, int height, void* outputBuffer) {
257 // TODO (krajcevski)
258 return NULL;
259 }
260
261 } // SkTextureCompressor
OLDNEW
« src/utils/SkTextureCompressor.h ('K') | « src/utils/SkTextureCompressor_ASTC.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698