Compare commits

..

3 Commits

Author SHA1 Message Date
gdkchan
0a24aa6af2 Allow textures to have their data partially mapped (#2629)
* Allow textures to have their data partially mapped

* Explicitly check for invalid memory ranges on the MultiRangeList

* Update GetWritableRegion to also support unmapped ranges
2022-02-22 13:34:16 -03:00
riperiperi
c9c65af59e Perform unscaled 2d engine copy on CPU if source texture isn't in cache. (#3112)
* Initial implementation of fast 2d copy

TODO: Partial copy for mismatching region/size.

* WIP

* Cleanup

* Update Ryujinx.Graphics.Gpu/Engine/Twod/TwodClass.cs

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2022-02-22 11:21:29 -03:00
merry
dc063eac83 ARMeilleure: Implement single stepping (#3133)
* Decoder: Implement SingleInstruction decoder mode

* Translator: Implement Step

* DecoderMode: Rename Normal to MultipleBlocks
2022-02-22 11:11:42 -03:00
12 changed files with 423 additions and 82 deletions

View File

@@ -18,7 +18,7 @@ namespace ARMeilleure.Decoders
// For lower code quality translation, we set a lower limit since we're blocking execution. // For lower code quality translation, we set a lower limit since we're blocking execution.
private const int MaxInstsPerFunctionLowCq = 500; private const int MaxInstsPerFunctionLowCq = 500;
public static Block[] Decode(IMemoryManager memory, ulong address, ExecutionMode mode, bool highCq, bool singleBlock) public static Block[] Decode(IMemoryManager memory, ulong address, ExecutionMode mode, bool highCq, DecoderMode dMode)
{ {
List<Block> blocks = new List<Block>(); List<Block> blocks = new List<Block>();
@@ -38,7 +38,7 @@ namespace ARMeilleure.Decoders
{ {
block = new Block(blkAddress); block = new Block(blkAddress);
if ((singleBlock && visited.Count >= 1) || opsCount > instructionLimit || !memory.IsMapped(blkAddress)) if ((dMode != DecoderMode.MultipleBlocks && visited.Count >= 1) || opsCount > instructionLimit || !memory.IsMapped(blkAddress))
{ {
block.Exit = true; block.Exit = true;
block.EndAddress = blkAddress; block.EndAddress = blkAddress;
@@ -96,6 +96,12 @@ namespace ARMeilleure.Decoders
} }
} }
if (dMode == DecoderMode.SingleInstruction)
{
// Only read at most one instruction
limitAddress = currBlock.Address + 1;
}
FillBlock(memory, mode, currBlock, limitAddress); FillBlock(memory, mode, currBlock, limitAddress);
opsCount += currBlock.OpCodes.Count; opsCount += currBlock.OpCodes.Count;
@@ -143,7 +149,7 @@ namespace ARMeilleure.Decoders
throw new InvalidOperationException($"Decoded a single empty exit block. Entry point = 0x{address:X}."); throw new InvalidOperationException($"Decoded a single empty exit block. Entry point = 0x{address:X}.");
} }
if (!singleBlock) if (dMode == DecoderMode.MultipleBlocks)
{ {
return TailCallRemover.RunPass(address, blocks); return TailCallRemover.RunPass(address, blocks);
} }

View File

@@ -0,0 +1,9 @@
namespace ARMeilleure.Decoders
{
enum DecoderMode
{
MultipleBlocks,
SingleBlock,
SingleInstruction,
}
}

View File

@@ -209,6 +209,17 @@ namespace ARMeilleure.Translation
return nextAddr; return nextAddr;
} }
public ulong Step(State.ExecutionContext context, ulong address)
{
TranslatedFunction func = Translate(address, context.ExecutionMode, highCq: false, singleStep: true);
address = func.Execute(context);
EnqueueForDeletion(address, func);
return address;
}
internal TranslatedFunction GetOrTranslate(ulong address, ExecutionMode mode) internal TranslatedFunction GetOrTranslate(ulong address, ExecutionMode mode)
{ {
if (!Functions.TryGetValue(address, out TranslatedFunction func)) if (!Functions.TryGetValue(address, out TranslatedFunction func))
@@ -242,7 +253,7 @@ namespace ARMeilleure.Translation
} }
} }
internal TranslatedFunction Translate(ulong address, ExecutionMode mode, bool highCq) internal TranslatedFunction Translate(ulong address, ExecutionMode mode, bool highCq, bool singleStep = false)
{ {
var context = new ArmEmitterContext( var context = new ArmEmitterContext(
Memory, Memory,
@@ -255,7 +266,7 @@ namespace ARMeilleure.Translation
Logger.StartPass(PassName.Decoding); Logger.StartPass(PassName.Decoding);
Block[] blocks = Decoder.Decode(Memory, address, mode, highCq, singleBlock: false); Block[] blocks = Decoder.Decode(Memory, address, mode, highCq, singleStep ? DecoderMode.SingleInstruction : DecoderMode.MultipleBlocks);
Logger.EndPass(PassName.Decoding); Logger.EndPass(PassName.Decoding);
@@ -285,14 +296,14 @@ namespace ARMeilleure.Translation
var options = highCq ? CompilerOptions.HighCq : CompilerOptions.None; var options = highCq ? CompilerOptions.HighCq : CompilerOptions.None;
if (context.HasPtc) if (context.HasPtc && !singleStep)
{ {
options |= CompilerOptions.Relocatable; options |= CompilerOptions.Relocatable;
} }
CompiledFunction compiledFunc = Compiler.Compile(cfg, argTypes, retType, options); CompiledFunction compiledFunc = Compiler.Compile(cfg, argTypes, retType, options);
if (context.HasPtc) if (context.HasPtc && !singleStep)
{ {
Hash128 hash = Ptc.ComputeHash(Memory, address, funcSize); Hash128 hash = Ptc.ComputeHash(Memory, address, funcSize);

View File

@@ -1,11 +1,15 @@
using Ryujinx.Graphics.Device; using Ryujinx.Common;
using Ryujinx.Graphics.Device;
using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Engine.Types; using Ryujinx.Graphics.Gpu.Engine.Types;
using Ryujinx.Graphics.Gpu.Image; using Ryujinx.Graphics.Gpu.Image;
using Ryujinx.Graphics.Texture; using Ryujinx.Graphics.Texture;
using Ryujinx.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
namespace Ryujinx.Graphics.Gpu.Engine.Twod namespace Ryujinx.Graphics.Gpu.Engine.Twod
{ {
@@ -44,6 +48,180 @@ namespace Ryujinx.Graphics.Gpu.Engine.Twod
/// <param name="data">Data to be written</param> /// <param name="data">Data to be written</param>
public void Write(int offset, int data) => _state.Write(offset, data); public void Write(int offset, int data) => _state.Write(offset, data);
/// <summary>
/// Determines if data is compatible between the source and destination texture.
/// The two textures must have the same size, layout, and bytes per pixel.
/// </summary>
/// <param name="lhs">Info for the first texture</param>
/// <param name="rhs">Info for the second texture</param>
/// <param name="lhsFormat">Format of the first texture</param>
/// <param name="rhsFormat">Format of the second texture</param>
/// <returns>True if the data is compatible, false otherwise</returns>
private bool IsDataCompatible(TwodTexture lhs, TwodTexture rhs, FormatInfo lhsFormat, FormatInfo rhsFormat)
{
if (lhsFormat.BytesPerPixel != rhsFormat.BytesPerPixel ||
lhs.Height != rhs.Height ||
lhs.Depth != rhs.Depth ||
lhs.LinearLayout != rhs.LinearLayout ||
lhs.MemoryLayout.Packed != rhs.MemoryLayout.Packed)
{
return false;
}
if (lhs.LinearLayout)
{
return lhs.Stride == rhs.Stride;
}
else
{
return lhs.Width == rhs.Width;
}
}
/// <summary>
/// Determine if the given region covers the full texture, also considering width alignment.
/// </summary>
/// <param name="texture">The texture to check</param>
/// <param name="formatInfo"></param>
/// <param name="x1">Region start x</param>
/// <param name="y1">Region start y</param>
/// <param name="x2">Region end x</param>
/// <param name="y2">Region end y</param>
/// <returns>True if the region covers the full texture, false otherwise</returns>
private bool IsCopyRegionComplete(TwodTexture texture, FormatInfo formatInfo, int x1, int y1, int x2, int y2)
{
if (x1 != 0 || y1 != 0 || y2 != texture.Height)
{
return false;
}
int width;
int widthAlignment;
if (texture.LinearLayout)
{
widthAlignment = 1;
width = texture.Stride / formatInfo.BytesPerPixel;
}
else
{
widthAlignment = Constants.GobAlignment / formatInfo.BytesPerPixel;
width = texture.Width;
}
return width == BitUtils.AlignUp(x2, widthAlignment);
}
/// <summary>
/// Performs a full data copy between two textures, reading and writing guest memory directly.
/// The textures must have a matching layout, size, and bytes per pixel.
/// </summary>
/// <param name="src">The source texture</param>
/// <param name="dst">The destination texture</param>
/// <param name="w">Copy width</param>
/// <param name="h">Copy height</param>
/// <param name="bpp">Bytes per pixel</param>
private void UnscaledFullCopy(TwodTexture src, TwodTexture dst, int w, int h, int bpp)
{
var srcCalculator = new OffsetCalculator(
w,
h,
src.Stride,
src.LinearLayout,
src.MemoryLayout.UnpackGobBlocksInY(),
src.MemoryLayout.UnpackGobBlocksInZ(),
bpp);
(int _, int srcSize) = srcCalculator.GetRectangleRange(0, 0, w, h);
var memoryManager = _channel.MemoryManager;
ulong srcGpuVa = src.Address.Pack();
ulong dstGpuVa = dst.Address.Pack();
ReadOnlySpan<byte> srcSpan = memoryManager.GetSpan(srcGpuVa, srcSize, true);
int width;
int height = src.Height;
if (src.LinearLayout)
{
width = src.Stride / bpp;
}
else
{
width = src.Width;
}
// If the copy is not equal to the width and height of the texture, we will need to copy partially.
// It's worth noting that it has already been established that the src and dst are the same size.
if (w == width && h == height)
{
memoryManager.Write(dstGpuVa, srcSpan);
}
else
{
using WritableRegion dstRegion = memoryManager.GetWritableRegion(dstGpuVa, srcSize, true);
Span<byte> dstSpan = dstRegion.Memory.Span;
if (src.LinearLayout)
{
int stride = src.Stride;
int offset = 0;
int lineSize = width * bpp;
for (int y = 0; y < height; y++)
{
srcSpan.Slice(offset, lineSize).CopyTo(dstSpan.Slice(offset));
offset += stride;
}
}
else
{
// Copy with the block linear layout in mind.
// Recreate the offset calculate with bpp 1 for copy.
int stride = w * bpp;
srcCalculator = new OffsetCalculator(
stride,
h,
0,
false,
src.MemoryLayout.UnpackGobBlocksInY(),
src.MemoryLayout.UnpackGobBlocksInZ(),
1);
int strideTrunc = BitUtils.AlignDown(stride, 16);
ReadOnlySpan<Vector128<byte>> srcVec = MemoryMarshal.Cast<byte, Vector128<byte>>(srcSpan);
Span<Vector128<byte>> dstVec = MemoryMarshal.Cast<byte, Vector128<byte>>(dstSpan);
for (int y = 0; y < h; y++)
{
int x = 0;
srcCalculator.SetY(y);
for (; x < strideTrunc; x += 16)
{
int offset = srcCalculator.GetOffset(x) >> 4;
dstVec[offset] = srcVec[offset];
}
for (; x < stride; x++)
{
int offset = srcCalculator.GetOffset(x);
dstSpan[offset] = srcSpan[offset];
}
}
}
}
}
/// <summary> /// <summary>
/// Performs the blit operation, triggered by the register write. /// Performs the blit operation, triggered by the register write.
/// </summary> /// </summary>
@@ -114,16 +292,31 @@ namespace Ryujinx.Graphics.Gpu.Engine.Twod
srcX1 = 0; srcX1 = 0;
} }
FormatInfo dstCopyTextureFormat = dstCopyTexture.Format.Convert();
bool canDirectCopy = GraphicsConfig.Fast2DCopy &&
srcX2 == dstX2 && srcY2 == dstY2 &&
IsDataCompatible(srcCopyTexture, dstCopyTexture, srcCopyTextureFormat, dstCopyTextureFormat) &&
IsCopyRegionComplete(srcCopyTexture, srcCopyTextureFormat, srcX1, srcY1, srcX2, srcY2) &&
IsCopyRegionComplete(dstCopyTexture, dstCopyTextureFormat, dstX1, dstY1, dstX2, dstY2);
var srcTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture( var srcTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture(
memoryManager, memoryManager,
srcCopyTexture, srcCopyTexture,
offset, offset,
srcCopyTextureFormat, srcCopyTextureFormat,
!canDirectCopy,
false, false,
srcHint); srcHint);
if (srcTexture == null) if (srcTexture == null)
{ {
if (canDirectCopy)
{
// Directly copy the data on CPU.
UnscaledFullCopy(srcCopyTexture, dstCopyTexture, srcX2, srcY2, srcCopyTextureFormat.BytesPerPixel);
}
return; return;
} }
@@ -132,7 +325,6 @@ namespace Ryujinx.Graphics.Gpu.Engine.Twod
// When the source texture that was found has a depth format, // When the source texture that was found has a depth format,
// we must enforce the target texture also has a depth format, // we must enforce the target texture also has a depth format,
// as copies between depth and color formats are not allowed. // as copies between depth and color formats are not allowed.
FormatInfo dstCopyTextureFormat;
if (srcTexture.Format.IsDepthOrStencil()) if (srcTexture.Format.IsDepthOrStencil())
{ {
@@ -148,6 +340,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Twod
dstCopyTexture, dstCopyTexture,
0, 0,
dstCopyTextureFormat, dstCopyTextureFormat,
true,
srcTexture.ScaleMode == TextureScaleMode.Scaled, srcTexture.ScaleMode == TextureScaleMode.Scaled,
dstHint); dstHint);

View File

@@ -28,6 +28,14 @@ namespace Ryujinx.Graphics.Gpu
/// </summary> /// </summary>
public static bool FastGpuTime = true; public static bool FastGpuTime = true;
/// <summary>
/// Enables or disables fast 2d engine texture copies entirely on CPU when possible.
/// Reduces stuttering and # of textures in games that copy textures around for streaming,
/// as textures will not need to be created for the copy, and the data does not need to be
/// flushed from GPU.
/// </summary>
public static bool Fast2DCopy = true;
/// <summary> /// <summary>
/// Enables or disables the Just-in-Time compiler for GPU Macro code. /// Enables or disables the Just-in-Time compiler for GPU Macro code.
/// </summary> /// </summary>

View File

@@ -40,6 +40,7 @@ namespace Ryujinx.Graphics.Gpu.Image
private readonly PhysicalMemory _physicalMemory; private readonly PhysicalMemory _physicalMemory;
private readonly MultiRangeList<Texture> _textures; private readonly MultiRangeList<Texture> _textures;
private readonly HashSet<Texture> _partiallyMappedTextures;
private Texture[] _textureOverlaps; private Texture[] _textureOverlaps;
private OverlapInfo[] _overlapInfo; private OverlapInfo[] _overlapInfo;
@@ -57,6 +58,7 @@ namespace Ryujinx.Graphics.Gpu.Image
_physicalMemory = physicalMemory; _physicalMemory = physicalMemory;
_textures = new MultiRangeList<Texture>(); _textures = new MultiRangeList<Texture>();
_partiallyMappedTextures = new HashSet<Texture>();
_textureOverlaps = new Texture[OverlapsBufferInitialCapacity]; _textureOverlaps = new Texture[OverlapsBufferInitialCapacity];
_overlapInfo = new OverlapInfo[OverlapsBufferInitialCapacity]; _overlapInfo = new OverlapInfo[OverlapsBufferInitialCapacity];
@@ -74,17 +76,7 @@ namespace Ryujinx.Graphics.Gpu.Image
Texture[] overlaps = new Texture[10]; Texture[] overlaps = new Texture[10];
int overlapCount; int overlapCount;
MultiRange unmapped; MultiRange unmapped = ((MemoryManager)sender).GetPhysicalRegions(e.Address, e.Size);
try
{
unmapped = ((MemoryManager)sender).GetPhysicalRegions(e.Address, e.Size);
}
catch (InvalidMemoryRegionException)
{
// This event fires on Map in case any mappings are overwritten. In that case, there may not be an existing mapping.
return;
}
lock (_textures) lock (_textures)
{ {
@@ -95,6 +87,24 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
overlaps[i].Unmapped(unmapped); overlaps[i].Unmapped(unmapped);
} }
// If any range was previously unmapped, we also need to purge
// all partially mapped texture, as they might be fully mapped now.
for (int i = 0; i < unmapped.Count; i++)
{
if (unmapped.GetSubRange(i).Address == MemoryManager.PteUnmapped)
{
lock (_partiallyMappedTextures)
{
foreach (var texture in _partiallyMappedTextures)
{
texture.Unmapped(unmapped);
}
}
break;
}
}
} }
/// <summary> /// <summary>
@@ -194,6 +204,7 @@ namespace Ryujinx.Graphics.Gpu.Image
TwodTexture copyTexture, TwodTexture copyTexture,
ulong offset, ulong offset,
FormatInfo formatInfo, FormatInfo formatInfo,
bool shouldCreate,
bool preferScaling = true, bool preferScaling = true,
Size? sizeHint = null) Size? sizeHint = null)
{ {
@@ -234,6 +245,11 @@ namespace Ryujinx.Graphics.Gpu.Image
flags |= TextureSearchFlags.WithUpscale; flags |= TextureSearchFlags.WithUpscale;
} }
if (!shouldCreate)
{
flags |= TextureSearchFlags.NoCreate;
}
Texture texture = FindOrCreateTexture(memoryManager, flags, info, 0, sizeHint); Texture texture = FindOrCreateTexture(memoryManager, flags, info, 0, sizeHint);
texture?.SynchronizeMemory(); texture?.SynchronizeMemory();
@@ -480,15 +496,29 @@ namespace Ryujinx.Graphics.Gpu.Image
return texture; return texture;
} }
else if (flags.HasFlag(TextureSearchFlags.NoCreate))
{
return null;
}
// Calculate texture sizes, used to find all overlapping textures. // Calculate texture sizes, used to find all overlapping textures.
SizeInfo sizeInfo = info.CalculateSizeInfo(layerSize); SizeInfo sizeInfo = info.CalculateSizeInfo(layerSize);
ulong size = (ulong)sizeInfo.TotalSize; ulong size = (ulong)sizeInfo.TotalSize;
bool partiallyMapped = false;
if (range == null) if (range == null)
{ {
range = memoryManager.GetPhysicalRegions(info.GpuAddress, size); range = memoryManager.GetPhysicalRegions(info.GpuAddress, size);
for (int i = 0; i < range.Value.Count; i++)
{
if (range.Value.GetSubRange(i).Address == MemoryManager.PteUnmapped)
{
partiallyMapped = true;
break;
}
}
} }
// Find view compatible matches. // Find view compatible matches.
@@ -658,7 +688,7 @@ namespace Ryujinx.Graphics.Gpu.Image
else else
{ {
bool dataOverlaps = texture.DataOverlaps(overlap, compatibility); bool dataOverlaps = texture.DataOverlaps(overlap, compatibility);
if (!overlap.IsView && dataOverlaps && !incompatibleOverlaps.Exists(incompatible => incompatible.Group == overlap.Group)) if (!overlap.IsView && dataOverlaps && !incompatibleOverlaps.Exists(incompatible => incompatible.Group == overlap.Group))
{ {
incompatibleOverlaps.Add(new TextureIncompatibleOverlap(overlap.Group, compatibility)); incompatibleOverlaps.Add(new TextureIncompatibleOverlap(overlap.Group, compatibility));
@@ -774,6 +804,14 @@ namespace Ryujinx.Graphics.Gpu.Image
_textures.Add(texture); _textures.Add(texture);
} }
if (partiallyMapped)
{
lock (_partiallyMappedTextures)
{
_partiallyMappedTextures.Add(texture);
}
}
ShrinkOverlapsBufferIfNeeded(); ShrinkOverlapsBufferIfNeeded();
for (int i = 0; i < overlapsCount; i++) for (int i = 0; i < overlapsCount; i++)
@@ -1069,6 +1107,11 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
_textures.Remove(texture); _textures.Remove(texture);
} }
lock (_partiallyMappedTextures)
{
_partiallyMappedTextures.Remove(texture);
}
} }
/// <summary> /// <summary>

View File

@@ -236,7 +236,7 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
/// <summary> /// <summary>
/// Synchronize memory for a given texture. /// Synchronize memory for a given texture.
/// If overlapping tracking handles are dirty, fully or partially synchronize the texture data. /// If overlapping tracking handles are dirty, fully or partially synchronize the texture data.
/// </summary> /// </summary>
/// <param name="texture">The texture being used</param> /// <param name="texture">The texture being used</param>
@@ -280,7 +280,7 @@ namespace Ryujinx.Graphics.Gpu.Image
// Evaluate if any copy dependencies need to be fulfilled. A few rules: // Evaluate if any copy dependencies need to be fulfilled. A few rules:
// If the copy handle needs to be synchronized, prefer our own state. // If the copy handle needs to be synchronized, prefer our own state.
// If we need to be synchronized and there is a copy present, prefer the copy. // If we need to be synchronized and there is a copy present, prefer the copy.
if (group.NeedsCopy && group.Copy(_context)) if (group.NeedsCopy && group.Copy(_context))
{ {
@@ -618,7 +618,7 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
/// <summary> /// <summary>
/// Evaluate the range of tracking handles which a view texture overlaps with, /// Evaluate the range of tracking handles which a view texture overlaps with,
/// using the view's position and slice/level counts. /// using the view's position and slice/level counts.
/// </summary> /// </summary>
/// <param name="firstLayer">The first layer of the texture</param> /// <param name="firstLayer">The first layer of the texture</param>
@@ -879,7 +879,7 @@ namespace Ryujinx.Graphics.Gpu.Image
int sliceStart = Math.Clamp(offset, 0, subRangeSize); int sliceStart = Math.Clamp(offset, 0, subRangeSize);
int sliceEnd = Math.Clamp(endOffset, 0, subRangeSize); int sliceEnd = Math.Clamp(endOffset, 0, subRangeSize);
if (sliceStart != sliceEnd) if (sliceStart != sliceEnd && item.Address != MemoryManager.PteUnmapped)
{ {
result.Add(GenerateHandle(item.Address + (ulong)sliceStart, (ulong)(sliceEnd - sliceStart))); result.Add(GenerateHandle(item.Address + (ulong)sliceStart, (ulong)(sliceEnd - sliceStart)));
} }
@@ -1097,11 +1097,20 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
// Single dirty region. // Single dirty region.
var cpuRegionHandles = new CpuRegionHandle[TextureRange.Count]; var cpuRegionHandles = new CpuRegionHandle[TextureRange.Count];
int count = 0;
for (int i = 0; i < TextureRange.Count; i++) for (int i = 0; i < TextureRange.Count; i++)
{ {
var currentRange = TextureRange.GetSubRange(i); var currentRange = TextureRange.GetSubRange(i);
cpuRegionHandles[i] = GenerateHandle(currentRange.Address, currentRange.Size); if (currentRange.Address != MemoryManager.PteUnmapped)
{
cpuRegionHandles[count++] = GenerateHandle(currentRange.Address, currentRange.Size);
}
}
if (count != TextureRange.Count)
{
Array.Resize(ref cpuRegionHandles, count);
} }
var groupHandle = new TextureGroupHandle(this, 0, Storage.Size, _views, 0, 0, 0, _allOffsets.Length, cpuRegionHandles); var groupHandle = new TextureGroupHandle(this, 0, Storage.Size, _views, 0, 0, 0, _allOffsets.Length, cpuRegionHandles);
@@ -1277,7 +1286,7 @@ namespace Ryujinx.Graphics.Gpu.Image
TextureInfo info = Storage.Info; TextureInfo info = Storage.Info;
TextureInfo otherInfo = other.Storage.Info; TextureInfo otherInfo = other.Storage.Info;
if (TextureCompatibility.ViewLayoutCompatible(info, otherInfo, level, otherLevel) && if (TextureCompatibility.ViewLayoutCompatible(info, otherInfo, level, otherLevel) &&
TextureCompatibility.CopySizeMatches(info, otherInfo, level, otherLevel)) TextureCompatibility.CopySizeMatches(info, otherInfo, level, otherLevel))
{ {
// These textures are copy compatible. Create the dependency. // These textures are copy compatible. Create the dependency.

View File

@@ -12,6 +12,7 @@ namespace Ryujinx.Graphics.Gpu.Image
Strict = 1 << 0, Strict = 1 << 0,
ForSampler = 1 << 1, ForSampler = 1 << 1,
ForCopy = 1 << 2, ForCopy = 1 << 2,
WithUpscale = 1 << 3 WithUpscale = 1 << 3,
NoCreate = 1 << 4
} }
} }

View File

@@ -28,7 +28,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
private const int PtLvl1Bit = PtPageBits; private const int PtLvl1Bit = PtPageBits;
private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits; private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
public const ulong PteUnmapped = 0xffffffff_ffffffff; public const ulong PteUnmapped = ulong.MaxValue;
private readonly ulong[][] _pageTable; private readonly ulong[][] _pageTable;
@@ -154,14 +154,15 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <summary> /// <summary>
/// Gets a writable region from GPU mapped memory. /// Gets a writable region from GPU mapped memory.
/// </summary> /// </summary>
/// <param name="address">Start address of the range</param> /// <param name="va">Start address of the range</param>
/// <param name="size">Size in bytes to be range</param> /// <param name="size">Size in bytes to be range</param>
/// <param name="tracked">True if write tracking is triggered on the span</param>
/// <returns>A writable region with the data at the specified memory location</returns> /// <returns>A writable region with the data at the specified memory location</returns>
public WritableRegion GetWritableRegion(ulong va, int size) public WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
{ {
if (IsContiguous(va, size)) if (IsContiguous(va, size))
{ {
return Physical.GetWritableRegion(Translate(va), size); return Physical.GetWritableRegion(Translate(va), size, tracked);
} }
else else
{ {
@@ -169,7 +170,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
GetSpan(va, size).CopyTo(memory.Span); GetSpan(va, size).CopyTo(memory.Span);
return new WritableRegion(this, va, memory); return new WritableRegion(this, va, memory, tracked);
} }
} }
@@ -339,7 +340,6 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="va">Virtual address of the range</param> /// <param name="va">Virtual address of the range</param>
/// <param name="size">Size of the range</param> /// <param name="size">Size of the range</param>
/// <returns>Multi-range with the physical regions</returns> /// <returns>Multi-range with the physical regions</returns>
/// <exception cref="InvalidMemoryRegionException">The memory region specified by <paramref name="va"/> and <paramref name="size"/> is not fully mapped</exception>
public MultiRange GetPhysicalRegions(ulong va, ulong size) public MultiRange GetPhysicalRegions(ulong va, ulong size)
{ {
if (IsContiguous(va, (int)size)) if (IsContiguous(va, (int)size))
@@ -347,11 +347,6 @@ namespace Ryujinx.Graphics.Gpu.Memory
return new MultiRange(Translate(va), size); return new MultiRange(Translate(va), size);
} }
if (!IsMapped(va))
{
throw new InvalidMemoryRegionException($"The specified GPU virtual address 0x{va:X} is not mapped.");
}
ulong regionStart = Translate(va); ulong regionStart = Translate(va);
ulong regionSize = Math.Min(size, PageSize - (va & PageMask)); ulong regionSize = Math.Min(size, PageSize - (va & PageMask));
@@ -366,14 +361,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
for (int page = 0; page < pages - 1; page++) for (int page = 0; page < pages - 1; page++)
{ {
if (!IsMapped(va + PageSize)) ulong currPa = Translate(va);
{
throw new InvalidMemoryRegionException($"The specified GPU virtual memory range 0x{va:X}..0x{(va + size):X} is not fully mapped.");
}
ulong newPa = Translate(va + PageSize); ulong newPa = Translate(va + PageSize);
if (Translate(va) + PageSize != newPa) if ((currPa != PteUnmapped || newPa != PteUnmapped) && currPa + PageSize != newPa)
{ {
regions.Add(new MemoryRange(regionStart, regionSize)); regions.Add(new MemoryRange(regionStart, regionSize));
regionStart = newPa; regionStart = newPa;
@@ -404,18 +395,35 @@ namespace Ryujinx.Graphics.Gpu.Memory
{ {
MemoryRange currentRange = range.GetSubRange(i); MemoryRange currentRange = range.GetSubRange(i);
ulong address = currentRange.Address & ~PageMask; if (currentRange.Address != PteUnmapped)
ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
while (address < endAddress)
{ {
if (Translate(va) != address) ulong address = currentRange.Address & ~PageMask;
{ ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
return false;
}
va += PageSize; while (address < endAddress)
address += PageSize; {
if (Translate(va) != address)
{
return false;
}
va += PageSize;
address += PageSize;
}
}
else
{
ulong endVa = va + (((currentRange.Size) + PageMask) & ~PageMask);
while (va < endVa)
{
if (Translate(va) != PteUnmapped)
{
return false;
}
va += PageSize;
}
} }
} }

View File

@@ -7,8 +7,6 @@ using Ryujinx.Memory.Range;
using Ryujinx.Memory.Tracking; using Ryujinx.Memory.Tracking;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace Ryujinx.Graphics.Gpu.Memory namespace Ryujinx.Graphics.Gpu.Memory
@@ -19,8 +17,6 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary> /// </summary>
class PhysicalMemory : IDisposable class PhysicalMemory : IDisposable
{ {
public const int PageSize = 0x1000;
private readonly GpuContext _context; private readonly GpuContext _context;
private IVirtualMemoryManagerTracked _cpuMemory; private IVirtualMemoryManagerTracked _cpuMemory;
private int _referenceCount; private int _referenceCount;
@@ -103,24 +99,28 @@ namespace Ryujinx.Graphics.Gpu.Memory
if (range.Count == 1) if (range.Count == 1)
{ {
var singleRange = range.GetSubRange(0); var singleRange = range.GetSubRange(0);
return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked); if (singleRange.Address != MemoryManager.PteUnmapped)
}
else
{
Span<byte> data = new byte[range.GetSize()];
int offset = 0;
for (int i = 0; i < range.Count; i++)
{ {
var currentRange = range.GetSubRange(i); return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked);
int size = (int)currentRange.Size;
_cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
offset += size;
} }
return data;
} }
Span<byte> data = new byte[range.GetSize()];
int offset = 0;
for (int i = 0; i < range.Count; i++)
{
var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size;
if (currentRange.Address != MemoryManager.PteUnmapped)
{
_cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
}
offset += size;
}
return data;
} }
/// <summary> /// <summary>
@@ -156,11 +156,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
int offset = 0; int offset = 0;
for (int i = 0; i < range.Count; i++) for (int i = 0; i < range.Count; i++)
{ {
MemoryRange subrange = range.GetSubRange(i); var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size;
GetSpan(subrange.Address, (int)subrange.Size).CopyTo(memory.Span.Slice(offset, (int)subrange.Size)); if (currentRange.Address != MemoryManager.PteUnmapped)
{
offset += (int)subrange.Size; GetSpan(currentRange.Address, size).CopyTo(memory.Span.Slice(offset, size));
}
offset += size;
} }
return new WritableRegion(new MultiRangeWritableBlock(range, this), 0, memory, tracked); return new WritableRegion(new MultiRangeWritableBlock(range, this), 0, memory, tracked);
@@ -253,7 +255,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
if (range.Count == 1) if (range.Count == 1)
{ {
var singleRange = range.GetSubRange(0); var singleRange = range.GetSubRange(0);
writeCallback(singleRange.Address, data); if (singleRange.Address != MemoryManager.PteUnmapped)
{
writeCallback(singleRange.Address, data);
}
} }
else else
{ {
@@ -263,7 +268,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
{ {
var currentRange = range.GetSubRange(i); var currentRange = range.GetSubRange(i);
int size = (int)currentRange.Size; int size = (int)currentRange.Size;
writeCallback(currentRange.Address, data.Slice(offset, size)); if (currentRange.Address != MemoryManager.PteUnmapped)
{
writeCallback(currentRange.Address, data.Slice(offset, size));
}
offset += size; offset += size;
} }
} }
@@ -288,11 +296,20 @@ namespace Ryujinx.Graphics.Gpu.Memory
public GpuRegionHandle BeginTracking(MultiRange range) public GpuRegionHandle BeginTracking(MultiRange range)
{ {
var cpuRegionHandles = new CpuRegionHandle[range.Count]; var cpuRegionHandles = new CpuRegionHandle[range.Count];
int count = 0;
for (int i = 0; i < range.Count; i++) for (int i = 0; i < range.Count; i++)
{ {
var currentRange = range.GetSubRange(i); var currentRange = range.GetSubRange(i);
cpuRegionHandles[i] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size); if (currentRange.Address != MemoryManager.PteUnmapped)
{
cpuRegionHandles[count++] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size);
}
}
if (count != range.Count)
{
Array.Resize(ref cpuRegionHandles, count);
} }
return new GpuRegionHandle(cpuRegionHandles); return new GpuRegionHandle(cpuRegionHandles);

View File

@@ -50,6 +50,13 @@ namespace Ryujinx.Memory.Range
ulong otherAddress = other.Address; ulong otherAddress = other.Address;
ulong otherEndAddress = other.EndAddress; ulong otherEndAddress = other.EndAddress;
// If any of the ranges if invalid (address + size overflows),
// then they are never considered to overlap.
if (thisEndAddress < thisAddress || otherEndAddress < otherAddress)
{
return false;
}
return thisAddress < otherEndAddress && otherAddress < thisEndAddress; return thisAddress < otherEndAddress && otherAddress < thisEndAddress;
} }

View File

@@ -29,6 +29,12 @@ namespace Ryujinx.Memory.Range
for (int i = 0; i < range.Count; i++) for (int i = 0; i < range.Count; i++)
{ {
var subrange = range.GetSubRange(i); var subrange = range.GetSubRange(i);
if (IsInvalid(ref subrange))
{
continue;
}
_items.Add(subrange.Address, subrange.EndAddress, item); _items.Add(subrange.Address, subrange.EndAddress, item);
} }
@@ -49,6 +55,12 @@ namespace Ryujinx.Memory.Range
for (int i = 0; i < range.Count; i++) for (int i = 0; i < range.Count; i++)
{ {
var subrange = range.GetSubRange(i); var subrange = range.GetSubRange(i);
if (IsInvalid(ref subrange))
{
continue;
}
removed += _items.Remove(subrange.Address, item); removed += _items.Remove(subrange.Address, item);
} }
@@ -86,6 +98,12 @@ namespace Ryujinx.Memory.Range
for (int i = 0; i < range.Count; i++) for (int i = 0; i < range.Count; i++)
{ {
var subrange = range.GetSubRange(i); var subrange = range.GetSubRange(i);
if (IsInvalid(ref subrange))
{
continue;
}
overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount); overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount);
} }
@@ -124,6 +142,17 @@ namespace Ryujinx.Memory.Range
return overlapCount; return overlapCount;
} }
/// <summary>
/// Checks if a given sub-range of memory is invalid.
/// Those are used to represent unmapped memory regions (holes in the region mapping).
/// </summary>
/// <param name="subRange">Memory range to checl</param>
/// <returns>True if the memory range is considered invalid, false otherwise</returns>
private static bool IsInvalid(ref MemoryRange subRange)
{
return subRange.Address == ulong.MaxValue;
}
/// <summary> /// <summary>
/// Gets all items on the list starting at the specified memory address. /// Gets all items on the list starting at the specified memory address.
/// </summary> /// </summary>