From d9d18439f6900fd9f05bde41998526281f7638c5 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 11 Aug 2021 15:59:42 -0300 Subject: Use a new approach for shader BRX targets (#2532) * Use a new approach for shader BRX targets * Make shader cache actually work * Improve the shader pattern matching a bit * Extend LDC search to predecessor blocks, catches more cases * Nit * Only save the amount of constant buffer data actually used. Avoids crashes on partially mapped buffers * Ignore Rd on predicate instructions, as they do not have a Rd register (catches more cases) --- .../Shader/Cache/CacheCollection.cs | 51 ++++++++++++++++++- Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs | 53 ++++++++++++++++--- Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs | 10 ++++ .../Definition/GuestShaderCacheEntryHeader.cs | 10 ++-- Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs | 20 +++++++- Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs | 21 ++++++++ Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs | 59 ++++++++++++++++++---- Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs | 3 +- 8 files changed, 201 insertions(+), 26 deletions(-) (limited to 'Ryujinx.Graphics.Gpu/Shader') diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs index 2660e528..316e027f 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs @@ -38,6 +38,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// RemoveManifestEntries, + /// + /// Remove entries from the hash manifest and save it, and also deletes the temporary file. + /// + RemoveManifestEntryAndTempFile, + /// /// Flush temporary cache to archive. /// @@ -116,6 +121,9 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// private ZipArchive _cacheArchive; + /// + /// Indicates if the cache collection supports modification. + /// public bool IsReadOnly { get; } /// @@ -264,6 +272,21 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache } } + /// + /// Remove given entry from the manifest and delete the temporary file. + /// + /// Entry to remove from the manifest + private void RemoveManifestEntryAndTempFile(Hash128 entry) + { + lock (_hashTable) + { + _hashTable.Remove(entry); + SaveManifest(); + } + + File.Delete(GenCacheTempFilePath(entry)); + } + /// /// Queue a task to flush temporary files to the archive on the worker. /// @@ -440,6 +463,9 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache case CacheFileOperation.RemoveManifestEntries: RemoveManifestEntries((HashSet)task.Data); break; + case CacheFileOperation.RemoveManifestEntryAndTempFile: + RemoveManifestEntryAndTempFile((Hash128)task.Data); + break; case CacheFileOperation.FlushToArchive: FlushToArchive(); break; @@ -472,7 +498,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache { if (IsReadOnly) { - Logger.Warning?.Print(LogClass.Gpu, "Trying to add {keyHash} on a read-only cache, ignoring."); + Logger.Warning?.Print(LogClass.Gpu, $"Trying to add {keyHash} on a read-only cache, ignoring."); return; } @@ -521,7 +547,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache { if (IsReadOnly) { - Logger.Warning?.Print(LogClass.Gpu, "Trying to replace {keyHash} on a read-only cache, ignoring."); + Logger.Warning?.Print(LogClass.Gpu, $"Trying to replace {keyHash} on a read-only cache, ignoring."); return; } @@ -540,6 +566,27 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache }); } + /// + /// Removes a value at the given hash from the cache. + /// + /// The hash of the value in the cache + public void RemoveValue(ref Hash128 keyHash) + { + if (IsReadOnly) + { + Logger.Warning?.Print(LogClass.Gpu, $"Trying to remove {keyHash} on a read-only cache, ignoring."); + + return; + } + + // Only queue file change operations + _fileWriterWorkerQueue.Add(new CacheFileOperationTask + { + Type = CacheFileOperation.RemoveManifestEntryAndTempFile, + Data = keyHash + }); + } + public void Dispose() { Dispose(true); diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs index f6caddef..33da42db 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs @@ -371,11 +371,13 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// /// Create guest shader cache entries from the runtime contexts. /// - /// The GPU memory manager in use + /// The GPU channel in use /// The runtime contexts /// Guest shader cahe entries from the runtime contexts - public static GuestShaderCacheEntry[] CreateShaderCacheEntries(MemoryManager memoryManager, ReadOnlySpan shaderContexts) + public static GuestShaderCacheEntry[] CreateShaderCacheEntries(GpuChannel channel, ReadOnlySpan shaderContexts) { + MemoryManager memoryManager = channel.MemoryManager; + int startIndex = shaderContexts.Length > 1 ? 1 : 0; GuestShaderCacheEntry[] entries = new GuestShaderCacheEntry[shaderContexts.Length - startIndex]; @@ -389,31 +391,66 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache continue; } + GpuAccessor gpuAccessor = context.GpuAccessor as GpuAccessor; + + ulong cb1DataAddress; + int cb1DataSize = gpuAccessor?.Cb1DataSize ?? 0; + + if (context.Stage == ShaderStage.Compute) + { + cb1DataAddress = channel.BufferManager.GetComputeUniformBufferAddress(1); + } + else + { + int stageIndex = context.Stage switch + { + ShaderStage.TessellationControl => 1, + ShaderStage.TessellationEvaluation => 2, + ShaderStage.Geometry => 3, + ShaderStage.Fragment => 4, + _ => 0 + }; + + cb1DataAddress = channel.BufferManager.GetGraphicsUniformBufferAddress(stageIndex, 1); + } + + int size = context.Size; + TranslatorContext translatorContext2 = i == 1 ? shaderContexts[0] : null; int sizeA = translatorContext2 != null ? translatorContext2.Size : 0; - byte[] code = new byte[context.Size + sizeA]; + byte[] code = new byte[size + cb1DataSize + sizeA]; - memoryManager.GetSpan(context.Address, context.Size).CopyTo(code); + memoryManager.GetSpan(context.Address, size).CopyTo(code); + + if (cb1DataAddress != 0 && cb1DataSize != 0) + { + memoryManager.Physical.GetSpan(cb1DataAddress, cb1DataSize).CopyTo(code.AsSpan().Slice(size, cb1DataSize)); + } if (translatorContext2 != null) { - memoryManager.GetSpan(translatorContext2.Address, sizeA).CopyTo(code.AsSpan().Slice(context.Size, sizeA)); + memoryManager.GetSpan(translatorContext2.Address, sizeA).CopyTo(code.AsSpan().Slice(size + cb1DataSize, sizeA)); } GuestGpuAccessorHeader gpuAccessorHeader = CreateGuestGpuAccessorCache(context.GpuAccessor); - if (context.GpuAccessor is GpuAccessor) + if (gpuAccessor != null) { gpuAccessorHeader.TextureDescriptorCount = context.TextureHandlesForCache.Count; } - GuestShaderCacheEntryHeader header = new GuestShaderCacheEntryHeader(context.Stage, context.Size, sizeA, gpuAccessorHeader); + GuestShaderCacheEntryHeader header = new GuestShaderCacheEntryHeader( + context.Stage, + size + cb1DataSize, + sizeA, + cb1DataSize, + gpuAccessorHeader); GuestShaderCacheEntry entry = new GuestShaderCacheEntry(header, code); - if (context.GpuAccessor is GpuAccessor gpuAccessor) + if (gpuAccessor != null) { foreach (int textureHandle in context.TextureHandlesForCache) { diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs index 1ac37704..3fc11e82 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs @@ -114,6 +114,16 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache _hostProgramCache.ReplaceValue(ref programCodeHash, data); } + /// + /// Removes a shader program present in the program cache. + /// + /// Target program code hash + public void RemoveProgram(ref Hash128 programCodeHash) + { + _guestProgramCache.RemoveValue(ref programCodeHash); + _hostProgramCache.RemoveValue(ref programCodeHash); + } + /// /// Get all guest program hashes. /// diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs index 6d5bb28d..9b22cac5 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs @@ -40,9 +40,9 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition public int SizeA; /// - /// Unused/reserved. + /// Constant buffer 1 data size. /// - public int Reserved4; + public int Cb1DataSize; /// /// The header of the cached gpu accessor. @@ -55,12 +55,14 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition /// The stage of this shader /// The size of the code section /// The size of the code2 section if present (Vertex A) + /// Constant buffer 1 data size /// The header of the cached gpu accessor - public GuestShaderCacheEntryHeader(ShaderStage stage, int size, int sizeA, GuestGpuAccessorHeader gpuAccessorHeader) : this() + public GuestShaderCacheEntryHeader(ShaderStage stage, int size, int sizeA, int cb1DataSize, GuestGpuAccessorHeader gpuAccessorHeader) : this() { Stage = stage; - Size = size; + Size = size; SizeA = sizeA; + Cb1DataSize = cb1DataSize; GpuAccessorHeader = gpuAccessorHeader; } } diff --git a/Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs b/Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs index a7bd4edb..452dfd83 100644 --- a/Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs +++ b/Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs @@ -11,6 +11,7 @@ namespace Ryujinx.Graphics.Gpu.Shader { private readonly GpuContext _context; private readonly ReadOnlyMemory _data; + private readonly ReadOnlyMemory _cb1Data; private readonly GuestGpuAccessorHeader _header; private readonly Dictionary _textureDescriptors; @@ -19,12 +20,19 @@ namespace Ryujinx.Graphics.Gpu.Shader /// /// GPU context /// The data of the shader + /// The constant buffer 1 data of the shader /// The cache of the GPU accessor /// The cache of the texture descriptors - public CachedGpuAccessor(GpuContext context, ReadOnlyMemory data, GuestGpuAccessorHeader header, Dictionary guestTextureDescriptors) + public CachedGpuAccessor( + GpuContext context, + ReadOnlyMemory data, + ReadOnlyMemory cb1Data, + GuestGpuAccessorHeader header, + Dictionary guestTextureDescriptors) { _context = context; _data = data; + _cb1Data = cb1Data; _header = header; _textureDescriptors = new Dictionary(); @@ -34,6 +42,16 @@ namespace Ryujinx.Graphics.Gpu.Shader } } + /// + /// Reads data from the constant buffer 1. + /// + /// Offset in bytes to read from + /// Value at the given offset + public uint ConstantBuffer1Read(int offset) + { + return MemoryMarshal.Cast(_cb1Data.Span.Slice(offset))[0]; + } + /// /// Prints a log message. /// diff --git a/Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs b/Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs index b7059b51..6254b1c2 100644 --- a/Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs +++ b/Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs @@ -20,6 +20,8 @@ namespace Ryujinx.Graphics.Gpu.Shader private readonly int _localMemorySize; private readonly int _sharedMemorySize; + public int Cb1DataSize { get; private set; } + /// /// Creates a new instance of the GPU state accessor for graphics shader translation. /// @@ -67,6 +69,25 @@ namespace Ryujinx.Graphics.Gpu.Shader _sharedMemorySize = sharedMemorySize; } + /// + /// Reads data from the constant buffer 1. + /// + /// Offset in bytes to read from + /// Value at the given offset + public uint ConstantBuffer1Read(int offset) + { + if (Cb1DataSize < offset + 4) + { + Cb1DataSize = offset + 4; + } + + ulong baseAddress = _compute + ? _channel.BufferManager.GetComputeUniformBufferAddress(1) + : _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1); + + return _channel.MemoryManager.Physical.Read(baseAddress + (ulong)offset); + } + /// /// Prints a log message. /// diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs index a5712a14..754449fb 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs @@ -38,7 +38,7 @@ namespace Ryujinx.Graphics.Gpu.Shader /// /// Version of the codegen (to be changed when codegen or guest format change). /// - private const ulong ShaderCodeGenVersion = 2469; + private const ulong ShaderCodeGenVersion = 2530; // Progress reporting helpers private volatile int _shaderCount; @@ -112,7 +112,7 @@ namespace Ryujinx.Graphics.Gpu.Shader int programIndex = 0; List activeTasks = new List(); - AutoResetEvent taskDoneEvent = new AutoResetEvent(false); + using AutoResetEvent taskDoneEvent = new AutoResetEvent(false); // This thread dispatches tasks to do shader translation, and creates programs that OpenGL will link in the background. // The program link status is checked in a non-blocking manner so that multiple shaders can be compiled at once. @@ -191,7 +191,14 @@ namespace Ryujinx.Graphics.Gpu.Shader Task compileTask = Task.Run(() => { - IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors); + var binaryCode = new Memory(entry.Code); + + var gpuAccessor = new CachedGpuAccessor( + _context, + binaryCode, + binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize), + entry.Header.GpuAccessorHeader, + entry.TextureDescriptors); var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, DefaultFlags | TranslationFlags.Compute); program = Translator.CreateContext(0, gpuAccessor, options).Translate(out shaderProgramInfo); @@ -199,12 +206,20 @@ namespace Ryujinx.Graphics.Gpu.Shader task.OnTask(compileTask, (bool _, ShaderCompileTask task) => { + if (task.IsFaulted) + { + Logger.Warning?.Print(LogClass.Gpu, $"Host shader {key} is corrupted or incompatible, discarding..."); + + _cacheManager.RemoveProgram(ref key); + return true; // Exit early, the decoding step failed. + } + ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code); Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest..."); // Compile shader and create program as the shader program binary got invalidated. - shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code); + shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, program.Code); hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null); task.OnCompiled(hostProgram, (bool isNewProgramValid, ShaderCompileTask task) => @@ -298,7 +313,14 @@ namespace Ryujinx.Graphics.Gpu.Shader } else { - IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors); + var binaryCode = new Memory(entry.Code); + + var gpuAccessor = new CachedGpuAccessor( + _context, + binaryCode, + binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize), + entry.Header.GpuAccessorHeader, + entry.TextureDescriptors); var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags); var options2 = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags | TranslationFlags.VertexA); @@ -310,7 +332,7 @@ namespace Ryujinx.Graphics.Gpu.Shader } // NOTE: Vertex B comes first in the shader cache. - byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size).ToArray(); + byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray(); byte[] code2 = entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray(); shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2); @@ -326,13 +348,22 @@ namespace Ryujinx.Graphics.Gpu.Shader } else { - IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors); + var binaryCode = new Memory(entry.Code); + + var gpuAccessor = new CachedGpuAccessor( + _context, + binaryCode, + binaryCode.Slice(binaryCode.Length - entry.Header.Cb1DataSize), + entry.Header.GpuAccessorHeader, + entry.TextureDescriptors); var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags); program = Translator.CreateContext(0, gpuAccessor, options, counts).Translate(out shaderProgramInfo); } - shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code); + byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray(); + + shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code); } shaderPrograms.Add(program); @@ -341,6 +372,14 @@ namespace Ryujinx.Graphics.Gpu.Shader task.OnTask(compileTask, (bool _, ShaderCompileTask task) => { + if (task.IsFaulted) + { + Logger.Warning?.Print(LogClass.Gpu, $"Host shader {key} is corrupted or incompatible, discarding..."); + + _cacheManager.RemoveProgram(ref key); + return true; // Exit early, the decoding step failed. + } + // If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again. if (!isHostProgramValid) { @@ -537,7 +576,7 @@ namespace Ryujinx.Graphics.Gpu.Shader isShaderCacheReadOnly = _cacheManager.IsReadOnly; // Compute hash and prepare data for shader disk cache comparison. - shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel.MemoryManager, shaderContexts); + shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel, shaderContexts); programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries); } @@ -659,7 +698,7 @@ namespace Ryujinx.Graphics.Gpu.Shader isShaderCacheReadOnly = _cacheManager.IsReadOnly; // Compute hash and prepare data for shader disk cache comparison. - shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel.MemoryManager, shaderContexts); + shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel, shaderContexts); programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries, tfd); } diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs index ff48fab0..a9283de2 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs @@ -1,5 +1,4 @@ using Ryujinx.Graphics.GAL; -using System; using System.Threading; using System.Threading.Tasks; @@ -20,6 +19,8 @@ namespace Ryujinx.Graphics.Gpu.Shader private ShaderCompileTaskCallback _action; private AutoResetEvent _taskDoneEvent; + public bool IsFaulted => _programsTask.IsFaulted; + /// /// Create a new shader compile task, with an event to signal whenever a subtask completes. /// -- cgit v1.2.3