TextureReader.cs 13 KB

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