SurfaceReader.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Common.Memory;
  3. using Ryujinx.Graphics.Texture;
  4. using Ryujinx.Graphics.Vic.Types;
  5. using System;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.Intrinsics;
  8. using System.Runtime.Intrinsics.X86;
  9. using static Ryujinx.Graphics.Vic.Image.SurfaceCommon;
  10. namespace Ryujinx.Graphics.Vic.Image
  11. {
  12. static class SurfaceReader
  13. {
  14. public static Surface Read(
  15. ResourceManager rm,
  16. ref SlotConfig config,
  17. ref SlotSurfaceConfig surfaceConfig,
  18. ref Array8<PlaneOffsets> offsets)
  19. {
  20. switch (surfaceConfig.SlotPixelFormat)
  21. {
  22. case PixelFormat.Y8___V8U8_N420: return ReadNv12(rm, ref config, ref surfaceConfig, ref offsets);
  23. }
  24. Logger.Error?.Print(LogClass.Vic, $"Unsupported pixel format \"{surfaceConfig.SlotPixelFormat}\".");
  25. int lw = surfaceConfig.SlotLumaWidth + 1;
  26. int lh = surfaceConfig.SlotLumaHeight + 1;
  27. return new Surface(rm.SurfacePool, lw, lh);
  28. }
  29. private unsafe static Surface ReadNv12(
  30. ResourceManager rm,
  31. ref SlotConfig config,
  32. ref SlotSurfaceConfig surfaceConfig,
  33. ref Array8<PlaneOffsets> offsets)
  34. {
  35. InputSurface input = ReadSurface(rm, ref config, ref surfaceConfig, ref offsets, 1, 2);
  36. int width = input.Width;
  37. int height = input.Height;
  38. int yStride = GetPitch(width, 1);
  39. int uvStride = GetPitch(input.UvWidth, 2);
  40. Surface output = new Surface(rm.SurfacePool, width, height);
  41. if (Sse41.IsSupported)
  42. {
  43. Vector128<byte> shufMask = Vector128.Create(
  44. (byte)0, (byte)2, (byte)3, (byte)1,
  45. (byte)4, (byte)6, (byte)7, (byte)5,
  46. (byte)8, (byte)10, (byte)11, (byte)9,
  47. (byte)12, (byte)14, (byte)15, (byte)13);
  48. Vector128<short> alphaMask = Vector128.Create(0xffUL << 48).AsInt16();
  49. int yStrideGap = yStride - width;
  50. int uvStrideGap = uvStride - input.UvWidth;
  51. int widthTrunc = width & ~0xf;
  52. fixed (Pixel* dstPtr = output.Data)
  53. {
  54. Pixel* op = dstPtr;
  55. fixed (byte* src0Ptr = input.Buffer0, src1Ptr = input.Buffer1)
  56. {
  57. byte* i0p = src0Ptr;
  58. for (int y = 0; y < height; y++)
  59. {
  60. byte* i1p = src1Ptr + (y >> 1) * uvStride;
  61. int x = 0;
  62. for (; x < widthTrunc; x += 16, i0p += 16, i1p += 16)
  63. {
  64. Vector128<short> ya0 = Sse41.ConvertToVector128Int16(i0p);
  65. Vector128<short> ya1 = Sse41.ConvertToVector128Int16(i0p + 8);
  66. Vector128<byte> uv = Sse2.LoadVector128(i1p);
  67. Vector128<short> uv0 = Sse2.UnpackLow(uv.AsInt16(), uv.AsInt16());
  68. Vector128<short> uv1 = Sse2.UnpackHigh(uv.AsInt16(), uv.AsInt16());
  69. Vector128<short> rgba0 = Sse2.UnpackLow(ya0, uv0);
  70. Vector128<short> rgba1 = Sse2.UnpackHigh(ya0, uv0);
  71. Vector128<short> rgba2 = Sse2.UnpackLow(ya1, uv1);
  72. Vector128<short> rgba3 = Sse2.UnpackHigh(ya1, uv1);
  73. rgba0 = Ssse3.Shuffle(rgba0.AsByte(), shufMask).AsInt16();
  74. rgba1 = Ssse3.Shuffle(rgba1.AsByte(), shufMask).AsInt16();
  75. rgba2 = Ssse3.Shuffle(rgba2.AsByte(), shufMask).AsInt16();
  76. rgba3 = Ssse3.Shuffle(rgba3.AsByte(), shufMask).AsInt16();
  77. Vector128<short> rgba16_0 = Sse41.ConvertToVector128Int16(rgba0.AsByte());
  78. Vector128<short> rgba16_1 = Sse41.ConvertToVector128Int16(HighToLow(rgba0.AsByte()));
  79. Vector128<short> rgba16_2 = Sse41.ConvertToVector128Int16(rgba1.AsByte());
  80. Vector128<short> rgba16_3 = Sse41.ConvertToVector128Int16(HighToLow(rgba1.AsByte()));
  81. Vector128<short> rgba16_4 = Sse41.ConvertToVector128Int16(rgba2.AsByte());
  82. Vector128<short> rgba16_5 = Sse41.ConvertToVector128Int16(HighToLow(rgba2.AsByte()));
  83. Vector128<short> rgba16_6 = Sse41.ConvertToVector128Int16(rgba3.AsByte());
  84. Vector128<short> rgba16_7 = Sse41.ConvertToVector128Int16(HighToLow(rgba3.AsByte()));
  85. rgba16_0 = Sse2.Or(rgba16_0, alphaMask);
  86. rgba16_1 = Sse2.Or(rgba16_1, alphaMask);
  87. rgba16_2 = Sse2.Or(rgba16_2, alphaMask);
  88. rgba16_3 = Sse2.Or(rgba16_3, alphaMask);
  89. rgba16_4 = Sse2.Or(rgba16_4, alphaMask);
  90. rgba16_5 = Sse2.Or(rgba16_5, alphaMask);
  91. rgba16_6 = Sse2.Or(rgba16_6, alphaMask);
  92. rgba16_7 = Sse2.Or(rgba16_7, alphaMask);
  93. rgba16_0 = Sse2.ShiftLeftLogical(rgba16_0, 2);
  94. rgba16_1 = Sse2.ShiftLeftLogical(rgba16_1, 2);
  95. rgba16_2 = Sse2.ShiftLeftLogical(rgba16_2, 2);
  96. rgba16_3 = Sse2.ShiftLeftLogical(rgba16_3, 2);
  97. rgba16_4 = Sse2.ShiftLeftLogical(rgba16_4, 2);
  98. rgba16_5 = Sse2.ShiftLeftLogical(rgba16_5, 2);
  99. rgba16_6 = Sse2.ShiftLeftLogical(rgba16_6, 2);
  100. rgba16_7 = Sse2.ShiftLeftLogical(rgba16_7, 2);
  101. Sse2.Store((short*)(op + (uint)x + 0), rgba16_0);
  102. Sse2.Store((short*)(op + (uint)x + 2), rgba16_1);
  103. Sse2.Store((short*)(op + (uint)x + 4), rgba16_2);
  104. Sse2.Store((short*)(op + (uint)x + 6), rgba16_3);
  105. Sse2.Store((short*)(op + (uint)x + 8), rgba16_4);
  106. Sse2.Store((short*)(op + (uint)x + 10), rgba16_5);
  107. Sse2.Store((short*)(op + (uint)x + 12), rgba16_6);
  108. Sse2.Store((short*)(op + (uint)x + 14), rgba16_7);
  109. }
  110. for (; x < width; x++, i1p += (x & 1) * 2)
  111. {
  112. Pixel* px = op + (uint)x;
  113. px->R = Upsample(*i0p++);
  114. px->G = Upsample(*i1p);
  115. px->B = Upsample(*(i1p + 1));
  116. px->A = 0x3ff;
  117. }
  118. op += width;
  119. i0p += yStrideGap;
  120. i1p += uvStrideGap;
  121. }
  122. }
  123. }
  124. }
  125. else
  126. {
  127. for (int y = 0; y < height; y++)
  128. {
  129. int uvBase = (y >> 1) * uvStride;
  130. for (int x = 0; x < width; x++)
  131. {
  132. output.SetR(x, y, Upsample(input.Buffer0[y * yStride + x]));
  133. int uvOffs = uvBase + (x & ~1);
  134. output.SetG(x, y, Upsample(input.Buffer1[uvOffs]));
  135. output.SetB(x, y, Upsample(input.Buffer1[uvOffs + 1]));
  136. output.SetA(x, y, 0x3ff);
  137. }
  138. }
  139. }
  140. input.Return(rm.BufferPool);
  141. return output;
  142. }
  143. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  144. private static Vector128<byte> HighToLow(Vector128<byte> value)
  145. {
  146. return Sse.MoveHighToLow(value.AsSingle(), value.AsSingle()).AsByte();
  147. }
  148. private static InputSurface ReadSurface(
  149. ResourceManager rm,
  150. ref SlotConfig config,
  151. ref SlotSurfaceConfig surfaceConfig,
  152. ref Array8<PlaneOffsets> offsets,
  153. int bytesPerPixel,
  154. int planes)
  155. {
  156. InputSurface surface = new InputSurface();
  157. surface.Initialize();
  158. int gobBlocksInY = 1 << surfaceConfig.SlotBlkHeight;
  159. bool linear = surfaceConfig.SlotBlkKind == 0;
  160. int lw = surfaceConfig.SlotLumaWidth + 1;
  161. int lh = surfaceConfig.SlotLumaHeight + 1;
  162. int cw = surfaceConfig.SlotChromaWidth + 1;
  163. int ch = surfaceConfig.SlotChromaHeight + 1;
  164. // Interlaced inputs have double the height when deinterlaced.
  165. int heightShift = config.FrameFormat.IsField() ? 1 : 0;
  166. surface.Width = lw;
  167. surface.Height = lh << heightShift;
  168. surface.UvWidth = cw;
  169. surface.UvHeight = ch << heightShift;
  170. if (planes > 0)
  171. {
  172. surface.SetBuffer0(ReadBuffer(rm, ref config, ref offsets, linear, 0, lw, lh, bytesPerPixel, gobBlocksInY));
  173. }
  174. if (planes > 1)
  175. {
  176. surface.SetBuffer1(ReadBuffer(rm, ref config, ref offsets, linear, 1, cw, ch, planes == 2 ? 2 : 1, gobBlocksInY));
  177. }
  178. if (planes > 2)
  179. {
  180. surface.SetBuffer2(ReadBuffer(rm, ref config, ref offsets, linear, 2, cw, ch, 1, gobBlocksInY));
  181. }
  182. return surface;
  183. }
  184. private static RentedBuffer ReadBuffer(
  185. ResourceManager rm,
  186. scoped ref SlotConfig config,
  187. scoped ref Array8<PlaneOffsets> offsets,
  188. bool linear,
  189. int plane,
  190. int width,
  191. int height,
  192. int bytesPerPixel,
  193. int gobBlocksInY)
  194. {
  195. FrameFormat frameFormat = config.FrameFormat;
  196. bool isLuma = plane == 0;
  197. bool isField = frameFormat.IsField();
  198. bool isTopField = frameFormat.IsTopField(isLuma);
  199. int stride = GetPitch(width, bytesPerPixel);
  200. uint offset = GetOffset(ref offsets[0], plane);
  201. int dstStart = 0;
  202. int dstStride = stride;
  203. if (isField)
  204. {
  205. dstStart = isTopField ? 0 : stride;
  206. dstStride = stride * 2;
  207. }
  208. RentedBuffer buffer;
  209. if (linear)
  210. {
  211. buffer = ReadBufferLinear(rm, offset, width, height, dstStart, dstStride, bytesPerPixel);
  212. }
  213. else
  214. {
  215. buffer = ReadBufferBlockLinear(rm, offset, width, height, dstStart, dstStride, bytesPerPixel, gobBlocksInY);
  216. }
  217. if (isField || frameFormat.IsInterlaced())
  218. {
  219. RentedBuffer prevBuffer = RentedBuffer.Empty;
  220. RentedBuffer nextBuffer = RentedBuffer.Empty;
  221. if (config.PrevFieldEnable)
  222. {
  223. prevBuffer = ReadBufferNoDeinterlace(rm, ref offsets[1], linear, plane, width, height, bytesPerPixel, gobBlocksInY);
  224. }
  225. if (config.NextFieldEnable)
  226. {
  227. nextBuffer = ReadBufferNoDeinterlace(rm, ref offsets[2], linear, plane, width, height, bytesPerPixel, gobBlocksInY);
  228. }
  229. int w = width * bytesPerPixel;
  230. switch (config.DeinterlaceMode)
  231. {
  232. case DeinterlaceMode.Weave:
  233. Scaler.DeinterlaceWeave(buffer.Data, prevBuffer.Data, w, stride, isTopField);
  234. break;
  235. case DeinterlaceMode.BobField:
  236. Scaler.DeinterlaceBob(buffer.Data, w, stride, isTopField);
  237. break;
  238. case DeinterlaceMode.Bob:
  239. bool isCurrentTop = isLuma ? config.IsEven : config.ChromaEven;
  240. Scaler.DeinterlaceBob(buffer.Data, w, stride, isCurrentTop ^ frameFormat.IsInterlacedBottomFirst());
  241. break;
  242. case DeinterlaceMode.NewBob:
  243. case DeinterlaceMode.Disi1:
  244. Scaler.DeinterlaceMotionAdaptive(buffer.Data, prevBuffer.Data, nextBuffer.Data, w, stride, isTopField);
  245. break;
  246. case DeinterlaceMode.WeaveLumaBobFieldChroma:
  247. if (isLuma)
  248. {
  249. Scaler.DeinterlaceWeave(buffer.Data, prevBuffer.Data, w, stride, isTopField);
  250. }
  251. else
  252. {
  253. Scaler.DeinterlaceBob(buffer.Data, w, stride, isTopField);
  254. }
  255. break;
  256. default:
  257. Logger.Error?.Print(LogClass.Vic, $"Unsupported deinterlace mode \"{config.DeinterlaceMode}\".");
  258. break;
  259. }
  260. prevBuffer.Return(rm.BufferPool);
  261. nextBuffer.Return(rm.BufferPool);
  262. }
  263. return buffer;
  264. }
  265. private static uint GetOffset(ref PlaneOffsets offsets, int plane)
  266. {
  267. return plane switch
  268. {
  269. 0 => offsets.LumaOffset,
  270. 1 => offsets.ChromaUOffset,
  271. 2 => offsets.ChromaVOffset,
  272. _ => throw new ArgumentOutOfRangeException(nameof(plane))
  273. };
  274. }
  275. private static RentedBuffer ReadBufferNoDeinterlace(
  276. ResourceManager rm,
  277. ref PlaneOffsets offsets,
  278. bool linear,
  279. int plane,
  280. int width,
  281. int height,
  282. int bytesPerPixel,
  283. int gobBlocksInY)
  284. {
  285. int stride = GetPitch(width, bytesPerPixel);
  286. uint offset = GetOffset(ref offsets, plane);
  287. if (linear)
  288. {
  289. return ReadBufferLinear(rm, offset, width, height, 0, stride, bytesPerPixel);
  290. }
  291. return ReadBufferBlockLinear(rm, offset, width, height, 0, stride, bytesPerPixel, gobBlocksInY);
  292. }
  293. private static RentedBuffer ReadBufferLinear(
  294. ResourceManager rm,
  295. uint offset,
  296. int width,
  297. int height,
  298. int dstStart,
  299. int dstStride,
  300. int bytesPerPixel)
  301. {
  302. int srcStride = GetPitch(width, bytesPerPixel);
  303. int inSize = srcStride * height;
  304. ReadOnlySpan<byte> src = rm.Gmm.GetSpan(ExtendOffset(offset), inSize);
  305. int outSize = dstStride * height;
  306. int bufferIndex = rm.BufferPool.RentMinimum(outSize, out byte[] buffer);
  307. Span<byte> dst = buffer;
  308. dst = dst.Slice(0, outSize);
  309. for (int y = 0; y < height; y++)
  310. {
  311. src.Slice(y * srcStride, srcStride).CopyTo(dst.Slice(dstStart + y * dstStride, srcStride));
  312. }
  313. return new RentedBuffer(dst, bufferIndex);
  314. }
  315. private static RentedBuffer ReadBufferBlockLinear(
  316. ResourceManager rm,
  317. uint offset,
  318. int width,
  319. int height,
  320. int dstStart,
  321. int dstStride,
  322. int bytesPerPixel,
  323. int gobBlocksInY)
  324. {
  325. int inSize = GetBlockLinearSize(width, height, bytesPerPixel, gobBlocksInY);
  326. ReadOnlySpan<byte> src = rm.Gmm.GetSpan(ExtendOffset(offset), inSize);
  327. int outSize = dstStride * height;
  328. int bufferIndex = rm.BufferPool.RentMinimum(outSize, out byte[] buffer);
  329. Span<byte> dst = buffer;
  330. dst = dst.Slice(0, outSize);
  331. LayoutConverter.ConvertBlockLinearToLinear(dst.Slice(dstStart), width, height, dstStride, bytesPerPixel, gobBlocksInY, src);
  332. return new RentedBuffer(dst, bufferIndex);
  333. }
  334. }
  335. }