mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2025-01-22 07:41:39 +00:00
Speed up buffer -> texture copies.
No longer copies byte by byte. Fast path when formats are identical.
This commit is contained in:
parent
fc2d5086e7
commit
8a7e25de71
|
@ -1,11 +1,13 @@
|
||||||
using Ryujinx.Graphics.Gpu.State;
|
using Ryujinx.Graphics.Gpu.State;
|
||||||
using Ryujinx.Graphics.Texture;
|
using Ryujinx.Graphics.Texture;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.Intrinsics;
|
||||||
|
|
||||||
namespace Ryujinx.Graphics.Gpu.Engine
|
namespace Ryujinx.Graphics.Gpu.Engine
|
||||||
{
|
{
|
||||||
partial class Methods
|
partial class Methods
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs a buffer to buffer, or buffer to texture copy.
|
/// Performs a buffer to buffer, or buffer to texture copy.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -56,20 +58,56 @@ namespace Ryujinx.Graphics.Gpu.Engine
|
||||||
ulong srcBaseAddress = _context.MemoryManager.Translate(cbp.SrcAddress.Pack());
|
ulong srcBaseAddress = _context.MemoryManager.Translate(cbp.SrcAddress.Pack());
|
||||||
ulong dstBaseAddress = _context.MemoryManager.Translate(cbp.DstAddress.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 = new Span<byte>(_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))
|
||||||
|
{
|
||||||
|
srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsafe int Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
|
||||||
|
{
|
||||||
|
fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
|
||||||
|
{
|
||||||
for (int y = 0; y < cbp.YCount; y++)
|
for (int y = 0; y < cbp.YCount; y++)
|
||||||
|
{
|
||||||
|
srcCalculator.SetY(src.RegionY + y);
|
||||||
|
dstCalculator.SetY(dst.RegionY + y);
|
||||||
|
|
||||||
for (int x = 0; x < cbp.XCount; x++)
|
for (int x = 0; x < cbp.XCount; x++)
|
||||||
{
|
{
|
||||||
int srcOffset = srcCalculator.GetOffset(src.RegionX + x, src.RegionY + y);
|
int srcOffset = srcBaseOffset + srcCalculator.GetOffset(src.RegionX + x);
|
||||||
int dstOffset = dstCalculator.GetOffset(dst.RegionX + x, dst.RegionY + y);
|
int dstOffset = dstBaseOffset + dstCalculator.GetOffset(dst.RegionX + x);
|
||||||
|
|
||||||
ulong srcAddress = srcBaseAddress + (ulong)srcOffset;
|
*(T*)(dstPtr + dstOffset) = *(T*)(srcPtr + srcOffset);
|
||||||
ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
|
|
||||||
|
|
||||||
ReadOnlySpan<byte> pixel = _context.PhysicalMemory.GetSpan(srcAddress, srcBpp);
|
|
||||||
|
|
||||||
_context.PhysicalMemory.Write(dstAddress, pixel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _ = 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.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.PhysicalMemory.Write(dstBaseAddress, dstSpan);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Buffer to buffer copy.
|
// Buffer to buffer copy.
|
||||||
|
|
|
@ -102,6 +102,23 @@ namespace Ryujinx.Graphics.Texture
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public (int offset, int size) GetRectangleRange(int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
// Justification:
|
||||||
|
// The offset is a combination of separate x and y parts.
|
||||||
|
// Both components increase with input and never overlap bits.
|
||||||
|
// Therefore for each component, the minimum input value is the lowest that component can go. Opposite goes for maximum.
|
||||||
|
|
||||||
|
int start = GetOffset(x, y, 0);
|
||||||
|
int end = GetOffset(x + width, y + height, 0);
|
||||||
|
return (start, (end - start) + _texBpp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool LayoutMatches(BlockLinearLayout other)
|
||||||
|
{
|
||||||
|
return _robSize == other._robSize && _sliceSize == other._sliceSize && _texBpp == other._texBpp && _bhMask == other._bhMask && _bdMask == other._bdMask;
|
||||||
|
}
|
||||||
|
|
||||||
// Functions for built in iteration.
|
// Functions for built in iteration.
|
||||||
// Components of the offset can be updated separately, and combined to save some time.
|
// Components of the offset can be updated separately, and combined to save some time.
|
||||||
|
|
||||||
|
|
11
Ryujinx.Graphics.Texture/Bpp12Pixel.cs
Normal file
11
Ryujinx.Graphics.Texture/Bpp12Pixel.cs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Ryujinx.Graphics.Texture
|
||||||
|
{
|
||||||
|
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 12)]
|
||||||
|
public struct Bpp12Pixel
|
||||||
|
{
|
||||||
|
private ulong _elem1;
|
||||||
|
private uint _elem2;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
using Ryujinx.Common;
|
using Ryujinx.Common;
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Runtime.Intrinsics;
|
using System.Runtime.Intrinsics;
|
||||||
using static Ryujinx.Graphics.Texture.BlockLinearConstants;
|
using static Ryujinx.Graphics.Texture.BlockLinearConstants;
|
||||||
|
|
||||||
|
@ -8,13 +7,6 @@ namespace Ryujinx.Graphics.Texture
|
||||||
{
|
{
|
||||||
public static class LayoutConverter
|
public static class LayoutConverter
|
||||||
{
|
{
|
||||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 12)]
|
|
||||||
private struct Bpp12Pixel
|
|
||||||
{
|
|
||||||
private ulong _elem1;
|
|
||||||
private uint _elem2;
|
|
||||||
}
|
|
||||||
|
|
||||||
private const int HostStrideAlignment = 4;
|
private const int HostStrideAlignment = 4;
|
||||||
|
|
||||||
public static Span<byte> ConvertBlockLinearToLinear(
|
public static Span<byte> ConvertBlockLinearToLinear(
|
||||||
|
@ -288,7 +280,6 @@ namespace Ryujinx.Graphics.Texture
|
||||||
{
|
{
|
||||||
fixed (byte* outputBPtr = output, dataBPtr = data)
|
fixed (byte* outputBPtr = output, dataBPtr = data)
|
||||||
{
|
{
|
||||||
T* outputPtr = (T*)outputBPtr, dataPtr = (T*)dataBPtr;
|
|
||||||
for (int layer = 0; layer < layers; layer++)
|
for (int layer = 0; layer < layers; layer++)
|
||||||
{
|
{
|
||||||
int outBaseOffset = layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level);
|
int outBaseOffset = layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level);
|
||||||
|
|
|
@ -1,17 +1,22 @@
|
||||||
using Ryujinx.Common;
|
using Ryujinx.Common;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using static Ryujinx.Graphics.Texture.BlockLinearConstants;
|
using static Ryujinx.Graphics.Texture.BlockLinearConstants;
|
||||||
|
|
||||||
namespace Ryujinx.Graphics.Texture
|
namespace Ryujinx.Graphics.Texture
|
||||||
{
|
{
|
||||||
public class OffsetCalculator
|
public class OffsetCalculator
|
||||||
{
|
{
|
||||||
|
private int _width;
|
||||||
|
private int _height;
|
||||||
private int _stride;
|
private int _stride;
|
||||||
private bool _isLinear;
|
private bool _isLinear;
|
||||||
private int _bytesPerPixel;
|
private int _bytesPerPixel;
|
||||||
|
|
||||||
private BlockLinearLayout _layoutConverter;
|
private BlockLinearLayout _layoutConverter;
|
||||||
|
|
||||||
|
// Variables for built in iteration.
|
||||||
|
private int _yPart;
|
||||||
|
|
||||||
public OffsetCalculator(
|
public OffsetCalculator(
|
||||||
int width,
|
int width,
|
||||||
int height,
|
int height,
|
||||||
|
@ -20,6 +25,8 @@ namespace Ryujinx.Graphics.Texture
|
||||||
int gobBlocksInY,
|
int gobBlocksInY,
|
||||||
int bytesPerPixel)
|
int bytesPerPixel)
|
||||||
{
|
{
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
_stride = stride;
|
_stride = stride;
|
||||||
_isLinear = isLinear;
|
_isLinear = isLinear;
|
||||||
_bytesPerPixel = bytesPerPixel;
|
_bytesPerPixel = bytesPerPixel;
|
||||||
|
@ -40,6 +47,18 @@ namespace Ryujinx.Graphics.Texture
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetY(int y)
|
||||||
|
{
|
||||||
|
if (_isLinear)
|
||||||
|
{
|
||||||
|
_yPart = y * _stride;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_layoutConverter.SetY(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int GetOffset(int x, int y)
|
public int GetOffset(int x, int y)
|
||||||
{
|
{
|
||||||
if (_isLinear)
|
if (_isLinear)
|
||||||
|
@ -51,5 +70,55 @@ namespace Ryujinx.Graphics.Texture
|
||||||
return _layoutConverter.GetOffset(x, y, 0);
|
return _layoutConverter.GetOffset(x, y, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int GetOffset(int x)
|
||||||
|
{
|
||||||
|
if (_isLinear)
|
||||||
|
{
|
||||||
|
return x * _bytesPerPixel + _yPart;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _layoutConverter.GetOffset(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int GetOffsetWithLineOffset(int x)
|
||||||
|
{
|
||||||
|
if (_isLinear)
|
||||||
|
{
|
||||||
|
return x + _yPart;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _layoutConverter.GetOffsetWithLineOffset(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public (int offset, int size) GetRectangleRange(int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
if (_isLinear)
|
||||||
|
{
|
||||||
|
return (y * _stride + x, height * _stride);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _layoutConverter.GetRectangleRange(x, y, width, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool LayoutMatches(OffsetCalculator other)
|
||||||
|
{
|
||||||
|
if (_isLinear)
|
||||||
|
{
|
||||||
|
return other._isLinear && _width == other._width && _height == other._height && _stride == other._stride && _bytesPerPixel == other._bytesPerPixel;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return !other._isLinear && _layoutConverter.LayoutMatches(other._layoutConverter);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue