TextureReader.cs 13 KB

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