LayoutConverter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 void ConvertBlockLinearToLinear(
  11. Span<byte> dst,
  12. int width,
  13. int height,
  14. int stride,
  15. int bytesPerPixel,
  16. int gobBlocksInY,
  17. ReadOnlySpan<byte> data)
  18. {
  19. int gobHeight = gobBlocksInY * GobHeight;
  20. int strideTrunc = BitUtils.AlignDown(width * bytesPerPixel, 16);
  21. int strideTrunc64 = BitUtils.AlignDown(width * bytesPerPixel, 64);
  22. int xStart = strideTrunc / bytesPerPixel;
  23. int outStrideGap = stride - width * bytesPerPixel;
  24. int alignment = GobStride / bytesPerPixel;
  25. int wAligned = BitUtils.AlignUp(width, alignment);
  26. BlockLinearLayout layoutConverter = new BlockLinearLayout(wAligned, height, gobBlocksInY, 1, bytesPerPixel);
  27. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  28. {
  29. fixed (byte* outputPtr = output, dataPtr = data)
  30. {
  31. byte* outPtr = outputPtr;
  32. for (int y = 0; y < height; y++)
  33. {
  34. layoutConverter.SetY(y);
  35. for (int x = 0; x < strideTrunc64; x += 64, outPtr += 64)
  36. {
  37. byte* offset = dataPtr + layoutConverter.GetOffsetWithLineOffset64(x);
  38. byte* offset2 = offset + 0x20;
  39. byte* offset3 = offset + 0x100;
  40. byte* offset4 = offset + 0x120;
  41. Vector128<byte> value = *(Vector128<byte>*)offset;
  42. Vector128<byte> value2 = *(Vector128<byte>*)offset2;
  43. Vector128<byte> value3 = *(Vector128<byte>*)offset3;
  44. Vector128<byte> value4 = *(Vector128<byte>*)offset4;
  45. *(Vector128<byte>*)outPtr = value;
  46. *(Vector128<byte>*)(outPtr + 16) = value2;
  47. *(Vector128<byte>*)(outPtr + 32) = value3;
  48. *(Vector128<byte>*)(outPtr + 48) = value4;
  49. }
  50. for (int x = strideTrunc64; x < strideTrunc; x += 16, outPtr += 16)
  51. {
  52. byte* offset = dataPtr + layoutConverter.GetOffsetWithLineOffset16(x);
  53. *(Vector128<byte>*)outPtr = *(Vector128<byte>*)offset;
  54. }
  55. for (int x = xStart; x < width; x++, outPtr += bytesPerPixel)
  56. {
  57. byte* offset = dataPtr + layoutConverter.GetOffset(x);
  58. *(T*)outPtr = *(T*)offset;
  59. }
  60. outPtr += outStrideGap;
  61. }
  62. }
  63. return true;
  64. }
  65. bool _ = bytesPerPixel switch
  66. {
  67. 1 => Convert<byte>(dst, data),
  68. 2 => Convert<ushort>(dst, data),
  69. 4 => Convert<uint>(dst, data),
  70. 8 => Convert<ulong>(dst, data),
  71. 12 => Convert<Bpp12Pixel>(dst, data),
  72. 16 => Convert<Vector128<byte>>(dst, data),
  73. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  74. };
  75. }
  76. public static Span<byte> ConvertBlockLinearToLinear(
  77. int width,
  78. int height,
  79. int depth,
  80. int levels,
  81. int layers,
  82. int blockWidth,
  83. int blockHeight,
  84. int bytesPerPixel,
  85. int gobBlocksInY,
  86. int gobBlocksInZ,
  87. int gobBlocksInTileX,
  88. SizeInfo sizeInfo,
  89. ReadOnlySpan<byte> data)
  90. {
  91. int outSize = GetTextureSize(
  92. width,
  93. height,
  94. depth,
  95. levels,
  96. layers,
  97. blockWidth,
  98. blockHeight,
  99. bytesPerPixel);
  100. Span<byte> output = new byte[outSize];
  101. int outOffs = 0;
  102. int mipGobBlocksInY = gobBlocksInY;
  103. int mipGobBlocksInZ = gobBlocksInZ;
  104. int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
  105. int gobHeight = gobBlocksInY * GobHeight;
  106. for (int level = 0; level < levels; level++)
  107. {
  108. int w = Math.Max(1, width >> level);
  109. int h = Math.Max(1, height >> level);
  110. int d = Math.Max(1, depth >> level);
  111. w = BitUtils.DivRoundUp(w, blockWidth);
  112. h = BitUtils.DivRoundUp(h, blockHeight);
  113. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  114. {
  115. mipGobBlocksInY >>= 1;
  116. }
  117. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  118. {
  119. mipGobBlocksInZ >>= 1;
  120. }
  121. int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16);
  122. int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64);
  123. int xStart = strideTrunc / bytesPerPixel;
  124. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  125. int outStrideGap = stride - w * bytesPerPixel;
  126. int alignment = gobWidth;
  127. if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight)
  128. {
  129. alignment = GobStride / bytesPerPixel;
  130. }
  131. int wAligned = BitUtils.AlignUp(w, alignment);
  132. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  133. wAligned,
  134. h,
  135. mipGobBlocksInY,
  136. mipGobBlocksInZ,
  137. bytesPerPixel);
  138. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  139. {
  140. fixed (byte* outputPtr = output, dataPtr = data)
  141. {
  142. byte* outPtr = outputPtr + outOffs;
  143. for (int layer = 0; layer < layers; layer++)
  144. {
  145. byte* inBaseOffset = dataPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level));
  146. for (int z = 0; z < d; z++)
  147. {
  148. layoutConverter.SetZ(z);
  149. for (int y = 0; y < h; y++)
  150. {
  151. layoutConverter.SetY(y);
  152. for (int x = 0; x < strideTrunc64; x += 64, outPtr += 64)
  153. {
  154. byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x);
  155. byte* offset2 = offset + 0x20;
  156. byte* offset3 = offset + 0x100;
  157. byte* offset4 = offset + 0x120;
  158. Vector128<byte> value = *(Vector128<byte>*)offset;
  159. Vector128<byte> value2 = *(Vector128<byte>*)offset2;
  160. Vector128<byte> value3 = *(Vector128<byte>*)offset3;
  161. Vector128<byte> value4 = *(Vector128<byte>*)offset4;
  162. *(Vector128<byte>*)outPtr = value;
  163. *(Vector128<byte>*)(outPtr + 16) = value2;
  164. *(Vector128<byte>*)(outPtr + 32) = value3;
  165. *(Vector128<byte>*)(outPtr + 48) = value4;
  166. }
  167. for (int x = strideTrunc64; x < strideTrunc; x += 16, outPtr += 16)
  168. {
  169. byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x);
  170. *(Vector128<byte>*)outPtr = *(Vector128<byte>*)offset;
  171. }
  172. for (int x = xStart; x < w; x++, outPtr += bytesPerPixel)
  173. {
  174. byte* offset = inBaseOffset + layoutConverter.GetOffset(x);
  175. *(T*)outPtr = *(T*)offset;
  176. }
  177. outPtr += outStrideGap;
  178. }
  179. }
  180. }
  181. outOffs += stride * h * d * layers;
  182. }
  183. return true;
  184. }
  185. bool _ = bytesPerPixel switch
  186. {
  187. 1 => Convert<byte>(output, data),
  188. 2 => Convert<ushort>(output, data),
  189. 4 => Convert<uint>(output, data),
  190. 8 => Convert<ulong>(output, data),
  191. 12 => Convert<Bpp12Pixel>(output, data),
  192. 16 => Convert<Vector128<byte>>(output, data),
  193. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  194. };
  195. }
  196. return output;
  197. }
  198. public static Span<byte> ConvertLinearStridedToLinear(
  199. int width,
  200. int height,
  201. int blockWidth,
  202. int blockHeight,
  203. int stride,
  204. int bytesPerPixel,
  205. ReadOnlySpan<byte> data)
  206. {
  207. int w = BitUtils.DivRoundUp(width, blockWidth);
  208. int h = BitUtils.DivRoundUp(height, blockHeight);
  209. int outStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  210. int lineSize = Math.Min(stride, outStride);
  211. Span<byte> output = new byte[h * outStride];
  212. int outOffs = 0;
  213. int inOffs = 0;
  214. for (int y = 0; y < h; y++)
  215. {
  216. data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize));
  217. inOffs += stride;
  218. outOffs += outStride;
  219. }
  220. return output;
  221. }
  222. public static void ConvertLinearToBlockLinear(
  223. Span<byte> dst,
  224. int width,
  225. int height,
  226. int stride,
  227. int bytesPerPixel,
  228. int gobBlocksInY,
  229. ReadOnlySpan<byte> data)
  230. {
  231. int gobHeight = gobBlocksInY * GobHeight;
  232. int strideTrunc = BitUtils.AlignDown(width * bytesPerPixel, 16);
  233. int strideTrunc64 = BitUtils.AlignDown(width * bytesPerPixel, 64);
  234. int xStart = strideTrunc / bytesPerPixel;
  235. int inStrideGap = stride - width * bytesPerPixel;
  236. int alignment = GobStride / bytesPerPixel;
  237. int wAligned = BitUtils.AlignUp(width, alignment);
  238. BlockLinearLayout layoutConverter = new BlockLinearLayout(wAligned, height, gobBlocksInY, 1, bytesPerPixel);
  239. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  240. {
  241. fixed (byte* outputPtr = output, dataPtr = data)
  242. {
  243. byte* inPtr = dataPtr;
  244. for (int y = 0; y < height; y++)
  245. {
  246. layoutConverter.SetY(y);
  247. for (int x = 0; x < strideTrunc64; x += 64, inPtr += 64)
  248. {
  249. byte* offset = outputPtr + layoutConverter.GetOffsetWithLineOffset64(x);
  250. byte* offset2 = offset + 0x20;
  251. byte* offset3 = offset + 0x100;
  252. byte* offset4 = offset + 0x120;
  253. Vector128<byte> value = *(Vector128<byte>*)inPtr;
  254. Vector128<byte> value2 = *(Vector128<byte>*)(inPtr + 16);
  255. Vector128<byte> value3 = *(Vector128<byte>*)(inPtr + 32);
  256. Vector128<byte> value4 = *(Vector128<byte>*)(inPtr + 48);
  257. *(Vector128<byte>*)offset = value;
  258. *(Vector128<byte>*)offset2 = value2;
  259. *(Vector128<byte>*)offset3 = value3;
  260. *(Vector128<byte>*)offset4 = value4;
  261. }
  262. for (int x = strideTrunc64; x < strideTrunc; x += 16, inPtr += 16)
  263. {
  264. byte* offset = outputPtr + layoutConverter.GetOffsetWithLineOffset16(x);
  265. *(Vector128<byte>*)offset = *(Vector128<byte>*)inPtr;
  266. }
  267. for (int x = xStart; x < width; x++, inPtr += bytesPerPixel)
  268. {
  269. byte* offset = outputPtr + layoutConverter.GetOffset(x);
  270. *(T*)offset = *(T*)inPtr;
  271. }
  272. inPtr += inStrideGap;
  273. }
  274. }
  275. return true;
  276. }
  277. bool _ = bytesPerPixel switch
  278. {
  279. 1 => Convert<byte>(dst, data),
  280. 2 => Convert<ushort>(dst, data),
  281. 4 => Convert<uint>(dst, data),
  282. 8 => Convert<ulong>(dst, data),
  283. 12 => Convert<Bpp12Pixel>(dst, data),
  284. 16 => Convert<Vector128<byte>>(dst, data),
  285. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  286. };
  287. }
  288. public static ReadOnlySpan<byte> ConvertLinearToBlockLinear(
  289. Span<byte> output,
  290. int width,
  291. int height,
  292. int depth,
  293. int levels,
  294. int layers,
  295. int blockWidth,
  296. int blockHeight,
  297. int bytesPerPixel,
  298. int gobBlocksInY,
  299. int gobBlocksInZ,
  300. int gobBlocksInTileX,
  301. SizeInfo sizeInfo,
  302. ReadOnlySpan<byte> data)
  303. {
  304. if (output.Length == 0)
  305. {
  306. output = new byte[sizeInfo.TotalSize];
  307. }
  308. int inOffs = 0;
  309. int mipGobBlocksInY = gobBlocksInY;
  310. int mipGobBlocksInZ = gobBlocksInZ;
  311. int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
  312. int gobHeight = gobBlocksInY * GobHeight;
  313. for (int level = 0; level < levels; level++)
  314. {
  315. int w = Math.Max(1, width >> level);
  316. int h = Math.Max(1, height >> level);
  317. int d = Math.Max(1, depth >> level);
  318. w = BitUtils.DivRoundUp(w, blockWidth);
  319. h = BitUtils.DivRoundUp(h, blockHeight);
  320. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  321. {
  322. mipGobBlocksInY >>= 1;
  323. }
  324. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  325. {
  326. mipGobBlocksInZ >>= 1;
  327. }
  328. int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16);
  329. int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64);
  330. int xStart = strideTrunc / bytesPerPixel;
  331. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  332. int inStrideGap = stride - w * bytesPerPixel;
  333. int alignment = gobWidth;
  334. if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight)
  335. {
  336. alignment = GobStride / bytesPerPixel;
  337. }
  338. int wAligned = BitUtils.AlignUp(w, alignment);
  339. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  340. wAligned,
  341. h,
  342. mipGobBlocksInY,
  343. mipGobBlocksInZ,
  344. bytesPerPixel);
  345. unsafe bool Convert<T>(Span<byte> output, ReadOnlySpan<byte> data) where T : unmanaged
  346. {
  347. fixed (byte* outputPtr = output, dataPtr = data)
  348. {
  349. byte* inPtr = dataPtr + inOffs;
  350. for (int layer = 0; layer < layers; layer++)
  351. {
  352. byte* outBaseOffset = outputPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level));
  353. for (int z = 0; z < d; z++)
  354. {
  355. layoutConverter.SetZ(z);
  356. for (int y = 0; y < h; y++)
  357. {
  358. layoutConverter.SetY(y);
  359. for (int x = 0; x < strideTrunc64; x += 64, inPtr += 64)
  360. {
  361. byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x);
  362. byte* offset2 = offset + 0x20;
  363. byte* offset3 = offset + 0x100;
  364. byte* offset4 = offset + 0x120;
  365. Vector128<byte> value = *(Vector128<byte>*)inPtr;
  366. Vector128<byte> value2 = *(Vector128<byte>*)(inPtr + 16);
  367. Vector128<byte> value3 = *(Vector128<byte>*)(inPtr + 32);
  368. Vector128<byte> value4 = *(Vector128<byte>*)(inPtr + 48);
  369. *(Vector128<byte>*)offset = value;
  370. *(Vector128<byte>*)offset2 = value2;
  371. *(Vector128<byte>*)offset3 = value3;
  372. *(Vector128<byte>*)offset4 = value4;
  373. }
  374. for (int x = strideTrunc64; x < strideTrunc; x += 16, inPtr += 16)
  375. {
  376. byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x);
  377. *(Vector128<byte>*)offset = *(Vector128<byte>*)inPtr;
  378. }
  379. for (int x = xStart; x < w; x++, inPtr += bytesPerPixel)
  380. {
  381. byte* offset = outBaseOffset + layoutConverter.GetOffset(x);
  382. *(T*)offset = *(T*)inPtr;
  383. }
  384. inPtr += inStrideGap;
  385. }
  386. }
  387. }
  388. inOffs += stride * h * d * layers;
  389. }
  390. return true;
  391. }
  392. bool _ = bytesPerPixel switch
  393. {
  394. 1 => Convert<byte>(output, data),
  395. 2 => Convert<ushort>(output, data),
  396. 4 => Convert<uint>(output, data),
  397. 8 => Convert<ulong>(output, data),
  398. 12 => Convert<Bpp12Pixel>(output, data),
  399. 16 => Convert<Vector128<byte>>(output, data),
  400. _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.")
  401. };
  402. }
  403. return output;
  404. }
  405. public static ReadOnlySpan<byte> ConvertLinearToLinearStrided(
  406. Span<byte> output,
  407. int width,
  408. int height,
  409. int blockWidth,
  410. int blockHeight,
  411. int stride,
  412. int bytesPerPixel,
  413. ReadOnlySpan<byte> data)
  414. {
  415. int w = BitUtils.DivRoundUp(width, blockWidth);
  416. int h = BitUtils.DivRoundUp(height, blockHeight);
  417. int inStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  418. int lineSize = width * bytesPerPixel;
  419. if (inStride == stride)
  420. {
  421. if (output.Length != 0)
  422. {
  423. data.CopyTo(output);
  424. return output;
  425. }
  426. else
  427. {
  428. return data;
  429. }
  430. }
  431. if (output.Length == 0)
  432. {
  433. output = new byte[h * stride];
  434. }
  435. int inOffs = 0;
  436. int outOffs = 0;
  437. for (int y = 0; y < h; y++)
  438. {
  439. data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize));
  440. inOffs += inStride;
  441. outOffs += stride;
  442. }
  443. return output;
  444. }
  445. private static int GetTextureSize(
  446. int width,
  447. int height,
  448. int depth,
  449. int levels,
  450. int layers,
  451. int blockWidth,
  452. int blockHeight,
  453. int bytesPerPixel)
  454. {
  455. int layerSize = 0;
  456. for (int level = 0; level < levels; level++)
  457. {
  458. int w = Math.Max(1, width >> level);
  459. int h = Math.Max(1, height >> level);
  460. int d = Math.Max(1, depth >> level);
  461. w = BitUtils.DivRoundUp(w, blockWidth);
  462. h = BitUtils.DivRoundUp(h, blockHeight);
  463. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  464. layerSize += stride * h * d;
  465. }
  466. return layerSize * layers;
  467. }
  468. }
  469. }