Initialize all shader caches concurrently

This commit is contained in:
Marco Carvalho 2024-05-08 15:29:37 -03:00
commit f665ace2db
5 changed files with 29 additions and 19 deletions

View file

@ -8,7 +8,9 @@ using Ryujinx.Graphics.Gpu.Synchronization;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.Graphics.Gpu namespace Ryujinx.Graphics.Gpu
{ {
@ -274,14 +276,11 @@ namespace Ryujinx.Graphics.Gpu
/// <summary> /// <summary>
/// Initialize the GPU shader cache. /// Initialize the GPU shader cache.
/// </summary> /// </summary>
public void InitializeShaderCache(CancellationToken cancellationToken) public async Task InitializeShaderCache(CancellationToken cancellationToken)
{ {
HostInitalized.WaitOne(); HostInitalized.WaitOne();
foreach (var physicalMemory in PhysicalMemoryRegistry.Values) await Task.WhenAll(PhysicalMemoryRegistry.Values.Select(pm => pm.ShaderCache.Initialize(cancellationToken)));
{
physicalMemory.ShaderCache.Initialize(cancellationToken);
}
_gpuReadyEvent.Set(); _gpuReadyEvent.Set();
} }

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Ryujinx.Graphics.Gpu.Shader.DiskCache namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
{ {
@ -194,7 +195,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
/// <param name="dataFileStream">Guest data file stream</param> /// <param name="dataFileStream">Guest data file stream</param>
/// <param name="index">Guest shader index</param> /// <param name="index">Guest shader index</param>
/// <returns>Guest code and constant buffer 1 data</returns> /// <returns>Guest code and constant buffer 1 data</returns>
public GuestCodeAndCbData LoadShader(Stream tocFileStream, Stream dataFileStream, int index) public async Task<GuestCodeAndCbData> LoadShader(Stream tocFileStream, Stream dataFileStream, int index)
{ {
if (_cache == null || index >= _cache.Length) if (_cache == null || index >= _cache.Length)
{ {
@ -220,7 +221,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
} }
dataFileStream.Seek((long)entry.Offset, SeekOrigin.Begin); dataFileStream.Seek((long)entry.Offset, SeekOrigin.Begin);
dataFileStream.Read(cb1Data); await dataFileStream.ReadAsync(cb1Data);
BinarySerializer.ReadCompressed(dataFileStream, guestCode); BinarySerializer.ReadCompressed(dataFileStream, guestCode);
_cache[index] = (guestCode, cb1Data); _cache[index] = (guestCode, cb1Data);

View file

@ -5,6 +5,7 @@ using System;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Ryujinx.Graphics.Gpu.Shader.DiskCache namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
{ {
@ -289,7 +290,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
/// </summary> /// </summary>
/// <param name="context">GPU context</param> /// <param name="context">GPU context</param>
/// <param name="loader">Parallel disk cache loader</param> /// <param name="loader">Parallel disk cache loader</param>
public void LoadShaders(GpuContext context, ParallelDiskCacheLoader loader) public async Task LoadShaders(GpuContext context, ParallelDiskCacheLoader loader)
{ {
if (!CacheExists()) if (!CacheExists())
{ {
@ -301,11 +302,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
try try
{ {
using var tocFileStream = DiskCacheCommon.OpenFile(_basePath, SharedTocFileName, writable: false); await using var tocFileStream = DiskCacheCommon.OpenFile(_basePath, SharedTocFileName, writable: false);
using var dataFileStream = DiskCacheCommon.OpenFile(_basePath, SharedDataFileName, writable: false); await using var dataFileStream = DiskCacheCommon.OpenFile(_basePath, SharedDataFileName, writable: false);
using var guestTocFileStream = _guestStorage.OpenTocFileStream(); await using var guestTocFileStream = _guestStorage.OpenTocFileStream();
using var guestDataFileStream = _guestStorage.OpenDataFileStream(); await using var guestDataFileStream = _guestStorage.OpenDataFileStream();
BinarySerializer tocReader = new(tocFileStream); BinarySerializer tocReader = new(tocFileStream);
BinarySerializer dataReader = new(dataFileStream); BinarySerializer dataReader = new(dataFileStream);
@ -365,7 +366,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
dataReader.Read(ref stageEntry); dataReader.Read(ref stageEntry);
guestShaders[stageIndex] = _guestStorage.LoadShader( guestShaders[stageIndex] = await _guestStorage.LoadShader(
guestTocFileStream, guestTocFileStream,
guestDataFileStream, guestDataFileStream,
stageEntry.GuestCodeIndex); stageEntry.GuestCodeIndex);
@ -432,8 +433,15 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
{ {
_guestStorage.ClearMemoryCache(); _guestStorage.ClearMemoryCache();
hostTocFileStream?.Dispose(); if (hostTocFileStream != null)
hostDataFileStream?.Dispose(); {
await hostTocFileStream.DisposeAsync();
}
if (hostDataFileStream != null)
{
await hostDataFileStream.DisposeAsync();
}
} }
} }

View file

@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using static Ryujinx.Graphics.Gpu.Shader.ShaderCache; using static Ryujinx.Graphics.Gpu.Shader.ShaderCache;
namespace Ryujinx.Graphics.Gpu.Shader.DiskCache namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
@ -226,7 +227,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
/// <summary> /// <summary>
/// Loads all shaders from the cache. /// Loads all shaders from the cache.
/// </summary> /// </summary>
public void LoadShaders() public async Task LoadShaders()
{ {
Thread[] workThreads = new Thread[ThreadCount]; Thread[] workThreads = new Thread[ThreadCount];
@ -254,7 +255,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
try try
{ {
_hostStorage.LoadShaders(_context, this); await _hostStorage.LoadShaders(_context, this);
} }
catch (DiskCacheLoadException diskCacheLoadException) catch (DiskCacheLoadException diskCacheLoadException)
{ {

View file

@ -12,6 +12,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.Graphics.Gpu.Shader namespace Ryujinx.Graphics.Gpu.Shader
{ {
@ -152,7 +153,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// Initialize the cache. /// Initialize the cache.
/// </summary> /// </summary>
/// <param name="cancellationToken">Cancellation token to cancel the shader cache initialization process</param> /// <param name="cancellationToken">Cancellation token to cancel the shader cache initialization process</param>
internal void Initialize(CancellationToken cancellationToken) internal async Task Initialize(CancellationToken cancellationToken)
{ {
if (_diskCacheHostStorage.CacheEnabled) if (_diskCacheHostStorage.CacheEnabled)
{ {
@ -164,7 +165,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
ShaderCacheStateUpdate, ShaderCacheStateUpdate,
cancellationToken); cancellationToken);
loader.LoadShaders(); await loader.LoadShaders();
int errorCount = loader.ErrorCount; int errorCount = loader.ErrorCount;
if (errorCount != 0) if (errorCount != 0)