Ryujinx/Ryujinx.Graphics.Gpu/Engine/MethodCopyBuffer.cs

120 lines
5.2 KiB
C#
Raw Normal View History

2019-10-12 23:02:07 -07:00
using Ryujinx.Graphics.Gpu.State;
using Ryujinx.Graphics.Texture;
using System;
using System.Runtime.Intrinsics;
2019-10-12 23:02:07 -07:00
namespace Ryujinx.Graphics.Gpu.Engine
{
partial class Methods
{
/// <summary>
/// Performs a buffer to buffer, or buffer to texture copy.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="argument">Method call argument</param>
2019-11-21 18:46:14 -08:00
private void CopyBuffer(GpuState state, int argument)
2019-10-12 23:02:07 -07:00
{
2019-11-21 18:46:14 -08:00
var cbp = state.Get<CopyBufferParams>(MethodOffset.CopyBufferParams);
2019-10-12 23:02:07 -07:00
2019-11-21 18:46:14 -08:00
var swizzle = state.Get<CopyBufferSwizzle>(MethodOffset.CopyBufferSwizzle);
2019-10-12 23:02:07 -07:00
bool srcLinear = (argument & (1 << 7)) != 0;
bool dstLinear = (argument & (1 << 8)) != 0;
bool copy2D = (argument & (1 << 9)) != 0;
int size = cbp.XCount;
if (size == 0)
{
return;
}
if (copy2D)
{
// Buffer to texture copy.
int srcBpp = swizzle.UnpackSrcComponentsCount() * swizzle.UnpackComponentSize();
int dstBpp = swizzle.UnpackDstComponentsCount() * swizzle.UnpackComponentSize();
2019-11-21 18:46:14 -08:00
var dst = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferDstTexture);
var src = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferSrcTexture);
2019-10-12 23:02:07 -07:00
var srcCalculator = new OffsetCalculator(
src.Width,
src.Height,
cbp.SrcStride,
srcLinear,
src.MemoryLayout.UnpackGobBlocksInY(),
srcBpp);
var dstCalculator = new OffsetCalculator(
dst.Width,
dst.Height,
cbp.DstStride,
dstLinear,
dst.MemoryLayout.UnpackGobBlocksInY(),
dstBpp);
ulong srcBaseAddress = _context.MemoryManager.Translate(cbp.SrcAddress.Pack());
ulong dstBaseAddress = _context.MemoryManager.Translate(cbp.DstAddress.Pack());
(int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(src.RegionX, src.RegionY, cbp.XCount, cbp.YCount);
(int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dst.RegionX, dst.RegionY, cbp.XCount, cbp.YCount);
ReadOnlySpan<byte> srcSpan = _context.PhysicalMemory.GetSpan(srcBaseAddress + (ulong)srcBaseOffset, srcSize);
Span<byte> dstSpan = _context.PhysicalMemory.GetSpan(dstBaseAddress + (ulong)dstBaseOffset, dstSize).ToArray();
bool completeSource = src.RegionX == 0 && src.RegionY == 0 && src.Width == cbp.XCount && src.Height == cbp.YCount;
bool completeDest = dst.RegionX == 0 && dst.RegionY == 0 && dst.Width == cbp.XCount && dst.Height == cbp.YCount;
if (completeSource && completeDest && srcCalculator.LayoutMatches(dstCalculator))
2019-10-12 23:02:07 -07:00
{
srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
}
else
{
unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
{
fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
{
byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
byte* srcBase = srcPtr - srcBaseOffset;
for (int y = 0; y < cbp.YCount; y++)
{
srcCalculator.SetY(src.RegionY + y);
dstCalculator.SetY(dst.RegionY + y);
2019-10-12 23:02:07 -07:00
for (int x = 0; x < cbp.XCount; x++)
{
int srcOffset = srcCalculator.GetOffset(src.RegionX + x);
int dstOffset = dstCalculator.GetOffset(dst.RegionX + x);
2019-10-12 23:02:07 -07:00
*(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
}
}
}
return true;
}
2019-10-12 23:02:07 -07:00
bool _ = srcBpp switch
{
1 => Convert<byte>(dstSpan, srcSpan),
2 => Convert<ushort>(dstSpan, srcSpan),
4 => Convert<uint>(dstSpan, srcSpan),
8 => Convert<ulong>(dstSpan, srcSpan),
12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
_ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
};
2019-10-12 23:02:07 -07:00
}
_context.PhysicalMemory.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
2019-10-12 23:02:07 -07:00
}
else
{
// Buffer to buffer copy.
2019-12-29 09:41:50 -08:00
BufferManager.CopyBuffer(cbp.SrcAddress, cbp.DstAddress, (uint)size);
2019-10-12 23:02:07 -07:00
}
}
}
}