TextureReader.cs 13 KB

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