TextureReader.cs 11 KB

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