TextureReader.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.R32G32: return Read8Bpp (Memory, Texture);
  15. case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture);
  16. case GalTextureFormat.A2B10G10R10: return Read4Bpp (Memory, Texture);
  17. case GalTextureFormat.R32: return Read4Bpp (Memory, Texture);
  18. case GalTextureFormat.BF10GF11RF11: return Read4Bpp (Memory, Texture);
  19. case GalTextureFormat.Z24S8: return Read4Bpp (Memory, Texture);
  20. case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture);
  21. case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture);
  22. case GalTextureFormat.A4B4G4R4: return Read2Bpp (Memory, Texture);
  23. case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture);
  24. case GalTextureFormat.R16: return Read2Bpp (Memory, Texture);
  25. case GalTextureFormat.R8: return Read1Bpp (Memory, Texture);
  26. case GalTextureFormat.BC6H_SF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  27. case GalTextureFormat.BC6H_UF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  28. case GalTextureFormat.BC7U: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  29. case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture);
  30. case GalTextureFormat.BC2: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  31. case GalTextureFormat.BC3: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  32. case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture);
  33. case GalTextureFormat.BC5: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  34. case GalTextureFormat.ZF32: return Read4Bpp (Memory, Texture);
  35. case GalTextureFormat.ZF32_X24S8: return Read8Bpp (Memory, Texture);
  36. case GalTextureFormat.Astc2D4x4: return Read16BptCompressedTexture(Memory, Texture, 4, 4);
  37. case GalTextureFormat.Astc2D5x5: return Read16BptCompressedTexture(Memory, Texture, 5, 5);
  38. case GalTextureFormat.Astc2D6x6: return Read16BptCompressedTexture(Memory, Texture, 6, 6);
  39. case GalTextureFormat.Astc2D8x8: return Read16BptCompressedTexture(Memory, Texture, 8, 8);
  40. case GalTextureFormat.Astc2D10x10: return Read16BptCompressedTexture(Memory, Texture, 10, 10);
  41. case GalTextureFormat.Astc2D12x12: return Read16BptCompressedTexture(Memory, Texture, 12, 12);
  42. case GalTextureFormat.Astc2D5x4: return Read16BptCompressedTexture(Memory, Texture, 5, 4);
  43. case GalTextureFormat.Astc2D6x5: return Read16BptCompressedTexture(Memory, Texture, 6, 5);
  44. case GalTextureFormat.Astc2D8x6: return Read16BptCompressedTexture(Memory, Texture, 8, 6);
  45. case GalTextureFormat.Astc2D10x8: return Read16BptCompressedTexture(Memory, Texture, 10, 8);
  46. case GalTextureFormat.Astc2D12x10: return Read16BptCompressedTexture(Memory, Texture, 12, 10);
  47. case GalTextureFormat.Astc2D8x5: return Read16BptCompressedTexture(Memory, Texture, 8, 5);
  48. case GalTextureFormat.Astc2D10x5: return Read16BptCompressedTexture(Memory, Texture, 10, 5);
  49. case GalTextureFormat.Astc2D10x6: return Read16BptCompressedTexture(Memory, Texture, 10, 6);
  50. }
  51. throw new NotImplementedException("0x" + Texture.Format.ToString("x2"));
  52. }
  53. private unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture)
  54. {
  55. int Width = Texture.Width;
  56. int Height = Texture.Height;
  57. byte[] Output = new byte[Width * Height];
  58. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 1);
  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. byte Pixel = CpuMem.ReadByte(Position + Offset);
  70. *(BuffPtr + OutOffs) = Pixel;
  71. OutOffs++;
  72. }
  73. }
  74. return Output;
  75. }
  76. private unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture)
  77. {
  78. int Width = Texture.Width;
  79. int Height = Texture.Height;
  80. byte[] Output = new byte[Width * Height * 2];
  81. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
  82. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  83. Memory,
  84. Texture.Position);
  85. fixed (byte* BuffPtr = Output)
  86. {
  87. long OutOffs = 0;
  88. for (int Y = 0; Y < Height; Y++)
  89. for (int X = 0; X < Width; X++)
  90. {
  91. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  92. uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset);
  93. Pixel = (Pixel & 0x001f) << 11 |
  94. (Pixel & 0x03e0) << 1 |
  95. (Pixel & 0x7c00) >> 9 |
  96. (Pixel & 0x8000) >> 15;
  97. *(short*)(BuffPtr + OutOffs) = (short)Pixel;
  98. OutOffs += 2;
  99. }
  100. }
  101. return Output;
  102. }
  103. private unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture)
  104. {
  105. int Width = Texture.Width;
  106. int Height = Texture.Height;
  107. byte[] Output = new byte[Width * Height * 2];
  108. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
  109. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  110. Memory,
  111. Texture.Position);
  112. fixed (byte* BuffPtr = Output)
  113. {
  114. long OutOffs = 0;
  115. for (int Y = 0; Y < Height; Y++)
  116. for (int X = 0; X < Width; X++)
  117. {
  118. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  119. uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset);
  120. Pixel = (Pixel & 0x001f) << 11 |
  121. (Pixel & 0x07e0) |
  122. (Pixel & 0xf800) >> 11;
  123. *(short*)(BuffPtr + OutOffs) = (short)Pixel;
  124. OutOffs += 2;
  125. }
  126. }
  127. return Output;
  128. }
  129. private unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture)
  130. {
  131. int Width = Texture.Width;
  132. int Height = Texture.Height;
  133. byte[] Output = new byte[Width * Height * 2];
  134. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2);
  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. short Pixel = CpuMem.ReadInt16(Position + Offset);
  146. *(short*)(BuffPtr + OutOffs) = Pixel;
  147. OutOffs += 2;
  148. }
  149. }
  150. return Output;
  151. }
  152. private unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture)
  153. {
  154. int Width = Texture.Width;
  155. int Height = Texture.Height;
  156. byte[] Output = new byte[Width * Height * 4];
  157. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4);
  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. int Pixel = CpuMem.ReadInt32(Position + Offset);
  169. *(int*)(BuffPtr + OutOffs) = Pixel;
  170. OutOffs += 4;
  171. }
  172. }
  173. return Output;
  174. }
  175. private unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture)
  176. {
  177. int Width = Texture.Width;
  178. int Height = Texture.Height;
  179. byte[] Output = new byte[Width * Height * 8];
  180. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 8);
  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 Pixel = CpuMem.ReadInt64(Position + Offset);
  192. *(long*)(BuffPtr + OutOffs) = Pixel;
  193. OutOffs += 8;
  194. }
  195. }
  196. return Output;
  197. }
  198. private unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture)
  199. {
  200. int Width = Texture.Width;
  201. int Height = Texture.Height;
  202. byte[] Output = new byte[Width * Height * 16];
  203. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 16);
  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 PxLow = CpuMem.ReadInt64(Position + Offset + 0);
  215. long PxHigh = CpuMem.ReadInt64(Position + Offset + 8);
  216. *(long*)(BuffPtr + OutOffs + 0) = PxLow;
  217. *(long*)(BuffPtr + OutOffs + 8) = PxHigh;
  218. OutOffs += 16;
  219. }
  220. }
  221. return Output;
  222. }
  223. private unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture)
  224. {
  225. int Width = (Texture.Width + 3) / 4;
  226. int Height = (Texture.Height + 3) / 4;
  227. byte[] Output = new byte[Width * Height * 8];
  228. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 4, 8);
  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 Tile = CpuMem.ReadInt64(Position + Offset);
  240. *(long*)(BuffPtr + OutOffs) = Tile;
  241. OutOffs += 8;
  242. }
  243. }
  244. return Output;
  245. }
  246. private unsafe static byte[] Read16BptCompressedTexture(IAMemory Memory, TextureInfo Texture, int BlockWidth, int BlockHeight)
  247. {
  248. int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth;
  249. int Height = (Texture.Height + (BlockHeight - 1)) / BlockHeight;
  250. byte[] Output = new byte[Width * Height * 16];
  251. ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, BlockWidth, 16);
  252. (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition(
  253. Memory,
  254. Texture.Position);
  255. fixed (byte* BuffPtr = Output)
  256. {
  257. long OutOffs = 0;
  258. for (int Y = 0; Y < Height; Y++)
  259. for (int X = 0; X < Width; X++)
  260. {
  261. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  262. long Tile0 = CpuMem.ReadInt64(Position + Offset + 0);
  263. long Tile1 = CpuMem.ReadInt64(Position + Offset + 8);
  264. *(long*)(BuffPtr + OutOffs + 0) = Tile0;
  265. *(long*)(BuffPtr + OutOffs + 8) = Tile1;
  266. OutOffs += 16;
  267. }
  268. }
  269. return Output;
  270. }
  271. }
  272. }