forked from Mirror/Ryujinx
b4d8d893a4
* WIP Range Tracking - Texture invalidation seems to have large problems - Buffer/Pool invalidation may have problems - Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution. - Native project is in the messiest possible location. - [HACK] JIT memory access always uses native "fast" path - [HACK] Trying some things with texture invalidation and views. It works :) Still a few hacks, messy things, slow things More work in progress stuff (also move to memory project) Quite a bit faster now. - Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former. - The Virtual range list is now non-overlapping like the physical one. - Fixed some bugs where regions could leak. - Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road) Move some stuff. I think we'll eventually just put the dll and so for this in a nuget package. Fix rebase. [WIP] MultiRegionHandle variable size ranges - Avoid reprotecting regions that change often (needs some tweaking) - There's still a bug in buffers, somehow. - Might want different api for minimum granularity Fix rebase issue Commit everything needed for software only tracking. Remove native components. Remove more native stuff. Cleanup Use a separate window for the background context, update opentk. (fixes linux) Some experimental changes Should get things working up to scratch - still need to try some things with flush/modification and res scale. Include address with the region action. Initial work to make range tracking work Still a ton of bugs Fix some issues with the new stuff. * Fix texture flush instability There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it) * Find the destination texture for Buffer->Texture full copy Greatly improves performance for nvdec videos (with range tracking) * Further improve texture tracking * Disable Memory Tracking for view parents This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice) The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future. * Introduce some tracking tests. WIP * Complete base tests. * Add more tests for multiregion, fix existing test. * Cleanup Part 1 * Remove unnecessary code from memory tracking * Fix some inconsistencies with 3D texture rule. * Add dispose tests. * Use a background thread for the background context. Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster. Also nerf the multithreading test a bit. * Copy to texture with matching alignment This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size. * Track reads for buffer copies. Synchronize new buffers before copying overlaps. * Remove old texture flushing mechanisms. Range tracking all the way, baby. * Wake the background thread when disposing. Avoids a deadlock when games are closed. * Address Feedback 1 * Separate TextureCopy instance for background thread Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread. * Add missing XML docs. * Address Feedback * Maybe I should start drinking coffee. * Some more feedback. * Remove flush warning, Refocus window after making background context
197 lines
No EOL
8.3 KiB
C#
197 lines
No EOL
8.3 KiB
C#
using Ryujinx.Common;
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
using Ryujinx.Graphics.Texture;
|
|
using System;
|
|
using System.Runtime.Intrinsics;
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
{
|
|
partial class Methods
|
|
{
|
|
private const int StrideAlignment = 32;
|
|
private const int GobAlignment = 64;
|
|
|
|
/// <summary>
|
|
/// Determine if a buffer-to-texture region covers the entirety of a texture.
|
|
/// </summary>
|
|
/// <param name="cbp">Copy command parameters</param>
|
|
/// <param name="tex">Texture to compare</param>
|
|
/// <param name="linear">True if the texture is linear, false if block linear</param>
|
|
/// <param name="bpp">Texture bytes per pixel</param>
|
|
/// <param name="stride">Texture stride</param>
|
|
/// <returns></returns>
|
|
private bool IsTextureCopyComplete(CopyBufferParams cbp, CopyBufferTexture tex, bool linear, int bpp, int stride)
|
|
{
|
|
if (linear)
|
|
{
|
|
int alignWidth = StrideAlignment / bpp;
|
|
return tex.RegionX == 0 &&
|
|
tex.RegionY == 0 &&
|
|
stride / bpp == BitUtils.AlignUp(cbp.XCount, alignWidth);
|
|
}
|
|
else
|
|
{
|
|
int alignWidth = GobAlignment / bpp;
|
|
return tex.RegionX == 0 &&
|
|
tex.RegionY == 0 &&
|
|
tex.Width == BitUtils.AlignUp(cbp.XCount, alignWidth) &&
|
|
tex.Height == cbp.YCount;
|
|
}
|
|
}
|
|
|
|
/// <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>
|
|
private void CopyBuffer(GpuState state, int argument)
|
|
{
|
|
var cbp = state.Get<CopyBufferParams>(MethodOffset.CopyBufferParams);
|
|
|
|
var swizzle = state.Get<CopyBufferSwizzle>(MethodOffset.CopyBufferSwizzle);
|
|
|
|
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();
|
|
|
|
var dst = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferDstTexture);
|
|
var src = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferSrcTexture);
|
|
|
|
var srcCalculator = new OffsetCalculator(
|
|
src.Width,
|
|
src.Height,
|
|
cbp.SrcStride,
|
|
srcLinear,
|
|
src.MemoryLayout.UnpackGobBlocksInY(),
|
|
src.MemoryLayout.UnpackGobBlocksInZ(),
|
|
srcBpp);
|
|
|
|
var dstCalculator = new OffsetCalculator(
|
|
dst.Width,
|
|
dst.Height,
|
|
cbp.DstStride,
|
|
dstLinear,
|
|
dst.MemoryLayout.UnpackGobBlocksInY(),
|
|
dst.MemoryLayout.UnpackGobBlocksInZ(),
|
|
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, true);
|
|
Span<byte> dstSpan = _context.PhysicalMemory.GetSpan(dstBaseAddress + (ulong)dstBaseOffset, dstSize).ToArray();
|
|
|
|
bool completeSource = IsTextureCopyComplete(cbp, src, srcLinear, srcBpp, cbp.SrcStride);
|
|
bool completeDest = IsTextureCopyComplete(cbp, dst, dstLinear, dstBpp, cbp.DstStride);
|
|
|
|
if (completeSource && completeDest)
|
|
{
|
|
Image.Texture target = TextureManager.FindTexture(dst, cbp, swizzle, dstLinear);
|
|
if (target != null)
|
|
{
|
|
ReadOnlySpan<byte> data;
|
|
if (srcLinear)
|
|
{
|
|
data = LayoutConverter.ConvertLinearStridedToLinear(
|
|
target.Info.Width,
|
|
target.Info.Height,
|
|
1,
|
|
1,
|
|
cbp.SrcStride,
|
|
target.Info.FormatInfo.BytesPerPixel,
|
|
srcSpan);
|
|
}
|
|
else
|
|
{
|
|
data = LayoutConverter.ConvertBlockLinearToLinear(
|
|
src.Width,
|
|
src.Height,
|
|
1,
|
|
target.Info.Levels,
|
|
1,
|
|
1,
|
|
1,
|
|
srcBpp,
|
|
src.MemoryLayout.UnpackGobBlocksInY(),
|
|
src.MemoryLayout.UnpackGobBlocksInZ(),
|
|
src.MemoryLayout.UnpackGobBlocksInX(),
|
|
new SizeInfo((int)target.Size),
|
|
srcSpan);
|
|
}
|
|
|
|
target.SetData(data);
|
|
target.SignalModified();
|
|
|
|
return;
|
|
}
|
|
else if (srcCalculator.LayoutMatches(dstCalculator))
|
|
{
|
|
srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
|
|
|
|
_context.PhysicalMemory.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
for (int x = 0; x < cbp.XCount; x++)
|
|
{
|
|
int srcOffset = srcCalculator.GetOffset(src.RegionX + x);
|
|
int dstOffset = dstCalculator.GetOffset(dst.RegionX + x);
|
|
|
|
*(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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.")
|
|
};
|
|
|
|
_context.PhysicalMemory.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
|
|
}
|
|
else
|
|
{
|
|
// Buffer to buffer copy.
|
|
BufferManager.CopyBuffer(cbp.SrcAddress, cbp.DstAddress, (uint)size);
|
|
}
|
|
}
|
|
}
|
|
} |