SurfaceWriter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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.X86;
  7. using static Ryujinx.Graphics.Vic.Image.SurfaceCommon;
  8. namespace Ryujinx.Graphics.Vic.Image
  9. {
  10. class SurfaceWriter
  11. {
  12. public static void Write(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  13. {
  14. switch (config.OutPixelFormat)
  15. {
  16. case PixelFormat.A8B8G8R8:
  17. WriteA8B8G8R8(rm, input, ref config, ref offsets);
  18. break;
  19. case PixelFormat.A8R8G8B8:
  20. WriteA8R8G8B8(rm, input, ref config, ref offsets);
  21. break;
  22. case PixelFormat.Y8___V8U8_N420:
  23. WriteNv12(rm, input, ref config, ref offsets);
  24. break;
  25. default:
  26. Logger.Error?.Print(LogClass.Vic, $"Unsupported pixel format \"{config.OutPixelFormat}\".");
  27. break;
  28. }
  29. }
  30. private unsafe static void WriteA8B8G8R8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  31. {
  32. int width = input.Width;
  33. int height = input.Height;
  34. int stride = GetPitch(width, 4);
  35. int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst);
  36. if (Sse2.IsSupported)
  37. {
  38. int widthTrunc = width & ~7;
  39. int strideGap = stride - width * 4;
  40. fixed (Pixel* srcPtr = input.Data)
  41. {
  42. Pixel* ip = srcPtr;
  43. fixed (byte* dstPtr = dst)
  44. {
  45. byte* op = dstPtr;
  46. for (int y = 0; y < height; y++, ip += input.Width)
  47. {
  48. int x = 0;
  49. for (; x < widthTrunc; x += 8)
  50. {
  51. Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x));
  52. Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2));
  53. Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4));
  54. Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6));
  55. pixel12 = Sse2.ShiftRightLogical(pixel12, 2);
  56. pixel34 = Sse2.ShiftRightLogical(pixel34, 2);
  57. pixel56 = Sse2.ShiftRightLogical(pixel56, 2);
  58. pixel78 = Sse2.ShiftRightLogical(pixel78, 2);
  59. Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16());
  60. Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16());
  61. Sse2.Store(op + 0x00, pixel1234);
  62. Sse2.Store(op + 0x10, pixel5678);
  63. op += 0x20;
  64. }
  65. for (; x < width; x++)
  66. {
  67. Pixel* px = ip + (uint)x;
  68. *(op + 0) = Downsample(px->R);
  69. *(op + 1) = Downsample(px->G);
  70. *(op + 2) = Downsample(px->B);
  71. *(op + 3) = Downsample(px->A);
  72. op += 4;
  73. }
  74. op += strideGap;
  75. }
  76. }
  77. }
  78. }
  79. else
  80. {
  81. for (int y = 0; y < height; y++)
  82. {
  83. int baseOffs = y * stride;
  84. for (int x = 0; x < width; x++)
  85. {
  86. int offs = baseOffs + x * 4;
  87. dst[offs + 0] = Downsample(input.GetR(x, y));
  88. dst[offs + 1] = Downsample(input.GetG(x, y));
  89. dst[offs + 2] = Downsample(input.GetB(x, y));
  90. dst[offs + 3] = Downsample(input.GetA(x, y));
  91. }
  92. }
  93. }
  94. bool outLinear = config.OutBlkKind == 0;
  95. int gobBlocksInY = 1 << config.OutBlkHeight;
  96. WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY);
  97. rm.BufferPool.Return(dstIndex);
  98. }
  99. private unsafe static void WriteA8R8G8B8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  100. {
  101. int width = input.Width;
  102. int height = input.Height;
  103. int stride = GetPitch(width, 4);
  104. int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst);
  105. if (Ssse3.IsSupported)
  106. {
  107. Vector128<byte> shuffleMask = Vector128.Create(
  108. (byte)2, (byte)1, (byte)0, (byte)3,
  109. (byte)6, (byte)5, (byte)4, (byte)7,
  110. (byte)10, (byte)9, (byte)8, (byte)11,
  111. (byte)14, (byte)13, (byte)12, (byte)15);
  112. int widthTrunc = width & ~7;
  113. int strideGap = stride - width * 4;
  114. fixed (Pixel* srcPtr = input.Data)
  115. {
  116. Pixel* ip = srcPtr;
  117. fixed (byte* dstPtr = dst)
  118. {
  119. byte* op = dstPtr;
  120. for (int y = 0; y < height; y++, ip += input.Width)
  121. {
  122. int x = 0;
  123. for (; x < widthTrunc; x += 8)
  124. {
  125. Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x));
  126. Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2));
  127. Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4));
  128. Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6));
  129. pixel12 = Sse2.ShiftRightLogical(pixel12, 2);
  130. pixel34 = Sse2.ShiftRightLogical(pixel34, 2);
  131. pixel56 = Sse2.ShiftRightLogical(pixel56, 2);
  132. pixel78 = Sse2.ShiftRightLogical(pixel78, 2);
  133. Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16());
  134. Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16());
  135. pixel1234 = Ssse3.Shuffle(pixel1234, shuffleMask);
  136. pixel5678 = Ssse3.Shuffle(pixel5678, shuffleMask);
  137. Sse2.Store(op + 0x00, pixel1234);
  138. Sse2.Store(op + 0x10, pixel5678);
  139. op += 0x20;
  140. }
  141. for (; x < width; x++)
  142. {
  143. Pixel* px = ip + (uint)x;
  144. *(op + 0) = Downsample(px->B);
  145. *(op + 1) = Downsample(px->G);
  146. *(op + 2) = Downsample(px->R);
  147. *(op + 3) = Downsample(px->A);
  148. op += 4;
  149. }
  150. op += strideGap;
  151. }
  152. }
  153. }
  154. }
  155. else
  156. {
  157. for (int y = 0; y < height; y++)
  158. {
  159. int baseOffs = y * stride;
  160. for (int x = 0; x < width; x++)
  161. {
  162. int offs = baseOffs + x * 4;
  163. dst[offs + 0] = Downsample(input.GetB(x, y));
  164. dst[offs + 1] = Downsample(input.GetG(x, y));
  165. dst[offs + 2] = Downsample(input.GetR(x, y));
  166. dst[offs + 3] = Downsample(input.GetA(x, y));
  167. }
  168. }
  169. }
  170. bool outLinear = config.OutBlkKind == 0;
  171. int gobBlocksInY = 1 << config.OutBlkHeight;
  172. WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY);
  173. rm.BufferPool.Return(dstIndex);
  174. }
  175. private unsafe static void WriteNv12(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets)
  176. {
  177. int gobBlocksInY = 1 << config.OutBlkHeight;
  178. bool outLinear = config.OutBlkKind == 0;
  179. int width = Math.Min(config.OutLumaWidth + 1, input.Width);
  180. int height = Math.Min(config.OutLumaHeight + 1, input.Height);
  181. int yStride = GetPitch(config.OutLumaWidth + 1, 1);
  182. int dstYIndex = rm.BufferPool.Rent((config.OutLumaHeight + 1) * yStride, out Span<byte> dstY);
  183. if (Sse41.IsSupported)
  184. {
  185. Vector128<ushort> mask = Vector128.Create(0xffffUL).AsUInt16();
  186. int widthTrunc = width & ~0xf;
  187. int strideGap = yStride - width;
  188. fixed (Pixel* srcPtr = input.Data)
  189. {
  190. Pixel* ip = srcPtr;
  191. fixed (byte* dstPtr = dstY)
  192. {
  193. byte* op = dstPtr;
  194. for (int y = 0; y < height; y++, ip += input.Width)
  195. {
  196. int x = 0;
  197. for (; x < widthTrunc; x += 16)
  198. {
  199. byte* baseOffset = (byte*)(ip + (ulong)(uint)x);
  200. Vector128<ushort> pixelp1 = Sse2.LoadVector128((ushort*)baseOffset);
  201. Vector128<ushort> pixelp2 = Sse2.LoadVector128((ushort*)(baseOffset + 0x10));
  202. Vector128<ushort> pixelp3 = Sse2.LoadVector128((ushort*)(baseOffset + 0x20));
  203. Vector128<ushort> pixelp4 = Sse2.LoadVector128((ushort*)(baseOffset + 0x30));
  204. Vector128<ushort> pixelp5 = Sse2.LoadVector128((ushort*)(baseOffset + 0x40));
  205. Vector128<ushort> pixelp6 = Sse2.LoadVector128((ushort*)(baseOffset + 0x50));
  206. Vector128<ushort> pixelp7 = Sse2.LoadVector128((ushort*)(baseOffset + 0x60));
  207. Vector128<ushort> pixelp8 = Sse2.LoadVector128((ushort*)(baseOffset + 0x70));
  208. pixelp1 = Sse2.And(pixelp1, mask);
  209. pixelp2 = Sse2.And(pixelp2, mask);
  210. pixelp3 = Sse2.And(pixelp3, mask);
  211. pixelp4 = Sse2.And(pixelp4, mask);
  212. pixelp5 = Sse2.And(pixelp5, mask);
  213. pixelp6 = Sse2.And(pixelp6, mask);
  214. pixelp7 = Sse2.And(pixelp7, mask);
  215. pixelp8 = Sse2.And(pixelp8, mask);
  216. Vector128<ushort> pixelq1 = Sse41.PackUnsignedSaturate(pixelp1.AsInt32(), pixelp2.AsInt32());
  217. Vector128<ushort> pixelq2 = Sse41.PackUnsignedSaturate(pixelp3.AsInt32(), pixelp4.AsInt32());
  218. Vector128<ushort> pixelq3 = Sse41.PackUnsignedSaturate(pixelp5.AsInt32(), pixelp6.AsInt32());
  219. Vector128<ushort> pixelq4 = Sse41.PackUnsignedSaturate(pixelp7.AsInt32(), pixelp8.AsInt32());
  220. pixelq1 = Sse41.PackUnsignedSaturate(pixelq1.AsInt32(), pixelq2.AsInt32());
  221. pixelq2 = Sse41.PackUnsignedSaturate(pixelq3.AsInt32(), pixelq4.AsInt32());
  222. pixelq1 = Sse2.ShiftRightLogical(pixelq1, 2);
  223. pixelq2 = Sse2.ShiftRightLogical(pixelq2, 2);
  224. Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixelq1.AsInt16(), pixelq2.AsInt16());
  225. Sse2.Store(op, pixel);
  226. op += 0x10;
  227. }
  228. for (; x < width; x++)
  229. {
  230. Pixel* px = ip + (uint)x;
  231. *op++ = Downsample(px->R);
  232. }
  233. op += strideGap;
  234. }
  235. }
  236. }
  237. }
  238. else
  239. {
  240. for (int y = 0; y < height; y++)
  241. {
  242. for (int x = 0; x < width; x++)
  243. {
  244. dstY[y * yStride + x] = Downsample(input.GetR(x, y));
  245. }
  246. }
  247. }
  248. WriteBuffer(
  249. rm,
  250. dstY,
  251. offsets.LumaOffset,
  252. outLinear,
  253. config.OutLumaWidth + 1,
  254. config.OutLumaHeight + 1,
  255. 1,
  256. gobBlocksInY);
  257. rm.BufferPool.Return(dstYIndex);
  258. int uvWidth = Math.Min(config.OutChromaWidth + 1, (width + 1) >> 1);
  259. int uvHeight = Math.Min(config.OutChromaHeight + 1, (height + 1) >> 1);
  260. int uvStride = GetPitch(config.OutChromaWidth + 1, 2);
  261. int dstUvIndex = rm.BufferPool.Rent((config.OutChromaHeight + 1) * uvStride, out Span<byte> dstUv);
  262. if (Sse2.IsSupported)
  263. {
  264. int widthTrunc = uvWidth & ~7;
  265. int strideGap = uvStride - uvWidth * 2;
  266. fixed (Pixel* srcPtr = input.Data)
  267. {
  268. Pixel* ip = srcPtr;
  269. fixed (byte* dstPtr = dstUv)
  270. {
  271. byte* op = dstPtr;
  272. for (int y = 0; y < uvHeight; y++, ip += input.Width * 2)
  273. {
  274. int x = 0;
  275. for (; x < widthTrunc; x += 8)
  276. {
  277. byte* baseOffset = (byte*)ip + (ulong)(uint)x * 16;
  278. Vector128<uint> pixel1 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x02));
  279. Vector128<uint> pixel2 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x12));
  280. Vector128<uint> pixel3 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x22));
  281. Vector128<uint> pixel4 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x32));
  282. Vector128<uint> pixel5 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x42));
  283. Vector128<uint> pixel6 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x52));
  284. Vector128<uint> pixel7 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x62));
  285. Vector128<uint> pixel8 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x72));
  286. Vector128<uint> pixel12 = Sse2.UnpackLow(pixel1, pixel2);
  287. Vector128<uint> pixel34 = Sse2.UnpackLow(pixel3, pixel4);
  288. Vector128<uint> pixel56 = Sse2.UnpackLow(pixel5, pixel6);
  289. Vector128<uint> pixel78 = Sse2.UnpackLow(pixel7, pixel8);
  290. Vector128<ulong> pixel1234 = Sse2.UnpackLow(pixel12.AsUInt64(), pixel34.AsUInt64());
  291. Vector128<ulong> pixel5678 = Sse2.UnpackLow(pixel56.AsUInt64(), pixel78.AsUInt64());
  292. pixel1234 = Sse2.ShiftRightLogical(pixel1234, 2);
  293. pixel5678 = Sse2.ShiftRightLogical(pixel5678, 2);
  294. Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixel1234.AsInt16(), pixel5678.AsInt16());
  295. Sse2.Store(op, pixel);
  296. op += 0x10;
  297. }
  298. for (; x < uvWidth; x++)
  299. {
  300. Pixel* px = ip + (uint)(x << 1);
  301. *op++ = Downsample(px->G);
  302. *op++ = Downsample(px->B);
  303. }
  304. op += strideGap;
  305. }
  306. }
  307. }
  308. }
  309. else
  310. {
  311. for (int y = 0; y < uvHeight; y++)
  312. {
  313. for (int x = 0; x < uvWidth; x++)
  314. {
  315. int xx = x << 1;
  316. int yy = y << 1;
  317. int uvOffs = y * uvStride + xx;
  318. dstUv[uvOffs + 0] = Downsample(input.GetG(xx, yy));
  319. dstUv[uvOffs + 1] = Downsample(input.GetB(xx, yy));
  320. }
  321. }
  322. }
  323. WriteBuffer(
  324. rm,
  325. dstUv,
  326. offsets.ChromaUOffset,
  327. outLinear,
  328. config.OutChromaWidth + 1,
  329. config.OutChromaHeight + 1, 2,
  330. gobBlocksInY);
  331. rm.BufferPool.Return(dstUvIndex);
  332. }
  333. private static void WriteBuffer(
  334. ResourceManager rm,
  335. ReadOnlySpan<byte> src,
  336. uint offset,
  337. bool linear,
  338. int width,
  339. int height,
  340. int bytesPerPixel,
  341. int gobBlocksInY)
  342. {
  343. if (linear)
  344. {
  345. rm.Gmm.Write(ExtendOffset(offset), src);
  346. return;
  347. }
  348. WriteBuffer(rm, src, offset, width, height, bytesPerPixel, gobBlocksInY);
  349. }
  350. private static void WriteBuffer(
  351. ResourceManager rm,
  352. ReadOnlySpan<byte> src,
  353. uint offset,
  354. int width,
  355. int height,
  356. int bytesPerPixel,
  357. int gobBlocksInY)
  358. {
  359. int outSize = GetBlockLinearSize(width, height, bytesPerPixel, gobBlocksInY);
  360. int dstStride = GetPitch(width, bytesPerPixel);
  361. int dstIndex = rm.BufferPool.Rent(outSize, out Span<byte> dst);
  362. LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, dstStride, bytesPerPixel, gobBlocksInY, src);
  363. rm.Gmm.Write(ExtendOffset(offset), dst);
  364. rm.BufferPool.Return(dstIndex);
  365. }
  366. }
  367. }