SurfaceWriter.cs 19 KB

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