etc1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include "common/bit_field.h"
  6. #include "common/color.h"
  7. #include "common/common_types.h"
  8. #include "common/math_util.h"
  9. #include "common/vector_math.h"
  10. #include "video_core/texture/etc1.h"
  11. namespace Pica {
  12. namespace Texture {
  13. namespace {
  14. constexpr std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
  15. {2, 8}, {5, 17}, {9, 29}, {13, 42}, {18, 60}, {24, 80}, {33, 106}, {47, 183},
  16. }};
  17. union ETC1Tile {
  18. u64 raw;
  19. // Each of these two is a collection of 16 bits (one per lookup value)
  20. BitField<0, 16, u64> table_subindexes;
  21. BitField<16, 16, u64> negation_flags;
  22. unsigned GetTableSubIndex(unsigned index) const {
  23. return (table_subindexes >> index) & 1;
  24. }
  25. bool GetNegationFlag(unsigned index) const {
  26. return ((negation_flags >> index) & 1) == 1;
  27. }
  28. BitField<32, 1, u64> flip;
  29. BitField<33, 1, u64> differential_mode;
  30. BitField<34, 3, u64> table_index_2;
  31. BitField<37, 3, u64> table_index_1;
  32. union {
  33. // delta value + base value
  34. BitField<40, 3, s64> db;
  35. BitField<43, 5, u64> b;
  36. BitField<48, 3, s64> dg;
  37. BitField<51, 5, u64> g;
  38. BitField<56, 3, s64> dr;
  39. BitField<59, 5, u64> r;
  40. } differential;
  41. union {
  42. BitField<40, 4, u64> b2;
  43. BitField<44, 4, u64> b1;
  44. BitField<48, 4, u64> g2;
  45. BitField<52, 4, u64> g1;
  46. BitField<56, 4, u64> r2;
  47. BitField<60, 4, u64> r1;
  48. } separate;
  49. const Math::Vec3<u8> GetRGB(unsigned int x, unsigned int y) const {
  50. int texel = 4 * x + y;
  51. if (flip)
  52. std::swap(x, y);
  53. // Lookup base value
  54. Math::Vec3<int> ret;
  55. if (differential_mode) {
  56. ret.r() = static_cast<int>(differential.r);
  57. ret.g() = static_cast<int>(differential.g);
  58. ret.b() = static_cast<int>(differential.b);
  59. if (x >= 2) {
  60. ret.r() += static_cast<int>(differential.dr);
  61. ret.g() += static_cast<int>(differential.dg);
  62. ret.b() += static_cast<int>(differential.db);
  63. }
  64. ret.r() = Color::Convert5To8(ret.r());
  65. ret.g() = Color::Convert5To8(ret.g());
  66. ret.b() = Color::Convert5To8(ret.b());
  67. } else {
  68. if (x < 2) {
  69. ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
  70. ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
  71. ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
  72. } else {
  73. ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
  74. ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
  75. ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
  76. }
  77. }
  78. // Add modifier
  79. unsigned table_index =
  80. static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
  81. int modifier = etc1_modifier_table[table_index][GetTableSubIndex(texel)];
  82. if (GetNegationFlag(texel))
  83. modifier *= -1;
  84. ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
  85. ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
  86. ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
  87. return ret.Cast<u8>();
  88. }
  89. };
  90. } // anonymous namespace
  91. Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y) {
  92. ETC1Tile tile{value};
  93. return tile.GetRGB(x, y);
  94. }
  95. } // namespace Texture
  96. } // namespace Pica