Compare commits

..

12 Commits

Author SHA1 Message Date
gdk
c5f1d1749a Revert address space mirror changes 2022-09-10 16:23:49 +02:00
gdk
7dd69f2d0e Allocation free tree lookup 2022-09-10 16:23:49 +02:00
gdk
c646638680 Update several methods to use GetNode directly and avoid array allocations 2022-09-10 16:23:49 +02:00
gdk
65f2a82b97 Optimize PlaceholderManager.UnreserveRange 2022-09-10 16:23:49 +02:00
gdk
93dd6d525a Fix potential issue with partial unmap
We must also do the unmap operation with the RWLock, otherwise faults on the unmapped region will cause crashes and the whole thing becomes pointless
2022-09-10 16:23:49 +02:00
gdk
96d4ad952c Fix reprotection regression 2022-09-10 16:23:49 +02:00
gdk
6a07f80b76 Make RBTree node fields internal again
Prevents someone from accidentaly messing with them and leaving the tree in a invalid state
2022-09-10 16:23:49 +02:00
gdk
22214ac664 Delete unused code 2022-09-10 16:23:49 +02:00
gdk
45e520a27c Rewrite PlaceholderManager4KB to use intrusive RBTree, and to coalesce free placeholders
Also make the other placeholder manager use intrusive RBTree, allows the IntervalTree that was added just for this to be deleted
2022-09-10 16:23:49 +02:00
gdk
5b5810a46a Defer address space mirror mapping and use it only if strictly needed 2022-09-10 16:23:49 +02:00
gdkchan
619ac86bd0 Do not output ViewportIndex on SPIR-V if GPU does not support it (#3644)
* Do not output ViewportIndex on SPIR-V if GPU does not support it

* Bump shader cache version
2022-09-10 13:20:23 +00:00
EmulationFanatic
7a1ab71c73 Update README.MD verbiage and compatibility 2022-09-10 15:07:37 +02:00
13 changed files with 300 additions and 604 deletions

View File

@@ -36,8 +36,8 @@
## Compatibility
As of January 2022, Ryujinx has been tested on approximately 3,500 titles; over 3,200 boot past menus and into gameplay, with roughly 2,500 of those being considered playable.
You can check out the compatibility list [here](https://github.com/Ryujinx/Ryujinx-Games-List/issues). Anyone is free to submit an updated test on an existing game entry; simply follow the new issue template and testing guidelines, or post as a reply to the applicable game issue. Use the search function to see if a game has been tested already!
As of September 2022, Ryujinx has been tested on approximately 3,600 titles; over 3,400 boot past menus and into gameplay, with roughly 2,700 of those being considered playable. You can check out the compatibility list [here](https://github.com/Ryujinx/Ryujinx-Games-List/issues).
Anyone is free to submit a new game test or update an existing game test entry; simply follow the new issue template and testing guidelines, or post as a reply to the applicable game issue. Use the search function to see if a game has been tested already!
## Usage
@@ -118,11 +118,11 @@ If you'd like to support the project financially, Ryujinx has an active Patreon
<img src="https://images.squarespace-cdn.com/content/v1/560c1d39e4b0b4fae0c9cf2a/1567548955044-WVD994WZP76EWF15T0L3/Patreon+Button.png?format=500w" width="150">
</a>
All the developers working on the project do so on their free time, but the project has several expenses:
All developers working on the project do so in their free time, but the project has several expenses:
* Hackable Nintendo Switch consoles to reverse-engineer the hardware
* Additional computer hardware for testing purposes (e.g. GPUs to diagnose graphical bugs, etc.)
* Licenses for various software development tools (e.g. Jetbrains, LDN servers, IDA)
* Web hosting and infrastructure maintenance
* Licenses for various software development tools (e.g. Jetbrains, IDA)
* Web hosting and infrastructure maintenance (e.g. LDN servers)
All funds received through Patreon are considered a donation to support the project. Patrons receive early access to progress reports and exclusive access to developer interviews.

View File

@@ -5,10 +5,10 @@ namespace Ryujinx.Common.Collections
/// </summary>
public class IntrusiveRedBlackTreeNode<T> where T : IntrusiveRedBlackTreeNode<T>
{
public bool Color = true;
public T Left;
public T Right;
public T Parent;
internal bool Color = true;
internal T Left;
internal T Right;
internal T Parent;
public T Predecessor => IntrusiveRedBlackTreeImpl<T>.PredecessorOf((T)this);
public T Successor => IntrusiveRedBlackTreeImpl<T>.SuccessorOf((T)this);

View File

@@ -274,7 +274,8 @@ namespace Ryujinx.Cpu.Jit
/// <inheritdoc/>
public void Write(ulong va, ReadOnlySpan<byte> data)
{
try {
try
{
SignalMemoryTracking(va, (ulong)data.Length, write: true);
_addressSpaceMirror.Write(va, data);

View File

@@ -26,6 +26,7 @@ namespace Ryujinx.Graphics.GAL
public readonly bool SupportsNonConstantTextureOffset;
public readonly bool SupportsShaderBallot;
public readonly bool SupportsTextureShadowLod;
public readonly bool SupportsViewportIndex;
public readonly bool SupportsViewportSwizzle;
public readonly bool SupportsIndirectParameters;
@@ -59,6 +60,7 @@ namespace Ryujinx.Graphics.GAL
bool supportsNonConstantTextureOffset,
bool supportsShaderBallot,
bool supportsTextureShadowLod,
bool supportsViewportIndex,
bool supportsViewportSwizzle,
bool supportsIndirectParameters,
uint maximumUniformBuffersPerStage,
@@ -89,6 +91,7 @@ namespace Ryujinx.Graphics.GAL
SupportsNonConstantTextureOffset = supportsNonConstantTextureOffset;
SupportsShaderBallot = supportsShaderBallot;
SupportsTextureShadowLod = supportsTextureShadowLod;
SupportsViewportIndex = supportsViewportIndex;
SupportsViewportSwizzle = supportsViewportSwizzle;
SupportsIndirectParameters = supportsIndirectParameters;
MaximumUniformBuffersPerStage = maximumUniformBuffersPerStage;

View File

@@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
private const ushort FileFormatVersionMajor = 1;
private const ushort FileFormatVersionMinor = 2;
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
private const uint CodeGenVersion = 3672;
private const uint CodeGenVersion = 3644;
private const string SharedTocFileName = "shared.toc";
private const string SharedDataFileName = "shared.data";
@@ -827,4 +827,4 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
}
}
}
}
}

View File

@@ -134,6 +134,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
public bool QueryHostSupportsTextureShadowLod() => _context.Capabilities.SupportsTextureShadowLod;
public bool QueryHostSupportsViewportIndex() => _context.Capabilities.SupportsViewportIndex;
/// <summary>
/// Converts a packed Maxwell texture format to the shader translator texture format.
/// </summary>

View File

@@ -120,6 +120,7 @@ namespace Ryujinx.Graphics.OpenGL
supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
supportsViewportIndex: true,
supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
maximumUniformBuffersPerStage: 13, // TODO: Avoid hardcoding those limits here and get from driver?

View File

@@ -267,6 +267,15 @@ namespace Ryujinx.Graphics.Shader
return true;
}
/// <summary>
/// Queries host GPU shader viewport index output support.
/// </summary>
/// <returns>True if the GPU and driver supports shader viewport index output, false otherwise</returns>
bool QueryHostSupportsViewportIndex()
{
return true;
}
/// <summary>
/// Queries the point size from the GPU state, used when it is not explicitly set on the shader.
/// </summary>

View File

@@ -78,6 +78,11 @@ namespace Ryujinx.Graphics.Shader.Translation
public static bool Validate(ShaderConfig config, int value, bool isOutAttr)
{
if (value == AttributeConsts.ViewportIndex && !config.GpuAccessor.QueryHostSupportsViewportIndex())
{
return false;
}
return From(config, value, isOutAttr).IsValid;
}

View File

@@ -380,7 +380,7 @@ namespace Ryujinx.Graphics.Vulkan
return BufferManager.GetData(buffer, offset, size);
}
public Capabilities GetCapabilities()
public unsafe Capabilities GetCapabilities()
{
FormatFeatureFlags compressedFormatFeatureFlags =
FormatFeatureFlags.FormatFeatureSampledImageBit |
@@ -409,7 +409,19 @@ namespace Ryujinx.Graphics.Vulkan
GAL.Format.Bc7Srgb,
GAL.Format.Bc7Unorm);
Api.GetPhysicalDeviceFeatures(_physicalDevice, out var features);
PhysicalDeviceVulkan12Features featuresVk12 = new PhysicalDeviceVulkan12Features()
{
SType = StructureType.PhysicalDeviceVulkan12Features
};
PhysicalDeviceFeatures2 features2 = new PhysicalDeviceFeatures2()
{
SType = StructureType.PhysicalDeviceFeatures2,
PNext = &featuresVk12
};
Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2);
Api.GetPhysicalDeviceProperties(_physicalDevice, out var properties);
var limits = properties.Limits;
@@ -419,7 +431,7 @@ namespace Ryujinx.Graphics.Vulkan
GpuVendor,
hasFrontFacingBug: IsIntelWindows,
hasVectorIndexingBug: Vendor == Vendor.Qualcomm,
supportsAstcCompression: features.TextureCompressionAstcLdr,
supportsAstcCompression: features2.Features.TextureCompressionAstcLdr,
supportsBc123Compression: supportsBc123CompressionFormat,
supportsBc45Compression: supportsBc45CompressionFormat,
supportsBc67Compression: supportsBc67CompressionFormat,
@@ -429,12 +441,13 @@ namespace Ryujinx.Graphics.Vulkan
supportsFragmentShaderInterlock: Capabilities.SupportsFragmentShaderInterlock,
supportsFragmentShaderOrderingIntel: false,
supportsGeometryShaderPassthrough: Capabilities.SupportsGeometryShaderPassthrough,
supportsImageLoadFormatted: features.ShaderStorageImageReadWithoutFormat,
supportsImageLoadFormatted: features2.Features.ShaderStorageImageReadWithoutFormat,
supportsMismatchingViewFormat: true,
supportsCubemapView: !IsAmdGcn,
supportsNonConstantTextureOffset: false,
supportsShaderBallot: false,
supportsTextureShadowLod: false,
supportsViewportIndex: featuresVk12.ShaderOutputViewportIndex,
supportsViewportSwizzle: false,
supportsIndirectParameters: Capabilities.SupportsIndirectParameters,
maximumUniformBuffersPerStage: Constants.MaxUniformBuffersPerStage,

View File

@@ -1,453 +0,0 @@
using Ryujinx.Common.Collections;
using System;
using System.Collections.Generic;
namespace Ryujinx.Memory.WindowsShared
{
/// <summary>
/// An Augmented Interval Tree based off of the "TreeDictionary"'s Red-Black Tree. Allows fast overlap checking of ranges.
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
class IntervalTree<K, V> : IntrusiveRedBlackTreeImpl<IntervalTreeNode<K, V>> where K : IComparable<K>
{
private const int ArrayGrowthSize = 32;
#region Public Methods
/// <summary>
/// Gets the values of the interval whose key is <paramref name="key"/>.
/// </summary>
/// <param name="key">Key of the node value to get</param>
/// <param name="value">Value with the given <paramref name="key"/></param>
/// <returns>True if the key is on the dictionary, false otherwise</returns>
public bool TryGet(K key, out V value)
{
IntervalTreeNode<K, V> node = GetNode(key);
if (node == null)
{
value = default;
return false;
}
value = node.Value;
return true;
}
/// <summary>
/// Returns the start addresses of the intervals whose start and end keys overlap the given range.
/// </summary>
/// <param name="start">Start of the range</param>
/// <param name="end">End of the range</param>
/// <param name="overlaps">Overlaps array to place results in</param>
/// <param name="overlapCount">Index to start writing results into the array. Defaults to 0</param>
/// <returns>Number of intervals found</returns>
public int Get(K start, K end, ref IntervalTreeNode<K, V>[] overlaps, int overlapCount = 0)
{
GetNodes(Root, start, end, ref overlaps, ref overlapCount);
return overlapCount;
}
/// <summary>
/// Adds a new interval into the tree whose start is <paramref name="start"/>, end is <paramref name="end"/> and value is <paramref name="value"/>.
/// </summary>
/// <param name="start">Start of the range to add</param>
/// <param name="end">End of the range to insert</param>
/// <param name="value">Value to add</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
public void Add(K start, K end, V value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
BSTInsert(start, end, value, null, out _);
}
/// <summary>
/// Removes a value from the tree, searching for it with <paramref name="key"/>.
/// </summary>
/// <param name="key">Key of the node to remove</param>
/// <returns>Number of deleted values</returns>
public int Remove(K key)
{
return Remove(GetNode(key));
}
/// <summary>
/// Removes a value from the tree, searching for it with <paramref name="key"/>.
/// </summary>
/// <param name="nodeToDelete">Node to be removed</param>
/// <returns>Number of deleted values</returns>
public int Remove(IntervalTreeNode<K, V> nodeToDelete)
{
if (nodeToDelete == null)
{
return 0;
}
Delete(nodeToDelete);
Count--;
return 1;
}
/// <summary>
/// Adds all the nodes in the dictionary into <paramref name="list"/>.
/// </summary>
/// <returns>A list of all values sorted by Key Order</returns>
public List<V> AsList()
{
List<V> list = new List<V>();
AddToList(Root, list);
return list;
}
#endregion
#region Private Methods (BST)
/// <summary>
/// Adds all values that are children of or contained within <paramref name="node"/> into <paramref name="list"/>, in Key Order.
/// </summary>
/// <param name="node">The node to search for values within</param>
/// <param name="list">The list to add values to</param>
private void AddToList(IntervalTreeNode<K, V> node, List<V> list)
{
if (node == null)
{
return;
}
AddToList(node.Left, list);
list.Add(node.Value);
AddToList(node.Right, list);
}
/// <summary>
/// Retrieve the node reference whose key is <paramref name="key"/>, or null if no such node exists.
/// </summary>
/// <param name="key">Key of the node to get</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
/// <returns>Node reference in the tree</returns>
private IntervalTreeNode<K, V> GetNode(K key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Start);
if (cmp < 0)
{
node = node.Left;
}
else if (cmp > 0)
{
node = node.Right;
}
else
{
return node;
}
}
return null;
}
/// <summary>
/// Retrieve all nodes that overlap the given start and end keys.
/// </summary>
/// <param name="start">Start of the range</param>
/// <param name="end">End of the range</param>
/// <param name="overlaps">Overlaps array to place results in</param>
/// <param name="overlapCount">Overlaps count to update</param>
private void GetNodes(IntervalTreeNode<K, V> node, K start, K end, ref IntervalTreeNode<K, V>[] overlaps, ref int overlapCount)
{
if (node == null || start.CompareTo(node.Max) >= 0)
{
return;
}
GetNodes(node.Left, start, end, ref overlaps, ref overlapCount);
bool endsOnRight = end.CompareTo(node.Start) > 0;
if (endsOnRight)
{
if (start.CompareTo(node.End) < 0)
{
if (overlaps.Length >= overlapCount)
{
Array.Resize(ref overlaps, overlapCount + ArrayGrowthSize);
}
overlaps[overlapCount++] = node;
}
GetNodes(node.Right, start, end, ref overlaps, ref overlapCount);
}
}
/// <summary>
/// Propagate an increase in max value starting at the given node, heading up the tree.
/// This should only be called if the max increases - not for rebalancing or removals.
/// </summary>
/// <param name="node">The node to start propagating from</param>
private void PropagateIncrease(IntervalTreeNode<K, V> node)
{
K max = node.Max;
IntervalTreeNode<K, V> ptr = node;
while ((ptr = ptr.Parent) != null)
{
if (max.CompareTo(ptr.Max) > 0)
{
ptr.Max = max;
}
else
{
break;
}
}
}
/// <summary>
/// Propagate recalculating max value starting at the given node, heading up the tree.
/// This fully recalculates the max value from all children when there is potential for it to decrease.
/// </summary>
/// <param name="node">The node to start propagating from</param>
private void PropagateFull(IntervalTreeNode<K, V> node)
{
IntervalTreeNode<K, V> ptr = node;
do
{
K max = ptr.End;
if (ptr.Left != null && ptr.Left.Max.CompareTo(max) > 0)
{
max = ptr.Left.Max;
}
if (ptr.Right != null && ptr.Right.Max.CompareTo(max) > 0)
{
max = ptr.Right.Max;
}
ptr.Max = max;
} while ((ptr = ptr.Parent) != null);
}
/// <summary>
/// Insertion Mechanism for the interval tree. Similar to a BST insert, with the start of the range as the key.
/// Iterates the tree starting from the root and inserts a new node where all children in the left subtree are less than <paramref name="start"/>, and all children in the right subtree are greater than <paramref name="start"/>.
/// Each node can contain multiple values, and has an end address which is the maximum of all those values.
/// Post insertion, the "max" value of the node and all parents are updated.
/// </summary>
/// <param name="start">Start of the range to insert</param>
/// <param name="end">End of the range to insert</param>
/// <param name="value">Value to insert</param>
/// <param name="updateFactoryCallback">Optional factory used to create a new value if <paramref name="start"/> is already on the tree</param>
/// <param name="outNode">Node that was inserted or modified</param>
/// <returns>True if <paramref name="start"/> was not yet on the tree, false otherwise</returns>
private bool BSTInsert(K start, K end, V value, Func<K, V, V> updateFactoryCallback, out IntervalTreeNode<K, V> outNode)
{
IntervalTreeNode<K, V> parent = null;
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
parent = node;
int cmp = start.CompareTo(node.Start);
if (cmp < 0)
{
node = node.Left;
}
else if (cmp > 0)
{
node = node.Right;
}
else
{
outNode = node;
if (updateFactoryCallback != null)
{
// Replace
node.Value = updateFactoryCallback(start, node.Value);
int endCmp = end.CompareTo(node.End);
if (endCmp > 0)
{
node.End = end;
if (end.CompareTo(node.Max) > 0)
{
node.Max = end;
PropagateIncrease(node);
RestoreBalanceAfterInsertion(node);
}
}
else if (endCmp < 0)
{
node.End = end;
PropagateFull(node);
}
}
return false;
}
}
IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
if (newNode.Parent == null)
{
Root = newNode;
}
else if (start.CompareTo(parent.Start) < 0)
{
parent.Left = newNode;
}
else
{
parent.Right = newNode;
}
PropagateIncrease(newNode);
Count++;
RestoreBalanceAfterInsertion(newNode);
outNode = newNode;
return true;
}
/// <summary>
/// Removes the value from the dictionary after searching for it with <paramref name="key">.
/// </summary>
/// <param name="key">Tree node to be removed</param>
private void Delete(IntervalTreeNode<K, V> nodeToDelete)
{
IntervalTreeNode<K, V> replacementNode;
if (LeftOf(nodeToDelete) == null || RightOf(nodeToDelete) == null)
{
replacementNode = nodeToDelete;
}
else
{
replacementNode = nodeToDelete.Predecessor;
}
IntervalTreeNode<K, V> tmp = LeftOf(replacementNode) ?? RightOf(replacementNode);
if (tmp != null)
{
tmp.Parent = ParentOf(replacementNode);
}
if (ParentOf(replacementNode) == null)
{
Root = tmp;
}
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
ParentOf(replacementNode).Left = tmp;
}
else
{
ParentOf(replacementNode).Right = tmp;
}
if (replacementNode != nodeToDelete)
{
nodeToDelete.Start = replacementNode.Start;
nodeToDelete.Value = replacementNode.Value;
nodeToDelete.End = replacementNode.End;
nodeToDelete.Max = replacementNode.Max;
}
PropagateFull(replacementNode);
if (tmp != null && ColorOf(replacementNode) == Black)
{
RestoreBalanceAfterRemoval(tmp);
}
}
#endregion
#region Private Methods (RBL)
protected override void RotateLeft(IntervalTreeNode<K, V> node)
{
if (node != null)
{
base.RotateLeft(node);
PropagateFull(node);
}
}
protected override void RotateRight(IntervalTreeNode<K, V> node)
{
if (node != null)
{
base.RotateRight(node);
PropagateFull(node);
}
}
#endregion
public bool ContainsKey(K key)
{
return GetNode(key) != null;
}
}
/// <summary>
/// Represents a node in the IntervalTree which contains start and end keys of type K, and a value of generic type V.
/// </summary>
/// <typeparam name="K">Key type of the node</typeparam>
/// <typeparam name="V">Value type of the node</typeparam>
class IntervalTreeNode<K, V> : IntrusiveRedBlackTreeNode<IntervalTreeNode<K, V>>
{
/// <summary>
/// The start of the range.
/// </summary>
public K Start;
/// <summary>
/// The end of the range.
/// </summary>
public K End;
/// <summary>
/// The maximum end value of this node and all its children.
/// </summary>
public K Max;
/// <summary>
/// Value stored on this node.
/// </summary>
public V Value;
public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
{
Start = start;
End = end;
Max = end;
Value = value;
Parent = parent;
}
}
}

View File

@@ -0,0 +1,87 @@
using Ryujinx.Common.Collections;
using System;
namespace Ryujinx.Memory.WindowsShared
{
/// <summary>
/// A intrusive Red-Black Tree that also supports getting nodes overlapping a given range.
/// </summary>
/// <typeparam name="T">Type of the value stored on the node</typeparam>
class MappingTree<T> : IntrusiveRedBlackTree<RangeNode<T>>
{
private const int ArrayGrowthSize = 16;
public int GetNodes(ulong start, ulong end, ref RangeNode<T>[] overlaps, int overlapCount = 0)
{
RangeNode<T> node = this.GetNodeByKey(start);
for (; node != null; node = node.Successor)
{
if (overlaps.Length <= overlapCount)
{
Array.Resize(ref overlaps, overlapCount + ArrayGrowthSize);
}
overlaps[overlapCount++] = node;
if (node.End >= end)
{
break;
}
}
return overlapCount;
}
}
class RangeNode<T> : IntrusiveRedBlackTreeNode<RangeNode<T>>, IComparable<RangeNode<T>>, IComparable<ulong>
{
public ulong Start { get; }
public ulong End { get; private set; }
public T Value { get; }
public RangeNode(ulong start, ulong end, T value)
{
Start = start;
End = end;
Value = value;
}
public void Extend(ulong sizeDelta)
{
End += sizeDelta;
}
public int CompareTo(RangeNode<T> other)
{
if (Start < other.Start)
{
return -1;
}
else if (Start <= other.End - 1UL)
{
return 0;
}
else
{
return 1;
}
}
public int CompareTo(ulong address)
{
if (address < Start)
{
return 1;
}
else if (address <= End - 1UL)
{
return 0;
}
else
{
return -1;
}
}
}
}

View File

@@ -1,3 +1,4 @@
using Ryujinx.Common.Collections;
using Ryujinx.Common.Memory.PartialUnmaps;
using System;
using System.Diagnostics;
@@ -13,10 +14,10 @@ namespace Ryujinx.Memory.WindowsShared
[SupportedOSPlatform("windows")]
class PlaceholderManager
{
private const ulong MinimumPageSize = 0x1000;
private const int InitialOverlapsSize = 10;
private readonly IntervalTree<ulong, ulong> _mappings;
private readonly IntervalTree<ulong, MemoryPermission> _protections;
private readonly MappingTree<ulong> _mappings;
private readonly MappingTree<MemoryPermission> _protections;
private readonly IntPtr _partialUnmapStatePtr;
private readonly Thread _partialUnmapTrimThread;
@@ -25,8 +26,8 @@ namespace Ryujinx.Memory.WindowsShared
/// </summary>
public PlaceholderManager()
{
_mappings = new IntervalTree<ulong, ulong>();
_protections = new IntervalTree<ulong, MemoryPermission>();
_mappings = new MappingTree<ulong>();
_protections = new MappingTree<MemoryPermission>();
_partialUnmapStatePtr = PartialUnmapState.GlobalState;
@@ -67,7 +68,12 @@ namespace Ryujinx.Memory.WindowsShared
{
lock (_mappings)
{
_mappings.Add(address, address + size, ulong.MaxValue);
_mappings.Add(new RangeNode<ulong>(address, address + size, ulong.MaxValue));
}
lock (_protections)
{
_protections.Add(new RangeNode<MemoryPermission>(address, size, MemoryPermission.None));
}
}
@@ -81,35 +87,30 @@ namespace Ryujinx.Memory.WindowsShared
{
ulong endAddress = address + size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
int count;
lock (_mappings)
{
count = _mappings.Get(address, endAddress, ref overlaps);
RangeNode<ulong> node = _mappings.GetNodeByKey(address);
RangeNode<ulong> successorNode;
for (int index = 0; index < count; index++)
for (; node != null; node = successorNode)
{
var overlap = overlaps[index];
successorNode = node.Successor;
if (IsMapped(overlap.Value))
if (IsMapped(node.Value))
{
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)node.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
}
_mappings.Remove(overlap);
}
}
_mappings.Remove(node);
if (count > 1)
{
CheckFreeResult(WindowsApi.VirtualFree(
(IntPtr)address,
(IntPtr)size,
AllocationType.Release | AllocationType.CoalescePlaceholders));
if (node.End >= endAddress)
{
break;
}
}
}
RemoveProtection(address, size);
@@ -130,7 +131,7 @@ namespace Ryujinx.Memory.WindowsShared
try
{
UnmapViewInternal(sharedMemory, location, size, owner);
UnmapViewInternal(sharedMemory, location, size, owner, updateProtection: false);
MapViewInternal(sharedMemory, srcOffset, location, size);
}
finally
@@ -166,6 +167,8 @@ namespace Ryujinx.Memory.WindowsShared
{
throw new WindowsApiException("MapViewOfFile3");
}
UpdateProtection((ulong)location, (ulong)size, MemoryPermission.ReadAndWrite);
}
/// <summary>
@@ -178,18 +181,17 @@ namespace Ryujinx.Memory.WindowsShared
{
ulong endAddress = address + size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
var overlaps = new RangeNode<ulong>[InitialOverlapsSize];
lock (_mappings)
{
int count = _mappings.Get(address, endAddress, ref overlaps);
int count = _mappings.GetNodes(address, endAddress, ref overlaps);
Debug.Assert(count == 1);
Debug.Assert(!IsMapped(overlaps[0].Value));
var overlap = overlaps[0];
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
ulong overlapStart = overlap.Start;
ulong overlapEnd = overlap.End;
ulong overlapValue = overlap.Value;
@@ -206,8 +208,8 @@ namespace Ryujinx.Memory.WindowsShared
(IntPtr)size,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(overlapStart, address, overlapValue);
_mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart));
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
_mappings.Add(new RangeNode<ulong>(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart)));
}
else if (overlapStartsBefore)
{
@@ -218,7 +220,7 @@ namespace Ryujinx.Memory.WindowsShared
(IntPtr)overlappedSize,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(overlapStart, address, overlapValue);
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
}
else if (overlapEndsAfter)
{
@@ -229,10 +231,10 @@ namespace Ryujinx.Memory.WindowsShared
(IntPtr)overlappedSize,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize));
_mappings.Add(new RangeNode<ulong>(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize)));
}
_mappings.Add(address, endAddress, backingOffset);
_mappings.Add(new RangeNode<ulong>(address, endAddress, backingOffset));
}
}
@@ -254,7 +256,7 @@ namespace Ryujinx.Memory.WindowsShared
try
{
UnmapViewInternal(sharedMemory, location, size, owner);
UnmapViewInternal(sharedMemory, location, size, owner, updateProtection: true);
}
finally
{
@@ -273,19 +275,20 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="location">Address to unmap</param>
/// <param name="size">Size of the region to unmap in bytes</param>
/// <param name="owner">Memory block that owns the mapping</param>
/// <param name="updateProtection">Indicates if the memory protections should be updated after the unmap</param>
/// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unmapping or remapping the memory</exception>
private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner, bool updateProtection)
{
ulong startAddress = (ulong)location;
ulong unmapSize = (ulong)size;
ulong endAddress = startAddress + unmapSize;
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
var overlaps = new RangeNode<ulong>[InitialOverlapsSize];
int count;
lock (_mappings)
{
count = _mappings.Get(startAddress, endAddress, ref overlaps);
count = _mappings.GetNodes(startAddress, endAddress, ref overlaps);
}
for (int index = 0; index < count; index++)
@@ -294,19 +297,14 @@ namespace Ryujinx.Memory.WindowsShared
if (IsMapped(overlap.Value))
{
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
ulong overlapStart = overlap.Start;
ulong overlapEnd = overlap.End;
ulong overlapValue = overlap.Value;
lock (_mappings)
{
_mappings.Remove(overlap);
_mappings.Add(overlapStart, overlapEnd, ulong.MaxValue);
_mappings.Add(new RangeNode<ulong>(overlap.Start, overlap.End, ulong.MaxValue));
}
bool overlapStartsBefore = overlapStart < startAddress;
bool overlapEndsAfter = overlapEnd > endAddress;
bool overlapStartsBefore = overlap.Start < startAddress;
bool overlapEndsAfter = overlap.End > endAddress;
if (overlapStartsBefore || overlapEndsAfter)
{
@@ -323,25 +321,25 @@ namespace Ryujinx.Memory.WindowsShared
{
partialUnmapState.PartialUnmapsCount++;
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlapStart, 2))
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
if (overlapStartsBefore)
{
ulong remapSize = startAddress - overlapStart;
ulong remapSize = startAddress - overlap.Start;
MapViewInternal(sharedMemory, overlapValue, (IntPtr)overlapStart, (IntPtr)remapSize);
RestoreRangeProtection(overlapStart, remapSize);
MapViewInternal(sharedMemory, overlap.Value, (IntPtr)overlap.Start, (IntPtr)remapSize);
RestoreRangeProtection(overlap.Start, remapSize);
}
if (overlapEndsAfter)
{
ulong overlappedSize = endAddress - overlapStart;
ulong remapBackingOffset = overlapValue + overlappedSize;
ulong remapAddress = overlapStart + overlappedSize;
ulong remapSize = overlapEnd - endAddress;
ulong overlappedSize = endAddress - overlap.Start;
ulong remapBackingOffset = overlap.Value + overlappedSize;
ulong remapAddress = overlap.Start + overlappedSize;
ulong remapSize = overlap.End - endAddress;
MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize);
RestoreRangeProtection(remapAddress, remapSize);
@@ -352,7 +350,7 @@ namespace Ryujinx.Memory.WindowsShared
partialUnmapLock.DowngradeFromWriterLock();
}
}
else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlapStart, 2))
else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
@@ -360,7 +358,11 @@ namespace Ryujinx.Memory.WindowsShared
}
CoalesceForUnmap(startAddress, unmapSize, owner);
RemoveProtection(startAddress, unmapSize);
if (updateProtection)
{
UpdateProtection(startAddress, unmapSize, MemoryPermission.None);
}
}
/// <summary>
@@ -374,44 +376,58 @@ namespace Ryujinx.Memory.WindowsShared
ulong endAddress = address + size;
ulong blockAddress = (ulong)owner.Pointer;
ulong blockEnd = blockAddress + owner.Size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
int unmappedCount = 0;
lock (_mappings)
{
int count = _mappings.Get(
Math.Max(address - MinimumPageSize, blockAddress),
Math.Min(endAddress + MinimumPageSize, blockEnd), ref overlaps);
RangeNode<ulong> node = _mappings.GetNodeByKey(address);
if (count < 2)
if (node == null)
{
// Nothing to coalesce if we only have 1 or no overlaps.
// Nothing to coalesce if we have no overlaps.
return;
}
for (int index = 0; index < count; index++)
RangeNode<ulong> predecessor = node.Predecessor;
RangeNode<ulong> successor = null;
for (; node != null; node = successor)
{
var overlap = overlaps[index];
successor = node.Successor;
var overlap = node;
if (!IsMapped(overlap.Value))
{
if (address > overlap.Start)
{
address = overlap.Start;
}
if (endAddress < overlap.End)
{
endAddress = overlap.End;
}
address = Math.Min(address, overlap.Start);
endAddress = Math.Max(endAddress, overlap.End);
_mappings.Remove(overlap);
unmappedCount++;
}
if (node.End >= endAddress)
{
break;
}
}
_mappings.Add(address, endAddress, ulong.MaxValue);
if (predecessor != null && !IsMapped(predecessor.Value) && predecessor.Start >= blockAddress)
{
address = Math.Min(address, predecessor.Start);
_mappings.Remove(predecessor);
unmappedCount++;
}
if (successor != null && !IsMapped(successor.Value) && successor.End <= blockEnd)
{
endAddress = Math.Max(endAddress, successor.End);
_mappings.Remove(successor);
unmappedCount++;
}
_mappings.Add(new RangeNode<ulong>(address, endAddress, ulong.MaxValue));
}
if (unmappedCount > 1)
@@ -462,60 +478,55 @@ namespace Ryujinx.Memory.WindowsShared
ulong reprotectSize = (ulong)size;
ulong endAddress = reprotectAddress + reprotectSize;
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
int count;
bool success = true;
lock (_mappings)
{
count = _mappings.Get(reprotectAddress, endAddress, ref overlaps);
}
RangeNode<ulong> node = _mappings.GetNodeByKey(reprotectAddress);
RangeNode<ulong> successorNode;
bool success = true;
for (int index = 0; index < count; index++)
{
var overlap = overlaps[index];
ulong mappedAddress = overlap.Start;
ulong mappedSize = overlap.End - overlap.Start;
if (mappedAddress < reprotectAddress)
for (; node != null; node = successorNode)
{
ulong delta = reprotectAddress - mappedAddress;
mappedAddress = reprotectAddress;
mappedSize -= delta;
}
successorNode = node.Successor;
var overlap = node;
ulong mappedEndAddress = mappedAddress + mappedSize;
ulong mappedAddress = overlap.Start;
ulong mappedSize = overlap.End - overlap.Start;
if (mappedEndAddress > endAddress)
{
ulong delta = mappedEndAddress - endAddress;
mappedSize -= delta;
}
if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
{
if (throwOnError)
if (mappedAddress < reprotectAddress)
{
throw new WindowsApiException("VirtualProtect");
ulong delta = reprotectAddress - mappedAddress;
mappedAddress = reprotectAddress;
mappedSize -= delta;
}
success = false;
}
ulong mappedEndAddress = mappedAddress + mappedSize;
// We only keep track of "non-standard" protections,
// that is, everything that is not just RW (which is the default when views are mapped).
if (permission == MemoryPermission.ReadAndWrite)
{
RemoveProtection(mappedAddress, mappedSize);
}
else
{
AddProtection(mappedAddress, mappedSize, permission);
if (mappedEndAddress > endAddress)
{
ulong delta = mappedEndAddress - endAddress;
mappedSize -= delta;
}
if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
{
if (throwOnError)
{
throw new WindowsApiException("VirtualProtect");
}
success = false;
}
if (node.End >= endAddress)
{
break;
}
}
}
UpdateProtection(reprotectAddress, reprotectSize, permission);
return success;
}
@@ -564,29 +575,30 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="address">Address of the protected region</param>
/// <param name="size">Size of the protected region in bytes</param>
/// <param name="permission">Memory permissions of the region</param>
private void AddProtection(ulong address, ulong size, MemoryPermission permission)
private void UpdateProtection(ulong address, ulong size, MemoryPermission permission)
{
ulong endAddress = address + size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
int count;
lock (_protections)
{
count = _protections.Get(address, endAddress, ref overlaps);
RangeNode<MemoryPermission> node = _protections.GetNodeByKey(address);
if (count == 1 &&
overlaps[0].Start <= address &&
overlaps[0].End >= endAddress &&
overlaps[0].Value == permission)
if (node != null &&
node.Start <= address &&
node.End >= endAddress &&
node.Value == permission)
{
return;
}
RangeNode<MemoryPermission> successorNode;
ulong startAddress = address;
for (int index = 0; index < count; index++)
for (; node != null; node = successorNode)
{
var protection = overlaps[index];
successorNode = node.Successor;
var protection = node;
ulong protAddress = protection.Start;
ulong protEndAddress = protection.End;
@@ -610,17 +622,22 @@ namespace Ryujinx.Memory.WindowsShared
{
if (startAddress > protAddress)
{
_protections.Add(protAddress, startAddress, protPermission);
_protections.Add(new RangeNode<MemoryPermission>(protAddress, startAddress, protPermission));
}
if (endAddress < protEndAddress)
{
_protections.Add(endAddress, protEndAddress, protPermission);
_protections.Add(new RangeNode<MemoryPermission>(endAddress, protEndAddress, protPermission));
}
}
if (node.End >= endAddress)
{
break;
}
}
_protections.Add(startAddress, endAddress, permission);
_protections.Add(new RangeNode<MemoryPermission>(startAddress, endAddress, permission));
}
}
@@ -632,16 +649,16 @@ namespace Ryujinx.Memory.WindowsShared
private void RemoveProtection(ulong address, ulong size)
{
ulong endAddress = address + size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
int count;
lock (_protections)
{
count = _protections.Get(address, endAddress, ref overlaps);
RangeNode<MemoryPermission> node = _protections.GetNodeByKey(address);
RangeNode<MemoryPermission> successorNode;
for (int index = 0; index < count; index++)
for (; node != null; node = successorNode)
{
var protection = overlaps[index];
successorNode = node.Successor;
var protection = node;
ulong protAddress = protection.Start;
ulong protEndAddress = protection.End;
@@ -651,12 +668,17 @@ namespace Ryujinx.Memory.WindowsShared
if (address > protAddress)
{
_protections.Add(protAddress, address, protPermission);
_protections.Add(new RangeNode<MemoryPermission>(protAddress, address, protPermission));
}
if (endAddress < protEndAddress)
{
_protections.Add(endAddress, protEndAddress, protPermission);
_protections.Add(new RangeNode<MemoryPermission>(endAddress, protEndAddress, protPermission));
}
if (node.End >= endAddress)
{
break;
}
}
}
@@ -670,12 +692,12 @@ namespace Ryujinx.Memory.WindowsShared
private void RestoreRangeProtection(ulong address, ulong size)
{
ulong endAddress = address + size;
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
var overlaps = new RangeNode<MemoryPermission>[InitialOverlapsSize];
int count;
lock (_protections)
{
count = _protections.Get(address, endAddress, ref overlaps);
count = _protections.GetNodes(address, endAddress, ref overlaps);
}
ulong startAddress = address;
@@ -684,6 +706,12 @@ namespace Ryujinx.Memory.WindowsShared
{
var protection = overlaps[index];
// If protection is R/W we don't need to reprotect as views are initially mapped as R/W.
if (protection.Value == MemoryPermission.ReadAndWrite)
{
continue;
}
ulong protAddress = protection.Start;
ulong protEndAddress = protection.End;