TextureReader.cs 13 KB

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