diff options
| author | 2017-02-04 12:59:33 -0800 | |
|---|---|---|
| committer | 2017-02-04 12:59:33 -0800 | |
| commit | 18c981b99606b40897d8bc2da218e34509873246 (patch) | |
| tree | 7b95528950ba5644b07c3c9340ebdadb0c41db3d /src/video_core/texture/etc1.cpp | |
| parent | changed the WIN32 macro in microprofileui (#2528) (diff) | |
| parent | Pica/Texture: Move part of ETC1 decoding to new file and cleanups (diff) | |
| download | yuzu-18c981b99606b40897d8bc2da218e34509873246.tar.gz yuzu-18c981b99606b40897d8bc2da218e34509873246.tar.xz yuzu-18c981b99606b40897d8bc2da218e34509873246.zip | |
Merge pull request #2414 from yuriks/texture-decode
Texture decoding cleanups
Diffstat (limited to 'src/video_core/texture/etc1.cpp')
| -rw-r--r-- | src/video_core/texture/etc1.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/video_core/texture/etc1.cpp b/src/video_core/texture/etc1.cpp new file mode 100644 index 000000000..af60cde1e --- /dev/null +++ b/src/video_core/texture/etc1.cpp | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include "common/bit_field.h" | ||
| 9 | #include "common/color.h" | ||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "common/math_util.h" | ||
| 12 | #include "common/vector_math.h" | ||
| 13 | #include "video_core/texture/etc1.h" | ||
| 14 | |||
| 15 | namespace Pica { | ||
| 16 | namespace Texture { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | constexpr std::array<u8[2], 8> etc1_modifier_table = {{ | ||
| 21 | {2, 8}, {5, 17}, {9, 29}, {13, 42}, {18, 60}, {24, 80}, {33, 106}, {47, 183}, | ||
| 22 | }}; | ||
| 23 | |||
| 24 | union ETC1Tile { | ||
| 25 | u64 raw; | ||
| 26 | |||
| 27 | // Each of these two is a collection of 16 bits (one per lookup value) | ||
| 28 | BitField<0, 16, u64> table_subindexes; | ||
| 29 | BitField<16, 16, u64> negation_flags; | ||
| 30 | |||
| 31 | unsigned GetTableSubIndex(unsigned index) const { | ||
| 32 | return (table_subindexes >> index) & 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | bool GetNegationFlag(unsigned index) const { | ||
| 36 | return ((negation_flags >> index) & 1) == 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | BitField<32, 1, u64> flip; | ||
| 40 | BitField<33, 1, u64> differential_mode; | ||
| 41 | |||
| 42 | BitField<34, 3, u64> table_index_2; | ||
| 43 | BitField<37, 3, u64> table_index_1; | ||
| 44 | |||
| 45 | union { | ||
| 46 | // delta value + base value | ||
| 47 | BitField<40, 3, s64> db; | ||
| 48 | BitField<43, 5, u64> b; | ||
| 49 | |||
| 50 | BitField<48, 3, s64> dg; | ||
| 51 | BitField<51, 5, u64> g; | ||
| 52 | |||
| 53 | BitField<56, 3, s64> dr; | ||
| 54 | BitField<59, 5, u64> r; | ||
| 55 | } differential; | ||
| 56 | |||
| 57 | union { | ||
| 58 | BitField<40, 4, u64> b2; | ||
| 59 | BitField<44, 4, u64> b1; | ||
| 60 | |||
| 61 | BitField<48, 4, u64> g2; | ||
| 62 | BitField<52, 4, u64> g1; | ||
| 63 | |||
| 64 | BitField<56, 4, u64> r2; | ||
| 65 | BitField<60, 4, u64> r1; | ||
| 66 | } separate; | ||
| 67 | |||
| 68 | const Math::Vec3<u8> GetRGB(unsigned int x, unsigned int y) const { | ||
| 69 | int texel = 4 * x + y; | ||
| 70 | |||
| 71 | if (flip) | ||
| 72 | std::swap(x, y); | ||
| 73 | |||
| 74 | // Lookup base value | ||
| 75 | Math::Vec3<int> ret; | ||
| 76 | if (differential_mode) { | ||
| 77 | ret.r() = static_cast<int>(differential.r); | ||
| 78 | ret.g() = static_cast<int>(differential.g); | ||
| 79 | ret.b() = static_cast<int>(differential.b); | ||
| 80 | if (x >= 2) { | ||
| 81 | ret.r() += static_cast<int>(differential.dr); | ||
| 82 | ret.g() += static_cast<int>(differential.dg); | ||
| 83 | ret.b() += static_cast<int>(differential.db); | ||
| 84 | } | ||
| 85 | ret.r() = Color::Convert5To8(ret.r()); | ||
| 86 | ret.g() = Color::Convert5To8(ret.g()); | ||
| 87 | ret.b() = Color::Convert5To8(ret.b()); | ||
| 88 | } else { | ||
| 89 | if (x < 2) { | ||
| 90 | ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1)); | ||
| 91 | ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1)); | ||
| 92 | ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1)); | ||
| 93 | } else { | ||
| 94 | ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2)); | ||
| 95 | ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2)); | ||
| 96 | ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2)); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | // Add modifier | ||
| 101 | unsigned table_index = | ||
| 102 | static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value()); | ||
| 103 | |||
| 104 | int modifier = etc1_modifier_table[table_index][GetTableSubIndex(texel)]; | ||
| 105 | if (GetNegationFlag(texel)) | ||
| 106 | modifier *= -1; | ||
| 107 | |||
| 108 | ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255); | ||
| 109 | ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255); | ||
| 110 | ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255); | ||
| 111 | |||
| 112 | return ret.Cast<u8>(); | ||
| 113 | } | ||
| 114 | }; | ||
| 115 | |||
| 116 | } // anonymous namespace | ||
| 117 | |||
| 118 | Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y) { | ||
| 119 | ETC1Tile tile{value}; | ||
| 120 | return tile.GetRGB(x, y); | ||
| 121 | } | ||
| 122 | |||
| 123 | } // namespace Texture | ||
| 124 | } // namespace Pica | ||