TextureReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. namespace Ryujinx.HLE.Gpu.Texture
  5. {
  6. static class TextureReader
  7. {
  8. public static byte[] Read(IAMemory Memory, TextureInfo Texture)
  9. {
  10. switch (Texture.Format)
  11. {
  12. case GalTextureFormat.R32G32B32A32: return Read16Bpp (Memory, Texture);
  13. case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture);
  14. case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture);
  15. case GalTextureFormat.R32: return Read4Bpp (Memory, Texture);
  16. case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture);
  17. case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture);
  18. case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture);
  19. case GalTextureFormat.R16: return Read2Bpp (Memory, Texture);
  20. case GalTextureFormat.R8: return Read1Bpp (Memory, Texture);
  21. case GalTextureFormat.BC7U: return Read16Bpt4x4(Memory, Texture);
  22. case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture);
  23. case GalTextureFormat.BC2: return Read16Bpt4x4(Memory, Texture);
  24. case GalTextureFormat.BC3: return Read16Bpt4x4(Memory, Texture);
  25. case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture);
  26. case GalTextureFormat.BC5: return Read16Bpt4x4(Memory, Texture);
  27. case GalTextureFormat.ZF32: return Read4Bpp (Memory, Texture);
  28. case GalTextureFormat.Astc2D4x4: return Read16Bpt4x4(Memory, Texture);
  29. }
  30. throw new NotImplementedException(Texture.Format.ToString());
  31. }
  32. private unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture)
  33. {
  34. int Width = Texture.Width;
  35. int Height = Texture.Height;
  36. byte[] Output = new byte[Width * Height];
  37. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 1);
  38. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  39. Memory,
  40. Texture.Position);
  41. fixed (byte* BuffPtr = Output)
  42. {
  43. long OutOffs = 0;
  44. for (int Y = 0; Y < Height; Y++)
  45. for (int X = 0; X < Width; X++)
  46. {
  47. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  48. byte Pixel = CpuMem.ReadByteUnchecked(Position + Offset);
  49. *(BuffPtr + OutOffs) = Pixel;
  50. OutOffs++;
  51. }
  52. }
  53. return Output;
  54. }
  55. private unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture)
  56. {
  57. int Width = Texture.Width;
  58. int Height = Texture.Height;
  59. byte[] Output = new byte[Width * Height * 2];
  60. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2);
  61. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  62. Memory,
  63. Texture.Position);
  64. fixed (byte* BuffPtr = Output)
  65. {
  66. long OutOffs = 0;
  67. for (int Y = 0; Y < Height; Y++)
  68. for (int X = 0; X < Width; X++)
  69. {
  70. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  71. uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset);
  72. Pixel = (Pixel & 0x001f) << 11 |
  73. (Pixel & 0x03e0) << 1 |
  74. (Pixel & 0x7c00) >> 9 |
  75. (Pixel & 0x8000) >> 15;
  76. *(short*)(BuffPtr + OutOffs) = (short)Pixel;
  77. OutOffs += 2;
  78. }
  79. }
  80. return Output;
  81. }
  82. private unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture)
  83. {
  84. int Width = Texture.Width;
  85. int Height = Texture.Height;
  86. byte[] Output = new byte[Width * Height * 2];
  87. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2);
  88. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  89. Memory,
  90. Texture.Position);
  91. fixed (byte* BuffPtr = Output)
  92. {
  93. long OutOffs = 0;
  94. for (int Y = 0; Y < Height; Y++)
  95. for (int X = 0; X < Width; X++)
  96. {
  97. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  98. uint Pixel = (uint)CpuMem.ReadInt16Unchecked(Position + Offset);
  99. Pixel = (Pixel & 0x001f) << 11 |
  100. (Pixel & 0x07e0) |
  101. (Pixel & 0xf800) >> 11;
  102. *(short*)(BuffPtr + OutOffs) = (short)Pixel;
  103. OutOffs += 2;
  104. }
  105. }
  106. return Output;
  107. }
  108. private unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture)
  109. {
  110. int Width = Texture.Width;
  111. int Height = Texture.Height;
  112. byte[] Output = new byte[Width * Height * 2];
  113. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2);
  114. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  115. Memory,
  116. Texture.Position);
  117. fixed (byte* BuffPtr = Output)
  118. {
  119. long OutOffs = 0;
  120. for (int Y = 0; Y < Height; Y++)
  121. for (int X = 0; X < Width; X++)
  122. {
  123. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  124. short Pixel = CpuMem.ReadInt16Unchecked(Position + Offset);
  125. *(short*)(BuffPtr + OutOffs) = Pixel;
  126. OutOffs += 2;
  127. }
  128. }
  129. return Output;
  130. }
  131. private unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture)
  132. {
  133. int Width = Texture.Width;
  134. int Height = Texture.Height;
  135. byte[] Output = new byte[Width * Height * 4];
  136. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4);
  137. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  138. Memory,
  139. Texture.Position);
  140. fixed (byte* BuffPtr = Output)
  141. {
  142. long OutOffs = 0;
  143. for (int Y = 0; Y < Height; Y++)
  144. for (int X = 0; X < Width; X++)
  145. {
  146. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  147. int Pixel = CpuMem.ReadInt32Unchecked(Position + Offset);
  148. *(int*)(BuffPtr + OutOffs) = Pixel;
  149. OutOffs += 4;
  150. }
  151. }
  152. return Output;
  153. }
  154. private unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture)
  155. {
  156. int Width = Texture.Width;
  157. int Height = Texture.Height;
  158. byte[] Output = new byte[Width * Height * 8];
  159. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8);
  160. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  161. Memory,
  162. Texture.Position);
  163. fixed (byte* BuffPtr = Output)
  164. {
  165. long OutOffs = 0;
  166. for (int Y = 0; Y < Height; Y++)
  167. for (int X = 0; X < Width; X++)
  168. {
  169. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  170. long Pixel = CpuMem.ReadInt64Unchecked(Position + Offset);
  171. *(long*)(BuffPtr + OutOffs) = Pixel;
  172. OutOffs += 8;
  173. }
  174. }
  175. return Output;
  176. }
  177. private unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture)
  178. {
  179. int Width = Texture.Width;
  180. int Height = Texture.Height;
  181. byte[] Output = new byte[Width * Height * 16];
  182. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16);
  183. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  184. Memory,
  185. Texture.Position);
  186. fixed (byte* BuffPtr = Output)
  187. {
  188. long OutOffs = 0;
  189. for (int Y = 0; Y < Height; Y++)
  190. for (int X = 0; X < Width; X++)
  191. {
  192. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  193. long PxLow = CpuMem.ReadInt64Unchecked(Position + Offset + 0);
  194. long PxHigh = CpuMem.ReadInt64Unchecked(Position + Offset + 8);
  195. *(long*)(BuffPtr + OutOffs + 0) = PxLow;
  196. *(long*)(BuffPtr + OutOffs + 8) = PxHigh;
  197. OutOffs += 16;
  198. }
  199. }
  200. return Output;
  201. }
  202. private unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture)
  203. {
  204. int Width = (Texture.Width + 3) / 4;
  205. int Height = (Texture.Height + 3) / 4;
  206. byte[] Output = new byte[Width * Height * 8];
  207. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8);
  208. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  209. Memory,
  210. Texture.Position);
  211. fixed (byte* BuffPtr = Output)
  212. {
  213. long OutOffs = 0;
  214. for (int Y = 0; Y < Height; Y++)
  215. for (int X = 0; X < Width; X++)
  216. {
  217. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  218. long Tile = CpuMem.ReadInt64Unchecked(Position + Offset);
  219. *(long*)(BuffPtr + OutOffs) = Tile;
  220. OutOffs += 8;
  221. }
  222. }
  223. return Output;
  224. }
  225. private unsafe static byte[] Read16Bpt4x4(IAMemory Memory, TextureInfo Texture)
  226. {
  227. int Width = (Texture.Width + 3) / 4;
  228. int Height = (Texture.Height + 3) / 4;
  229. byte[] Output = new byte[Width * Height * 16];
  230. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16);
  231. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  232. Memory,
  233. Texture.Position);
  234. fixed (byte* BuffPtr = Output)
  235. {
  236. long OutOffs = 0;
  237. for (int Y = 0; Y < Height; Y++)
  238. for (int X = 0; X < Width; X++)
  239. {
  240. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  241. long Tile0 = CpuMem.ReadInt64Unchecked(Position + Offset + 0);
  242. long Tile1 = CpuMem.ReadInt64Unchecked(Position + Offset + 8);
  243. *(long*)(BuffPtr + OutOffs + 0) = Tile0;
  244. *(long*)(BuffPtr + OutOffs + 8) = Tile1;
  245. OutOffs += 16;
  246. }
  247. }
  248. return Output;
  249. }
  250. }
  251. }