TextureReader.cs 11 KB

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