TextureDecoder.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <span>
  6. #include <tuple>
  7. #include "Common/CommonTypes.h"
  8. #include "Common/EnumFormatter.h"
  9. #include "Common/SpanUtils.h"
  10. enum
  11. {
  12. TMEM_SIZE = 1024 * 1024,
  13. TMEM_LINE_SIZE = 32,
  14. };
  15. alignas(16) extern std::array<u8, TMEM_SIZE> s_tex_mem;
  16. enum class TextureFormat
  17. {
  18. // These values represent texture format in GX registers.
  19. I4 = 0x0,
  20. I8 = 0x1,
  21. IA4 = 0x2,
  22. IA8 = 0x3,
  23. RGB565 = 0x4,
  24. RGB5A3 = 0x5,
  25. RGBA8 = 0x6,
  26. C4 = 0x8,
  27. C8 = 0x9,
  28. C14X2 = 0xA,
  29. CMPR = 0xE,
  30. // Special texture format used to represent YUVY xfb copies.
  31. // They aren't really textures, but they share so much hardware and usecases that it makes sense
  32. // to emulate them as part of texture cache.
  33. // This isn't a real value that can be used on console; it only exists for ease of implementation.
  34. XFB = 0xF,
  35. };
  36. template <>
  37. struct fmt::formatter<TextureFormat> : EnumFormatter<TextureFormat::CMPR>
  38. {
  39. static constexpr array_type names = {"I4", "I8", "IA4", "IA8", "RGB565",
  40. "RGB5A3", "RGBA8", nullptr, "C4", "C8",
  41. "C14X2", nullptr, nullptr, nullptr, "CMPR"};
  42. constexpr formatter() : EnumFormatter(names) {}
  43. };
  44. static inline bool IsColorIndexed(TextureFormat format)
  45. {
  46. return format == TextureFormat::C4 || format == TextureFormat::C8 ||
  47. format == TextureFormat::C14X2;
  48. }
  49. static inline bool IsValidTextureFormat(TextureFormat format)
  50. {
  51. return format <= TextureFormat::RGBA8 || IsColorIndexed(format) || format == TextureFormat::CMPR;
  52. }
  53. // The EFB Copy pipeline looks like:
  54. //
  55. // 1. Read EFB -> 2. Select color/depth -> 3. Downscale (optional)
  56. // -> 4. YUV conversion (optional) -> 5. Encode Tiles -> 6. Write RAM
  57. //
  58. // The "Encode Tiles" stage receives RGBA8 texels from previous stages and encodes them to various
  59. // formats. EFBCopyFormat is the tile encoder mode. Note that the tile encoder does not care about
  60. // color vs. depth or intensity formats - it only sees RGBA8 texels.
  61. enum class EFBCopyFormat
  62. {
  63. // These values represent EFB copy format in GX registers.
  64. // Most (but not all) of these values correspond to values of TextureFormat.
  65. R4 = 0x0, // R4, I4, Z4
  66. // FIXME: Does 0x1 (Z8) have identical results to 0x8 (Z8H)?
  67. // Is either or both of 0x1 and 0x8 used in games?
  68. R8_0x1 = 0x1, // R8, I8, Z8H (?)
  69. RA4 = 0x2, // RA4, IA4
  70. // FIXME: Earlier versions of this file named the value 0x3 "GX_TF_Z16", which does not reflect
  71. // the results one would expect when copying from the depth buffer with this format.
  72. // For reference: When copying from the depth buffer, R should receive the top 8 bits of
  73. // the Z value, and A should be either 0xFF or 0 (please investigate).
  74. // Please test original hardware and make sure dolphin-emu implements this format
  75. // correctly.
  76. RA8 = 0x3, // RA8, IA8, (FIXME: Z16 too?)
  77. RGB565 = 0x4,
  78. RGB5A3 = 0x5,
  79. RGBA8 = 0x6, // RGBA8, Z24
  80. A8 = 0x7,
  81. R8 = 0x8, // R8, I8, Z8H
  82. G8 = 0x9, // G8, Z8M
  83. B8 = 0xA, // B8, Z8L
  84. RG8 = 0xB, // RG8, Z16R (Note: G and R are reversed)
  85. GB8 = 0xC, // GB8, Z16L
  86. // Special texture format used to represent YUVY xfb copies.
  87. // They aren't really textures, but they share so much hardware and usecases that it makes sense
  88. // to emulate them as part of texture cache.
  89. // This isn't a real value that can be used on console; it only exists for ease of implementation.
  90. XFB = 0xF,
  91. };
  92. template <>
  93. struct fmt::formatter<EFBCopyFormat> : EnumFormatter<EFBCopyFormat::GB8>
  94. {
  95. static constexpr array_type names = {
  96. "R4/I4/Z4", "R8/I8/Z8H (?)", "RA4/IA4", "RA8/IA8 (Z16 too?)",
  97. "RGB565", "RGB5A3", "RGBA8", "A8",
  98. "R8/I8/Z8H", "G8/Z8M", "B8/Z8L", "RG8/Z16R (Note: G and R are reversed)",
  99. "GB8/Z16L",
  100. };
  101. constexpr formatter() : EnumFormatter(names) {}
  102. };
  103. enum class TLUTFormat
  104. {
  105. // These values represent TLUT format in GX registers.
  106. IA8 = 0x0,
  107. RGB565 = 0x1,
  108. RGB5A3 = 0x2,
  109. };
  110. template <>
  111. struct fmt::formatter<TLUTFormat> : EnumFormatter<TLUTFormat::RGB5A3>
  112. {
  113. constexpr formatter() : EnumFormatter({"IA8", "RGB565", "RGB5A3"}) {}
  114. };
  115. static inline bool IsValidTLUTFormat(TLUTFormat tlutfmt)
  116. {
  117. return tlutfmt == TLUTFormat::IA8 || tlutfmt == TLUTFormat::RGB565 ||
  118. tlutfmt == TLUTFormat::RGB5A3;
  119. }
  120. static inline bool IsCompatibleTextureFormat(TextureFormat from_format, TextureFormat to_format)
  121. {
  122. if (from_format == to_format)
  123. return true;
  124. // Indexed and paletted formats are "compatible", that is do not require conversion.
  125. switch (from_format)
  126. {
  127. case TextureFormat::I4:
  128. case TextureFormat::C4:
  129. return to_format == TextureFormat::I4 || to_format == TextureFormat::C4;
  130. case TextureFormat::I8:
  131. case TextureFormat::C8:
  132. return to_format == TextureFormat::I8 || to_format == TextureFormat::C8;
  133. default:
  134. return false;
  135. }
  136. }
  137. static inline bool CanReinterpretTextureOnGPU(TextureFormat from_format, TextureFormat to_format)
  138. {
  139. // Currently, we can only reinterpret textures of the same width.
  140. switch (from_format)
  141. {
  142. case TextureFormat::I8:
  143. case TextureFormat::IA4:
  144. return to_format == TextureFormat::I8 || to_format == TextureFormat::IA4;
  145. case TextureFormat::IA8:
  146. case TextureFormat::RGB565:
  147. case TextureFormat::RGB5A3:
  148. return to_format == TextureFormat::IA8 || to_format == TextureFormat::RGB565 ||
  149. to_format == TextureFormat::RGB5A3;
  150. default:
  151. return false;
  152. }
  153. }
  154. inline std::span<u8> TexDecoder_GetTmemSpan(size_t offset = 0)
  155. {
  156. return Common::SafeSubspan(std::span<u8>(s_tex_mem), offset);
  157. }
  158. int TexDecoder_GetTexelSizeInNibbles(TextureFormat format);
  159. int TexDecoder_GetTextureSizeInBytes(int width, int height, TextureFormat format);
  160. int TexDecoder_GetBlockWidthInTexels(TextureFormat format);
  161. int TexDecoder_GetBlockHeightInTexels(TextureFormat format);
  162. int TexDecoder_GetEFBCopyBlockWidthInTexels(EFBCopyFormat format);
  163. int TexDecoder_GetEFBCopyBlockHeightInTexels(EFBCopyFormat format);
  164. int TexDecoder_GetPaletteSize(TextureFormat fmt);
  165. TextureFormat TexDecoder_GetEFBCopyBaseFormat(EFBCopyFormat format);
  166. void TexDecoder_Decode(u8* dst, const u8* src, int width, int height, TextureFormat texformat,
  167. const u8* tlut, TLUTFormat tlutfmt);
  168. void TexDecoder_DecodeRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int width,
  169. int height);
  170. void TexDecoder_DecodeTexel(u8* dst, std::span<const u8> src, int s, int t, int imageWidth,
  171. TextureFormat texformat, std::span<const u8> tlut, TLUTFormat tlutfmt);
  172. void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, std::span<const u8> src_ar,
  173. std::span<const u8> src_gb, int s, int t, int imageWidth);
  174. void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int s, int t,
  175. int imageWidth);
  176. void TexDecoder_DecodeXFB(u8* dst, const u8* src, u32 width, u32 height, u32 stride);
  177. void TexDecoder_SetTexFmtOverlayOptions(bool enable, bool center);
  178. /* Internal method, implemented by TextureDecoder_Generic and TextureDecoder_x64. */
  179. void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat,
  180. const u8* tlut, TLUTFormat tlutfmt);