2019-10-13 06:02:07 +00:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.Engine;
|
2020-07-24 02:53:25 +00:00
|
|
|
using Ryujinx.Graphics.Gpu.Engine.GPFifo;
|
2019-10-13 06:02:07 +00:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2021-06-29 17:32:02 +00:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader;
|
2020-04-19 01:25:57 +00:00
|
|
|
using Ryujinx.Graphics.Gpu.Synchronization;
|
2019-10-13 06:02:07 +00:00
|
|
|
using System;
|
2021-06-29 17:32:02 +00:00
|
|
|
using System.Collections.Concurrent;
|
2021-01-17 20:08:06 +00:00
|
|
|
using System.Collections.Generic;
|
2020-11-12 23:15:34 +00:00
|
|
|
using System.Threading;
|
2019-10-13 06:02:07 +00:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
{
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// GPU emulation context.
|
|
|
|
/// </summary>
|
2019-12-31 22:09:49 +00:00
|
|
|
public sealed class GpuContext : IDisposable
|
2019-10-13 06:02:07 +00:00
|
|
|
{
|
2020-11-12 23:15:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Event signaled when the host emulation context is ready to be used by the gpu context.
|
|
|
|
/// </summary>
|
|
|
|
public ManualResetEvent HostInitalized { get; }
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Host renderer.
|
|
|
|
/// </summary>
|
2019-10-13 06:02:07 +00:00
|
|
|
public IRenderer Renderer { get; }
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// GPU engine methods processing.
|
|
|
|
/// </summary>
|
2019-10-13 06:02:07 +00:00
|
|
|
internal Methods Methods { get; }
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
2020-07-24 02:53:25 +00:00
|
|
|
/// GPU General Purpose FIFO queue.
|
2019-12-31 20:08:20 +00:00
|
|
|
/// </summary>
|
2020-07-24 02:53:25 +00:00
|
|
|
public GPFifoDevice GPFifo { get; }
|
2019-10-13 06:02:07 +00:00
|
|
|
|
2020-04-19 01:25:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// GPU synchronization manager.
|
|
|
|
/// </summary>
|
|
|
|
public SynchronizationManager Synchronization { get; }
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Presentation window.
|
|
|
|
/// </summary>
|
2019-11-24 02:24:03 +00:00
|
|
|
public Window Window { get; }
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Internal sequence number, used to avoid needless resource data updates
|
|
|
|
/// in the middle of a command buffer before synchronizations.
|
|
|
|
/// </summary>
|
2019-10-13 06:02:07 +00:00
|
|
|
internal int SequenceNumber { get; private set; }
|
|
|
|
|
2021-01-17 20:08:06 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Internal sync number, used to denote points at which host synchronization can be requested.
|
|
|
|
/// </summary>
|
|
|
|
internal ulong SyncNumber { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Actions to be performed when a CPU waiting sync point is triggered.
|
|
|
|
/// If there are more than 0 items when this happens, a host sync object will be generated for the given <see cref="SyncNumber"/>,
|
|
|
|
/// and the SyncNumber will be incremented.
|
|
|
|
/// </summary>
|
|
|
|
internal List<Action> SyncActions { get; }
|
|
|
|
|
2021-06-23 23:51:41 +00:00
|
|
|
/// <summary>
|
2021-06-29 17:32:02 +00:00
|
|
|
/// Queue with deferred actions that must run on the render thread.
|
2021-06-23 23:51:41 +00:00
|
|
|
/// </summary>
|
2021-06-29 17:32:02 +00:00
|
|
|
internal Queue<Action> DeferredActions { get; }
|
2021-06-23 23:51:41 +00:00
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Registry with physical memories that can be used with this GPU context, keyed by owner process ID.
|
|
|
|
/// </summary>
|
|
|
|
internal ConcurrentDictionary<long, PhysicalMemory> PhysicalMemoryRegistry { get; }
|
2019-10-13 06:02:07 +00:00
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Host hardware capabilities.
|
|
|
|
/// </summary>
|
2019-10-13 06:02:07 +00:00
|
|
|
internal Capabilities Capabilities => _caps.Value;
|
|
|
|
|
2021-03-03 00:39:36 +00:00
|
|
|
/// <summary>
|
2021-03-22 18:40:07 +00:00
|
|
|
/// Event for signalling shader cache loading progress.
|
2021-03-03 00:39:36 +00:00
|
|
|
/// </summary>
|
2021-06-29 17:32:02 +00:00
|
|
|
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
|
|
|
|
|
|
|
private readonly Lazy<Capabilities> _caps;
|
2021-03-03 00:39:36 +00:00
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU emulation context.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
2019-10-13 06:02:07 +00:00
|
|
|
public GpuContext(IRenderer renderer)
|
|
|
|
{
|
|
|
|
Renderer = renderer;
|
|
|
|
|
|
|
|
Methods = new Methods(this);
|
|
|
|
|
2020-07-24 02:53:25 +00:00
|
|
|
GPFifo = new GPFifoDevice(this);
|
2019-10-13 06:02:07 +00:00
|
|
|
|
2020-04-19 01:25:57 +00:00
|
|
|
Synchronization = new SynchronizationManager();
|
|
|
|
|
2019-11-24 02:24:03 +00:00
|
|
|
Window = new Window(this);
|
|
|
|
|
2020-11-12 23:15:34 +00:00
|
|
|
HostInitalized = new ManualResetEvent(false);
|
2021-01-17 20:08:06 +00:00
|
|
|
|
|
|
|
SyncActions = new List<Action>();
|
2021-06-23 23:51:41 +00:00
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
DeferredActions = new Queue<Action>();
|
|
|
|
|
|
|
|
PhysicalMemoryRegistry = new ConcurrentDictionary<long, PhysicalMemory>();
|
|
|
|
|
|
|
|
_caps = new Lazy<Capabilities>(Renderer.GetCapabilities);
|
2021-06-23 23:51:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new GPU channel.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The GPU channel</returns>
|
2021-06-23 23:51:41 +00:00
|
|
|
public GpuChannel CreateChannel()
|
|
|
|
{
|
|
|
|
return new GpuChannel(this);
|
2020-11-12 23:15:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new GPU memory manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pid">ID of the process that owns the memory manager</param>
|
|
|
|
/// <returns>The memory manager</returns>
|
|
|
|
/// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
|
|
|
|
public MemoryManager CreateMemoryManager(long pid)
|
|
|
|
{
|
|
|
|
if (!PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MemoryManager(physicalMemory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Registers virtual memory used by a process for GPU memory access, caching and read/write tracking.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pid">ID of the process that owns <paramref name="cpuMemory"/></param>
|
|
|
|
/// <param name="cpuMemory">Virtual memory owned by the process</param>
|
|
|
|
/// <exception cref="ArgumentException">Thrown if <paramref name="pid"/> was already registered</exception>
|
|
|
|
public void RegisterProcess(long pid, Cpu.IVirtualMemoryManagerTracked cpuMemory)
|
|
|
|
{
|
|
|
|
var physicalMemory = new PhysicalMemory(this, cpuMemory);
|
|
|
|
if (!PhysicalMemoryRegistry.TryAdd(pid, physicalMemory))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("The PID was already registered", nameof(pid));
|
|
|
|
}
|
|
|
|
|
|
|
|
physicalMemory.ShaderCache.ShaderCacheStateChanged += ShaderCacheStateUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unregisters a process, indicating that its memory will no longer be used, and that caches can be freed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pid">ID of the process</param>
|
|
|
|
public void UnregisterProcess(long pid)
|
|
|
|
{
|
|
|
|
if (PhysicalMemoryRegistry.TryRemove(pid, out var physicalMemory))
|
|
|
|
{
|
|
|
|
physicalMemory.ShaderCache.ShaderCacheStateChanged -= ShaderCacheStateUpdate;
|
|
|
|
physicalMemory.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Shader cache state update handler.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current state of the shader cache load process</param>
|
|
|
|
/// <param name="current">Number of the current shader being processed</param>
|
|
|
|
/// <param name="total">Total number of shaders to process</param>
|
|
|
|
private void ShaderCacheStateUpdate(ShaderCacheState state, int current, int total)
|
|
|
|
{
|
|
|
|
ShaderCacheStateChanged?.Invoke(state, current, total);
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:15:34 +00:00
|
|
|
/// <summary>
|
2020-11-17 21:40:19 +00:00
|
|
|
/// Initialize the GPU shader cache.
|
2020-11-12 23:15:34 +00:00
|
|
|
/// </summary>
|
2020-11-17 21:40:19 +00:00
|
|
|
public void InitializeShaderCache()
|
2020-11-12 23:15:34 +00:00
|
|
|
{
|
|
|
|
HostInitalized.WaitOne();
|
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
|
|
|
|
{
|
|
|
|
physicalMemory.ShaderCache.Initialize();
|
|
|
|
}
|
2019-10-13 06:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Advances internal sequence number.
|
|
|
|
/// This forces the update of any modified GPU resource.
|
|
|
|
/// </summary>
|
2019-10-13 06:02:07 +00:00
|
|
|
internal void AdvanceSequence()
|
|
|
|
{
|
|
|
|
SequenceNumber++;
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:08:06 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Registers an action to be performed the next time a syncpoint is incremented.
|
|
|
|
/// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="action">The action to be performed on sync object creation</param>
|
|
|
|
public void RegisterSyncAction(Action action)
|
|
|
|
{
|
|
|
|
SyncActions.Add(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a host sync object if there are any pending sync actions. The actions will then be called.
|
|
|
|
/// If no actions are present, a host sync object is not created.
|
|
|
|
/// </summary>
|
|
|
|
public void CreateHostSyncIfNeeded()
|
|
|
|
{
|
|
|
|
if (SyncActions.Count > 0)
|
|
|
|
{
|
|
|
|
Renderer.CreateSync(SyncNumber);
|
|
|
|
|
|
|
|
SyncNumber++;
|
|
|
|
|
|
|
|
foreach (Action action in SyncActions)
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncActions.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 23:51:41 +00:00
|
|
|
/// <summary>
|
2021-06-29 17:32:02 +00:00
|
|
|
/// Performs deferred actions.
|
|
|
|
/// This is useful for actions that must run on the render thread, such as resource disposal.
|
2021-06-23 23:51:41 +00:00
|
|
|
/// </summary>
|
2021-06-29 17:32:02 +00:00
|
|
|
internal void RunDeferredActions()
|
2021-06-23 23:51:41 +00:00
|
|
|
{
|
2021-06-29 17:32:02 +00:00
|
|
|
while (DeferredActions.TryDequeue(out Action action))
|
2021-06-23 23:51:41 +00:00
|
|
|
{
|
2021-06-29 17:32:02 +00:00
|
|
|
action();
|
2021-06-23 23:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 22:09:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Disposes all GPU resources currently cached.
|
|
|
|
/// It's an error to push any GPU commands after disposal.
|
|
|
|
/// Additionally, the GPU commands FIFO must be empty for disposal,
|
|
|
|
/// and processing of all commands must have finished.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-01-21 22:23:11 +00:00
|
|
|
Renderer.Dispose();
|
2020-07-24 02:53:25 +00:00
|
|
|
GPFifo.Dispose();
|
2020-11-12 23:15:34 +00:00
|
|
|
HostInitalized.Dispose();
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 20:52:44 +00:00
|
|
|
|
2021-06-29 17:32:02 +00:00
|
|
|
// Has to be disposed before processing deferred actions, as it will produce some.
|
|
|
|
foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
|
|
|
|
{
|
|
|
|
physicalMemory.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalMemoryRegistry.Clear();
|
|
|
|
|
|
|
|
RunDeferredActions();
|
2019-12-31 22:09:49 +00:00
|
|
|
}
|
2019-10-13 06:02:07 +00:00
|
|
|
}
|
|
|
|
}
|