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

Unified Diff: src/utils/SkTextureCompressor_ASTC.cpp

Issue 417933005: Add preliminary ASTC encoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Code review changes Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/utils/SkTextureCompressor_ASTC.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..543ff57ddbef33a37a508d62373c816237354928
--- /dev/null
+++ b/src/utils/SkTextureCompressor_ASTC.cpp
@@ -0,0 +1,271 @@
+/*
+ * 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,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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) {
+ SkASSERT(x >= 0 && x < 12);
+ SkASSERT(y >= 0 && y < 12);
+ SkASSERT(rowBytes >= 12);
+ return *(src + y*rowBytes + x);
+}
+
+// Output the 16 bytes stored in top and bottom and advance the pointer. The bytes
+// are stored as the integers are represented in memory, so they should be swapped
+// if necessary.
+static inline void send_packing(uint8_t** dst, const uint64_t top, const uint64_t bottom) {
+ uint64_t* dst64 = reinterpret_cast<uint64_t*>(*dst);
+ dst64[0] = top;
+ dst64[1] = bottom;
+ *dst += 16;
+}
+
+// Compresses an ASTC block, by looking up the proper contributions from
+// k6x5To12x12Table and computing an index from the associated values.
+static void compress_a8_astc_block(uint8_t** dst, const uint8_t* src, int rowBytes) {
+ // Check for single color
+ bool constant = true;
+ const uint32_t firstInt = *(reinterpret_cast<const uint32_t*>(src));
+ for (int i = 0; i < 12; ++i) {
+ const uint32_t *rowInt = reinterpret_cast<const uint32_t *>(src + i*rowBytes);
+ constant = constant && (rowInt[0] == firstInt);
+ constant = constant && (rowInt[1] == firstInt);
+ constant = constant && (rowInt[2] == firstInt);
+ }
+
+ if (constant) {
+ if (0 == firstInt) {
+ // All of the indices are set to zero, and the colors are
+ // v0 = 0, v1 = 255, so everything will be transparent.
+ send_packing(dst, SkTEndian_SwapLE64(0x0000000001FE000173ULL), 0);
+ return;
+ } else if (0xFFFFFFFF == firstInt) {
+ // All of the indices are set to zero, and the colors are
+ // v0 = 255, v1 = 0, so everything will be opaque.
+ send_packing(dst, SkTEndian_SwapLE64(0x000000000001FE0173ULL), 0);
+ return;
+ }
+ }
+
+ uint8_t indices[30]; // 6x5 index grid
+ for (int idx = 0; idx < 30; ++idx) {
+ int weightTot = 0;
+ int alphaTot = 0;
+
+ for (int w = 0; w < 20; ++w) {
+ const int weight = k6x5To12x12Table[idx][w*3];
+ if (weight > 0) {
+ const int x = k6x5To12x12Table[idx][w*3 + 1];
+ const int y = k6x5To12x12Table[idx][w*3 + 2];
+ weightTot += weight;
+ alphaTot += weight * get_alpha(src, rowBytes, x, y);
+ }
+ }
+
+ indices[idx] = (alphaTot + (weightTot/2)) / weightTot;
+ }
+
+ // 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
+ // are currently stored in the indices variable.
+
+ uint64_t top = 0x0000000001FE000173ULL;
+ uint64_t bottom = 0;
+
+ for (int idx = 0; idx <= 20; ++idx) {
+ const uint8_t index = (indices[idx] & 0xE0) >> 5;
+ bottom |= static_cast<uint64_t>(index) << (61-(idx*3));
+ }
+
+ // index 21 straddles top and bottom
+ {
+ const uint8_t index = (indices[21] & 0xE0) >> 5;
+ bottom |= index & 1;
+ top |= static_cast<uint64_t>((index >> 2) | (index & 2)) << 62;
+ }
+
+ for (int idx = 22; idx < 30; ++idx) {
+ const uint8_t index = (indices[idx] & 0xE0) >> 5;
+ top |= static_cast<uint64_t>(index) << (59-(idx-22)*3);
+ }
+
+ // Reverse each 3-bit index since indices are read in reverse order...
+ uint64_t t = (bottom ^ (bottom >> 2)) & 0x2492492492492492ULL;
+ bottom = bottom ^ t ^ (t << 2);
+
+ t = (top ^ (top >> 2)) & 0x0924924000000000ULL;
+ top = top ^ t ^ (t << 2);
+
+ send_packing(dst, SkEndian_SwapLE64(top), SkEndian_SwapLE64(bottom));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+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 y = 0; y < height; y+=12) {
+ for (int x = 0; x < width; x+=12) {
+ compress_a8_astc_block(dstPtr, src + y*rowBytes + x, rowBytes);
+ }
+ }
+
+ return true;
+}
+
+SkBlitter* CreateASTCBlitter(int width, int height, void* outputBuffer) {
+ // TODO (krajcevski)
+ return NULL;
+}
+
+} // SkTextureCompressor
« no previous file with comments | « src/utils/SkTextureCompressor_ASTC.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698