diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2021-08-11 15:59:42 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-11 20:59:42 +0200 |
| commit | d9d18439f6900fd9f05bde41998526281f7638c5 (patch) | |
| tree | 14e8cd74e10ca9c92d1b85ccf17cecad00e3a8f7 /Ryujinx.Graphics.Gpu/Shader | |
| parent | 70f79e689bc947313aab11c41e59928ce43be517 (diff) | |
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)
Diffstat (limited to 'Ryujinx.Graphics.Gpu/Shader')
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs | 51 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs | 53 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs | 10 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/Definition/GuestShaderCacheEntryHeader.cs | 10 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs | 20 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/GpuAccessor.cs | 21 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs | 59 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/ShaderCompileTask.cs | 3 |
8 files changed, 201 insertions, 26 deletions
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 @@ -39,6 +39,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache RemoveManifestEntries, /// <summary> + /// Remove entries from the hash manifest and save it, and also deletes the temporary file. + /// </summary> + RemoveManifestEntryAndTempFile, + + /// <summary> /// Flush temporary cache to archive. /// </summary> FlushToArchive, @@ -116,6 +121,9 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// </summary> private ZipArchive _cacheArchive; + /// <summary> + /// Indicates if the cache collection supports modification. + /// </summary> public bool IsReadOnly { get; } /// <summary> @@ -265,6 +273,21 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache } /// <summary> + /// Remove given entry from the manifest and delete the temporary file. + /// </summary> + /// <param name="entry">Entry to remove from the manifest</param> + private void RemoveManifestEntryAndTempFile(Hash128 entry) + { + lock (_hashTable) + { + _hashTable.Remove(entry); + SaveManifest(); + } + + File.Delete(GenCacheTempFilePath(entry)); + } + + /// <summary> /// Queue a task to flush temporary files to the archive on the worker. /// </summary> public void FlushToArchiveAsync() @@ -440,6 +463,9 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache case CacheFileOperation.RemoveManifestEntries: RemoveManifestEntries((HashSet<Hash128>)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 }); } + /// <summary> + /// Removes a value at the given hash from the cache. + /// </summary> + /// <param name="keyHash">The hash of the value in the cache</param> + 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 /// <summary> /// Create guest shader cache entries from the runtime contexts. /// </summary> - /// <param name="memoryManager">The GPU memory manager in use</param> + /// <param name="channel">The GPU channel in use</param> /// <param name="shaderContexts">The runtime contexts</param> /// <returns>Guest shader cahe entries from the runtime contexts</returns> - public static GuestShaderCacheEntry[] CreateShaderCacheEntries(MemoryManager memoryManager, ReadOnlySpan<TranslatorContext> shaderContexts) + public static GuestShaderCacheEntry[] CreateShaderCacheEntries(GpuChannel channel, ReadOnlySpan<TranslatorContext> 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 @@ -115,6 +115,16 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache } /// <summary> + /// Removes a shader program present in the program cache. + /// </summary> + /// <param name="programCodeHash">Target program code hash</param> + public void RemoveProgram(ref Hash128 programCodeHash) + { + _guestProgramCache.RemoveValue(ref programCodeHash); + _hostProgramCache.RemoveValue(ref programCodeHash); + } + + /// <summary> /// Get all guest program hashes. /// </summary> /// <returns>All guest program hashes</returns> 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; /// <summary> - /// Unused/reserved. + /// Constant buffer 1 data size. /// </summary> - public int Reserved4; + public int Cb1DataSize; /// <summary> /// The header of the cached gpu accessor. @@ -55,12 +55,14 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition /// <param name="stage">The stage of this shader</param> /// <param name="size">The size of the code section</param> /// <param name="sizeA">The size of the code2 section if present (Vertex A)</param> + /// <param name="cb1DataSize">Constant buffer 1 data size</param> /// <param name="gpuAccessorHeader">The header of the cached gpu accessor</param> - 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<byte> _data; + private readonly ReadOnlyMemory<byte> _cb1Data; private readonly GuestGpuAccessorHeader _header; private readonly Dictionary<int, GuestTextureDescriptor> _textureDescriptors; @@ -19,12 +20,19 @@ namespace Ryujinx.Graphics.Gpu.Shader /// </summary> /// <param name="context">GPU context</param> /// <param name="data">The data of the shader</param> + /// <param name="cb1Data">The constant buffer 1 data of the shader</param> /// <param name="header">The cache of the GPU accessor</param> /// <param name="guestTextureDescriptors">The cache of the texture descriptors</param> - public CachedGpuAccessor(GpuContext context, ReadOnlyMemory<byte> data, GuestGpuAccessorHeader header, Dictionary<int, GuestTextureDescriptor> guestTextureDescriptors) + public CachedGpuAccessor( + GpuContext context, + ReadOnlyMemory<byte> data, + ReadOnlyMemory<byte> cb1Data, + GuestGpuAccessorHeader header, + Dictionary<int, GuestTextureDescriptor> guestTextureDescriptors) { _context = context; _data = data; + _cb1Data = cb1Data; _header = header; _textureDescriptors = new Dictionary<int, GuestTextureDescriptor>(); @@ -35,6 +43,16 @@ namespace Ryujinx.Graphics.Gpu.Shader } /// <summary> + /// Reads data from the constant buffer 1. + /// </summary> + /// <param name="offset">Offset in bytes to read from</param> + /// <returns>Value at the given offset</returns> + public uint ConstantBuffer1Read(int offset) + { + return MemoryMarshal.Cast<byte, uint>(_cb1Data.Span.Slice(offset))[0]; + } + + /// <summary> /// Prints a log message. /// </summary> /// <param name="message">Message to print</param> 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; } + /// <summary> /// Creates a new instance of the GPU state accessor for graphics shader translation. /// </summary> @@ -68,6 +70,25 @@ namespace Ryujinx.Graphics.Gpu.Shader } /// <summary> + /// Reads data from the constant buffer 1. + /// </summary> + /// <param name="offset">Offset in bytes to read from</param> + /// <returns>Value at the given offset</returns> + 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<uint>(baseAddress + (ulong)offset); + } + + /// <summary> /// Prints a log message. /// </summary> /// <param name="message">Message to print</param> 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 /// <summary> /// Version of the codegen (to be changed when codegen or guest format change). /// </summary> - 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<ShaderCompileTask> activeTasks = new List<ShaderCompileTask>(); - 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<byte>(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<byte>(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<byte>(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; + /// <summary> /// Create a new shader compile task, with an event to signal whenever a subtask completes. /// </summary> |
