texture_decode.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/swap.h"
  10. #include "common/vector_math.h"
  11. #include "video_core/pica.h"
  12. #include "video_core/texture/etc1.h"
  13. #include "video_core/texture/texture_decode.h"
  14. #include "video_core/utils.h"
  15. using TextureFormat = Pica::Regs::TextureFormat;
  16. namespace Pica {
  17. namespace Texture {
  18. constexpr size_t TILE_SIZE = 8 * 8;
  19. constexpr size_t ETC1_SUBTILES = 2 * 2;
  20. size_t CalculateTileSize(TextureFormat format) {
  21. switch (format) {
  22. case TextureFormat::RGBA8:
  23. return 4 * TILE_SIZE;
  24. case TextureFormat::RGB8:
  25. return 3 * TILE_SIZE;
  26. case TextureFormat::RGB5A1:
  27. case TextureFormat::RGB565:
  28. case TextureFormat::RGBA4:
  29. case TextureFormat::IA8:
  30. case TextureFormat::RG8:
  31. return 2 * TILE_SIZE;
  32. case TextureFormat::I8:
  33. case TextureFormat::A8:
  34. case TextureFormat::IA4:
  35. return 1 * TILE_SIZE;
  36. case TextureFormat::I4:
  37. case TextureFormat::A4:
  38. return TILE_SIZE / 2;
  39. case TextureFormat::ETC1:
  40. return ETC1_SUBTILES * 8;
  41. case TextureFormat::ETC1A4:
  42. return ETC1_SUBTILES * 16;
  43. default: // placeholder for yet unknown formats
  44. UNIMPLEMENTED();
  45. return 0;
  46. }
  47. }
  48. Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
  49. const TextureInfo& info, bool disable_alpha) {
  50. // Coordinate in tiles
  51. const unsigned int coarse_x = x / 8;
  52. const unsigned int coarse_y = y / 8;
  53. // Coordinate inside the tile
  54. const unsigned int fine_x = x % 8;
  55. const unsigned int fine_y = y % 8;
  56. const u8* line = source + coarse_y * info.stride;
  57. const u8* tile = line + coarse_x * CalculateTileSize(info.format);
  58. return LookupTexelInTile(tile, fine_x, fine_y, info, disable_alpha);
  59. }
  60. Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
  61. const TextureInfo& info, bool disable_alpha) {
  62. DEBUG_ASSERT(x < 8);
  63. DEBUG_ASSERT(y < 8);
  64. using VideoCore::MortonInterleave;
  65. switch (info.format) {
  66. case Regs::TextureFormat::RGBA8: {
  67. auto res = Color::DecodeRGBA8(source + MortonInterleave(x, y) * 4);
  68. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  69. }
  70. case Regs::TextureFormat::RGB8: {
  71. auto res = Color::DecodeRGB8(source + MortonInterleave(x, y) * 3);
  72. return {res.r(), res.g(), res.b(), 255};
  73. }
  74. case Regs::TextureFormat::RGB5A1: {
  75. auto res = Color::DecodeRGB5A1(source + MortonInterleave(x, y) * 2);
  76. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  77. }
  78. case Regs::TextureFormat::RGB565: {
  79. auto res = Color::DecodeRGB565(source + MortonInterleave(x, y) * 2);
  80. return {res.r(), res.g(), res.b(), 255};
  81. }
  82. case Regs::TextureFormat::RGBA4: {
  83. auto res = Color::DecodeRGBA4(source + MortonInterleave(x, y) * 2);
  84. return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
  85. }
  86. case Regs::TextureFormat::IA8: {
  87. const u8* source_ptr = source + MortonInterleave(x, y) * 2;
  88. if (disable_alpha) {
  89. // Show intensity as red, alpha as green
  90. return {source_ptr[1], source_ptr[0], 0, 255};
  91. } else {
  92. return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
  93. }
  94. }
  95. case Regs::TextureFormat::RG8: {
  96. auto res = Color::DecodeRG8(source + MortonInterleave(x, y) * 2);
  97. return {res.r(), res.g(), 0, 255};
  98. }
  99. case Regs::TextureFormat::I8: {
  100. const u8* source_ptr = source + MortonInterleave(x, y);
  101. return {*source_ptr, *source_ptr, *source_ptr, 255};
  102. }
  103. case Regs::TextureFormat::A8: {
  104. const u8* source_ptr = source + MortonInterleave(x, y);
  105. if (disable_alpha) {
  106. return {*source_ptr, *source_ptr, *source_ptr, 255};
  107. } else {
  108. return {0, 0, 0, *source_ptr};
  109. }
  110. }
  111. case Regs::TextureFormat::IA4: {
  112. const u8* source_ptr = source + MortonInterleave(x, y);
  113. u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
  114. u8 a = Color::Convert4To8((*source_ptr) & 0xF);
  115. if (disable_alpha) {
  116. // Show intensity as red, alpha as green
  117. return {i, a, 0, 255};
  118. } else {
  119. return {i, i, i, a};
  120. }
  121. }
  122. case Regs::TextureFormat::I4: {
  123. u32 morton_offset = MortonInterleave(x, y);
  124. const u8* source_ptr = source + morton_offset / 2;
  125. u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
  126. i = Color::Convert4To8(i);
  127. return {i, i, i, 255};
  128. }
  129. case Regs::TextureFormat::A4: {
  130. u32 morton_offset = MortonInterleave(x, y);
  131. const u8* source_ptr = source + morton_offset / 2;
  132. u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
  133. a = Color::Convert4To8(a);
  134. if (disable_alpha) {
  135. return {a, a, a, 255};
  136. } else {
  137. return {0, 0, 0, a};
  138. }
  139. }
  140. case Regs::TextureFormat::ETC1:
  141. case Regs::TextureFormat::ETC1A4: {
  142. bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
  143. size_t subtile_size = has_alpha ? 16 : 8;
  144. // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
  145. constexpr unsigned int subtile_width = 4;
  146. constexpr unsigned int subtile_height = 4;
  147. unsigned int subtile_index = (x / subtile_width) + 2 * (y / subtile_height);
  148. x %= subtile_width;
  149. y %= subtile_height;
  150. const u8* subtile_ptr = source + subtile_index * subtile_size;
  151. u8 alpha = 255;
  152. if (has_alpha) {
  153. u64_le packed_alpha;
  154. memcpy(&packed_alpha, subtile_ptr, sizeof(u64));
  155. subtile_ptr += sizeof(u64);
  156. alpha = Color::Convert4To8((packed_alpha >> (4 * (x * subtile_width + y))) & 0xF);
  157. }
  158. u64_le subtile_data;
  159. memcpy(&subtile_data, subtile_ptr, sizeof(u64));
  160. return Math::MakeVec(SampleETC1Subtile(subtile_data, x, y),
  161. disable_alpha ? (u8)255 : alpha);
  162. }
  163. default:
  164. LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
  165. DEBUG_ASSERT(false);
  166. return {};
  167. }
  168. }
  169. TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
  170. const Regs::TextureFormat& format) {
  171. TextureInfo info;
  172. info.physical_address = config.GetPhysicalAddress();
  173. info.width = config.width;
  174. info.height = config.height;
  175. info.format = format;
  176. info.SetDefaultStride();
  177. return info;
  178. }
  179. } // namespace Texture
  180. } // namespace Pica