LayoutConverter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Runtime.Intrinsics;
  4. using static Ryujinx.Graphics.Texture.BlockLinearConstants;
  5. namespace Ryujinx.Graphics.Texture
  6. {
  7. public static class LayoutConverter
  8. {
  9. private const int HostStrideAlignment = 4;
  10. public static Span<byte> ConvertBlockLinearToLinear(
  11. int width,
  12. int height,
  13. int depth,
  14. int levels,
  15. int layers,
  16. int blockWidth,
  17. int blockHeight,
  18. int bytesPerPixel,
  19. int gobBlocksInY,
  20. int gobBlocksInZ,
  21. int gobBlocksInTileX,
  22. SizeInfo sizeInfo,
  23. ReadOnlySpan<byte> data)
  24. {
  25. int outSize = GetTextureSize(
  26. width,
  27. height,
  28. depth,
  29. levels,
  30. layers,
  31. blockWidth,
  32. blockHeight,
  33. bytesPerPixel);
  34. Span<byte> output = new byte[outSize];
  35. int outOffs = 0;
  36. int mipGobBlocksInY = gobBlocksInY;
  37. int mipGobBlocksInZ = gobBlocksInZ;
  38. int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
  39. int gobHeight = gobBlocksInY * GobHeight;
  40. for (int level = 0; level < levels; level++)
  41. {
  42. int w = Math.Max(1, width >> level);
  43. int h = Math.Max(1, height >> level);
  44. int d = Math.Max(1, depth >> level);
  45. w = BitUtils.DivRoundUp(w, blockWidth);
  46. h = BitUtils.DivRoundUp(h, blockHeight);
  47. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  48. {
  49. mipGobBlocksInY >>= 1;
  50. }
  51. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  52. {
  53. mipGobBlocksInZ >>= 1;
  54. }
  55. int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16);
  56. int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64);
  57. int xStart = strideTrunc / bytesPerPixel;
  58. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  59. int outStrideGap = stride - w * bytesPerPixel;
  60. int alignment = gobWidth;
  61. if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight)
  62. {
  63. alignment = GobStride / bytesPerPixel;
  64. }
  65. int wAligned = BitUtils.AlignUp(w, alignment);
  66. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  67. wAligned,
  68. h,
  69. d,
  70. mipGobBlocksInY,
  71. mipGobBlocksInZ,
  72. bytesPerPixel);
  73. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  74. {
  75. fixed (byte* outputPtr = output, dataPtr = data)
  76. {
  77. byte* outPtr = outputPtr + outOffs;
  78. for (int layer = 0; layer < layers; layer++)
  79. {
  80. byte* inBaseOffset = dataPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level));
  81. for (int z = 0; z < d; z++)
  82. {
  83. layoutConverter.SetZ(z);
  84. for (int y = 0; y < h; y++)
  85. {
  86. layoutConverter.SetY(y);
  87. for (int x = 0; x < strideTrunc64; x += 64, outPtr += 64)
  88. {
  89. byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x);
  90. byte* offset2 = offset + 0x20;
  91. byte* offset3 = offset + 0x100;
  92. byte* offset4 = offset + 0x120;
  93. Vector128<byte> value = *(Vector128<byte>*)offset;
  94. Vector128<byte> value2 = *(Vector128<byte>*)offset2;
  95. Vector128<byte> value3 = *(Vector128<byte>*)offset3;
  96. Vector128<byte> value4 = *(Vector128<byte>*)offset4;
  97. *(Vector128<byte>*)outPtr = value;
  98. *(Vector128<byte>*)(outPtr + 16) = value2;
  99. *(Vector128<byte>*)(outPtr + 32) = value3;
  100. *(Vector128<byte>*)(outPtr + 48) = value4;
  101. }
  102. for (int x = strideTrunc64; x < strideTrunc; x += 16, outPtr += 16)
  103. {
  104. byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x);
  105. *(Vector128<byte>*)outPtr = *(Vector128<byte>*)offset;
  106. }
  107. for (int x = xStart; x < w; x++, outPtr += bytesPerPixel)
  108. {
  109. byte* offset = inBaseOffset + layoutConverter.GetOffset(x);
  110. *(T*)outPtr = *(T*)offset;
  111. }
  112. outPtr += outStrideGap;
  113. }
  114. }
  115. }
  116. outOffs += stride * h * d * layers;
  117. }
  118. return true;
  119. }
  120. bool _ = bytesPerPixel switch
  121. {
  122. 1 => Convert<byte>(output, data),
  123. 2 => Convert<ushort>(output, data),
  124. 4 => Convert<uint>(output, data),
  125. 8 => Convert<ulong>(output, data),
  126. 12 => Convert<Bpp12Pixel>(output, data),
  127. 16 => Convert<Vector128<byte>>(output, data),
  128. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  129. };
  130. }
  131. return output;
  132. }
  133. public static Span<byte> ConvertLinearStridedToLinear(
  134. int width,
  135. int height,
  136. int blockWidth,
  137. int blockHeight,
  138. int stride,
  139. int bytesPerPixel,
  140. ReadOnlySpan<byte> data)
  141. {
  142. int w = BitUtils.DivRoundUp(width, blockWidth);
  143. int h = BitUtils.DivRoundUp(height, blockHeight);
  144. int outStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  145. int lineSize = w * bytesPerPixel;
  146. Span<byte> output = new byte[h * outStride];
  147. int outOffs = 0;
  148. int inOffs = 0;
  149. for (int y = 0; y < h; y++)
  150. {
  151. data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize));
  152. inOffs += stride;
  153. outOffs += outStride;
  154. }
  155. return output;
  156. }
  157. public static Span<byte> ConvertLinearToBlockLinear(
  158. int width,
  159. int height,
  160. int depth,
  161. int levels,
  162. int layers,
  163. int blockWidth,
  164. int blockHeight,
  165. int bytesPerPixel,
  166. int gobBlocksInY,
  167. int gobBlocksInZ,
  168. int gobBlocksInTileX,
  169. SizeInfo sizeInfo,
  170. ReadOnlySpan<byte> data)
  171. {
  172. Span<byte> output = new byte[sizeInfo.TotalSize];
  173. int inOffs = 0;
  174. int mipGobBlocksInY = gobBlocksInY;
  175. int mipGobBlocksInZ = gobBlocksInZ;
  176. int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
  177. int gobHeight = gobBlocksInY * GobHeight;
  178. for (int level = 0; level < levels; level++)
  179. {
  180. int w = Math.Max(1, width >> level);
  181. int h = Math.Max(1, height >> level);
  182. int d = Math.Max(1, depth >> level);
  183. w = BitUtils.DivRoundUp(w, blockWidth);
  184. h = BitUtils.DivRoundUp(h, blockHeight);
  185. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  186. {
  187. mipGobBlocksInY >>= 1;
  188. }
  189. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  190. {
  191. mipGobBlocksInZ >>= 1;
  192. }
  193. int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16);
  194. int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64);
  195. int xStart = strideTrunc / bytesPerPixel;
  196. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  197. int inStrideGap = stride - w * bytesPerPixel;
  198. int alignment = gobWidth;
  199. if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight)
  200. {
  201. alignment = GobStride / bytesPerPixel;
  202. }
  203. int wAligned = BitUtils.AlignUp(w, alignment);
  204. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  205. wAligned,
  206. h,
  207. d,
  208. mipGobBlocksInY,
  209. mipGobBlocksInZ,
  210. bytesPerPixel);
  211. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  212. {
  213. fixed (byte* outputPtr = output, dataPtr = data)
  214. {
  215. byte* inPtr = dataPtr + inOffs;
  216. for (int layer = 0; layer < layers; layer++)
  217. {
  218. byte* outBaseOffset = outputPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level));
  219. for (int z = 0; z < d; z++)
  220. {
  221. layoutConverter.SetZ(z);
  222. for (int y = 0; y < h; y++)
  223. {
  224. layoutConverter.SetY(y);
  225. for (int x = 0; x < strideTrunc64; x += 64, inPtr += 64)
  226. {
  227. byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x);
  228. byte* offset2 = offset + 0x20;
  229. byte* offset3 = offset + 0x100;
  230. byte* offset4 = offset + 0x120;
  231. Vector128<byte> value = *(Vector128<byte>*)inPtr;
  232. Vector128<byte> value2 = *(Vector128<byte>*)(inPtr + 16);
  233. Vector128<byte> value3 = *(Vector128<byte>*)(inPtr + 32);
  234. Vector128<byte> value4 = *(Vector128<byte>*)(inPtr + 48);
  235. *(Vector128<byte>*)offset = value;
  236. *(Vector128<byte>*)offset2 = value2;
  237. *(Vector128<byte>*)offset3 = value3;
  238. *(Vector128<byte>*)offset4 = value4;
  239. }
  240. for (int x = strideTrunc64; x < strideTrunc; x += 16, inPtr += 16)
  241. {
  242. byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x);
  243. *(Vector128<byte>*)offset = *(Vector128<byte>*)inPtr;
  244. }
  245. for (int x = xStart; x < w; x++, inPtr += bytesPerPixel)
  246. {
  247. byte* offset = outBaseOffset + layoutConverter.GetOffset(x);
  248. *(T*)offset = *(T*)inPtr;
  249. }
  250. inPtr += inStrideGap;
  251. }
  252. }
  253. }
  254. inOffs += stride * h * d * layers;
  255. }
  256. return true;
  257. }
  258. bool _ = bytesPerPixel switch
  259. {
  260. 1 => Convert<byte>(output, data),
  261. 2 => Convert<ushort>(output, data),
  262. 4 => Convert<uint>(output, data),
  263. 8 => Convert<ulong>(output, data),
  264. 12 => Convert<Bpp12Pixel>(output, data),
  265. 16 => Convert<Vector128<byte>>(output, data),
  266. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  267. };
  268. }
  269. return output;
  270. }
  271. public static Span<byte> ConvertLinearToLinearStrided(
  272. int width,
  273. int height,
  274. int blockWidth,
  275. int blockHeight,
  276. int stride,
  277. int bytesPerPixel,
  278. ReadOnlySpan<byte> data)
  279. {
  280. int w = BitUtils.DivRoundUp(width, blockWidth);
  281. int h = BitUtils.DivRoundUp(height, blockHeight);
  282. int inStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  283. int lineSize = width * bytesPerPixel;
  284. Span<byte> output = new byte[h * stride];
  285. int inOffs = 0;
  286. int outOffs = 0;
  287. for (int y = 0; y < h; y++)
  288. {
  289. data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize));
  290. inOffs += inStride;
  291. outOffs += stride;
  292. }
  293. return output;
  294. }
  295. private static int GetTextureSize(
  296. int width,
  297. int height,
  298. int depth,
  299. int levels,
  300. int layers,
  301. int blockWidth,
  302. int blockHeight,
  303. int bytesPerPixel)
  304. {
  305. int layerSize = 0;
  306. for (int level = 0; level < levels; level++)
  307. {
  308. int w = Math.Max(1, width >> level);
  309. int h = Math.Max(1, height >> level);
  310. int d = Math.Max(1, depth >> level);
  311. w = BitUtils.DivRoundUp(w, blockWidth);
  312. h = BitUtils.DivRoundUp(h, blockHeight);
  313. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  314. layerSize += stride * h * d;
  315. }
  316. return layerSize * layers;
  317. }
  318. }
  319. }