TextureReader.cs 11 KB

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