TextureReader.cs 11 KB

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