Chromium Code Reviews| Index: src/utils/SkTextureCompressor_ASTC.cpp |
| diff --git a/src/utils/SkTextureCompressor_ASTC.cpp b/src/utils/SkTextureCompressor_ASTC.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d16af1b2a589c33c2e242c7303d63002a8c7674c |
| --- /dev/null |
| +++ b/src/utils/SkTextureCompressor_ASTC.cpp |
| @@ -0,0 +1,261 @@ |
| +/* |
| + * Copyright 2014 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkTextureCompressor_ASTC.h" |
| + |
| +#include "SkBlitter.h" |
| +#include "SkEndian.h" |
| + |
| +// This table contains the weight values for each texel. This is used in determining |
| +// how to convert a 12x12 grid of alpha values into a 6x5 grid of index values. Since |
| +// we have a 6x5 grid, that gives 30 values that we have to compute. For each index, |
| +// we store up to 20 different triplets of values. In order the triplets are: |
| +// weight, texel-x, texel-y |
| +// The weight value corresponds to the amount that this index contributes to the final |
| +// index value of the given texel. Hence, we need to reconstruct the 6x5 index grid |
| +// from their relative contribution to the 12x12 texel grid. |
| +// |
| +// The algorithm is something like this: |
| +// foreach index i: |
| +// total-weight = 0; |
| +// total-alpha = 0; |
| +// for w = 1 to 20: |
| +// weight = table[i][w*3]; |
| +// texel-x = table[i][w*3 + 1]; |
| +// texel-y = table[i][w*3 + 2]; |
| +// if weight >= 0: |
| +// total-weight += weight; |
| +// total-alpha += weight * alphas[texel-x][texel-y]; |
| +// |
| +// total-alpha /= total-weight; |
| +// index = top three bits of total-alpha |
| +// |
| +// If the associated index does not contribute to 20 different texels (e.g. it's in |
| +// a corner), then the extra texels are stored with -1's in the table. |
| + |
| +static const int k6x5To12x12Table[30][60] = { |
| +{ 16, 0, 0, 9, 1, 0, 1, 2, 0, 10, 0, 1, 6, 1, 1, 1, 2, 1, 4, 0, 2, 2, |
| + 1, 2, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 7, 1, 0, 15, 2, 0, 10, 3, 0, 3, 4, 0, 4, 1, 1, 9, 2, 1, 6, 3, 1, 2, |
| + 4, 1, 2, 1, 2, 4, 2, 2, 3, 3, 2, 1, 4, 2, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 6, 3, 0, 13, 4, 0, 12, 5, 0, 4, 6, 0, 4, 3, 1, 8, 4, 1, 8, 5, 1, 3, |
| + 6, 1, 1, 3, 2, 3, 4, 2, 3, 5, 2, 1, 6, 2, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 4, 5, 0, 12, 6, 0, 13, 7, 0, 6, 8, 0, 2, 5, 1, 7, 6, 1, 8, 7, 1, 4, |
| + 8, 1, 1, 5, 2, 3, 6, 2, 3, 7, 2, 2, 8, 2, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 3, 7, 0, 10, 8, 0, 15, 9, 0, 7, 10, 0, 2, 7, 1, 6, 8, 1, 9, 9, 1, 4, |
| + 10, 1, 1, 7, 2, 2, 8, 2, 4, 9, 2, 2, 10, 2, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 1, 9, 0, 9, 10, 0, 16, 11, 0, 1, 9, 1, 6, 10, 1, 10, 11, 1, 2, 10, 2, 4, |
| + 11, 2, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 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+{ 1, 5, 6, 2, 6, 6, 2, 7, 6, 1, 8, 6, 2, 5, 7, 7, 6, 7, 7, 7, 7, 3, |
| + 8, 7, 4, 5, 8, 11, 6, 8, 12, 7, 8, 6, 8, 8, 3, 5, 9, 9, 6, 9, 10, 7, |
| + 9, 5, 8, 9, 1, 5, 10, 4, 6, 10, 5, 7, 10, 2, 8, 10}, // n = 20 |
| +{ 1, 7, 6, 2, 8, 6, 3, 9, 6, 1, 10, 6, 2, 7, 7, 6, 8, 7, 8, 9, 7, 4, |
| + 10, 7, 3, 7, 8, 9, 8, 8, 14, 9, 8, 7, 10, 8, 2, 7, 9, 7, 8, 9, 11, 9, |
| + 9, 5, 10, 9, 1, 7, 10, 4, 8, 10, 6, 9, 10, 3, 10, 10}, // n = 20 |
| +{ 2, 10, 6, 3, 11, 6, 1, 9, 7, 5, 10, 7, 9, 11, 7, 1, 9, 8, 8, 10, 8, 15, |
| + 11, 8, 1, 9, 9, 7, 10, 9, 12, 11, 9, 3, 10, 10, 6, 11, 10, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 4, 0, 9, 2, 1, 9, 10, 0, 10, 6, 1, 10, 1, 2, 10, 16, 0, 11, 9, 1, 11, 1, |
| + 2, 11, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 2, 1, 9, 4, 2, 9, 2, 3, 9, 1, 4, 9, 4, 1, 10, 9, 2, 10, 6, 3, 10, 2, |
| + 4, 10, 7, 1, 11, 15, 2, 11, 10, 3, 11, 3, 4, 11, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 2, 3, 9, 3, 4, 9, 3, 5, 9, 1, 6, 9, 4, 3, 10, 8, 4, 10, 7, 5, 10, 2, |
| + 6, 10, 6, 3, 11, 13, 4, 11, 12, 5, 11, 4, 6, 11, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 1, 5, 9, 3, 6, 9, 3, 7, 9, 1, 8, 9, 3, 5, 10, 8, 6, 10, 8, 7, 10, 4, |
| + 8, 10, 4, 5, 11, 12, 6, 11, 13, 7, 11, 6, 8, 11, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 1, 7, 9, 3, 8, 9, 4, 9, 9, 2, 10, 9, 2, 7, 10, 6, 8, 10, 9, 9, 10, 4, |
| + 10, 10, 3, 7, 11, 10, 8, 11, 15, 9, 11, 7, 10, 11, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}, // n = 20 |
| +{ 2, 10, 9, 4, 11, 9, 1, 9, 10, 6, 10, 10, 10, 11, 10, 1, 9, 11, 9, 10, 11, 16, |
| + 11, 11, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, |
| + 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0} // n = 20 |
| +}; |
| + |
| +// Returns the alpha value of a texel at position (x, y) from src. |
| +// (x, y) are assumed to be in the range [0, 12). |
| +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.
|
| + return *(src + y*rowBytes + x); |
| +} |
| + |
| +// Compresses an ASTC block, by looking up the proper contributions from |
| +// k6x5To12x12Table and computing an index from the associated values. |
| +void compress_a8_astc_block(uint8_t** dst, const uint8_t* src, int rowBytes) { |
| + 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'
|
| + for (int j = 0; j < 5; ++j) { |
| + for (int i = 0; i < 6; ++i) { |
| + const int idx = j*6+i; |
|
robertphillips
2014/07/24 17:47:39
weightTot, alphaTot ?
krajcevski
2014/07/24 19:59:08
Done.
|
| + int wtotal = 0; |
| + int atotal = 0; |
| + for (int w = 0; w < 20; ++w) { |
| + const int weight = k6x5To12x12Table[idx][w*3]; |
| + const int x = k6x5To12x12Table[idx][w*3 + 1]; |
| + const int y = k6x5To12x12Table[idx][w*3 + 2]; |
| + if (weight > 0) { |
| + wtotal += weight; |
| + atotal += weight * get_alpha(src, rowBytes, x, y); |
| + } |
| + } |
| + |
| + atotal = (atotal + (wtotal/2)) / wtotal; |
| + indices[j][i] = atotal; |
| + } |
| + } |
| + |
| + // Pack indices... The ASTC block layout is fairly complicated. An extensive |
| + // description can be found here: |
| + // https://www.opengl.org/registry/specs/KHR/texture_compression_astc_hdr.txt |
| + // |
| + // Here is a summary of the options that we've chosen: |
| + // 1. Block mode: 0b00101110011 |
| + // - 6x5 texel grid |
| + // - Single plane |
| + // - Low-precision index values |
| + // - Index range 0-7 (three bits per index) |
| + // 2. Partitions: 0b00 |
| + // - One partition |
| + // 3. Color Endpoint Mode: 0b0000 |
| + // - Direct luminance -- e0=(v0,v0,v0,0xFF); e1=(v1,v1,v1,0xFF); |
| + // 4. 8-bit endpoints: |
| + // v0 = 0, v1 = 255 |
| + // |
| + // 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.
|
| + // is currently stored in the indices[5][6] varaible. |
| + uint64_t top = 0x0000000001FE000173ULL; |
| + uint64_t bottom = 0; |
| + int idx = 0; |
| + for (int j = 0; j < 5; ++j) { |
| + for (int i = 0; i < 6; ++i) { |
| + // This is our index. |
| + uint8_t index = (indices[j][i] & 0xE0) >> 5; |
| + |
| + // Since we read from the end of the block in reverse, the three-bit values |
| + // 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
|
| + switch(index) { |
| + case 0: |
| + case 2: |
| + case 5: |
| + case 7: |
| + // Do nothing |
| + break; |
| + |
| + case 1: |
| + index = 4; |
| + break; |
| + case 4: |
| + index = 1; |
| + break; |
| + case 3: |
| + index = 6; |
| + break; |
| + case 6: |
| + index = 3; |
| + break; |
| + } |
| + |
| + // Finally, place the index in the proper place in the block based |
| + // on raster order. |
| + if (idx <= 20) { |
| + bottom |= static_cast<uint64_t>(index) << (61-idx*3); |
| + } else if (idx > 21) { |
| + top |= static_cast<uint64_t>(index) << (59-(idx-22)*3); |
| + } else { |
| + SkASSERT(21 == idx); |
| + bottom |= index >> 2; |
| + top |= static_cast<uint64_t>(index) << 62; |
| + } |
| + |
| + idx++; |
| + } |
| + } |
| + |
| + uint64_t* dst64 = reinterpret_cast<uint64_t*>(*dst); |
| + dst64[0] = SkEndian_SwapLE64(top); |
| + dst64[1] = SkEndian_SwapLE64(bottom); |
| + |
| + *dst += 16; |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +namespace SkTextureCompressor { |
| + |
| +bool CompressA8To12x12ASTC(uint8_t* dst, const uint8_t* src, int width, int height, int rowBytes) { |
| + if (width < 0 || ((width % 12) != 0) || height < 0 || ((height % 12) != 0)) { |
| + return false; |
| + } |
| + |
| + uint8_t** dstPtr = &dst; |
| + for (int j = 0; j < height; j+=12) { |
| + for (int i = 0; i < width; i+=12) { |
| + compress_a8_astc_block(dstPtr, src + j*rowBytes + i, rowBytes); |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +SkBlitter* CreateASTCBlitter(int width, int height, void* outputBuffer) { |
| + // TODO (krajcevski) |
| + return NULL; |
| +} |
| + |
| +} // SkTextureCompressor |