2022-01-27 22:50:32 +01:00
|
|
|
using Ryujinx.Common;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-07-24 04:53:25 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Engine.GPFifo;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2021-06-29 19:32:02 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader;
|
2020-04-19 03:25:57 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Synchronization;
|
2019-10-13 08:02:07 +02:00
|
|
|
using System;
|
2021-06-29 19:32:02 +02:00
|
|
|
using System.Collections.Concurrent;
|
2021-01-17 21:08:06 +01:00
|
|
|
using System.Collections.Generic;
|
2020-11-13 00:15:34 +01:00
|
|
|
using System.Threading;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
{
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU emulation context.
|
|
|
|
/// </summary>
|
2019-12-31 23:09:49 +01:00
|
|
|
public sealed class GpuContext : IDisposable
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2022-01-27 22:50:32 +01:00
|
|
|
private const int NsToTicksFractionNumerator = 384;
|
|
|
|
private const int NsToTicksFractionDenominator = 625;
|
|
|
|
|
2020-11-13 00:15:34 +01: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 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Host renderer.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public IRenderer Renderer { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
2020-07-24 04:53:25 +02:00
|
|
|
/// GPU General Purpose FIFO queue.
|
2019-12-31 21:08:20 +01:00
|
|
|
/// </summary>
|
2020-07-24 04:53:25 +02:00
|
|
|
public GPFifoDevice GPFifo { get; }
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
/// <summary>
|
|
|
|
/// GPU synchronization manager.
|
|
|
|
/// </summary>
|
|
|
|
public SynchronizationManager Synchronization { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Presentation window.
|
|
|
|
/// </summary>
|
2019-11-24 03:24:03 +01:00
|
|
|
public Window Window { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01: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 08:02:07 +02:00
|
|
|
internal int SequenceNumber { get; private set; }
|
|
|
|
|
2021-01-17 21:08:06 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Internal sync number, used to denote points at which host synchronization can be requested.
|
|
|
|
/// </summary>
|
|
|
|
internal ulong SyncNumber { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-09 17:28:48 +01:00
|
|
|
/// Actions to be performed when a CPU waiting syncpoint or barrier is triggered.
|
2021-01-17 21:08:06 +01:00
|
|
|
/// 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; }
|
|
|
|
|
2022-01-09 17:28:48 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Actions to be performed when a CPU waiting syncpoint 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> SyncpointActions { get; }
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
/// <summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// Queue with deferred actions that must run on the render thread.
|
2021-06-24 01:51:41 +02:00
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
internal Queue<Action> DeferredActions { get; }
|
2021-06-24 01:51:41 +02:00
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Registry with physical memories that can be used with this GPU context, keyed by owner process ID.
|
|
|
|
/// </summary>
|
2022-02-09 21:18:07 +01:00
|
|
|
internal ConcurrentDictionary<ulong, PhysicalMemory> PhysicalMemoryRegistry { get; }
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Host hardware capabilities.
|
|
|
|
/// </summary>
|
2022-08-20 21:20:27 +02:00
|
|
|
internal Capabilities Capabilities;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-03-03 01:39:36 +01:00
|
|
|
/// <summary>
|
2021-03-22 19:40:07 +01:00
|
|
|
/// Event for signalling shader cache loading progress.
|
2021-03-03 01:39:36 +01:00
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
|
|
|
|
2022-01-09 17:28:48 +01:00
|
|
|
private Thread _gpuThread;
|
2021-03-03 01:39:36 +01:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU emulation context.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public GpuContext(IRenderer renderer)
|
|
|
|
{
|
|
|
|
Renderer = renderer;
|
|
|
|
|
2020-07-24 04:53:25 +02:00
|
|
|
GPFifo = new GPFifoDevice(this);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
Synchronization = new SynchronizationManager();
|
|
|
|
|
2019-11-24 03:24:03 +01:00
|
|
|
Window = new Window(this);
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
HostInitalized = new ManualResetEvent(false);
|
2021-01-17 21:08:06 +01:00
|
|
|
|
|
|
|
SyncActions = new List<Action>();
|
2022-01-09 17:28:48 +01:00
|
|
|
SyncpointActions = new List<Action>();
|
2021-06-24 01:51:41 +02:00
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
DeferredActions = new Queue<Action>();
|
|
|
|
|
2022-02-09 21:18:07 +01:00
|
|
|
PhysicalMemoryRegistry = new ConcurrentDictionary<ulong, PhysicalMemory>();
|
2021-06-24 01:51:41 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new GPU channel.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The GPU channel</returns>
|
2021-06-24 01:51:41 +02:00
|
|
|
public GpuChannel CreateChannel()
|
|
|
|
{
|
|
|
|
return new GpuChannel(this);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 19:32:02 +02: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>
|
2022-02-09 21:18:07 +01:00
|
|
|
public MemoryManager CreateMemoryManager(ulong pid)
|
2021-06-29 19:32:02 +02:00
|
|
|
{
|
|
|
|
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>
|
2022-02-09 21:18:07 +01:00
|
|
|
public void RegisterProcess(ulong pid, Cpu.IVirtualMemoryManagerTracked cpuMemory)
|
2021-06-29 19:32:02 +02:00
|
|
|
{
|
|
|
|
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>
|
2022-02-09 21:18:07 +01:00
|
|
|
public void UnregisterProcess(ulong pid)
|
2021-06-29 19:32:02 +02:00
|
|
|
{
|
|
|
|
if (PhysicalMemoryRegistry.TryRemove(pid, out var physicalMemory))
|
|
|
|
{
|
|
|
|
physicalMemory.ShaderCache.ShaderCacheStateChanged -= ShaderCacheStateUpdate;
|
|
|
|
physicalMemory.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:50:32 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Converts a nanoseconds timestamp value to Maxwell time ticks.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The frequency is 614400000 Hz.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="nanoseconds">Timestamp in nanoseconds</param>
|
|
|
|
/// <returns>Maxwell ticks</returns>
|
|
|
|
private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
|
|
|
|
{
|
|
|
|
// We need to divide first to avoid overflows.
|
|
|
|
// We fix up the result later by calculating the difference and adding
|
|
|
|
// that to the result.
|
|
|
|
ulong divided = nanoseconds / NsToTicksFractionDenominator;
|
|
|
|
|
|
|
|
ulong rounded = divided * NsToTicksFractionDenominator;
|
|
|
|
|
|
|
|
ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
|
|
|
|
|
|
|
|
return divided * NsToTicksFractionNumerator + errorBias;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the value of the GPU timer.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The current GPU timestamp</returns>
|
|
|
|
public ulong GetTimestamp()
|
|
|
|
{
|
|
|
|
ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
|
|
|
|
|
|
|
|
if (GraphicsConfig.FastGpuTime)
|
|
|
|
{
|
|
|
|
// Divide by some amount to report time as if operations were performed faster than they really are.
|
|
|
|
// This can prevent some games from switching to a lower resolution because rendering is too slow.
|
|
|
|
ticks /= 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <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-13 00:15:34 +01:00
|
|
|
/// <summary>
|
2020-11-17 22:40:19 +01:00
|
|
|
/// Initialize the GPU shader cache.
|
2020-11-13 00:15:34 +01:00
|
|
|
/// </summary>
|
New shader cache implementation (#3194)
* New shader cache implementation
* Remove some debug code
* Take transform feedback varying count into account
* Create shader cache directory if it does not exist + fragment output map related fixes
* Remove debug code
* Only check texture descriptors if the constant buffer is bound
* Also check CPU VA on GetSpanMapped
* Remove more unused code and move cache related code
* XML docs + remove more unused methods
* Better codegen for TransformFeedbackDescriptor.AsSpan
* Support migration from old cache format, remove more unused code
Shader cache rebuild now also rewrites the shared toc and data files
* Fix migration error with BRX shaders
* Add a limit to the async translation queue
Avoid async translation threads not being able to keep up and the queue growing very large
* Re-create specialization state on recompile
This might be required if a new version of the shader translator requires more or less state, or if there is a bug related to the GPU state access
* Make shader cache more error resilient
* Add some missing XML docs and move GpuAccessor docs to the interface/use inheritdoc
* Address early PR feedback
* Fix rebase
* Remove IRenderer.CompileShader and IShader interface, replace with new ShaderSource struct passed to CreateProgram directly
* Handle some missing exceptions
* Make shader cache purge delete both old and new shader caches
* Register textures on new specialization state
* Translate and compile shaders in forward order (eliminates diffs due to different binding numbers)
* Limit in-flight shader compilation to the maximum number of compilation threads
* Replace ParallelDiskCacheLoader state changed event with a callback function
* Better handling for invalid constant buffer 1 data length
* Do not create the old cache directory structure if the old cache does not exist
* Constant buffer use should be per-stage. This change will invalidate existing new caches (file format version was incremented)
* Replace rectangle texture with just coordinate normalization
* Skip incompatible shaders that are missing texture information, instead of crashing
This is required if we, for example, support new texture instruction to the shader translator, and then they allow access to textures that were not accessed before. In this scenario, the old cache entry is no longer usable
* Fix coordinates normalization on cubemap textures
* Check if title ID is null before combining shader cache path
* More robust constant buffer address validation on spec state
* More robust constant buffer address validation on spec state (2)
* Regenerate shader cache with one stream, rather than one per shader.
* Only create shader cache directory during initialization
* Logging improvements
* Proper shader program disposal
* PR feedback, and add a comment on serialized structs
* XML docs for RegisterTexture
Co-authored-by: riperiperi <rhy3756547@hotmail.com>
2022-04-10 15:49:44 +02:00
|
|
|
public void InitializeShaderCache(CancellationToken cancellationToken)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
|
|
|
HostInitalized.WaitOne();
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
|
|
|
|
{
|
New shader cache implementation (#3194)
* New shader cache implementation
* Remove some debug code
* Take transform feedback varying count into account
* Create shader cache directory if it does not exist + fragment output map related fixes
* Remove debug code
* Only check texture descriptors if the constant buffer is bound
* Also check CPU VA on GetSpanMapped
* Remove more unused code and move cache related code
* XML docs + remove more unused methods
* Better codegen for TransformFeedbackDescriptor.AsSpan
* Support migration from old cache format, remove more unused code
Shader cache rebuild now also rewrites the shared toc and data files
* Fix migration error with BRX shaders
* Add a limit to the async translation queue
Avoid async translation threads not being able to keep up and the queue growing very large
* Re-create specialization state on recompile
This might be required if a new version of the shader translator requires more or less state, or if there is a bug related to the GPU state access
* Make shader cache more error resilient
* Add some missing XML docs and move GpuAccessor docs to the interface/use inheritdoc
* Address early PR feedback
* Fix rebase
* Remove IRenderer.CompileShader and IShader interface, replace with new ShaderSource struct passed to CreateProgram directly
* Handle some missing exceptions
* Make shader cache purge delete both old and new shader caches
* Register textures on new specialization state
* Translate and compile shaders in forward order (eliminates diffs due to different binding numbers)
* Limit in-flight shader compilation to the maximum number of compilation threads
* Replace ParallelDiskCacheLoader state changed event with a callback function
* Better handling for invalid constant buffer 1 data length
* Do not create the old cache directory structure if the old cache does not exist
* Constant buffer use should be per-stage. This change will invalidate existing new caches (file format version was incremented)
* Replace rectangle texture with just coordinate normalization
* Skip incompatible shaders that are missing texture information, instead of crashing
This is required if we, for example, support new texture instruction to the shader translator, and then they allow access to textures that were not accessed before. In this scenario, the old cache entry is no longer usable
* Fix coordinates normalization on cubemap textures
* Check if title ID is null before combining shader cache path
* More robust constant buffer address validation on spec state
* More robust constant buffer address validation on spec state (2)
* Regenerate shader cache with one stream, rather than one per shader.
* Only create shader cache directory during initialization
* Logging improvements
* Proper shader program disposal
* PR feedback, and add a comment on serialized structs
* XML docs for RegisterTexture
Co-authored-by: riperiperi <rhy3756547@hotmail.com>
2022-04-10 15:49:44 +02:00
|
|
|
physicalMemory.ShaderCache.Initialize(cancellationToken);
|
2021-06-29 19:32:02 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 17:28:48 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current thread as the main GPU thread.
|
|
|
|
/// </summary>
|
|
|
|
public void SetGpuThread()
|
|
|
|
{
|
|
|
|
_gpuThread = Thread.CurrentThread;
|
2022-05-14 16:58:33 +02:00
|
|
|
|
|
|
|
Capabilities = Renderer.GetCapabilities();
|
2022-01-09 17:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if the current thread is the GPU thread.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>True if the thread is the GPU thread, false otherwise</returns>
|
|
|
|
public bool IsGpuThread()
|
|
|
|
{
|
|
|
|
return _gpuThread == Thread.CurrentThread;
|
|
|
|
}
|
|
|
|
|
Add a Multithreading layer for the GAL, multi-thread shader compilation at runtime (#2501)
* Initial Implementation
About as fast as nvidia GL multithreading, can be improved with faster command queuing.
* Struct based command list
Speeds up a bit. Still a lot of time lost to resource copy.
* Do shader init while the render thread is active.
* Introduce circular span pool V1
Ideally should be able to use structs instead of references for storing these spans on commands. Will try that next.
* Refactor SpanRef some more
Use a struct to represent SpanRef, rather than a reference.
* Flush buffers on background thread
* Use a span for UpdateRenderScale.
Much faster than copying the array.
* Calculate command size using reflection
* WIP parallel shaders
* Some minor optimisation
* Only 2 max refs per command now.
The command with 3 refs is gone. :relieved:
* Don't cast on the GPU side
* Remove redundant casts, force sync on window present
* Fix Shader Cache
* Fix host shader save.
* Fixup to work with new renderer stuff
* Make command Run static, use array of delegates as lookup
Profile says this takes less time than the previous way.
* Bring up to date
* Add settings toggle. Fix Muiltithreading Off mode.
* Fix warning.
* Release tracking lock for flushes
* Fix Conditional Render fast path with threaded gal
* Make handle iteration safe when releasing the lock
This is mostly temporary.
* Attempt to set backend threading on driver
Only really works on nvidia before launching a game.
* Fix race condition with BufferModifiedRangeList, exceptions in tracking actions
* Update buffer set commands
* Some cleanup
* Only use stutter workaround when using opengl renderer non-threaded
* Add host-conditional reservation of counter events
There has always been the possibility that conditional rendering could use a query object just as it is disposed by the counter queue. This change makes it so that when the host decides to use host conditional rendering, the query object is reserved so that it cannot be deleted. Counter events can optionally start reserved, as the threaded implementation can reserve them before the backend creates them, and there would otherwise be a short amount of time where the counter queue could dispose the event before a call to reserve it could be made.
* Address Feedback
* Make counter flush tracked again.
Hopefully does not cause any issues this time.
* Wait for FlushTo on the main queue thread.
Currently assumes only one thread will want to FlushTo (in this case, the GPU thread)
* Add SDL2 headless integration
* Add HLE macro commands.
Co-authored-by: Mary <mary@mary.zone>
2021-08-27 00:31:29 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Processes the queue of shaders that must save their binaries to the disk cache.
|
|
|
|
/// </summary>
|
|
|
|
public void ProcessShaderCacheQueue()
|
|
|
|
{
|
|
|
|
foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
|
|
|
|
{
|
|
|
|
physicalMemory.ShaderCache.ProcessShaderCacheQueue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Advances internal sequence number.
|
|
|
|
/// This forces the update of any modified GPU resource.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
internal void AdvanceSequence()
|
|
|
|
{
|
|
|
|
SequenceNumber++;
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:08:06 +01: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>
|
2022-01-09 17:28:48 +01:00
|
|
|
/// <param name="syncpointOnly">True if the sync action should only run when syncpoints are incremented</param>
|
|
|
|
public void RegisterSyncAction(Action action, bool syncpointOnly = false)
|
2021-01-17 21:08:06 +01:00
|
|
|
{
|
2022-01-09 17:28:48 +01:00
|
|
|
if (syncpointOnly)
|
|
|
|
{
|
|
|
|
SyncpointActions.Add(action);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SyncActions.Add(action);
|
|
|
|
}
|
2021-01-17 21:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2022-01-09 17:28:48 +01:00
|
|
|
/// <param name="syncpoint">True if host sync is being created by a syncpoint</param>
|
|
|
|
public void CreateHostSyncIfNeeded(bool syncpoint)
|
2021-01-17 21:08:06 +01:00
|
|
|
{
|
2022-01-09 17:28:48 +01:00
|
|
|
if (SyncActions.Count > 0 || (syncpoint && SyncpointActions.Count > 0))
|
2021-01-17 21:08:06 +01:00
|
|
|
{
|
|
|
|
Renderer.CreateSync(SyncNumber);
|
|
|
|
|
|
|
|
SyncNumber++;
|
|
|
|
|
|
|
|
foreach (Action action in SyncActions)
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
|
2022-01-09 17:28:48 +01:00
|
|
|
foreach (Action action in SyncpointActions)
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:08:06 +01:00
|
|
|
SyncActions.Clear();
|
2022-01-09 17:28:48 +01:00
|
|
|
SyncpointActions.Clear();
|
2021-01-17 21:08:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
/// <summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// Performs deferred actions.
|
|
|
|
/// This is useful for actions that must run on the render thread, such as resource disposal.
|
2021-06-24 01:51:41 +02:00
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
internal void RunDeferredActions()
|
2021-06-24 01:51:41 +02:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
while (DeferredActions.TryDequeue(out Action action))
|
2021-06-24 01:51:41 +02:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
action();
|
2021-06-24 01:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 23:09:49 +01: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 23:23:11 +01:00
|
|
|
Renderer.Dispose();
|
2020-07-24 04:53:25 +02:00
|
|
|
GPFifo.Dispose();
|
2020-11-13 00:15:34 +01: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 22:52:44 +02:00
|
|
|
|
2021-06-29 19:32:02 +02: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 23:09:49 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|