SurfaceWriter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Gpu.Memory;
  3. using Ryujinx.Graphics.Texture;
  4. using Ryujinx.Graphics.Video;
  5. using System;
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.X86;
  8. using static Ryujinx.Graphics.Nvdec.Image.SurfaceCommon;
  9. using static Ryujinx.Graphics.Nvdec.MemoryExtensions;
  10. namespace Ryujinx.Graphics.Nvdec.Image
  11. {
  12. static class SurfaceWriter
  13. {
  14. public static void Write(MemoryManager gmm, ISurface surface, uint lumaOffset, uint chromaOffset)
  15. {
  16. int lumaSize = GetBlockLinearSize(surface.Width, surface.Height, 1);
  17. using var luma = gmm.GetWritableRegion(ExtendOffset(lumaOffset), lumaSize);
  18. WriteLuma(
  19. luma.Memory.Span,
  20. surface.YPlane.AsSpan(),
  21. surface.Stride,
  22. surface.Width,
  23. surface.Height);
  24. int chromaSize = GetBlockLinearSize(surface.UvWidth, surface.UvHeight, 2);
  25. using var chroma = gmm.GetWritableRegion(ExtendOffset(chromaOffset), chromaSize);
  26. WriteChroma(
  27. chroma.Memory.Span,
  28. surface.UPlane.AsSpan(),
  29. surface.VPlane.AsSpan(),
  30. surface.UvStride,
  31. surface.UvWidth,
  32. surface.UvHeight);
  33. }
  34. private static void WriteLuma(Span<byte> dst, ReadOnlySpan<byte> src, int srcStride, int width, int height)
  35. {
  36. LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, srcStride, 1, 2, src);
  37. }
  38. private unsafe static void WriteChroma(
  39. Span<byte> dst,
  40. ReadOnlySpan<byte> srcU,
  41. ReadOnlySpan<byte> srcV,
  42. int srcStride,
  43. int width,
  44. int height)
  45. {
  46. OffsetCalculator calc = new OffsetCalculator(width, height, 0, false, 2, 2);
  47. if (Sse2.IsSupported)
  48. {
  49. int strideTrunc64 = BitUtils.AlignDown(width * 2, 64);
  50. int inStrideGap = srcStride - width;
  51. fixed (byte* outputPtr = dst, srcUPtr = srcU, srcVPtr = srcV)
  52. {
  53. byte* inUPtr = srcUPtr;
  54. byte* inVPtr = srcVPtr;
  55. for (int y = 0; y < height; y++)
  56. {
  57. calc.SetY(y);
  58. for (int x = 0; x < strideTrunc64; x += 64, inUPtr += 32, inVPtr += 32)
  59. {
  60. byte* offset = outputPtr + calc.GetOffsetWithLineOffset64(x);
  61. byte* offset2 = offset + 0x20;
  62. byte* offset3 = offset + 0x100;
  63. byte* offset4 = offset + 0x120;
  64. Vector128<byte> value = *(Vector128<byte>*)inUPtr;
  65. Vector128<byte> value2 = *(Vector128<byte>*)inVPtr;
  66. Vector128<byte> value3 = *(Vector128<byte>*)(inUPtr + 16);
  67. Vector128<byte> value4 = *(Vector128<byte>*)(inVPtr + 16);
  68. Vector128<byte> uv0 = Sse2.UnpackLow(value, value2);
  69. Vector128<byte> uv1 = Sse2.UnpackHigh(value, value2);
  70. Vector128<byte> uv2 = Sse2.UnpackLow(value3, value4);
  71. Vector128<byte> uv3 = Sse2.UnpackHigh(value3, value4);
  72. *(Vector128<byte>*)offset = uv0;
  73. *(Vector128<byte>*)offset2 = uv1;
  74. *(Vector128<byte>*)offset3 = uv2;
  75. *(Vector128<byte>*)offset4 = uv3;
  76. }
  77. for (int x = strideTrunc64 / 2; x < width; x++, inUPtr++, inVPtr++)
  78. {
  79. byte* offset = outputPtr + calc.GetOffset(x);
  80. *offset = *inUPtr;
  81. *(offset + 1) = *inVPtr;
  82. }
  83. inUPtr += inStrideGap;
  84. inVPtr += inStrideGap;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. for (int y = 0; y < height; y++)
  91. {
  92. int srcBaseOffset = y * srcStride;
  93. calc.SetY(y);
  94. for (int x = 0; x < width; x++)
  95. {
  96. int dstOffset = calc.GetOffset(x);
  97. dst[dstOffset + 0] = srcU[srcBaseOffset + x];
  98. dst[dstOffset + 1] = srcV[srcBaseOffset + x];
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }