BCnDecoder.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Buffers.Binary;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace Ryujinx.Graphics.Texture
  8. {
  9. public static class BCnDecoder
  10. {
  11. private const int BlockWidth = 4;
  12. private const int BlockHeight = 4;
  13. public static byte[] DecodeBC1(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  14. {
  15. int size = 0;
  16. for (int l = 0; l < levels; l++)
  17. {
  18. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 4;
  19. }
  20. byte[] output = new byte[size];
  21. Span<byte> tile = stackalloc byte[BlockWidth * BlockHeight * 4];
  22. Span<uint> tileAsUint = MemoryMarshal.Cast<byte, uint>(tile);
  23. Span<uint> outputAsUint = MemoryMarshal.Cast<byte, uint>(output);
  24. Span<Vector128<byte>> tileAsVector128 = MemoryMarshal.Cast<byte, Vector128<byte>>(tile);
  25. Span<Vector128<byte>> outputLine0 = default;
  26. Span<Vector128<byte>> outputLine1 = default;
  27. Span<Vector128<byte>> outputLine2 = default;
  28. Span<Vector128<byte>> outputLine3 = default;
  29. int imageBaseOOffs = 0;
  30. for (int l = 0; l < levels; l++)
  31. {
  32. int w = BitUtils.DivRoundUp(width, BlockWidth);
  33. int h = BitUtils.DivRoundUp(height, BlockHeight);
  34. for (int l2 = 0; l2 < layers; l2++)
  35. {
  36. for (int z = 0; z < depth; z++)
  37. {
  38. for (int y = 0; y < h; y++)
  39. {
  40. int baseY = y * BlockHeight;
  41. int copyHeight = Math.Min(BlockHeight, height - baseY);
  42. int lineBaseOOffs = imageBaseOOffs + baseY * width;
  43. if (copyHeight == 4)
  44. {
  45. outputLine0 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs));
  46. outputLine1 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width));
  47. outputLine2 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 2));
  48. outputLine3 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 3));
  49. }
  50. for (int x = 0; x < w; x++)
  51. {
  52. int baseX = x * BlockWidth;
  53. int copyWidth = Math.Min(BlockWidth, width - baseX);
  54. BC1DecodeTileRgb(tile, data);
  55. if ((copyWidth | copyHeight) == 4)
  56. {
  57. outputLine0[x] = tileAsVector128[0];
  58. outputLine1[x] = tileAsVector128[1];
  59. outputLine2[x] = tileAsVector128[2];
  60. outputLine3[x] = tileAsVector128[3];
  61. }
  62. else
  63. {
  64. int pixelBaseOOffs = lineBaseOOffs + baseX;
  65. for (int tY = 0; tY < copyHeight; tY++)
  66. {
  67. tileAsUint.Slice(tY * 4, copyWidth).CopyTo(outputAsUint.Slice(pixelBaseOOffs + width * tY, copyWidth));
  68. }
  69. }
  70. data = data.Slice(8);
  71. }
  72. }
  73. imageBaseOOffs += width * height;
  74. }
  75. }
  76. width = Math.Max(1, width >> 1);
  77. height = Math.Max(1, height >> 1);
  78. depth = Math.Max(1, depth >> 1);
  79. }
  80. return output;
  81. }
  82. public static byte[] DecodeBC2(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  83. {
  84. int size = 0;
  85. for (int l = 0; l < levels; l++)
  86. {
  87. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 4;
  88. }
  89. byte[] output = new byte[size];
  90. Span<byte> tile = stackalloc byte[BlockWidth * BlockHeight * 4];
  91. Span<uint> tileAsUint = MemoryMarshal.Cast<byte, uint>(tile);
  92. Span<uint> outputAsUint = MemoryMarshal.Cast<byte, uint>(output);
  93. Span<Vector128<byte>> tileAsVector128 = MemoryMarshal.Cast<byte, Vector128<byte>>(tile);
  94. Span<Vector128<byte>> outputLine0 = default;
  95. Span<Vector128<byte>> outputLine1 = default;
  96. Span<Vector128<byte>> outputLine2 = default;
  97. Span<Vector128<byte>> outputLine3 = default;
  98. int imageBaseOOffs = 0;
  99. for (int l = 0; l < levels; l++)
  100. {
  101. int w = BitUtils.DivRoundUp(width, BlockWidth);
  102. int h = BitUtils.DivRoundUp(height, BlockHeight);
  103. for (int l2 = 0; l2 < layers; l2++)
  104. {
  105. for (int z = 0; z < depth; z++)
  106. {
  107. for (int y = 0; y < h; y++)
  108. {
  109. int baseY = y * BlockHeight;
  110. int copyHeight = Math.Min(BlockHeight, height - baseY);
  111. int lineBaseOOffs = imageBaseOOffs + baseY * width;
  112. if (copyHeight == 4)
  113. {
  114. outputLine0 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs));
  115. outputLine1 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width));
  116. outputLine2 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 2));
  117. outputLine3 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 3));
  118. }
  119. for (int x = 0; x < w; x++)
  120. {
  121. int baseX = x * BlockWidth;
  122. int copyWidth = Math.Min(BlockWidth, width - baseX);
  123. BC23DecodeTileRgb(tile, data.Slice(8));
  124. ulong block = BinaryPrimitives.ReadUInt64LittleEndian(data);
  125. for (int i = 3; i < BlockWidth * BlockHeight * 4; i += 4, block >>= 4)
  126. {
  127. tile[i] = (byte)((block & 0xf) | (block << 4));
  128. }
  129. if ((copyWidth | copyHeight) == 4)
  130. {
  131. outputLine0[x] = tileAsVector128[0];
  132. outputLine1[x] = tileAsVector128[1];
  133. outputLine2[x] = tileAsVector128[2];
  134. outputLine3[x] = tileAsVector128[3];
  135. }
  136. else
  137. {
  138. int pixelBaseOOffs = lineBaseOOffs + baseX;
  139. for (int tY = 0; tY < copyHeight; tY++)
  140. {
  141. tileAsUint.Slice(tY * 4, copyWidth).CopyTo(outputAsUint.Slice(pixelBaseOOffs + width * tY, copyWidth));
  142. }
  143. }
  144. data = data.Slice(16);
  145. }
  146. }
  147. imageBaseOOffs += width * height;
  148. }
  149. }
  150. width = Math.Max(1, width >> 1);
  151. height = Math.Max(1, height >> 1);
  152. depth = Math.Max(1, depth >> 1);
  153. }
  154. return output;
  155. }
  156. public static byte[] DecodeBC3(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  157. {
  158. int size = 0;
  159. for (int l = 0; l < levels; l++)
  160. {
  161. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 4;
  162. }
  163. byte[] output = new byte[size];
  164. Span<byte> tile = stackalloc byte[BlockWidth * BlockHeight * 4];
  165. Span<byte> rPal = stackalloc byte[8];
  166. Span<uint> tileAsUint = MemoryMarshal.Cast<byte, uint>(tile);
  167. Span<uint> outputAsUint = MemoryMarshal.Cast<byte, uint>(output);
  168. Span<Vector128<byte>> tileAsVector128 = MemoryMarshal.Cast<byte, Vector128<byte>>(tile);
  169. Span<Vector128<byte>> outputLine0 = default;
  170. Span<Vector128<byte>> outputLine1 = default;
  171. Span<Vector128<byte>> outputLine2 = default;
  172. Span<Vector128<byte>> outputLine3 = default;
  173. int imageBaseOOffs = 0;
  174. for (int l = 0; l < levels; l++)
  175. {
  176. int w = BitUtils.DivRoundUp(width, BlockWidth);
  177. int h = BitUtils.DivRoundUp(height, BlockHeight);
  178. for (int l2 = 0; l2 < layers; l2++)
  179. {
  180. for (int z = 0; z < depth; z++)
  181. {
  182. for (int y = 0; y < h; y++)
  183. {
  184. int baseY = y * BlockHeight;
  185. int copyHeight = Math.Min(BlockHeight, height - baseY);
  186. int lineBaseOOffs = imageBaseOOffs + baseY * width;
  187. if (copyHeight == 4)
  188. {
  189. outputLine0 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs));
  190. outputLine1 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width));
  191. outputLine2 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 2));
  192. outputLine3 = MemoryMarshal.Cast<uint, Vector128<byte>>(outputAsUint.Slice(lineBaseOOffs + width * 3));
  193. }
  194. for (int x = 0; x < w; x++)
  195. {
  196. int baseX = x * BlockWidth;
  197. int copyWidth = Math.Min(BlockWidth, width - baseX);
  198. BC23DecodeTileRgb(tile, data.Slice(8));
  199. ulong block = BinaryPrimitives.ReadUInt64LittleEndian(data);
  200. rPal[0] = (byte)block;
  201. rPal[1] = (byte)(block >> 8);
  202. BCnLerpAlphaUnorm(rPal);
  203. BCnDecodeTileAlphaRgba(tile, rPal, block >> 16);
  204. if ((copyWidth | copyHeight) == 4)
  205. {
  206. outputLine0[x] = tileAsVector128[0];
  207. outputLine1[x] = tileAsVector128[1];
  208. outputLine2[x] = tileAsVector128[2];
  209. outputLine3[x] = tileAsVector128[3];
  210. }
  211. else
  212. {
  213. int pixelBaseOOffs = lineBaseOOffs + baseX;
  214. for (int tY = 0; tY < copyHeight; tY++)
  215. {
  216. tileAsUint.Slice(tY * 4, copyWidth).CopyTo(outputAsUint.Slice(pixelBaseOOffs + width * tY, copyWidth));
  217. }
  218. }
  219. data = data.Slice(16);
  220. }
  221. }
  222. imageBaseOOffs += width * height;
  223. }
  224. }
  225. width = Math.Max(1, width >> 1);
  226. height = Math.Max(1, height >> 1);
  227. depth = Math.Max(1, depth >> 1);
  228. }
  229. return output;
  230. }
  231. public static byte[] DecodeBC4(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
  232. {
  233. int size = 0;
  234. for (int l = 0; l < levels; l++)
  235. {
  236. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers;
  237. }
  238. byte[] output = new byte[size];
  239. Span<byte> outputSpan = new Span<byte>(output);
  240. ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
  241. Span<byte> tile = stackalloc byte[BlockWidth * BlockHeight];
  242. Span<byte> rPal = stackalloc byte[8];
  243. Span<uint> tileAsUint = MemoryMarshal.Cast<byte, uint>(tile);
  244. Span<uint> outputLine0 = default;
  245. Span<uint> outputLine1 = default;
  246. Span<uint> outputLine2 = default;
  247. Span<uint> outputLine3 = default;
  248. int imageBaseOOffs = 0;
  249. for (int l = 0; l < levels; l++)
  250. {
  251. int w = BitUtils.DivRoundUp(width, BlockWidth);
  252. int h = BitUtils.DivRoundUp(height, BlockHeight);
  253. for (int l2 = 0; l2 < layers; l2++)
  254. {
  255. for (int z = 0; z < depth; z++)
  256. {
  257. for (int y = 0; y < h; y++)
  258. {
  259. int baseY = y * BlockHeight;
  260. int copyHeight = Math.Min(BlockHeight, height - baseY);
  261. int lineBaseOOffs = imageBaseOOffs + baseY * width;
  262. if (copyHeight == 4)
  263. {
  264. outputLine0 = MemoryMarshal.Cast<byte, uint>(outputSpan.Slice(lineBaseOOffs));
  265. outputLine1 = MemoryMarshal.Cast<byte, uint>(outputSpan.Slice(lineBaseOOffs + width));
  266. outputLine2 = MemoryMarshal.Cast<byte, uint>(outputSpan.Slice(lineBaseOOffs + width * 2));
  267. outputLine3 = MemoryMarshal.Cast<byte, uint>(outputSpan.Slice(lineBaseOOffs + width * 3));
  268. }
  269. for (int x = 0; x < w; x++)
  270. {
  271. int baseX = x * BlockWidth;
  272. int copyWidth = Math.Min(BlockWidth, width - baseX);
  273. ulong block = data64[0];
  274. rPal[0] = (byte)block;
  275. rPal[1] = (byte)(block >> 8);
  276. if (signed)
  277. {
  278. BCnLerpAlphaSnorm(rPal);
  279. }
  280. else
  281. {
  282. BCnLerpAlphaUnorm(rPal);
  283. }
  284. BCnDecodeTileAlpha(tile, rPal, block >> 16);
  285. if ((copyWidth | copyHeight) == 4)
  286. {
  287. outputLine0[x] = tileAsUint[0];
  288. outputLine1[x] = tileAsUint[1];
  289. outputLine2[x] = tileAsUint[2];
  290. outputLine3[x] = tileAsUint[3];
  291. }
  292. else
  293. {
  294. int pixelBaseOOffs = lineBaseOOffs + baseX;
  295. for (int tY = 0; tY < copyHeight; tY++)
  296. {
  297. tile.Slice(tY * 4, copyWidth).CopyTo(outputSpan.Slice(pixelBaseOOffs + width * tY, copyWidth));
  298. }
  299. }
  300. data64 = data64.Slice(1);
  301. }
  302. }
  303. imageBaseOOffs += width * height;
  304. }
  305. }
  306. width = Math.Max(1, width >> 1);
  307. height = Math.Max(1, height >> 1);
  308. depth = Math.Max(1, depth >> 1);
  309. }
  310. return output;
  311. }
  312. public static byte[] DecodeBC5(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
  313. {
  314. int size = 0;
  315. for (int l = 0; l < levels; l++)
  316. {
  317. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 2;
  318. }
  319. byte[] output = new byte[size];
  320. ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
  321. Span<byte> rTile = stackalloc byte[BlockWidth * BlockHeight * 2];
  322. Span<byte> gTile = stackalloc byte[BlockWidth * BlockHeight * 2];
  323. Span<byte> rPal = stackalloc byte[8];
  324. Span<byte> gPal = stackalloc byte[8];
  325. Span<ushort> outputAsUshort = MemoryMarshal.Cast<byte, ushort>(output);
  326. Span<uint> rTileAsUint = MemoryMarshal.Cast<byte, uint>(rTile);
  327. Span<uint> gTileAsUint = MemoryMarshal.Cast<byte, uint>(gTile);
  328. Span<ulong> outputLine0 = default;
  329. Span<ulong> outputLine1 = default;
  330. Span<ulong> outputLine2 = default;
  331. Span<ulong> outputLine3 = default;
  332. int imageBaseOOffs = 0;
  333. for (int l = 0; l < levels; l++)
  334. {
  335. int w = BitUtils.DivRoundUp(width, BlockWidth);
  336. int h = BitUtils.DivRoundUp(height, BlockHeight);
  337. for (int l2 = 0; l2 < layers; l2++)
  338. {
  339. for (int z = 0; z < depth; z++)
  340. {
  341. for (int y = 0; y < h; y++)
  342. {
  343. int baseY = y * BlockHeight;
  344. int copyHeight = Math.Min(BlockHeight, height - baseY);
  345. int lineBaseOOffs = imageBaseOOffs + baseY * width;
  346. if (copyHeight == 4)
  347. {
  348. outputLine0 = MemoryMarshal.Cast<ushort, ulong>(outputAsUshort.Slice(lineBaseOOffs));
  349. outputLine1 = MemoryMarshal.Cast<ushort, ulong>(outputAsUshort.Slice(lineBaseOOffs + width));
  350. outputLine2 = MemoryMarshal.Cast<ushort, ulong>(outputAsUshort.Slice(lineBaseOOffs + width * 2));
  351. outputLine3 = MemoryMarshal.Cast<ushort, ulong>(outputAsUshort.Slice(lineBaseOOffs + width * 3));
  352. }
  353. for (int x = 0; x < w; x++)
  354. {
  355. int baseX = x * BlockWidth;
  356. int copyWidth = Math.Min(BlockWidth, width - baseX);
  357. ulong blockL = data64[0];
  358. ulong blockH = data64[1];
  359. rPal[0] = (byte)blockL;
  360. rPal[1] = (byte)(blockL >> 8);
  361. gPal[0] = (byte)blockH;
  362. gPal[1] = (byte)(blockH >> 8);
  363. if (signed)
  364. {
  365. BCnLerpAlphaSnorm(rPal);
  366. BCnLerpAlphaSnorm(gPal);
  367. }
  368. else
  369. {
  370. BCnLerpAlphaUnorm(rPal);
  371. BCnLerpAlphaUnorm(gPal);
  372. }
  373. BCnDecodeTileAlpha(rTile, rPal, blockL >> 16);
  374. BCnDecodeTileAlpha(gTile, gPal, blockH >> 16);
  375. if ((copyWidth | copyHeight) == 4)
  376. {
  377. outputLine0[x] = InterleaveBytes(rTileAsUint[0], gTileAsUint[0]);
  378. outputLine1[x] = InterleaveBytes(rTileAsUint[1], gTileAsUint[1]);
  379. outputLine2[x] = InterleaveBytes(rTileAsUint[2], gTileAsUint[2]);
  380. outputLine3[x] = InterleaveBytes(rTileAsUint[3], gTileAsUint[3]);
  381. }
  382. else
  383. {
  384. int pixelBaseOOffs = lineBaseOOffs + baseX;
  385. for (int tY = 0; tY < copyHeight; tY++)
  386. {
  387. int line = pixelBaseOOffs + width * tY;
  388. for (int tX = 0; tX < copyWidth; tX++)
  389. {
  390. int texel = tY * BlockWidth + tX;
  391. outputAsUshort[line + tX] = (ushort)(rTile[texel] | (gTile[texel] << 8));
  392. }
  393. }
  394. }
  395. data64 = data64.Slice(2);
  396. }
  397. }
  398. imageBaseOOffs += width * height;
  399. }
  400. }
  401. width = Math.Max(1, width >> 1);
  402. height = Math.Max(1, height >> 1);
  403. depth = Math.Max(1, depth >> 1);
  404. }
  405. return output;
  406. }
  407. private static ulong InterleaveBytes(uint left, uint right)
  408. {
  409. return InterleaveBytesWithZeros(left) | (InterleaveBytesWithZeros(right) << 8);
  410. }
  411. private static ulong InterleaveBytesWithZeros(uint value)
  412. {
  413. ulong output = value;
  414. output = (output ^ (output << 16)) & 0xffff0000ffffUL;
  415. output = (output ^ (output << 8)) & 0xff00ff00ff00ffUL;
  416. return output;
  417. }
  418. private static void BCnLerpAlphaUnorm(Span<byte> alpha)
  419. {
  420. byte a0 = alpha[0];
  421. byte a1 = alpha[1];
  422. if (a0 > a1)
  423. {
  424. alpha[2] = (byte)((6 * a0 + 1 * a1) / 7);
  425. alpha[3] = (byte)((5 * a0 + 2 * a1) / 7);
  426. alpha[4] = (byte)((4 * a0 + 3 * a1) / 7);
  427. alpha[5] = (byte)((3 * a0 + 4 * a1) / 7);
  428. alpha[6] = (byte)((2 * a0 + 5 * a1) / 7);
  429. alpha[7] = (byte)((1 * a0 + 6 * a1) / 7);
  430. }
  431. else
  432. {
  433. alpha[2] = (byte)((4 * a0 + 1 * a1) / 5);
  434. alpha[3] = (byte)((3 * a0 + 2 * a1) / 5);
  435. alpha[4] = (byte)((2 * a0 + 3 * a1) / 5);
  436. alpha[5] = (byte)((1 * a0 + 4 * a1) / 5);
  437. alpha[6] = 0;
  438. alpha[7] = 0xff;
  439. }
  440. }
  441. private static void BCnLerpAlphaSnorm(Span<byte> alpha)
  442. {
  443. sbyte a0 = (sbyte)alpha[0];
  444. sbyte a1 = (sbyte)alpha[1];
  445. if (a0 > a1)
  446. {
  447. alpha[2] = (byte)((6 * a0 + 1 * a1) / 7);
  448. alpha[3] = (byte)((5 * a0 + 2 * a1) / 7);
  449. alpha[4] = (byte)((4 * a0 + 3 * a1) / 7);
  450. alpha[5] = (byte)((3 * a0 + 4 * a1) / 7);
  451. alpha[6] = (byte)((2 * a0 + 5 * a1) / 7);
  452. alpha[7] = (byte)((1 * a0 + 6 * a1) / 7);
  453. }
  454. else
  455. {
  456. alpha[2] = (byte)((4 * a0 + 1 * a1) / 5);
  457. alpha[3] = (byte)((3 * a0 + 2 * a1) / 5);
  458. alpha[4] = (byte)((2 * a0 + 3 * a1) / 5);
  459. alpha[5] = (byte)((1 * a0 + 4 * a1) / 5);
  460. alpha[6] = 0x80;
  461. alpha[7] = 0x7f;
  462. }
  463. }
  464. private unsafe static void BCnDecodeTileAlpha(Span<byte> output, Span<byte> rPal, ulong rI)
  465. {
  466. if (Avx2.IsSupported)
  467. {
  468. Span<Vector128<byte>> outputAsVector128 = MemoryMarshal.Cast<byte, Vector128<byte>>(output);
  469. Vector128<uint> shifts = Vector128.Create(0u, 3u, 6u, 9u);
  470. Vector128<uint> masks = Vector128.Create(7u);
  471. Vector128<byte> vClut;
  472. fixed (byte* pRPal = rPal)
  473. {
  474. vClut = Sse2.LoadScalarVector128((ulong*)pRPal).AsByte();
  475. }
  476. Vector128<uint> indices0 = Vector128.Create((uint)rI);
  477. Vector128<uint> indices1 = Vector128.Create((uint)(rI >> 24));
  478. Vector128<uint> indices00 = Avx2.ShiftRightLogicalVariable(indices0, shifts);
  479. Vector128<uint> indices10 = Avx2.ShiftRightLogicalVariable(indices1, shifts);
  480. Vector128<uint> indices01 = Sse2.ShiftRightLogical(indices00, 12);
  481. Vector128<uint> indices11 = Sse2.ShiftRightLogical(indices10, 12);
  482. indices00 = Sse2.And(indices00, masks);
  483. indices10 = Sse2.And(indices10, masks);
  484. indices01 = Sse2.And(indices01, masks);
  485. indices11 = Sse2.And(indices11, masks);
  486. Vector128<ushort> indicesW0 = Sse41.PackUnsignedSaturate(indices00.AsInt32(), indices01.AsInt32());
  487. Vector128<ushort> indicesW1 = Sse41.PackUnsignedSaturate(indices10.AsInt32(), indices11.AsInt32());
  488. Vector128<byte> indices = Sse2.PackUnsignedSaturate(indicesW0.AsInt16(), indicesW1.AsInt16());
  489. outputAsVector128[0] = Ssse3.Shuffle(vClut, indices);
  490. }
  491. else
  492. {
  493. for (int i = 0; i < BlockWidth * BlockHeight; i++, rI >>= 3)
  494. {
  495. output[i] = rPal[(int)(rI & 7)];
  496. }
  497. }
  498. }
  499. private unsafe static void BCnDecodeTileAlphaRgba(Span<byte> output, Span<byte> rPal, ulong rI)
  500. {
  501. if (Avx2.IsSupported)
  502. {
  503. Span<Vector256<uint>> outputAsVector256 = MemoryMarshal.Cast<byte, Vector256<uint>>(output);
  504. Vector256<uint> shifts = Vector256.Create(0u, 3u, 6u, 9u, 12u, 15u, 18u, 21u);
  505. Vector128<uint> vClut128;
  506. fixed (byte* pRPal = rPal)
  507. {
  508. vClut128 = Sse2.LoadScalarVector128((ulong*)pRPal).AsUInt32();
  509. }
  510. Vector256<uint> vClut = Avx2.ConvertToVector256Int32(vClut128.AsByte()).AsUInt32();
  511. vClut = Avx2.ShiftLeftLogical(vClut, 24);
  512. Vector256<uint> indices0 = Vector256.Create((uint)rI);
  513. Vector256<uint> indices1 = Vector256.Create((uint)(rI >> 24));
  514. indices0 = Avx2.ShiftRightLogicalVariable(indices0, shifts);
  515. indices1 = Avx2.ShiftRightLogicalVariable(indices1, shifts);
  516. outputAsVector256[0] = Avx2.Or(outputAsVector256[0], Avx2.PermuteVar8x32(vClut, indices0));
  517. outputAsVector256[1] = Avx2.Or(outputAsVector256[1], Avx2.PermuteVar8x32(vClut, indices1));
  518. }
  519. else
  520. {
  521. for (int i = 3; i < BlockWidth * BlockHeight * 4; i += 4, rI >>= 3)
  522. {
  523. output[i] = rPal[(int)(rI & 7)];
  524. }
  525. }
  526. }
  527. private unsafe static void BC1DecodeTileRgb(Span<byte> output, ReadOnlySpan<byte> input)
  528. {
  529. Span<uint> clut = stackalloc uint[4];
  530. uint c0c1 = BinaryPrimitives.ReadUInt32LittleEndian(input);
  531. uint c0 = (ushort)c0c1;
  532. uint c1 = (ushort)(c0c1 >> 16);
  533. clut[0] = ConvertRgb565ToRgb888(c0) | 0xff000000;
  534. clut[1] = ConvertRgb565ToRgb888(c1) | 0xff000000;
  535. clut[2] = BC1LerpRgb2(clut[0], clut[1], c0, c1);
  536. clut[3] = BC1LerpRgb3(clut[0], clut[1], c0, c1);
  537. BCnDecodeTileRgb(clut, output, input);
  538. }
  539. private unsafe static void BC23DecodeTileRgb(Span<byte> output, ReadOnlySpan<byte> input)
  540. {
  541. Span<uint> clut = stackalloc uint[4];
  542. uint c0c1 = BinaryPrimitives.ReadUInt32LittleEndian(input);
  543. uint c0 = (ushort)c0c1;
  544. uint c1 = (ushort)(c0c1 >> 16);
  545. clut[0] = ConvertRgb565ToRgb888(c0);
  546. clut[1] = ConvertRgb565ToRgb888(c1);
  547. clut[2] = BC23LerpRgb2(clut[0], clut[1]);
  548. clut[3] = BC23LerpRgb3(clut[0], clut[1]);
  549. BCnDecodeTileRgb(clut, output, input);
  550. }
  551. private unsafe static void BCnDecodeTileRgb(Span<uint> clut, Span<byte> output, ReadOnlySpan<byte> input)
  552. {
  553. if (Avx2.IsSupported)
  554. {
  555. Span<Vector256<uint>> outputAsVector256 = MemoryMarshal.Cast<byte, Vector256<uint>>(output);
  556. Vector256<uint> shifts0 = Vector256.Create(0u, 2u, 4u, 6u, 8u, 10u, 12u, 14u);
  557. Vector256<uint> shifts1 = Vector256.Create(16u, 18u, 20u, 22u, 24u, 26u, 28u, 30u);
  558. Vector256<uint> masks = Vector256.Create(3u);
  559. Vector256<uint> vClut;
  560. fixed (uint* pClut = &clut[0])
  561. {
  562. vClut = Sse2.LoadVector128(pClut).ToVector256Unsafe();
  563. }
  564. Vector256<uint> indices0;
  565. fixed (byte* pInput = input)
  566. {
  567. indices0 = Avx2.BroadcastScalarToVector256((uint*)(pInput + 4));
  568. }
  569. Vector256<uint> indices1 = indices0;
  570. indices0 = Avx2.ShiftRightLogicalVariable(indices0, shifts0);
  571. indices1 = Avx2.ShiftRightLogicalVariable(indices1, shifts1);
  572. indices0 = Avx2.And(indices0, masks);
  573. indices1 = Avx2.And(indices1, masks);
  574. outputAsVector256[0] = Avx2.PermuteVar8x32(vClut, indices0);
  575. outputAsVector256[1] = Avx2.PermuteVar8x32(vClut, indices1);
  576. }
  577. else
  578. {
  579. Span<uint> outputAsUint = MemoryMarshal.Cast<byte, uint>(output);
  580. uint indices = BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(4));
  581. for (int i = 0; i < BlockWidth * BlockHeight; i++, indices >>= 2)
  582. {
  583. outputAsUint[i] = clut[(int)(indices & 3)];
  584. }
  585. }
  586. }
  587. private static uint BC1LerpRgb2(uint color0, uint color1, uint c0, uint c1)
  588. {
  589. if (c0 > c1)
  590. {
  591. return BC23LerpRgb2(color0, color1) | 0xff000000;
  592. }
  593. uint carry = color0 & color1;
  594. uint addHalve = ((color0 ^ color1) >> 1) & 0x7f7f7f;
  595. return (addHalve + carry) | 0xff000000;
  596. }
  597. private static uint BC23LerpRgb2(uint color0, uint color1)
  598. {
  599. uint r0 = (byte)color0;
  600. uint g0 = color0 & 0xff00;
  601. uint b0 = color0 & 0xff0000;
  602. uint r1 = (byte)color1;
  603. uint g1 = color1 & 0xff00;
  604. uint b1 = color1 & 0xff0000;
  605. uint mixR = (2 * r0 + r1) / 3;
  606. uint mixG = (2 * g0 + g1) / 3;
  607. uint mixB = (2 * b0 + b1) / 3;
  608. return mixR | (mixG & 0xff00) | (mixB & 0xff0000);
  609. }
  610. private static uint BC1LerpRgb3(uint color0, uint color1, uint c0, uint c1)
  611. {
  612. if (c0 > c1)
  613. {
  614. return BC23LerpRgb3(color0, color1) | 0xff000000;
  615. }
  616. return 0;
  617. }
  618. private static uint BC23LerpRgb3(uint color0, uint color1)
  619. {
  620. uint r0 = (byte)color0;
  621. uint g0 = color0 & 0xff00;
  622. uint b0 = color0 & 0xff0000;
  623. uint r1 = (byte)color1;
  624. uint g1 = color1 & 0xff00;
  625. uint b1 = color1 & 0xff0000;
  626. uint mixR = (2 * r1 + r0) / 3;
  627. uint mixG = (2 * g1 + g0) / 3;
  628. uint mixB = (2 * b1 + b0) / 3;
  629. return mixR | (mixG & 0xff00) | (mixB & 0xff0000);
  630. }
  631. private static uint ConvertRgb565ToRgb888(uint value)
  632. {
  633. uint b = (value & 0x1f) << 19;
  634. uint g = (value << 5) & 0xfc00;
  635. uint r = (value >> 8) & 0xf8;
  636. b |= b >> 5;
  637. g |= g >> 6;
  638. r |= r >> 5;
  639. return r | (g & 0xff00) | (b & 0xff0000);
  640. }
  641. }
  642. }