SurfaceWriter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Texture;
  3. using Ryujinx.Graphics.Vic.Types;
  4. using System;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.Arm;
  7. using System.Runtime.Intrinsics.X86;
  8. using static Ryujinx.Graphics.Vic.Image.SurfaceCommon;
  9. namespace Ryujinx.Graphics.Vic.Image
  10. {
  11. class SurfaceWriter
  12. {
  13. public static void Write(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  14. {
  15. switch (config.OutPixelFormat)
  16. {
  17. case PixelFormat.A8B8G8R8:
  18. case PixelFormat.X8B8G8R8:
  19. WriteA8B8G8R8(rm, input, ref config, ref offsets);
  20. break;
  21. case PixelFormat.A8R8G8B8:
  22. WriteA8R8G8B8(rm, input, ref config, ref offsets);
  23. break;
  24. case PixelFormat.Y8___V8U8_N420:
  25. WriteNv12(rm, input, ref config, ref offsets);
  26. break;
  27. default:
  28. Logger.Error?.Print(LogClass.Vic, $"Unsupported pixel format \"{config.OutPixelFormat}\".");
  29. break;
  30. }
  31. }
  32. private unsafe static void WriteA8B8G8R8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  33. {
  34. int width = input.Width;
  35. int height = input.Height;
  36. int stride = GetPitch(width, 4);
  37. int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst);
  38. if (Sse2.IsSupported)
  39. {
  40. int widthTrunc = width & ~7;
  41. int strideGap = stride - width * 4;
  42. fixed (Pixel* srcPtr = input.Data)
  43. {
  44. Pixel* ip = srcPtr;
  45. fixed (byte* dstPtr = dst)
  46. {
  47. byte* op = dstPtr;
  48. for (int y = 0; y < height; y++, ip += input.Width)
  49. {
  50. int x = 0;
  51. for (; x < widthTrunc; x += 8)
  52. {
  53. Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x));
  54. Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2));
  55. Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4));
  56. Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6));
  57. pixel12 = Sse2.ShiftRightLogical(pixel12, 2);
  58. pixel34 = Sse2.ShiftRightLogical(pixel34, 2);
  59. pixel56 = Sse2.ShiftRightLogical(pixel56, 2);
  60. pixel78 = Sse2.ShiftRightLogical(pixel78, 2);
  61. Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16());
  62. Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16());
  63. Sse2.Store(op + 0x00, pixel1234);
  64. Sse2.Store(op + 0x10, pixel5678);
  65. op += 0x20;
  66. }
  67. for (; x < width; x++)
  68. {
  69. Pixel* px = ip + (uint)x;
  70. *(op + 0) = Downsample(px->R);
  71. *(op + 1) = Downsample(px->G);
  72. *(op + 2) = Downsample(px->B);
  73. *(op + 3) = Downsample(px->A);
  74. op += 4;
  75. }
  76. op += strideGap;
  77. }
  78. }
  79. }
  80. }
  81. else if (AdvSimd.IsSupported)
  82. {
  83. int widthTrunc = width & ~7;
  84. int strideGap = stride - width * 4;
  85. fixed (Pixel* srcPtr = input.Data)
  86. {
  87. Pixel* ip = srcPtr;
  88. fixed (byte* dstPtr = dst)
  89. {
  90. byte* op = dstPtr;
  91. for (int y = 0; y < height; y++, ip += input.Width)
  92. {
  93. int x = 0;
  94. for (; x < widthTrunc; x += 8)
  95. {
  96. Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x));
  97. Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2));
  98. Vector128<ushort> pixel56 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 4));
  99. Vector128<ushort> pixel78 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 6));
  100. pixel12 = AdvSimd.ShiftRightLogical(pixel12, 2);
  101. pixel34 = AdvSimd.ShiftRightLogical(pixel34, 2);
  102. pixel56 = AdvSimd.ShiftRightLogical(pixel56, 2);
  103. pixel78 = AdvSimd.ShiftRightLogical(pixel78, 2);
  104. Vector64<byte> lower12 = AdvSimd.ExtractNarrowingLower(pixel12.AsUInt16());
  105. Vector64<byte> lower56 = AdvSimd.ExtractNarrowingLower(pixel56.AsUInt16());
  106. Vector128<byte> pixel1234 = AdvSimd.ExtractNarrowingUpper(lower12, pixel34.AsUInt16());
  107. Vector128<byte> pixel5678 = AdvSimd.ExtractNarrowingUpper(lower56, pixel78.AsUInt16());
  108. AdvSimd.Store(op + 0x00, pixel1234);
  109. AdvSimd.Store(op + 0x10, pixel5678);
  110. op += 0x20;
  111. }
  112. for (; x < width; x++)
  113. {
  114. Pixel* px = ip + (uint)x;
  115. *(op + 0) = Downsample(px->R);
  116. *(op + 1) = Downsample(px->G);
  117. *(op + 2) = Downsample(px->B);
  118. *(op + 3) = Downsample(px->A);
  119. op += 4;
  120. }
  121. op += strideGap;
  122. }
  123. }
  124. }
  125. }
  126. else
  127. {
  128. for (int y = 0; y < height; y++)
  129. {
  130. int baseOffs = y * stride;
  131. for (int x = 0; x < width; x++)
  132. {
  133. int offs = baseOffs + x * 4;
  134. dst[offs + 0] = Downsample(input.GetR(x, y));
  135. dst[offs + 1] = Downsample(input.GetG(x, y));
  136. dst[offs + 2] = Downsample(input.GetB(x, y));
  137. dst[offs + 3] = Downsample(input.GetA(x, y));
  138. }
  139. }
  140. }
  141. bool outLinear = config.OutBlkKind == 0;
  142. int gobBlocksInY = 1 << config.OutBlkHeight;
  143. WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY);
  144. rm.BufferPool.Return(dstIndex);
  145. }
  146. private unsafe static void WriteA8R8G8B8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  147. {
  148. int width = input.Width;
  149. int height = input.Height;
  150. int stride = GetPitch(width, 4);
  151. int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst);
  152. if (Ssse3.IsSupported)
  153. {
  154. Vector128<byte> shuffleMask = Vector128.Create(
  155. (byte)2, (byte)1, (byte)0, (byte)3,
  156. (byte)6, (byte)5, (byte)4, (byte)7,
  157. (byte)10, (byte)9, (byte)8, (byte)11,
  158. (byte)14, (byte)13, (byte)12, (byte)15);
  159. int widthTrunc = width & ~7;
  160. int strideGap = stride - width * 4;
  161. fixed (Pixel* srcPtr = input.Data)
  162. {
  163. Pixel* ip = srcPtr;
  164. fixed (byte* dstPtr = dst)
  165. {
  166. byte* op = dstPtr;
  167. for (int y = 0; y < height; y++, ip += input.Width)
  168. {
  169. int x = 0;
  170. for (; x < widthTrunc; x += 8)
  171. {
  172. Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x));
  173. Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2));
  174. Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4));
  175. Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6));
  176. pixel12 = Sse2.ShiftRightLogical(pixel12, 2);
  177. pixel34 = Sse2.ShiftRightLogical(pixel34, 2);
  178. pixel56 = Sse2.ShiftRightLogical(pixel56, 2);
  179. pixel78 = Sse2.ShiftRightLogical(pixel78, 2);
  180. Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16());
  181. Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16());
  182. pixel1234 = Ssse3.Shuffle(pixel1234, shuffleMask);
  183. pixel5678 = Ssse3.Shuffle(pixel5678, shuffleMask);
  184. Sse2.Store(op + 0x00, pixel1234);
  185. Sse2.Store(op + 0x10, pixel5678);
  186. op += 0x20;
  187. }
  188. for (; x < width; x++)
  189. {
  190. Pixel* px = ip + (uint)x;
  191. *(op + 0) = Downsample(px->B);
  192. *(op + 1) = Downsample(px->G);
  193. *(op + 2) = Downsample(px->R);
  194. *(op + 3) = Downsample(px->A);
  195. op += 4;
  196. }
  197. op += strideGap;
  198. }
  199. }
  200. }
  201. }
  202. else
  203. {
  204. for (int y = 0; y < height; y++)
  205. {
  206. int baseOffs = y * stride;
  207. for (int x = 0; x < width; x++)
  208. {
  209. int offs = baseOffs + x * 4;
  210. dst[offs + 0] = Downsample(input.GetB(x, y));
  211. dst[offs + 1] = Downsample(input.GetG(x, y));
  212. dst[offs + 2] = Downsample(input.GetR(x, y));
  213. dst[offs + 3] = Downsample(input.GetA(x, y));
  214. }
  215. }
  216. }
  217. bool outLinear = config.OutBlkKind == 0;
  218. int gobBlocksInY = 1 << config.OutBlkHeight;
  219. WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY);
  220. rm.BufferPool.Return(dstIndex);
  221. }
  222. private unsafe static void WriteNv12(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  223. {
  224. int gobBlocksInY = 1 << config.OutBlkHeight;
  225. bool outLinear = config.OutBlkKind == 0;
  226. int width = Math.Min(config.OutLumaWidth + 1, input.Width);
  227. int height = Math.Min(config.OutLumaHeight + 1, input.Height);
  228. int yStride = GetPitch(config.OutLumaWidth + 1, 1);
  229. int dstYIndex = rm.BufferPool.Rent((config.OutLumaHeight + 1) * yStride, out Span<byte> dstY);
  230. if (Sse41.IsSupported)
  231. {
  232. Vector128<ushort> mask = Vector128.Create(0xffffUL).AsUInt16();
  233. int widthTrunc = width & ~0xf;
  234. int strideGap = yStride - width;
  235. fixed (Pixel* srcPtr = input.Data)
  236. {
  237. Pixel* ip = srcPtr;
  238. fixed (byte* dstPtr = dstY)
  239. {
  240. byte* op = dstPtr;
  241. for (int y = 0; y < height; y++, ip += input.Width)
  242. {
  243. int x = 0;
  244. for (; x < widthTrunc; x += 16)
  245. {
  246. byte* baseOffset = (byte*)(ip + (ulong)(uint)x);
  247. Vector128<ushort> pixelp1 = Sse2.LoadVector128((ushort*)baseOffset);
  248. Vector128<ushort> pixelp2 = Sse2.LoadVector128((ushort*)(baseOffset + 0x10));
  249. Vector128<ushort> pixelp3 = Sse2.LoadVector128((ushort*)(baseOffset + 0x20));
  250. Vector128<ushort> pixelp4 = Sse2.LoadVector128((ushort*)(baseOffset + 0x30));
  251. Vector128<ushort> pixelp5 = Sse2.LoadVector128((ushort*)(baseOffset + 0x40));
  252. Vector128<ushort> pixelp6 = Sse2.LoadVector128((ushort*)(baseOffset + 0x50));
  253. Vector128<ushort> pixelp7 = Sse2.LoadVector128((ushort*)(baseOffset + 0x60));
  254. Vector128<ushort> pixelp8 = Sse2.LoadVector128((ushort*)(baseOffset + 0x70));
  255. pixelp1 = Sse2.And(pixelp1, mask);
  256. pixelp2 = Sse2.And(pixelp2, mask);
  257. pixelp3 = Sse2.And(pixelp3, mask);
  258. pixelp4 = Sse2.And(pixelp4, mask);
  259. pixelp5 = Sse2.And(pixelp5, mask);
  260. pixelp6 = Sse2.And(pixelp6, mask);
  261. pixelp7 = Sse2.And(pixelp7, mask);
  262. pixelp8 = Sse2.And(pixelp8, mask);
  263. Vector128<ushort> pixelq1 = Sse41.PackUnsignedSaturate(pixelp1.AsInt32(), pixelp2.AsInt32());
  264. Vector128<ushort> pixelq2 = Sse41.PackUnsignedSaturate(pixelp3.AsInt32(), pixelp4.AsInt32());
  265. Vector128<ushort> pixelq3 = Sse41.PackUnsignedSaturate(pixelp5.AsInt32(), pixelp6.AsInt32());
  266. Vector128<ushort> pixelq4 = Sse41.PackUnsignedSaturate(pixelp7.AsInt32(), pixelp8.AsInt32());
  267. pixelq1 = Sse41.PackUnsignedSaturate(pixelq1.AsInt32(), pixelq2.AsInt32());
  268. pixelq2 = Sse41.PackUnsignedSaturate(pixelq3.AsInt32(), pixelq4.AsInt32());
  269. pixelq1 = Sse2.ShiftRightLogical(pixelq1, 2);
  270. pixelq2 = Sse2.ShiftRightLogical(pixelq2, 2);
  271. Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixelq1.AsInt16(), pixelq2.AsInt16());
  272. Sse2.Store(op, pixel);
  273. op += 0x10;
  274. }
  275. for (; x < width; x++)
  276. {
  277. Pixel* px = ip + (uint)x;
  278. *op++ = Downsample(px->R);
  279. }
  280. op += strideGap;
  281. }
  282. }
  283. }
  284. }
  285. else if (AdvSimd.IsSupported)
  286. {
  287. Vector128<ushort> mask = Vector128.Create(0xffffUL).AsUInt16();
  288. int widthTrunc = width & ~0xf;
  289. int strideGap = yStride - width;
  290. fixed (Pixel* srcPtr = input.Data)
  291. {
  292. Pixel* ip = srcPtr;
  293. fixed (byte* dstPtr = dstY)
  294. {
  295. byte* op = dstPtr;
  296. for (int y = 0; y < height; y++, ip += input.Width)
  297. {
  298. int x = 0;
  299. for (; x < widthTrunc; x += 16)
  300. {
  301. byte* baseOffset = (byte*)(ip + (ulong)(uint)x);
  302. Vector128<ushort> pixelp1 = AdvSimd.LoadVector128((ushort*)baseOffset);
  303. Vector128<ushort> pixelp2 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x10));
  304. Vector128<ushort> pixelp3 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x20));
  305. Vector128<ushort> pixelp4 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x30));
  306. Vector128<ushort> pixelp5 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x40));
  307. Vector128<ushort> pixelp6 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x50));
  308. Vector128<ushort> pixelp7 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x60));
  309. Vector128<ushort> pixelp8 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x70));
  310. pixelp1 = AdvSimd.And(pixelp1, mask);
  311. pixelp2 = AdvSimd.And(pixelp2, mask);
  312. pixelp3 = AdvSimd.And(pixelp3, mask);
  313. pixelp4 = AdvSimd.And(pixelp4, mask);
  314. pixelp5 = AdvSimd.And(pixelp5, mask);
  315. pixelp6 = AdvSimd.And(pixelp6, mask);
  316. pixelp7 = AdvSimd.And(pixelp7, mask);
  317. pixelp8 = AdvSimd.And(pixelp8, mask);
  318. Vector64<ushort> lowerp1 = AdvSimd.ExtractNarrowingLower(pixelp1.AsUInt32());
  319. Vector64<ushort> lowerp3 = AdvSimd.ExtractNarrowingLower(pixelp3.AsUInt32());
  320. Vector64<ushort> lowerp5 = AdvSimd.ExtractNarrowingLower(pixelp5.AsUInt32());
  321. Vector64<ushort> lowerp7 = AdvSimd.ExtractNarrowingLower(pixelp7.AsUInt32());
  322. Vector128<ushort> pixelq1 = AdvSimd.ExtractNarrowingUpper(lowerp1, pixelp2.AsUInt32());
  323. Vector128<ushort> pixelq2 = AdvSimd.ExtractNarrowingUpper(lowerp3, pixelp4.AsUInt32());
  324. Vector128<ushort> pixelq3 = AdvSimd.ExtractNarrowingUpper(lowerp5, pixelp6.AsUInt32());
  325. Vector128<ushort> pixelq4 = AdvSimd.ExtractNarrowingUpper(lowerp7, pixelp8.AsUInt32());
  326. Vector64<ushort> lowerq1 = AdvSimd.ExtractNarrowingLower(pixelq1.AsUInt32());
  327. Vector64<ushort> lowerq3 = AdvSimd.ExtractNarrowingLower(pixelq3.AsUInt32());
  328. pixelq1 = AdvSimd.ExtractNarrowingUpper(lowerq1, pixelq2.AsUInt32());
  329. pixelq2 = AdvSimd.ExtractNarrowingUpper(lowerq3, pixelq4.AsUInt32());
  330. pixelq1 = AdvSimd.ShiftRightLogical(pixelq1, 2);
  331. pixelq2 = AdvSimd.ShiftRightLogical(pixelq2, 2);
  332. Vector64<byte> pixelLower = AdvSimd.ExtractNarrowingLower(pixelq1.AsUInt16());
  333. Vector128<byte> pixel = AdvSimd.ExtractNarrowingUpper(pixelLower, pixelq2.AsUInt16());
  334. AdvSimd.Store(op, pixel);
  335. op += 0x10;
  336. }
  337. for (; x < width; x++)
  338. {
  339. Pixel* px = ip + (uint)x;
  340. *op++ = Downsample(px->R);
  341. }
  342. op += strideGap;
  343. }
  344. }
  345. }
  346. }
  347. else
  348. {
  349. for (int y = 0; y < height; y++)
  350. {
  351. for (int x = 0; x < width; x++)
  352. {
  353. dstY[y * yStride + x] = Downsample(input.GetR(x, y));
  354. }
  355. }
  356. }
  357. WriteBuffer(
  358. rm,
  359. dstY,
  360. offsets.LumaOffset,
  361. outLinear,
  362. config.OutLumaWidth + 1,
  363. config.OutLumaHeight + 1,
  364. 1,
  365. gobBlocksInY);
  366. rm.BufferPool.Return(dstYIndex);
  367. int uvWidth = Math.Min(config.OutChromaWidth + 1, (width + 1) >> 1);
  368. int uvHeight = Math.Min(config.OutChromaHeight + 1, (height + 1) >> 1);
  369. int uvStride = GetPitch(config.OutChromaWidth + 1, 2);
  370. int dstUvIndex = rm.BufferPool.Rent((config.OutChromaHeight + 1) * uvStride, out Span<byte> dstUv);
  371. if (Sse2.IsSupported)
  372. {
  373. int widthTrunc = uvWidth & ~7;
  374. int strideGap = uvStride - uvWidth * 2;
  375. fixed (Pixel* srcPtr = input.Data)
  376. {
  377. Pixel* ip = srcPtr;
  378. fixed (byte* dstPtr = dstUv)
  379. {
  380. byte* op = dstPtr;
  381. for (int y = 0; y < uvHeight; y++, ip += input.Width * 2)
  382. {
  383. int x = 0;
  384. for (; x < widthTrunc; x += 8)
  385. {
  386. byte* baseOffset = (byte*)ip + (ulong)(uint)x * 16;
  387. Vector128<uint> pixel1 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x02));
  388. Vector128<uint> pixel2 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x12));
  389. Vector128<uint> pixel3 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x22));
  390. Vector128<uint> pixel4 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x32));
  391. Vector128<uint> pixel5 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x42));
  392. Vector128<uint> pixel6 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x52));
  393. Vector128<uint> pixel7 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x62));
  394. Vector128<uint> pixel8 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x72));
  395. Vector128<uint> pixel12 = Sse2.UnpackLow(pixel1, pixel2);
  396. Vector128<uint> pixel34 = Sse2.UnpackLow(pixel3, pixel4);
  397. Vector128<uint> pixel56 = Sse2.UnpackLow(pixel5, pixel6);
  398. Vector128<uint> pixel78 = Sse2.UnpackLow(pixel7, pixel8);
  399. Vector128<ulong> pixel1234 = Sse2.UnpackLow(pixel12.AsUInt64(), pixel34.AsUInt64());
  400. Vector128<ulong> pixel5678 = Sse2.UnpackLow(pixel56.AsUInt64(), pixel78.AsUInt64());
  401. pixel1234 = Sse2.ShiftRightLogical(pixel1234, 2);
  402. pixel5678 = Sse2.ShiftRightLogical(pixel5678, 2);
  403. Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixel1234.AsInt16(), pixel5678.AsInt16());
  404. Sse2.Store(op, pixel);
  405. op += 0x10;
  406. }
  407. for (; x < uvWidth; x++)
  408. {
  409. Pixel* px = ip + (uint)(x << 1);
  410. *op++ = Downsample(px->G);
  411. *op++ = Downsample(px->B);
  412. }
  413. op += strideGap;
  414. }
  415. }
  416. }
  417. }
  418. else if (AdvSimd.Arm64.IsSupported)
  419. {
  420. int widthTrunc = uvWidth & ~7;
  421. int strideGap = uvStride - uvWidth * 2;
  422. fixed (Pixel* srcPtr = input.Data)
  423. {
  424. Pixel* ip = srcPtr;
  425. fixed (byte* dstPtr = dstUv)
  426. {
  427. byte* op = dstPtr;
  428. for (int y = 0; y < uvHeight; y++, ip += input.Width * 2)
  429. {
  430. int x = 0;
  431. for (; x < widthTrunc; x += 8)
  432. {
  433. byte* baseOffset = (byte*)ip + (ulong)(uint)x * 16;
  434. Vector128<uint> pixel1 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x02));
  435. Vector128<uint> pixel2 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x12));
  436. Vector128<uint> pixel3 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x22));
  437. Vector128<uint> pixel4 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x32));
  438. Vector128<uint> pixel5 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x42));
  439. Vector128<uint> pixel6 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x52));
  440. Vector128<uint> pixel7 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x62));
  441. Vector128<uint> pixel8 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x72));
  442. Vector128<uint> pixel12 = AdvSimd.Arm64.ZipLow(pixel1, pixel2);
  443. Vector128<uint> pixel34 = AdvSimd.Arm64.ZipLow(pixel3, pixel4);
  444. Vector128<uint> pixel56 = AdvSimd.Arm64.ZipLow(pixel5, pixel6);
  445. Vector128<uint> pixel78 = AdvSimd.Arm64.ZipLow(pixel7, pixel8);
  446. Vector128<ulong> pixel1234 = AdvSimd.Arm64.ZipLow(pixel12.AsUInt64(), pixel34.AsUInt64());
  447. Vector128<ulong> pixel5678 = AdvSimd.Arm64.ZipLow(pixel56.AsUInt64(), pixel78.AsUInt64());
  448. pixel1234 = AdvSimd.ShiftRightLogical(pixel1234, 2);
  449. pixel5678 = AdvSimd.ShiftRightLogical(pixel5678, 2);
  450. Vector64<byte> pixelLower = AdvSimd.ExtractNarrowingLower(pixel1234.AsUInt16());
  451. Vector128<byte> pixel = AdvSimd.ExtractNarrowingUpper(pixelLower, pixel5678.AsUInt16());
  452. AdvSimd.Store(op, pixel);
  453. op += 0x10;
  454. }
  455. for (; x < uvWidth; x++)
  456. {
  457. Pixel* px = ip + (uint)(x << 1);
  458. *op++ = Downsample(px->G);
  459. *op++ = Downsample(px->B);
  460. }
  461. op += strideGap;
  462. }
  463. }
  464. }
  465. }
  466. else
  467. {
  468. for (int y = 0; y < uvHeight; y++)
  469. {
  470. for (int x = 0; x < uvWidth; x++)
  471. {
  472. int xx = x << 1;
  473. int yy = y << 1;
  474. int uvOffs = y * uvStride + xx;
  475. dstUv[uvOffs + 0] = Downsample(input.GetG(xx, yy));
  476. dstUv[uvOffs + 1] = Downsample(input.GetB(xx, yy));
  477. }
  478. }
  479. }
  480. WriteBuffer(
  481. rm,
  482. dstUv,
  483. offsets.ChromaUOffset,
  484. outLinear,
  485. config.OutChromaWidth + 1,
  486. config.OutChromaHeight + 1, 2,
  487. gobBlocksInY);
  488. rm.BufferPool.Return(dstUvIndex);
  489. }
  490. private static void WriteBuffer(
  491. ResourceManager rm,
  492. ReadOnlySpan<byte> src,
  493. uint offset,
  494. bool linear,
  495. int width,
  496. int height,
  497. int bytesPerPixel,
  498. int gobBlocksInY)
  499. {
  500. if (linear)
  501. {
  502. rm.Gmm.WriteMapped(ExtendOffset(offset), src);
  503. return;
  504. }
  505. WriteBuffer(rm, src, offset, width, height, bytesPerPixel, gobBlocksInY);
  506. }
  507. private static void WriteBuffer(
  508. ResourceManager rm,
  509. ReadOnlySpan<byte> src,
  510. uint offset,
  511. int width,
  512. int height,
  513. int bytesPerPixel,
  514. int gobBlocksInY)
  515. {
  516. int outSize = GetBlockLinearSize(width, height, bytesPerPixel, gobBlocksInY);
  517. int dstStride = GetPitch(width, bytesPerPixel);
  518. int dstIndex = rm.BufferPool.Rent(outSize, out Span<byte> dst);
  519. LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, dstStride, bytesPerPixel, gobBlocksInY, src);
  520. rm.Gmm.WriteMapped(ExtendOffset(offset), dst);
  521. rm.BufferPool.Return(dstIndex);
  522. }
  523. }
  524. }