texture_decode.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/assert.h"
  6. #include "common/color.h"
  7. #include "common/logging/log.h"
  8. #include "common/math_util.h"
  9. #include "common/vector_math.h"
  10. #include "video_core/pica.h"
  11. #include "video_core/texture/texture_decode.h"
  12. #include "video_core/utils.h"
  13. using TextureFormat = Pica::Regs::TextureFormat;
  14. namespace Pica {
  15. namespace Texture {
  16. constexpr size_t TILE_SIZE = 8 * 8;
  17. constexpr size_t ETC1_SUBTILES = 2 * 2;
  18. size_t CalculateTileSize(TextureFormat format) {
  19. switch (format) {
  20. case TextureFormat::RGBA8:
  21. return 4 * TILE_SIZE;
  22. case TextureFormat::RGB8:
  23. return 3 * TILE_SIZE;
  24. case TextureFormat::RGB5A1:
  25. case TextureFormat::RGB565:
  26. case TextureFormat::RGBA4:
  27. case TextureFormat::IA8:
  28. case TextureFormat::RG8:
  29. return 2 * TILE_SIZE;
  30. case TextureFormat::I8:
  31. case TextureFormat::A8:
  32. case TextureFormat::IA4:
  33. return 1 * TILE_SIZE;
  34. case TextureFormat::I4:
  35. case TextureFormat::A4:
  36. return TILE_SIZE / 2;
  37. case TextureFormat::ETC1:
  38. return ETC1_SUBTILES * 8;
  39. case TextureFormat::ETC1A4:
  40. return ETC1_SUBTILES * 16;
  41. default: // placeholder for yet unknown formats
  42. UNIMPLEMENTED();
  43. return 0;
  44. }
  45. }
  46. Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
  47. const TextureInfo& info, bool disable_alpha) {
  48. // Coordinate in tiles
  49. const unsigned int coarse_x = x / 8;
  50. const unsigned int coarse_y = y / 8;
  51. // Coordinate inside the tile
  52. const unsigned int fine_x = x % 8;
  53. const unsigned int fine_y = y % 8;
  54. const u8* line = source + coarse_y * info.stride;
  55. const u8* tile = line + coarse_x * CalculateTileSize(info.format);
  56. return LookupTexelInTile(tile, fine_x, fine_y, info, disable_alpha);
  57. }
  58. Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
  59. const TextureInfo& info, bool disable_alpha) {
  60. DEBUG_ASSERT(x < 8);
  61. DEBUG_ASSERT(y < 8);
  62. using VideoCore::MortonInterleave;
  63. switch (info.format) {
  64. case Regs::TextureFormat::RGBA8: {
  65. auto res = Color::DecodeRGBA8(source + MortonInterleave(x, y) * 4);
  66. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  67. }
  68. case Regs::TextureFormat::RGB8: {
  69. auto res = Color::DecodeRGB8(source + MortonInterleave(x, y) * 3);
  70. return {res.r(), res.g(), res.b(), 255};
  71. }
  72. case Regs::TextureFormat::RGB5A1: {
  73. auto res = Color::DecodeRGB5A1(source + MortonInterleave(x, y) * 2);
  74. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  75. }
  76. case Regs::TextureFormat::RGB565: {
  77. auto res = Color::DecodeRGB565(source + MortonInterleave(x, y) * 2);
  78. return {res.r(), res.g(), res.b(), 255};
  79. }
  80. case Regs::TextureFormat::RGBA4: {
  81. auto res = Color::DecodeRGBA4(source + MortonInterleave(x, y) * 2);
  82. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  83. }
  84. case Regs::TextureFormat::IA8: {
  85. const u8* source_ptr = source + MortonInterleave(x, y) * 2;
  86. if (disable_alpha) {
  87. // Show intensity as red, alpha as green
  88. return {source_ptr[1], source_ptr[0], 0, 255};
  89. } else {
  90. return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
  91. }
  92. }
  93. case Regs::TextureFormat::RG8: {
  94. auto res = Color::DecodeRG8(source + MortonInterleave(x, y) * 2);
  95. return {res.r(), res.g(), 0, 255};
  96. }
  97. case Regs::TextureFormat::I8: {
  98. const u8* source_ptr = source + MortonInterleave(x, y);
  99. return {*source_ptr, *source_ptr, *source_ptr, 255};
  100. }
  101. case Regs::TextureFormat::A8: {
  102. const u8* source_ptr = source + MortonInterleave(x, y);
  103. if (disable_alpha) {
  104. return {*source_ptr, *source_ptr, *source_ptr, 255};
  105. } else {
  106. return {0, 0, 0, *source_ptr};
  107. }
  108. }
  109. case Regs::TextureFormat::IA4: {
  110. const u8* source_ptr = source + MortonInterleave(x, y);
  111. u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
  112. u8 a = Color::Convert4To8((*source_ptr) & 0xF);
  113. if (disable_alpha) {
  114. // Show intensity as red, alpha as green
  115. return {i, a, 0, 255};
  116. } else {
  117. return {i, i, i, a};
  118. }
  119. }
  120. case Regs::TextureFormat::I4: {
  121. u32 morton_offset = MortonInterleave(x, y);
  122. const u8* source_ptr = source + morton_offset / 2;
  123. u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
  124. i = Color::Convert4To8(i);
  125. return {i, i, i, 255};
  126. }
  127. case Regs::TextureFormat::A4: {
  128. u32 morton_offset = MortonInterleave(x, y);
  129. const u8* source_ptr = source + morton_offset / 2;
  130. u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
  131. a = Color::Convert4To8(a);
  132. if (disable_alpha) {
  133. return {a, a, a, 255};
  134. } else {
  135. return {0, 0, 0, a};
  136. }
  137. }
  138. case Regs::TextureFormat::ETC1:
  139. case Regs::TextureFormat::ETC1A4: {
  140. bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
  141. // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
  142. constexpr unsigned int subtile_width = 4;
  143. constexpr unsigned int subtile_height = 4;
  144. unsigned int subtile_index = (x / subtile_width) + 2 * (y / subtile_height);
  145. size_t subtile_size = has_alpha ? 16 : 8;
  146. // TODO(yuriks): Use memcpy instead of reinterpret_cast
  147. const u64* source_ptr = reinterpret_cast<const u64*>(source + subtile_index * subtile_size);
  148. u64 alpha = 0xFFFFFFFFFFFFFFFF;
  149. if (has_alpha) {
  150. alpha = *source_ptr;
  151. source_ptr++;
  152. }
  153. union ETC1Tile {
  154. // Each of these two is a collection of 16 bits (one per lookup value)
  155. BitField<0, 16, u64> table_subindexes;
  156. BitField<16, 16, u64> negation_flags;
  157. unsigned GetTableSubIndex(unsigned index) const {
  158. return (table_subindexes >> index) & 1;
  159. }
  160. bool GetNegationFlag(unsigned index) const {
  161. return ((negation_flags >> index) & 1) == 1;
  162. }
  163. BitField<32, 1, u64> flip;
  164. BitField<33, 1, u64> differential_mode;
  165. BitField<34, 3, u64> table_index_2;
  166. BitField<37, 3, u64> table_index_1;
  167. union {
  168. // delta value + base value
  169. BitField<40, 3, s64> db;
  170. BitField<43, 5, u64> b;
  171. BitField<48, 3, s64> dg;
  172. BitField<51, 5, u64> g;
  173. BitField<56, 3, s64> dr;
  174. BitField<59, 5, u64> r;
  175. } differential;
  176. union {
  177. BitField<40, 4, u64> b2;
  178. BitField<44, 4, u64> b1;
  179. BitField<48, 4, u64> g2;
  180. BitField<52, 4, u64> g1;
  181. BitField<56, 4, u64> r2;
  182. BitField<60, 4, u64> r1;
  183. } separate;
  184. const Math::Vec3<u8> GetRGB(int x, int y) const {
  185. int texel = 4 * x + y;
  186. if (flip)
  187. std::swap(x, y);
  188. // Lookup base value
  189. Math::Vec3<int> ret;
  190. if (differential_mode) {
  191. ret.r() = static_cast<int>(differential.r);
  192. ret.g() = static_cast<int>(differential.g);
  193. ret.b() = static_cast<int>(differential.b);
  194. if (x >= 2) {
  195. ret.r() += static_cast<int>(differential.dr);
  196. ret.g() += static_cast<int>(differential.dg);
  197. ret.b() += static_cast<int>(differential.db);
  198. }
  199. ret.r() = Color::Convert5To8(ret.r());
  200. ret.g() = Color::Convert5To8(ret.g());
  201. ret.b() = Color::Convert5To8(ret.b());
  202. } else {
  203. if (x < 2) {
  204. ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
  205. ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
  206. ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
  207. } else {
  208. ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
  209. ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
  210. ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
  211. }
  212. }
  213. // Add modifier
  214. unsigned table_index =
  215. static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
  216. static const std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
  217. {{2, 8}},
  218. {{5, 17}},
  219. {{9, 29}},
  220. {{13, 42}},
  221. {{18, 60}},
  222. {{24, 80}},
  223. {{33, 106}},
  224. {{47, 183}},
  225. }};
  226. int modifier = etc1_modifier_table.at(table_index).at(GetTableSubIndex(texel));
  227. if (GetNegationFlag(texel))
  228. modifier *= -1;
  229. ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
  230. ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
  231. ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
  232. return ret.Cast<u8>();
  233. }
  234. } const* etc1_tile = reinterpret_cast<const ETC1Tile*>(source_ptr);
  235. alpha >>= 4 * ((x & 3) * 4 + (y & 3));
  236. return Math::MakeVec(etc1_tile->GetRGB(x & 3, y & 3),
  237. disable_alpha ? (u8)255 : Color::Convert4To8(alpha & 0xF));
  238. }
  239. default:
  240. LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
  241. DEBUG_ASSERT(false);
  242. return {};
  243. }
  244. }
  245. TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
  246. const Regs::TextureFormat& format) {
  247. TextureInfo info;
  248. info.physical_address = config.GetPhysicalAddress();
  249. info.width = config.width;
  250. info.height = config.height;
  251. info.format = format;
  252. info.SetDefaultStride();
  253. return info;
  254. }
  255. } // namespace Texture
  256. } // namespace Pica