SurfaceWriter.cs 15 KB

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