2018-06-09 00:15:56 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-12-12 01:48:54 +00:00
|
|
|
using System.Collections.Concurrent;
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2018-09-08 17:51:50 +00:00
|
|
|
namespace Ryujinx.Graphics.Memory
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
|
|
|
class NvGpuVmmCache
|
|
|
|
{
|
2018-12-12 01:48:54 +00:00
|
|
|
private const int PageBits = MemoryManager.PageBits;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
private const long PageSize = MemoryManager.PageSize;
|
|
|
|
private const long PageMask = MemoryManager.PageMask;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private ConcurrentDictionary<long, int>[] _cachedPages;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
private MemoryManager _memory;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
public NvGpuVmmCache(MemoryManager memory)
|
|
|
|
{
|
|
|
|
_memory = memory;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_cachedPages = new ConcurrentDictionary<long, int>[1 << 20];
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
public bool IsRegionModified(long position, long size, NvGpuBufferType bufferType)
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2019-02-24 07:24:35 +00:00
|
|
|
long va = position;
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
long pa = _memory.GetPhysicalAddress(va);
|
2018-06-09 00:15:56 +00:00
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
long endAddr = (va + size + PageMask) & ~PageMask;
|
2018-07-29 04:39:15 +00:00
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
long addrTruncated = va & ~PageMask;
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
bool modified = _memory.IsRegionModified(addrTruncated, endAddr - addrTruncated);
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
int newBuffMask = 1 << (int)bufferType;
|
2018-07-29 04:39:15 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
long cachedPagesCount = 0;
|
2018-07-29 04:39:15 +00:00
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
while (va < endAddr)
|
2018-07-29 04:39:15 +00:00
|
|
|
{
|
2019-02-24 07:24:35 +00:00
|
|
|
long page = _memory.GetPhysicalAddress(va) >> PageBits;
|
2018-07-29 04:39:15 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ConcurrentDictionary<long, int> dictionary = _cachedPages[page];
|
2018-07-29 04:39:15 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
if (dictionary == null)
|
2018-07-29 04:39:15 +00:00
|
|
|
{
|
2018-12-12 01:48:54 +00:00
|
|
|
dictionary = new ConcurrentDictionary<long, int>();
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_cachedPages[page] = dictionary;
|
2018-07-29 04:39:15 +00:00
|
|
|
}
|
2019-02-24 07:24:35 +00:00
|
|
|
else if (modified)
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_cachedPages[page].Clear();
|
2019-02-24 07:24:35 +00:00
|
|
|
}
|
2018-11-17 04:01:31 +00:00
|
|
|
|
2018-12-12 01:48:54 +00:00
|
|
|
if (dictionary.TryGetValue(pa, out int currBuffMask))
|
2018-07-29 04:39:15 +00:00
|
|
|
{
|
2018-12-12 01:48:54 +00:00
|
|
|
if ((currBuffMask & newBuffMask) != 0)
|
|
|
|
{
|
|
|
|
cachedPagesCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dictionary[pa] |= newBuffMask;
|
|
|
|
}
|
2018-07-29 04:39:15 +00:00
|
|
|
}
|
2018-12-12 01:48:54 +00:00
|
|
|
else
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2018-12-12 01:48:54 +00:00
|
|
|
dictionary[pa] = newBuffMask;
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
va += PageSize;
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:24:35 +00:00
|
|
|
return cachedPagesCount != (endAddr - addrTruncated) >> PageBits;
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|