TextureReader.cs 13 KB

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