aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Gpu/Memory
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-06-23 21:39:25 -0300
committerGitHub <noreply@github.com>2018-06-23 21:39:25 -0300
commite7559f128f99058774a8d53aa45213b51c085e76 (patch)
tree42d367bfb4248b19cf2ce55439d24437bbbd2ccb /Ryujinx.HLE/Gpu/Memory
parent69697957e6406235d512a89d9144c160fe7efbfd (diff)
Small OpenGL Renderer refactoring (#177)
* Call OpenGL functions directly, remove the pfifo thread, some refactoring * Fix PerformanceStatistics calculating the wrong host fps, remove wait event on PFIFO as this wasn't exactly was causing the freezes (may replace with an exception later) * Organized the Gpu folder a bit more, renamed a few things, address PR feedback * Make PerformanceStatistics thread safe * Remove unused constant * Use unlimited update rate for better pref
Diffstat (limited to 'Ryujinx.HLE/Gpu/Memory')
-rw-r--r--Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs9
-rw-r--r--Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs23
-rw-r--r--Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs101
-rw-r--r--Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs410
-rw-r--r--Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs209
5 files changed, 752 insertions, 0 deletions
diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs
new file mode 100644
index 00000000..7474aa33
--- /dev/null
+++ b/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs
@@ -0,0 +1,9 @@
+namespace Ryujinx.HLE.Gpu.Memory
+{
+ enum NvGpuBufferType
+ {
+ Index,
+ Vertex,
+ Texture
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs
new file mode 100644
index 00000000..aba89e3c
--- /dev/null
+++ b/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.ObjectModel;
+
+namespace Ryujinx.HLE.Gpu.Memory
+{
+ struct NvGpuPBEntry
+ {
+ public int Method { get; private set; }
+
+ public int SubChannel { get; private set; }
+
+ private int[] m_Arguments;
+
+ public ReadOnlyCollection<int> Arguments => Array.AsReadOnly(m_Arguments);
+
+ public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments)
+ {
+ this.Method = Method;
+ this.SubChannel = SubChannel;
+ this.m_Arguments = Arguments;
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs
new file mode 100644
index 00000000..6121b3e6
--- /dev/null
+++ b/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs
@@ -0,0 +1,101 @@
+using System.Collections.Generic;
+using System.IO;
+
+namespace Ryujinx.HLE.Gpu.Memory
+{
+ static class NvGpuPushBuffer
+ {
+ private enum SubmissionMode
+ {
+ Incrementing = 1,
+ NonIncrementing = 3,
+ Immediate = 4,
+ IncrementOnce = 5
+ }
+
+ public static NvGpuPBEntry[] Decode(byte[] Data)
+ {
+ using (MemoryStream MS = new MemoryStream(Data))
+ {
+ BinaryReader Reader = new BinaryReader(MS);
+
+ List<NvGpuPBEntry> PushBuffer = new List<NvGpuPBEntry>();
+
+ bool CanRead() => MS.Position + 4 <= MS.Length;
+
+ while (CanRead())
+ {
+ int Packed = Reader.ReadInt32();
+
+ int Meth = (Packed >> 0) & 0x1fff;
+ int SubC = (Packed >> 13) & 7;
+ int Args = (Packed >> 16) & 0x1fff;
+ int Mode = (Packed >> 29) & 7;
+
+ switch ((SubmissionMode)Mode)
+ {
+ case SubmissionMode.Incrementing:
+ {
+ for (int Index = 0; Index < Args && CanRead(); Index++, Meth++)
+ {
+ PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32()));
+ }
+
+ break;
+ }
+
+ case SubmissionMode.NonIncrementing:
+ {
+ int[] Arguments = new int[Args];
+
+ for (int Index = 0; Index < Arguments.Length; Index++)
+ {
+ if (!CanRead())
+ {
+ break;
+ }
+
+ Arguments[Index] = Reader.ReadInt32();
+ }
+
+ PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments));
+
+ break;
+ }
+
+ case SubmissionMode.Immediate:
+ {
+ PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args));
+
+ break;
+ }
+
+ case SubmissionMode.IncrementOnce:
+ {
+ if (CanRead())
+ {
+ PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32()));
+ }
+
+ if (CanRead() && Args > 1)
+ {
+ int[] Arguments = new int[Args - 1];
+
+ for (int Index = 0; Index < Arguments.Length && CanRead(); Index++)
+ {
+ Arguments[Index] = Reader.ReadInt32();
+ }
+
+ PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments));
+ }
+
+ break;
+ }
+ }
+ }
+
+ return PushBuffer.ToArray();
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs
new file mode 100644
index 00000000..36f6406a
--- /dev/null
+++ b/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs
@@ -0,0 +1,410 @@
+using ChocolArm64.Memory;
+using Ryujinx.Graphics.Gal;
+using System.Collections.Concurrent;
+
+namespace Ryujinx.HLE.Gpu.Memory
+{
+ class NvGpuVmm : IAMemory, IGalMemory
+ {
+ public const long AddrSize = 1L << 40;
+
+ private const int PTLvl0Bits = 14;
+ private const int PTLvl1Bits = 14;
+ private const int PTPageBits = 12;
+
+ private const int PTLvl0Size = 1 << PTLvl0Bits;
+ private const int PTLvl1Size = 1 << PTLvl1Bits;
+ public const int PageSize = 1 << PTPageBits;
+
+ private const int PTLvl0Mask = PTLvl0Size - 1;
+ private const int PTLvl1Mask = PTLvl1Size - 1;
+ public const int PageMask = PageSize - 1;
+
+ private const int PTLvl0Bit = PTPageBits + PTLvl1Bits;
+ private const int PTLvl1Bit = PTPageBits;
+
+ public AMemory Memory { get; private set; }
+
+ private struct MappedMemory
+ {
+ public long Size;
+
+ public MappedMemory(long Size)
+ {
+ this.Size = Size;
+ }
+ }
+
+ private ConcurrentDictionary<long, MappedMemory> Maps;
+
+ private NvGpuVmmCache Cache;
+
+ private const long PteUnmapped = -1;
+ private const long PteReserved = -2;
+
+ private long[][] PageTable;
+
+ public NvGpuVmm(AMemory Memory)
+ {
+ this.Memory = Memory;
+
+ Maps = new ConcurrentDictionary<long, MappedMemory>();
+
+ Cache = new NvGpuVmmCache();
+
+ PageTable = new long[PTLvl0Size][];
+ }
+
+ public long Map(long PA, long VA, long Size)
+ {
+ lock (PageTable)
+ {
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ if (GetPte(VA + Offset) != PteReserved)
+ {
+ return Map(PA, Size);
+ }
+ }
+
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ SetPte(VA + Offset, PA + Offset);
+ }
+ }
+
+ return VA;
+ }
+
+ public long Map(long PA, long Size)
+ {
+ lock (PageTable)
+ {
+ long VA = GetFreePosition(Size);
+
+ if (VA != -1)
+ {
+ MappedMemory Map = new MappedMemory(Size);
+
+ Maps.AddOrUpdate(VA, Map, (Key, Old) => Map);
+
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ SetPte(VA + Offset, PA + Offset);
+ }
+ }
+
+ return VA;
+ }
+ }
+
+ public bool Unmap(long VA)
+ {
+ if (Maps.TryRemove(VA, out MappedMemory Map))
+ {
+ Free(VA, Map.Size);
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public long Reserve(long VA, long Size, long Align)
+ {
+ lock (PageTable)
+ {
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ if (IsPageInUse(VA + Offset))
+ {
+ return Reserve(Size, Align);
+ }
+ }
+
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ SetPte(VA + Offset, PteReserved);
+ }
+ }
+
+ return VA;
+ }
+
+ public long Reserve(long Size, long Align)
+ {
+ lock (PageTable)
+ {
+ long Position = GetFreePosition(Size, Align);
+
+ if (Position != -1)
+ {
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ SetPte(Position + Offset, PteReserved);
+ }
+ }
+
+ return Position;
+ }
+ }
+
+ public void Free(long VA, long Size)
+ {
+ lock (PageTable)
+ {
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ SetPte(VA + Offset, PteUnmapped);
+ }
+ }
+ }
+
+ private long GetFreePosition(long Size, long Align = 1)
+ {
+ long Position = 0;
+ long FreeSize = 0;
+
+ if (Align < 1)
+ {
+ Align = 1;
+ }
+
+ Align = (Align + PageMask) & ~PageMask;
+
+ while (Position + FreeSize < AddrSize)
+ {
+ if (!IsPageInUse(Position + FreeSize))
+ {
+ FreeSize += PageSize;
+
+ if (FreeSize >= Size)
+ {
+ return Position;
+ }
+ }
+ else
+ {
+ Position += FreeSize + PageSize;
+ FreeSize = 0;
+
+ long Remainder = Position % Align;
+
+ if (Remainder != 0)
+ {
+ Position = (Position - Remainder) + Align;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ public long GetPhysicalAddress(long VA)
+ {
+ long BasePos = GetPte(VA);
+
+ if (BasePos < 0)
+ {
+ return -1;
+ }
+
+ return BasePos + (VA & PageMask);
+ }
+
+ public bool IsRegionFree(long VA, long Size)
+ {
+ for (long Offset = 0; Offset < Size; Offset += PageSize)
+ {
+ if (IsPageInUse(VA + Offset))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private bool IsPageInUse(long VA)
+ {
+ if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0)
+ {
+ return false;
+ }
+
+ long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask;
+ long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask;
+
+ if (PageTable[L0] == null)
+ {
+ return false;
+ }
+
+ return PageTable[L0][L1] != PteUnmapped;
+ }
+
+ private long GetPte(long Position)
+ {
+ long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
+ long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
+
+ if (PageTable[L0] == null)
+ {
+ return -1;
+ }
+
+ return PageTable[L0][L1];
+ }
+
+ private void SetPte(long Position, long TgtAddr)
+ {
+ long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
+ long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
+
+ if (PageTable[L0] == null)
+ {
+ PageTable[L0] = new long[PTLvl1Size];
+
+ for (int Index = 0; Index < PTLvl1Size; Index++)
+ {
+ PageTable[L0][Index] = PteUnmapped;
+ }
+ }
+
+ PageTable[L0][L1] = TgtAddr;
+ }
+
+ public bool IsRegionModified(long Position, long Size, NvGpuBufferType BufferType)
+ {
+ long PA = GetPhysicalAddress(Position);
+
+ return Cache.IsRegionModified(Memory, BufferType, Position, PA, Size);
+ }
+
+ public byte ReadByte(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadByte(Position);
+ }
+
+ public ushort ReadUInt16(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadUInt16(Position);
+ }
+
+ public uint ReadUInt32(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadUInt32(Position);
+ }
+
+ public ulong ReadUInt64(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadUInt64(Position);
+ }
+
+ public sbyte ReadSByte(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadSByte(Position);
+ }
+
+ public short ReadInt16(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadInt16(Position);
+ }
+
+ public int ReadInt32(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadInt32(Position);
+ }
+
+ public long ReadInt64(long Position)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadInt64(Position);
+ }
+
+ public byte[] ReadBytes(long Position, long Size)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ return Memory.ReadBytes(Position, Size);
+ }
+
+ public void WriteByte(long Position, byte Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteByte(Position, Value);
+ }
+
+ public void WriteUInt16(long Position, ushort Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteUInt16(Position, Value);
+ }
+
+ public void WriteUInt32(long Position, uint Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteUInt32(Position, Value);
+ }
+
+ public void WriteUInt64(long Position, ulong Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteUInt64(Position, Value);
+ }
+
+ public void WriteSByte(long Position, sbyte Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteSByte(Position, Value);
+ }
+
+ public void WriteInt16(long Position, short Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteInt16(Position, Value);
+ }
+
+ public void WriteInt32(long Position, int Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteInt32(Position, Value);
+ }
+
+ public void WriteInt64(long Position, long Value)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteInt64(Position, Value);
+ }
+
+ public void WriteBytes(long Position, byte[] Data)
+ {
+ Position = GetPhysicalAddress(Position);
+
+ Memory.WriteBytes(Position, Data);
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs
new file mode 100644
index 00000000..c7108f00
--- /dev/null
+++ b/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs
@@ -0,0 +1,209 @@
+using ChocolArm64.Memory;
+using System;
+using System.Collections.Generic;
+
+namespace Ryujinx.HLE.Gpu.Memory
+{
+ class NvGpuVmmCache
+ {
+ private const int MaxCpCount = 10000;
+ private const int MaxCpTimeDelta = 60000;
+
+ private class CachedPage
+ {
+ private List<(long Start, long End)> Regions;
+
+ public LinkedListNode<long> Node { get; set; }
+
+ public int Count => Regions.Count;
+
+ public int Timestamp { get; private set; }
+
+ public long PABase { get; private set; }
+
+ public NvGpuBufferType BufferType { get; private set; }
+
+ public CachedPage(long PABase, NvGpuBufferType BufferType)
+ {
+ this.PABase = PABase;
+ this.BufferType = BufferType;
+
+ Regions = new List<(long, long)>();
+ }
+
+ public bool AddRange(long Start, long End)
+ {
+ for (int Index = 0; Index < Regions.Count; Index++)
+ {
+ (long RgStart, long RgEnd) = Regions[Index];
+
+ if (Start >= RgStart && End <= RgEnd)
+ {
+ return false;
+ }
+
+ if (Start <= RgEnd && RgStart <= End)
+ {
+ long MinStart = Math.Min(RgStart, Start);
+ long MaxEnd = Math.Max(RgEnd, End);
+
+ Regions[Index] = (MinStart, MaxEnd);
+
+ Timestamp = Environment.TickCount;
+
+ return true;
+ }
+ }
+
+ Regions.Add((Start, End));
+
+ Timestamp = Environment.TickCount;
+
+ return true;
+ }
+ }
+
+ private Dictionary<long, CachedPage> Cache;
+
+ private LinkedList<long> SortedCache;
+
+ private int CpCount;
+
+ public NvGpuVmmCache()
+ {
+ Cache = new Dictionary<long, CachedPage>();
+
+ SortedCache = new LinkedList<long>();
+ }
+
+ public bool IsRegionModified(
+ AMemory Memory,
+ NvGpuBufferType BufferType,
+ long VA,
+ long PA,
+ long Size)
+ {
+ ClearCachedPagesIfNeeded();
+
+ long PageSize = Memory.GetHostPageSize();
+
+ long Mask = PageSize - 1;
+
+ long VAEnd = VA + Size;
+ long PAEnd = PA + Size;
+
+ bool RegMod = false;
+
+ while (VA < VAEnd)
+ {
+ long Key = VA & ~Mask;
+ long PABase = PA & ~Mask;
+
+ long VAPgEnd = Math.Min((VA + PageSize) & ~Mask, VAEnd);
+ long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd);
+
+ bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
+
+ bool PgReset = false;
+
+ if (!IsCached)
+ {
+ Cp = new CachedPage(PABase, BufferType);
+
+ Cache.Add(Key, Cp);
+ }
+ else
+ {
+ CpCount -= Cp.Count;
+
+ SortedCache.Remove(Cp.Node);
+
+ if (Cp.PABase != PABase ||
+ Cp.BufferType != BufferType)
+ {
+ PgReset = true;
+ }
+ }
+
+ PgReset |= Memory.IsRegionModified(PA, PAPgEnd - PA) && IsCached;
+
+ if (PgReset)
+ {
+ Cp = new CachedPage(PABase, BufferType);
+
+ Cache[Key] = Cp;
+ }
+
+ Cp.Node = SortedCache.AddLast(Key);
+
+ RegMod |= Cp.AddRange(VA, VAPgEnd);
+
+ CpCount += Cp.Count;
+
+ VA = VAPgEnd;
+ PA = PAPgEnd;
+ }
+
+ return RegMod;
+ }
+
+ private void ClearCachedPagesIfNeeded()
+ {
+ if (CpCount <= MaxCpCount)
+ {
+ return;
+ }
+
+ int Timestamp = Environment.TickCount;
+
+ int TimeDelta;
+
+ do
+ {
+ if (!TryPopOldestCachedPageKey(Timestamp, out long Key))
+ {
+ break;
+ }
+
+ CachedPage Cp = Cache[Key];
+
+ Cache.Remove(Key);
+
+ CpCount -= Cp.Count;
+
+ TimeDelta = RingDelta(Cp.Timestamp, Timestamp);
+ }
+ while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta);
+ }
+
+ private bool TryPopOldestCachedPageKey(int Timestamp, out long Key)
+ {
+ LinkedListNode<long> Node = SortedCache.First;
+
+ if (Node == null)
+ {
+ Key = 0;
+
+ return false;
+ }
+
+ SortedCache.Remove(Node);
+
+ Key = Node.Value;
+
+ return true;
+ }
+
+ private int RingDelta(int Old, int New)
+ {
+ if ((uint)New < (uint)Old)
+ {
+ return New + (~Old + 1);
+ }
+ else
+ {
+ return New - Old;
+ }
+ }
+ }
+} \ No newline at end of file