TextureReader.cs 14 KB

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