aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Shader/Cache
diff options
context:
space:
mode:
authorMary <me@thog.eu>2020-12-13 08:46:07 +0100
committerGitHub <noreply@github.com>2020-12-13 08:46:07 +0100
commit6bc2733c1796788590c9f0114013d2e4b555e31e (patch)
treea729af2552637718fc3883108539c6da6523efd6 /Ryujinx.Graphics.Gpu/Shader/Cache
parent19d18662ea3ed5470898ed2b7bbb06d45f6004dd (diff)
salieri: Support read-only mode if archive is already opened (#1807)
This improves shader cache resilience when people opens another program that touch the cache.zip.
Diffstat (limited to 'Ryujinx.Graphics.Gpu/Shader/Cache')
-rw-r--r--Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs38
-rw-r--r--Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs22
-rw-r--r--Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs4
-rw-r--r--Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs7
4 files changed, 68 insertions, 3 deletions
diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs
index 9b61ef4a..2660e528 100644
--- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs
@@ -116,6 +116,8 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// </summary>
private ZipArchive _cacheArchive;
+ public bool IsReadOnly { get; }
+
/// <summary>
/// Immutable copy of the hash table.
/// </summary>
@@ -167,6 +169,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
_hashType = hashType;
_version = version;
_hashTable = new HashSet<Hash128>();
+ IsReadOnly = CacheHelper.IsArchiveReadOnly(GetArchivePath());
Load();
@@ -230,6 +233,13 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <param name="entries">Entries to remove from the manifest</param>
public void RemoveManifestEntriesAsync(HashSet<Hash128> entries)
{
+ if (IsReadOnly)
+ {
+ Logger.Warning?.Print(LogClass.Gpu, "Trying to remove manifest entries on a read-only cache, ignoring.");
+
+ return;
+ }
+
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
{
Type = CacheFileOperation.RemoveManifestEntries,
@@ -308,6 +318,20 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
string archivePath = GetArchivePath();
+ if (IsReadOnly)
+ {
+ Logger.Warning?.Print(LogClass.Gpu, $"Cache collection archive in read-only, archiving task skipped.");
+
+ return;
+ }
+
+ if (CacheHelper.IsArchiveReadOnly(archivePath))
+ {
+ Logger.Warning?.Print(LogClass.Gpu, $"Cache collection archive in use, archiving task skipped.");
+
+ return;
+ }
+
// Open the zip in read/write.
_cacheArchive = ZipFile.Open(archivePath, ZipArchiveMode.Update);
@@ -446,6 +470,13 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <param name="value">The value to cache</param>
public void AddValue(ref Hash128 keyHash, byte[] value)
{
+ if (IsReadOnly)
+ {
+ Logger.Warning?.Print(LogClass.Gpu, "Trying to add {keyHash} on a read-only cache, ignoring.");
+
+ return;
+ }
+
Debug.Assert(value != null);
bool isAlreadyPresent;
@@ -488,6 +519,13 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// <param name="value">The value to cache</param>
public void ReplaceValue(ref Hash128 keyHash, byte[] value)
{
+ if (IsReadOnly)
+ {
+ Logger.Warning?.Print(LogClass.Gpu, "Trying to replace {keyHash} on a read-only cache, ignoring.");
+
+ return;
+ }
+
Debug.Assert(value != null);
// Only queue file change operations
diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs
index d10e4671..d109f1cd 100644
--- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs
@@ -496,5 +496,27 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
}
}
}
+
+ public static bool IsArchiveReadOnly(string archivePath)
+ {
+ FileInfo info = new FileInfo(archivePath);
+
+ if (!info.Exists)
+ {
+ return false;
+ }
+
+ try
+ {
+ using (FileStream stream = info.Open(FileMode.Open, FileAccess.Read, FileShare.None))
+ {
+ return false;
+ }
+ }
+ catch (IOException)
+ {
+ return true;
+ }
+ }
}
}
diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs
index ca0070fd..1ac37704 100644
--- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs
@@ -1,9 +1,7 @@
using Ryujinx.Common;
-using Ryujinx.Common.Configuration;
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
using System;
using System.Collections.Generic;
-using System.IO;
namespace Ryujinx.Graphics.Gpu.Shader.Cache
{
@@ -31,6 +29,8 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
/// </summary>
private const ulong GuestCacheVersion = 1759;
+ public bool IsReadOnly => _guestProgramCache.IsReadOnly || _hostProgramCache.IsReadOnly;
+
/// <summary>
/// Create a new cache manager instance
/// </summary>
diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs
index 965287b5..839853c0 100644
--- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs
@@ -146,7 +146,12 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
string guestBaseCacheDirectory = CacheHelper.GenerateCachePath(baseCacheDirectory, CacheGraphicsApi.Guest, "", "program");
string hostBaseCacheDirectory = CacheHelper.GenerateCachePath(baseCacheDirectory, graphicsApi, shaderProvider, "host");
- if (CacheHelper.TryReadManifestHeader(CacheHelper.GetManifestPath(guestBaseCacheDirectory), out CacheManifestHeader header))
+ string guestArchivePath = CacheHelper.GetArchivePath(guestBaseCacheDirectory);
+ string hostArchivePath = CacheHelper.GetArchivePath(hostBaseCacheDirectory);
+
+ bool isReadOnly = CacheHelper.IsArchiveReadOnly(guestArchivePath) || CacheHelper.IsArchiveReadOnly(hostArchivePath);
+
+ if (!isReadOnly && CacheHelper.TryReadManifestHeader(CacheHelper.GetManifestPath(guestBaseCacheDirectory), out CacheManifestHeader header))
{
if (NeedHashRecompute(header.Version, out ulong newVersion))
{