1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-19 15:52:01 +00:00
Ryujinx/Ryujinx.Graphics.Nvdec/Image/SurfaceWriter.cs
gdkchan 4d02a2d2c0
New NVDEC and VIC implementation (#1384)
* Initial NVDEC and VIC implementation

* Update FFmpeg.AutoGen to 4.3.0

* Add nvdec dependencies for Windows

* Unify some VP9 structures

* Rename VP9 structure fields

* Improvements to Video API

* XML docs for Common.Memory

* Remove now unused or redundant overloads from MemoryAccessor

* NVDEC UV surface read/write scalar paths

* Add FIXME comments about hacky things/stuff that will need to be fixed in the future

* Cleaned up VP9 memory allocation

* Remove some debug logs

* Rename some VP9 structs

* Remove unused struct

* No need to compile Ryujinx.Graphics.Host1x with unsafe anymore

* Name AsyncWorkQueue threads to make debugging easier

* Make Vp9PictureInfo a ref struct

* LayoutConverter no longer needs the depth argument (broken by rebase)

* Pooling of VP9 buffers, plus fix a memory leak on VP9

* Really wish VS could rename projects properly...

* Address feedback

* Remove using

* Catch OperationCanceledException

* Add licensing informations

* Add THIRDPARTY.md to release too

Co-authored-by: Thog <me@thog.eu>
2020-07-12 05:07:01 +02:00

126 lines
4.6 KiB
C#

using Ryujinx.Common;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Texture;
using Ryujinx.Graphics.Video;
using System;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using static Ryujinx.Graphics.Nvdec.Image.SurfaceCommon;
using static Ryujinx.Graphics.Nvdec.MemoryExtensions;
namespace Ryujinx.Graphics.Nvdec.Image
{
static class SurfaceWriter
{
public static void Write(MemoryManager gmm, ISurface surface, uint lumaOffset, uint chromaOffset)
{
int lumaSize = GetBlockLinearSize(surface.Width, surface.Height, 1);
using var luma = gmm.GetWritableRegion(ExtendOffset(lumaOffset), lumaSize);
WriteLuma(
luma.Memory.Span,
surface.YPlane.AsSpan(),
surface.Stride,
surface.Width,
surface.Height);
int chromaSize = GetBlockLinearSize(surface.UvWidth, surface.UvHeight, 2);
using var chroma = gmm.GetWritableRegion(ExtendOffset(chromaOffset), chromaSize);
WriteChroma(
chroma.Memory.Span,
surface.UPlane.AsSpan(),
surface.VPlane.AsSpan(),
surface.UvStride,
surface.UvWidth,
surface.UvHeight);
}
private static void WriteLuma(Span<byte> dst, ReadOnlySpan<byte> src, int srcStride, int width, int height)
{
LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, srcStride, 1, 2, src);
}
private unsafe static void WriteChroma(
Span<byte> dst,
ReadOnlySpan<byte> srcU,
ReadOnlySpan<byte> srcV,
int srcStride,
int width,
int height)
{
OffsetCalculator calc = new OffsetCalculator(width, height, 0, false, 2, 2);
if (Sse2.IsSupported)
{
int strideTrunc64 = BitUtils.AlignDown(width * 2, 64);
int inStrideGap = srcStride - width;
fixed (byte* outputPtr = dst, srcUPtr = srcU, srcVPtr = srcV)
{
byte* inUPtr = srcUPtr;
byte* inVPtr = srcVPtr;
for (int y = 0; y < height; y++)
{
calc.SetY(y);
for (int x = 0; x < strideTrunc64; x += 64, inUPtr += 32, inVPtr += 32)
{
byte* offset = outputPtr + calc.GetOffsetWithLineOffset64(x);
byte* offset2 = offset + 0x20;
byte* offset3 = offset + 0x100;
byte* offset4 = offset + 0x120;
Vector128<byte> value = *(Vector128<byte>*)inUPtr;
Vector128<byte> value2 = *(Vector128<byte>*)inVPtr;
Vector128<byte> value3 = *(Vector128<byte>*)(inUPtr + 16);
Vector128<byte> value4 = *(Vector128<byte>*)(inVPtr + 16);
Vector128<byte> uv0 = Sse2.UnpackLow(value, value2);
Vector128<byte> uv1 = Sse2.UnpackHigh(value, value2);
Vector128<byte> uv2 = Sse2.UnpackLow(value3, value4);
Vector128<byte> uv3 = Sse2.UnpackHigh(value3, value4);
*(Vector128<byte>*)offset = uv0;
*(Vector128<byte>*)offset2 = uv1;
*(Vector128<byte>*)offset3 = uv2;
*(Vector128<byte>*)offset4 = uv3;
}
for (int x = strideTrunc64 / 2; x < width; x++, inUPtr++, inVPtr++)
{
byte* offset = outputPtr + calc.GetOffset(x);
*offset = *inUPtr;
*(offset + 1) = *inVPtr;
}
inUPtr += inStrideGap;
inVPtr += inStrideGap;
}
}
}
else
{
for (int y = 0; y < height; y++)
{
int srcBaseOffset = y * srcStride;
calc.SetY(y);
for (int x = 0; x < width; x++)
{
int dstOffset = calc.GetOffset(x);
dst[dstOffset + 0] = srcU[srcBaseOffset + x];
dst[dstOffset + 1] = srcV[srcBaseOffset + x];
}
}
}
}
}
}