OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTextureCompressor.h" | 8 #include "SkTextureCompressor.h" |
9 #include "SkTextureCompressor_Blitter.h" | 9 #include "SkTextureCompressor_Blitter.h" |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 // clamp[0, 2047](base_cw * 8 + 4 + mod_val*mul*8) | 22 // clamp[0, 2047](base_cw * 8 + 4 + mod_val*mul*8) |
23 // | 23 // |
24 // mod_val is chosen from a palette of values based on the index of the | 24 // mod_val is chosen from a palette of values based on the index of the |
25 // given pixel. The palette is chosen by the value stored in mod. | 25 // given pixel. The palette is chosen by the value stored in mod. |
26 // This formula returns a value between 0 and 2047, which is converted | 26 // This formula returns a value between 0 and 2047, which is converted |
27 // to a float from 0 to 1 in OpenGL. | 27 // to a float from 0 to 1 in OpenGL. |
28 // | 28 // |
29 // If mul is zero, then we set mul = 1/8, so that the formula becomes | 29 // If mul is zero, then we set mul = 1/8, so that the formula becomes |
30 // clamp[0, 2047](base_cw * 8 + 4 + mod_val) | 30 // clamp[0, 2047](base_cw * 8 + 4 + mod_val) |
31 | 31 |
| 32 #if COMPRESS_R11_EAC_SLOW |
| 33 |
32 static const int kNumR11EACPalettes = 16; | 34 static const int kNumR11EACPalettes = 16; |
33 static const int kR11EACPaletteSize = 8; | 35 static const int kR11EACPaletteSize = 8; |
34 static const int kR11EACModifierPalettes[kNumR11EACPalettes][kR11EACPaletteSize]
= { | 36 static const int kR11EACModifierPalettes[kNumR11EACPalettes][kR11EACPaletteSize]
= { |
35 {-3, -6, -9, -15, 2, 5, 8, 14}, | 37 {-3, -6, -9, -15, 2, 5, 8, 14}, |
36 {-3, -7, -10, -13, 2, 6, 9, 12}, | 38 {-3, -7, -10, -13, 2, 6, 9, 12}, |
37 {-2, -5, -8, -13, 1, 4, 7, 12}, | 39 {-2, -5, -8, -13, 1, 4, 7, 12}, |
38 {-2, -4, -6, -13, 1, 3, 5, 12}, | 40 {-2, -4, -6, -13, 1, 3, 5, 12}, |
39 {-3, -6, -8, -12, 2, 5, 7, 11}, | 41 {-3, -6, -8, -12, 2, 5, 7, 11}, |
40 {-3, -7, -9, -11, 2, 6, 8, 10}, | 42 {-3, -7, -9, -11, 2, 6, 8, 10}, |
41 {-4, -7, -8, -11, 3, 6, 7, 10}, | 43 {-4, -7, -8, -11, 3, 6, 7, 10}, |
42 {-3, -5, -8, -11, 2, 4, 7, 10}, | 44 {-3, -5, -8, -11, 2, 4, 7, 10}, |
43 {-2, -6, -8, -10, 1, 5, 7, 9}, | 45 {-2, -6, -8, -10, 1, 5, 7, 9}, |
44 {-2, -5, -8, -10, 1, 4, 7, 9}, | 46 {-2, -5, -8, -10, 1, 4, 7, 9}, |
45 {-2, -4, -8, -10, 1, 3, 7, 9}, | 47 {-2, -4, -8, -10, 1, 3, 7, 9}, |
46 {-2, -5, -7, -10, 1, 4, 6, 9}, | 48 {-2, -5, -7, -10, 1, 4, 6, 9}, |
47 {-3, -4, -7, -10, 2, 3, 6, 9}, | 49 {-3, -4, -7, -10, 2, 3, 6, 9}, |
48 {-1, -2, -3, -10, 0, 1, 2, 9}, | 50 {-1, -2, -3, -10, 0, 1, 2, 9}, |
49 {-4, -6, -8, -9, 3, 5, 7, 8}, | 51 {-4, -6, -8, -9, 3, 5, 7, 8}, |
50 {-3, -5, -7, -9, 2, 4, 6, 8} | 52 {-3, -5, -7, -9, 2, 4, 6, 8} |
51 }; | 53 }; |
52 | 54 |
53 #if COMPRESS_R11_EAC_SLOW | |
54 | |
55 // Pack the base codeword, palette, and multiplier into the 64 bits necessary | 55 // Pack the base codeword, palette, and multiplier into the 64 bits necessary |
56 // to decode it. | 56 // to decode it. |
57 static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t m
ultiplier, | 57 static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t m
ultiplier, |
58 uint64_t indices) { | 58 uint64_t indices) { |
59 SkASSERT(palette < 16); | 59 SkASSERT(palette < 16); |
60 SkASSERT(multiplier < 16); | 60 SkASSERT(multiplier < 16); |
61 SkASSERT(indices < (static_cast<uint64_t>(1) << 48)); | 61 SkASSERT(indices < (static_cast<uint64_t>(1) << 48)); |
62 | 62 |
63 const uint64_t b = static_cast<uint64_t>(base_cw) << 56; | 63 const uint64_t b = static_cast<uint64_t>(base_cw) << 56; |
64 const uint64_t m = static_cast<uint64_t>(multiplier) << 52; | 64 const uint64_t m = static_cast<uint64_t>(multiplier) << 52; |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 const uint32_t packedIndexColumn2 = pack_indices_vertical(indexColumn2); | 550 const uint32_t packedIndexColumn2 = pack_indices_vertical(indexColumn2); |
551 const uint32_t packedIndexColumn3 = pack_indices_vertical(indexColumn3); | 551 const uint32_t packedIndexColumn3 = pack_indices_vertical(indexColumn3); |
552 | 552 |
553 *dst = SkEndian_SwapBE64(0x8490000000000000ULL | | 553 *dst = SkEndian_SwapBE64(0x8490000000000000ULL | |
554 (static_cast<uint64_t>(packedIndexColumn0) << 36) | | 554 (static_cast<uint64_t>(packedIndexColumn0) << 36) | |
555 (static_cast<uint64_t>(packedIndexColumn1) << 24) | | 555 (static_cast<uint64_t>(packedIndexColumn1) << 24) | |
556 static_cast<uint64_t>(packedIndexColumn2 << 12) | | 556 static_cast<uint64_t>(packedIndexColumn2 << 12) | |
557 static_cast<uint64_t>(packedIndexColumn3)); | 557 static_cast<uint64_t>(packedIndexColumn3)); |
558 } | 558 } |
559 | 559 |
560 static inline int get_r11_eac_index(uint64_t block, int x, int y) { | |
561 SkASSERT(x >= 0 && x < 4); | |
562 SkASSERT(y >= 0 && y < 4); | |
563 const int idx = x*4 + y; | |
564 return (block >> ((15-idx)*3)) & 0x7; | |
565 } | |
566 | |
567 static void decompress_r11_eac_block(uint8_t* dst, int dstRowBytes, const uint8_
t* src) { | |
568 const uint64_t block = SkEndian_SwapBE64(*(reinterpret_cast<const uint64_t *
>(src))); | |
569 | |
570 const int base_cw = (block >> 56) & 0xFF; | |
571 const int mod = (block >> 52) & 0xF; | |
572 const int palette_idx = (block >> 48) & 0xF; | |
573 | |
574 const int* palette = kR11EACModifierPalettes[palette_idx]; | |
575 | |
576 for (int j = 0; j < 4; ++j) { | |
577 for (int i = 0; i < 4; ++i) { | |
578 const int idx = get_r11_eac_index(block, i, j); | |
579 const int val = base_cw*8 + 4 + palette[idx]*mod*8; | |
580 if (val < 0) { | |
581 dst[i] = 0; | |
582 } else if (val > 2047) { | |
583 dst[i] = 0xFF; | |
584 } else { | |
585 dst[i] = (val >> 3) & 0xFF; | |
586 } | |
587 } | |
588 dst += dstRowBytes; | |
589 } | |
590 } | |
591 | |
592 //////////////////////////////////////////////////////////////////////////////// | 560 //////////////////////////////////////////////////////////////////////////////// |
593 | 561 |
594 namespace SkTextureCompressor { | 562 namespace SkTextureCompressor { |
595 | 563 |
596 bool CompressA8ToR11EAC(uint8_t* dst, const uint8_t* src, int width, int height,
int rowBytes) { | 564 bool CompressA8ToR11EAC(uint8_t* dst, const uint8_t* src, int width, int height,
int rowBytes) { |
597 | 565 |
598 #if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST) | 566 #if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST) |
599 | 567 |
600 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_
r11eac_block); | 568 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_
r11eac_block); |
601 | 569 |
602 #elif COMPRESS_R11_EAC_FASTEST | 570 #elif COMPRESS_R11_EAC_FASTEST |
603 | 571 |
604 return compress_a8_to_r11eac_fast(dst, src, width, height, rowBytes); | 572 return compress_a8_to_r11eac_fast(dst, src, width, height, rowBytes); |
605 | 573 |
606 #else | 574 #else |
607 #error "Must choose R11 EAC algorithm" | 575 #error "Must choose R11 EAC algorithm" |
608 #endif | 576 #endif |
609 } | 577 } |
610 | 578 |
611 SkBlitter* CreateR11EACBlitter(int width, int height, void* outputBuffer) { | 579 SkBlitter* CreateR11EACBlitter(int width, int height, void* outputBuffer) { |
612 return new | 580 return new |
613 SkTCompressedAlphaBlitter<4, 8, compress_block_vertical> | 581 SkTCompressedAlphaBlitter<4, 8, compress_block_vertical> |
614 (width, height, outputBuffer); | 582 (width, height, outputBuffer); |
615 } | 583 } |
616 | 584 |
617 void DecompressR11EAC(uint8_t* dst, int dstRowBytes, const uint8_t* src, int wid
th, int height) { | |
618 for (int j = 0; j < height; j += 4) { | |
619 for (int i = 0; i < width; i += 4) { | |
620 decompress_r11_eac_block(dst + i, dstRowBytes, src); | |
621 src += 8; | |
622 } | |
623 dst += 4 * dstRowBytes; | |
624 } | |
625 } | |
626 | |
627 } // namespace SkTextureCompressor | 585 } // namespace SkTextureCompressor |
OLD | NEW |