SurfaceWriter.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public static void WriteInterlaced(
  35. MemoryManager gmm,
  36. ISurface surface,
  37. uint lumaTopOffset,
  38. uint chromaTopOffset,
  39. uint lumaBottomOffset,
  40. uint chromaBottomOffset)
  41. {
  42. int lumaSize = GetBlockLinearSize(surface.Width, surface.Height / 2, 1);
  43. using var lumaTop = gmm.GetWritableRegion(ExtendOffset(lumaTopOffset), lumaSize);
  44. using var lumaBottom = gmm.GetWritableRegion(ExtendOffset(lumaBottomOffset), lumaSize);
  45. WriteLuma(
  46. lumaTop.Memory.Span,
  47. surface.YPlane.AsSpan(),
  48. surface.Stride * 2,
  49. surface.Width,
  50. surface.Height / 2);
  51. WriteLuma(
  52. lumaBottom.Memory.Span,
  53. surface.YPlane.AsSpan().Slice(surface.Stride),
  54. surface.Stride * 2,
  55. surface.Width,
  56. surface.Height / 2);
  57. int chromaSize = GetBlockLinearSize(surface.UvWidth, surface.UvHeight / 2, 2);
  58. using var chromaTop = gmm.GetWritableRegion(ExtendOffset(chromaTopOffset), chromaSize);
  59. using var chromaBottom = gmm.GetWritableRegion(ExtendOffset(chromaBottomOffset), chromaSize);
  60. WriteChroma(
  61. chromaTop.Memory.Span,
  62. surface.UPlane.AsSpan(),
  63. surface.VPlane.AsSpan(),
  64. surface.UvStride * 2,
  65. surface.UvWidth,
  66. surface.UvHeight / 2);
  67. WriteChroma(
  68. chromaBottom.Memory.Span,
  69. surface.UPlane.AsSpan().Slice(surface.UvStride),
  70. surface.VPlane.AsSpan().Slice(surface.UvStride),
  71. surface.UvStride * 2,
  72. surface.UvWidth,
  73. surface.UvHeight / 2);
  74. }
  75. private static void WriteLuma(Span<byte> dst, ReadOnlySpan<byte> src, int srcStride, int width, int height)
  76. {
  77. LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, srcStride, 1, 2, src);
  78. }
  79. private unsafe static void WriteChroma(
  80. Span<byte> dst,
  81. ReadOnlySpan<byte> srcU,
  82. ReadOnlySpan<byte> srcV,
  83. int srcStride,
  84. int width,
  85. int height)
  86. {
  87. OffsetCalculator calc = new OffsetCalculator(width, height, 0, false, 2, 2);
  88. if (Sse2.IsSupported)
  89. {
  90. int strideTrunc64 = BitUtils.AlignDown(width * 2, 64);
  91. int inStrideGap = srcStride - width;
  92. fixed (byte* outputPtr = dst, srcUPtr = srcU, srcVPtr = srcV)
  93. {
  94. byte* inUPtr = srcUPtr;
  95. byte* inVPtr = srcVPtr;
  96. for (int y = 0; y < height; y++)
  97. {
  98. calc.SetY(y);
  99. for (int x = 0; x < strideTrunc64; x += 64, inUPtr += 32, inVPtr += 32)
  100. {
  101. byte* offset = outputPtr + calc.GetOffsetWithLineOffset64(x);
  102. byte* offset2 = offset + 0x20;
  103. byte* offset3 = offset + 0x100;
  104. byte* offset4 = offset + 0x120;
  105. Vector128<byte> value = *(Vector128<byte>*)inUPtr;
  106. Vector128<byte> value2 = *(Vector128<byte>*)inVPtr;
  107. Vector128<byte> value3 = *(Vector128<byte>*)(inUPtr + 16);
  108. Vector128<byte> value4 = *(Vector128<byte>*)(inVPtr + 16);
  109. Vector128<byte> uv0 = Sse2.UnpackLow(value, value2);
  110. Vector128<byte> uv1 = Sse2.UnpackHigh(value, value2);
  111. Vector128<byte> uv2 = Sse2.UnpackLow(value3, value4);
  112. Vector128<byte> uv3 = Sse2.UnpackHigh(value3, value4);
  113. *(Vector128<byte>*)offset = uv0;
  114. *(Vector128<byte>*)offset2 = uv1;
  115. *(Vector128<byte>*)offset3 = uv2;
  116. *(Vector128<byte>*)offset4 = uv3;
  117. }
  118. for (int x = strideTrunc64 / 2; x < width; x++, inUPtr++, inVPtr++)
  119. {
  120. byte* offset = outputPtr + calc.GetOffset(x);
  121. *offset = *inUPtr;
  122. *(offset + 1) = *inVPtr;
  123. }
  124. inUPtr += inStrideGap;
  125. inVPtr += inStrideGap;
  126. }
  127. }
  128. }
  129. else
  130. {
  131. for (int y = 0; y < height; y++)
  132. {
  133. int srcBaseOffset = y * srcStride;
  134. calc.SetY(y);
  135. for (int x = 0; x < width; x++)
  136. {
  137. int dstOffset = calc.GetOffset(x);
  138. dst[dstOffset + 0] = srcU[srcBaseOffset + x];
  139. dst[dstOffset + 1] = srcV[srcBaseOffset + x];
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }