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

Unified Diff: src/utils/SkTextureCompressor.cpp

Issue 373243002: Optimized R11 EAC compressor (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils/SkTextureCompressor.cpp
diff --git a/src/utils/SkTextureCompressor.cpp b/src/utils/SkTextureCompressor.cpp
index 52bf09afb8a3b52acd3d8ae29d3afe4a127b67b2..a8c3415c7efd93b4043998ab5d59c527d6c60c98 100644
--- a/src/utils/SkTextureCompressor.cpp
+++ b/src/utils/SkTextureCompressor.cpp
@@ -280,8 +280,8 @@ static uint64_t compress_latc_block(const uint8_t pixels[]) {
}
}
-static bool compress_a8_to_latc(uint8_t* dst, const uint8_t* src,
- int width, int height, int rowBytes) {
+static inline bool compress_a8_to_latc(uint8_t* dst, const uint8_t* src,
+ int width, int height, int rowBytes) {
return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_latc_block);
}
@@ -291,6 +291,10 @@ static bool compress_a8_to_latc(uint8_t* dst, const uint8_t* src,
//
////////////////////////////////////////////////////////////////////////////////
+// #define COMPRESS_R11_EAC_SLOW 1
+// #define COMPRESS_R11_EAC_FAST 1
+#define COMPRESS_R11_EAC_FASTEST 1
+
// Blocks compressed into R11 EAC are represented as follows:
// 0000000000000000000000000000000000000000000000000000000000000000
// |base_cw|mod|mul| ----------------- indices -------------------
@@ -327,6 +331,7 @@ static const int kR11EACModifierPalettes[kNumR11EACPalettes][kR11EACPaletteSize]
{-3, -5, -7, -9, 2, 4, 6, 8}
};
+#if COMPRESS_R11_EAC_SLOW
// Pack the base codeword, palette, and multiplier into the 64 bits necessary
// to decode it.
static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t multiplier,
@@ -354,7 +359,7 @@ static uint16_t compute_r11eac_pixel(int base_cw, int modifier, int multiplier)
// 2. Choose a multiplier based roughly on the size of the span of block values
// 3. Iterate through each palette and choose the one with the most accurate
// modifiers.
-static uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
+static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
// Find the center of the data...
uint16_t bmin = block[0];
uint16_t bmax = block[0];
@@ -432,7 +437,33 @@ static uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
// Finally, pack everything together...
return pack_r11eac_block(center, bestPalette, multiplier, bestIndices);
}
+#endif // COMPRESS_R11_EAC_SLOW
+
+#if COMPRESS_R11_EAC_FAST
robertphillips 2014/07/09 12:24:25 // This works by ... ?
krajcevski 2014/07/09 15:18:08 Done.
+static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
+ uint64_t retVal = static_cast<uint64_t>(0x8490) << 48;
+ for(int i = 0; i < 4; ++i) {
+ for(int j = 0; j < 4; ++j) {
+ const int shift = 45-3*(j*4+i);
+ SkASSERT(shift <= 45);
+ const uint64_t idx = block[i*4+j] >> 5;
+ SkASSERT(idx < 8);
+ switch(idx) {
robertphillips 2014/07/09 12:24:26 each of these guys on their own line ?
krajcevski 2014/07/09 15:18:07 Done.
+ case 0: case 1: case 2: case 3:
+ retVal |= (3-idx) << shift;
+ break;
+ default:
+ retVal |= idx << shift;
+ break;
+ }
+ }
+ }
+ return SkEndian_SwapBE64(retVal);
+}
+#endif // COMPRESS_R11_EAC_FAST
+
+#if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
static uint64_t compress_r11eac_block(const uint8_t block[16]) {
// Are all blocks a solid color?
bool solid = true;
@@ -443,62 +474,246 @@ static uint64_t compress_r11eac_block(const uint8_t block[16]) {
}
}
- // Fully transparent? We know the encoding...
- if (solid && 0 == block[0]) {
- // (0x0060 << 48) produces the following:
- // basw_cw: 0
- // mod: 6, palette: {-4, -7, -8, -11, 3, 6, 7, 10}
- // mod_val: -3
- //
- // this gives the following formula:
- // clamp[0, 2047](0*8+4+(-4)) = 0
- return SkEndian_SwapBE64(static_cast<uint64_t>(0x0060) << 48);
-
- // Fully opaque? We know this encoding too...
- } else if (solid && 255 == block[0]) {
- // -1 produces the following:
- // basw_cw: 255
- // mod: 15, palette: {-3, -5, -7, -9, 2, 4, 6, 8}
- // mod_val: 8
- //
- // this gives the following formula:
- // clamp[0, 2047](255*8+4+8*8*8) = clamp[0, 2047](2556) = 2047
- return static_cast<uint64_t>(-1);
+ if (solid) {
+ switch(block[0]) {
+ // Fully transparent? We know the encoding...
+ case 0:
+ // (0x0020 << 48) produces the following:
+ // basw_cw: 0
+ // mod: 0, palette: {-3, -6, -9, -15, 2, 5, 8, 14}
+ // multiplier: 2
+ // mod_val: -3
+ //
+ // this gives the following formula:
+ // clamp[0, 2047](0*8+4+(-3)*2*8) = 0
+ //
+ // Furthermore, it is impervious to endianness:
+ // 0x0020000000002000ULL
+ // Will produce one pixel with index 2, which gives:
+ // clamp[0, 2047](0*8+4+(-9)*2*8) = 0
+ return 0x0020000000002000ULL;
+
+ // Fully opaque? We know this encoding too...
+ case 255:
+
+ // -1 produces the following:
+ // basw_cw: 255
+ // mod: 15, palette: {-3, -5, -7, -9, 2, 4, 6, 8}
+ // mod_val: 8
+ //
+ // this gives the following formula:
+ // clamp[0, 2047](255*8+4+8*8*8) = clamp[0, 2047](2556) = 2047
+ return -1ULL;
+
+ default:
+ // !TODO! krajcevski:
+ // This will probably never happen, since we're using this format
+ // primarily for compressing alpha maps. Usually the only
+ // non-fullly opaque or fully transparent blocks are not a solid
+ // intermediate color. If we notice that they are, then we can
+ // add another optimization...
+ break;
+ }
}
-#if 0
- else if (solid) {
- // !TODO! krajcevski:
- // This will probably never happen, since we're using this format
- // primarily for compressing alpha maps. Usually the only
- // non-fullly opaque or fully transparent blocks are not a solid
- // intermediate color. If we notice that they are, then we can
- // add another optimization...
- }
+ return compress_heterogeneous_r11eac_block(block);
+}
+#endif // (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
+
+#if COMPRESS_R11_EAC_FASTEST
+static inline uint64_t interleave6(uint64_t a, uint64_t b) {
+ // If our block indices are laid out as:
+ // a b c d
+ // e f g h
+ // i j k l
+ // m n o p
+ //
robertphillips 2014/07/09 12:24:25 May want to pick different input names. 'a' and 'b
krajcevski 2014/07/09 15:18:08 Done.
+ // This function expects a and b to contain the first two rows interleaved
+ // in the least significant bits of a and b. In other words...
+ //
+ // If the architecture is big endian, then a and b will contain the following:
+ // Bits 0-31:
+ // a: 00 a e 00 b f 00 c g 00 d h
+ // b: 00 i m 00 j n 00 k o 00 l p
+ //
+ // If the architecture is little endian, then a and b will contain the following:
+ // Bits 0-31:
+ // a: 00 d h 00 c g 00 b f 00 a e
+ // b: 00 l p 00 k o 00 j n 00 i m
+ //
+ // This function returns a packing of the form:
+ // a e i m b f j n c g k o d h l p
+ //
+ // !SPEED! this function might be even faster if certain SIMD intrinsics are
+ // used..
+
+ // For both architectures, we can figure out a packing of the bits by
+ // using a shuffle and a few shift-rotates...
+ uint64_t x = (static_cast<uint64_t>(a) << 32) | static_cast<uint64_t>(b);
+
+ // x: 00 a e 00 b f 00 c g 00 d h 00 i m 00 j n 00 k o 00 l p
+
+ uint64_t t = (x ^ (x >> 10)) & 0x3FC0003FC00000ULL;
+ x = x ^ t ^ (t << 10);
+
+ // x: b f 00 00 00 a e c g i m 00 00 00 d h j n 00 k o 00 l p
+
+ x |= ((x << 52) & (0x3FULL << 52));
+ x = (x | ((x << 20) & (0x3FULL << 28))) >> 16;
+
+#if defined (SK_CPU_BENDIAN)
+ // x: 00 00 00 00 00 00 00 00 b f l p a e c g i m k o d h j n
+
+ t = (x ^ (x >> 6)) & 0xFC0000ULL;
+ x = x ^ t ^ (t << 6);
+
+ // x: 00 00 00 00 00 00 00 00 b f l p a e i m c g k o d h j n
+
+ t = (x ^ (x >> 36)) & 0x3FULL;
+ x = x ^ t ^ (t << 36);
+
+ // x: 00 00 00 00 00 00 00 00 b f j n a e i m c g k o d h l p
+
+ t = (x ^ (x >> 12)) & 0xFFF000000ULL;
+ x = x ^ t ^ (t << 12);
+
+ // x: 00 00 00 00 00 00 00 00 a e i m b f j n c g k o d h l p
+ return x;
+#else
+ // If our CPU is little endian, then the above logic will
+ // produce the following indices:
+ // x: 00 00 00 00 00 00 00 00 c g i m d h b f l p j n a e k o
+
+ t = (x ^ (x >> 6)) & 0xFC0000ULL;
+ x = x ^ t ^ (t << 6);
+
+ // x: 00 00 00 00 00 00 00 00 c g i m d h l p b f j n a e k o
+
+ t = (x ^ (x >> 36)) & 0xFC0ULL;
+ x = x ^ t ^ (t << 36);
+
+ // x: 00 00 00 00 00 00 00 00 a e i m d h l p b f j n c g k o
+
+ x = (x & (0xFFFULL << 36)) | ((x & 0xFFFFFFULL) << 12) | ((x >> 24) & 0xFFFULL);
+
+ // x: 00 00 00 00 00 00 00 00 a e i m b f j n c g k o d h l p
+
+ return x;
#endif
+}
- return compress_heterogeneous_r11eac_block(block);
+// This function converts an integer containing four bytes of alpha
+// values into an integer containing four bytes of indices into R11 EAC.
+// Note, there needs to be a mapping of indices:
+// 0 1 2 3 4 5 6 7
+// 3 2 1 0 4 5 6 7
+//
+// To compute this, we first negate each byte, and then add three, which
+// gives the mapping
+// 3 2 1 0 -1 -2 -3 -4
+//
+// Then we mask out the negative values, take their absolute value, and
+// add three.
+//
+// Most of the voodoo in this function comes from Hacker's Delight, section 2-18
robertphillips 2014/07/09 12:24:26 inline ?
krajcevski 2014/07/09 15:18:07 Done.
+static uint32_t convert_indices(uint32_t x) {
+ // Take the top three bits...
+ x = (x & 0xE0E0E0E0) >> 5;
+
+ // Negate...
+ x = ~((0x80808080 - x) ^ 0x7F7F7F7F);
+
+ // Add three
+ uint32_t s = (x & 0x7F7F7F7F) + 0x03030303;
+ x = ((x ^ 0x03030303) & 0x80808080) ^ s;
+
+ // Absolute value
+ uint32_t a = x & 0x80808080;
+
+ // Aside: mask negatives (m is three if the byte was negative)
+ uint32_t m = a >> 6;
+ m |= m >> 1;
+
+ // .. continue absolute value
+ uint32_t b = a >> 7;
+ x = (x ^ ((a - b) | a)) + b;
+
+ // Add three
+ return x + m;
}
-static bool compress_a8_to_r11eac(uint8_t* dst, const uint8_t* src,
- int width, int height, int rowBytes) {
+static uint64_t compress_r11eac_block_fast(const uint8_t* src, int rowBytes) {
robertphillips 2014/07/09 12:24:26 ri* -> inputRow* ? ri* -> alphaRow* ?
krajcevski 2014/07/09 15:18:08 Done.
+ const uint32_t ri1 = *(reinterpret_cast<const uint32_t*>(src));
+ const uint32_t ri2 = *(reinterpret_cast<const uint32_t*>(src + rowBytes));
+ const uint32_t ri3 = *(reinterpret_cast<const uint32_t*>(src + 2*rowBytes));
+ const uint32_t ri4 = *(reinterpret_cast<const uint32_t*>(src + 3*rowBytes));
+
+ if (ri1 == ri2 && ri1 == ri3 && ri1 == ri4) {
+ if (0 == ri1) {
robertphillips 2014/07/09 12:24:26 // Fully transparent block ?
krajcevski 2014/07/09 15:18:07 Done.
+ return 0x0020000000002000ULL;
+ } else if (0xFFFFFFFF == ri1) {
robertphillips 2014/07/09 12:24:25 // Fully opaque block ?
krajcevski 2014/07/09 15:18:08 Done.
+ return -1ULL;
+ }
+ }
+
robertphillips 2014/07/09 12:24:26 r* -> indexRow* ?
krajcevski 2014/07/09 15:18:08 Done.
+ const uint32_t r1 = convert_indices(ri1);
+ const uint32_t r2 = convert_indices(ri2);
+ const uint32_t r3 = convert_indices(ri3);
+ const uint32_t r4 = convert_indices(ri4);
robertphillips 2014/07/09 12:24:26 // comment ?
krajcevski 2014/07/09 15:18:07 Done.
+ const uint32_t r1r2 = (r1 << 3) | r2;
+ const uint32_t r3r4 = (r3 << 3) | r4;
+ const uint64_t indices = interleave6(r1r2, r3r4);
robertphillips 2014/07/09 12:24:25 // comment ?
krajcevski 2014/07/09 15:18:08 Done.
+ return SkEndian_SwapBE64(0x8490000000000000ULL | indices);
+}
+
+static bool compress_a8_to_r11eac_fast(uint8_t* dst, const uint8_t* src,
+ int width, int height, int rowBytes) {
+ // Make sure that our data is well-formed enough to be considered for compression
+ if (0 == width || 0 == height || (width % 4) != 0 || (height % 4) != 0) {
+ return false;
+ }
+
robertphillips 2014/07/09 12:24:25 const ?
krajcevski 2014/07/09 15:18:07 Done.
+ int blocksX = width >> 2;
+ int blocksY = height >> 2;
+
+ uint64_t* encPtr = reinterpret_cast<uint64_t*>(dst);
+ for (int y = 0; y < blocksY; ++y) {
+ for (int x = 0; x < blocksX; ++x) {
+ // Compress it
+ *encPtr = compress_r11eac_block_fast(src + 4*x, rowBytes);
+ ++encPtr;
+ }
+ src += 4 * rowBytes;
+ }
+ return true;
+}
+#endif // COMPRESS_R11_EAC_FASTEST
+
+static inline bool compress_a8_to_r11eac(uint8_t* dst, const uint8_t* src,
+ int width, int height, int rowBytes) {
+#if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_r11eac_block);
+#elif COMPRESS_R11_EAC_FASTEST
+ return compress_a8_to_r11eac_fast(dst, src, width, height, rowBytes);
+#else
+#error "Must choose R11 EAC algorithm"
+#endif
}
////////////////////////////////////////////////////////////////////////////////
namespace SkTextureCompressor {
-static size_t get_compressed_data_size(Format fmt, int width, int height) {
+static inline size_t get_compressed_data_size(Format fmt, int width, int height) {
switch (fmt) {
+ // These formats are 64 bits per 4x4 block.
case kR11_EAC_Format:
case kLATC_Format:
{
- // The LATC format is 64 bits per 4x4 block.
static const int kLATCEncodedBlockSize = 8;
- int blocksX = width / kLATCBlockSize;
- int blocksY = height / kLATCBlockSize;
+ const int blocksX = width / kLATCBlockSize;
+ const int blocksY = height / kLATCBlockSize;
return blocksX * blocksY * kLATCEncodedBlockSize;
}
@@ -520,7 +735,7 @@ bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcCol
kProcMap[kLATC_Format][kAlpha_8_SkColorType] = compress_a8_to_latc;
kProcMap[kR11_EAC_Format][kAlpha_8_SkColorType] = compress_a8_to_r11eac;
-
+
CompressBitmapProc proc = kProcMap[format][srcColorType];
if (NULL != proc) {
return proc(dst, src, width, height, rowBytes);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698