aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
diff options
context:
space:
mode:
authorMary <me@thog.eu>2020-11-13 00:15:34 +0100
committerGitHub <noreply@github.com>2020-11-13 00:15:34 +0100
commit48f6570557fc76496936514d94e3ccddf55ec633 (patch)
treece455833899cb33a312e5853a7a3d191bb5d18d9 /Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
parent7166e82c3cf1fd8cf2fce3281017ee88122684d8 (diff)
Salieri: shader cache (#1701)
Here come Salieri, my implementation of a disk shader cache! "I'm sure you know why I named it that." "It doesn't really mean anything." This implementation collects shaders at runtime and cache them to be later compiled when starting a game.
Diffstat (limited to 'Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs')
-rw-r--r--Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs639
1 files changed, 587 insertions, 52 deletions
diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
index 131aa6b7..ac5aedbe 100644
--- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
@@ -1,9 +1,17 @@
+using Ryujinx.Common;
+using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.Gpu.Shader.Cache;
+using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
using Ryujinx.Graphics.Gpu.State;
using Ryujinx.Graphics.Shader;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Gpu.Shader
{
@@ -21,6 +29,16 @@ namespace Ryujinx.Graphics.Gpu.Shader
private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
+ private CacheManager _cacheManager;
+
+ private Dictionary<Hash128, ShaderBundle> _gpProgramsDiskCache;
+ private Dictionary<Hash128, ShaderBundle> _cpProgramsDiskCache;
+
+ /// <summary>
+ /// Version of the codegen (to be incremented when codegen changes).
+ /// </summary>
+ private const ulong ShaderCodeGenVersion = 1;
+
/// <summary>
/// Creates a new instance of the shader cache.
/// </summary>
@@ -33,6 +51,251 @@ namespace Ryujinx.Graphics.Gpu.Shader
_cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
_gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
+ _gpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
+ _cpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
+ }
+
+ /// <summary>
+ /// Initialize the cache.
+ /// </summary>
+ internal void Initialize()
+ {
+ if (GraphicsConfig.EnableShaderCache && GraphicsConfig.TitleId != null)
+ {
+ _cacheManager = new CacheManager(CacheGraphicsApi.OpenGL, CacheHashType.XxHash128, "glsl", GraphicsConfig.TitleId, ShaderCodeGenVersion);
+
+ HashSet<Hash128> invalidEntries = new HashSet<Hash128>();
+
+ ReadOnlySpan<Hash128> guestProgramList = _cacheManager.GetGuestProgramList();
+
+ for (int programIndex = 0; programIndex < guestProgramList.Length; programIndex++)
+ {
+ Hash128 key = guestProgramList[programIndex];
+
+ Logger.Info?.Print(LogClass.Gpu, $"Compiling shader {key} ({programIndex + 1} / {guestProgramList.Length})");
+
+ byte[] hostProgramBinary = _cacheManager.GetHostProgramByHash(ref key);
+ bool hasHostCache = hostProgramBinary != null;
+
+ IProgram hostProgram = null;
+
+ // If the program sources aren't in the cache, compile from saved guest program.
+ byte[] guestProgram = _cacheManager.GetGuestProgramByHash(ref key);
+
+ if (guestProgram == null)
+ {
+ Logger.Error?.Print(LogClass.Gpu, $"Ignoring orphan shader hash {key} in cache (is the cache incomplete?)");
+
+ // Should not happen, but if someone messed with the cache it's better to catch it.
+ invalidEntries.Add(key);
+
+ continue;
+ }
+
+ ReadOnlySpan<byte> guestProgramReadOnlySpan = guestProgram;
+
+ ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader);
+
+ if (cachedShaderEntries[0].Header.Stage == ShaderStage.Compute)
+ {
+ Debug.Assert(cachedShaderEntries.Length == 1);
+
+ GuestShaderCacheEntry entry = cachedShaderEntries[0];
+
+ HostShaderCacheEntry[] hostShaderEntries = null;
+
+ // Try loading host shader binary.
+ if (hasHostCache)
+ {
+ hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
+ hostProgramBinary = hostProgramBinarySpan.ToArray();
+ hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
+ }
+
+ bool isHostProgramValid = hostProgram != null;
+
+ ShaderProgram program;
+ ShaderProgramInfo shaderProgramInfo;
+
+ // Reconstruct code holder.
+ if (isHostProgramValid)
+ {
+ program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
+ shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
+ }
+ else
+ {
+ IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
+
+ program = Translator.CreateContext(0, gpuAccessor, DefaultFlags | TranslationFlags.Compute).Translate(out shaderProgramInfo);
+ }
+
+ ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
+
+ // If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
+ if (hostProgram == null)
+ {
+ 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);
+ hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
+
+ // As the host program was invalidated, save the new entry in the cache.
+ hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
+
+ if (hasHostCache)
+ {
+ _cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
+ }
+ else
+ {
+ Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
+
+ _cacheManager.AddHostProgram(ref key, hostProgramBinary);
+ }
+ }
+
+ _cpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shader));
+ }
+ else
+ {
+ Debug.Assert(cachedShaderEntries.Length == Constants.ShaderStages);
+
+ ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
+ List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
+
+ TransformFeedbackDescriptor[] tfd = ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader);
+
+ TranslationFlags flags = DefaultFlags;
+
+ if (tfd != null)
+ {
+ flags = TranslationFlags.Feedback;
+ }
+
+ TranslationCounts counts = new TranslationCounts();
+
+ HostShaderCacheEntry[] hostShaderEntries = null;
+
+ // Try loading host shader binary.
+ if (hasHostCache)
+ {
+ hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
+ hostProgramBinary = hostProgramBinarySpan.ToArray();
+ hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
+ }
+
+ bool isHostProgramValid = hostProgram != null;
+
+ // Reconstruct code holder.
+ for (int i = 0; i < cachedShaderEntries.Length; i++)
+ {
+ GuestShaderCacheEntry entry = cachedShaderEntries[i];
+
+ if (entry == null)
+ {
+ continue;
+ }
+
+ ShaderProgram program;
+
+ if (entry.Header.SizeA != 0)
+ {
+ ShaderProgramInfo shaderProgramInfo;
+
+ if (isHostProgramValid)
+ {
+ program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
+ shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
+ }
+ else
+ {
+ IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
+
+ program = Translator.CreateContext((ulong)entry.Header.Size, 0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
+ }
+
+ // NOTE: Vertex B comes first in the shader cache.
+ byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size).ToArray();
+ byte[] code2 = entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray();
+
+ shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2);
+ }
+ else
+ {
+ ShaderProgramInfo shaderProgramInfo;
+
+ if (isHostProgramValid)
+ {
+ program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
+ shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
+ }
+ else
+ {
+ IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
+
+ program = Translator.CreateContext(0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
+ }
+
+ shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
+ }
+
+ shaderPrograms.Add(program);
+ }
+
+ // If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
+ if (!isHostProgramValid)
+ {
+ Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
+
+ List<IShader> hostShaders = new List<IShader>();
+
+ // Compile shaders and create program as the shader program binary got invalidated.
+ for (int stage = 0; stage < Constants.ShaderStages; stage++)
+ {
+ ShaderProgram program = shaders[stage]?.Program;
+
+ if (program == null)
+ {
+ continue;
+ }
+
+ IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
+
+ shaders[stage].HostShader = hostShader;
+
+ hostShaders.Add(hostShader);
+ }
+
+ hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
+
+ // As the host program was invalidated, save the new entry in the cache.
+ hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
+
+ if (hasHostCache)
+ {
+ _cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
+ }
+ else
+ {
+ Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
+
+ _cacheManager.AddHostProgram(ref key, hostProgramBinary);
+ }
+ }
+
+ _gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
+ }
+ }
+
+ // Remove entries that are broken in the cache
+ _cacheManager.RemoveManifestEntries(invalidEntries);
+ _cacheManager.FlushToArchive();
+ _cacheManager.Synchronize();
+
+ Logger.Info?.Print(LogClass.Gpu, "Shader cache loaded.");
+ }
}
/// <summary>
@@ -71,7 +334,9 @@ namespace Ryujinx.Graphics.Gpu.Shader
}
}
- ShaderCodeHolder shader = TranslateComputeShader(
+ TranslatorContext[] shaderContexts = new TranslatorContext[1];
+
+ shaderContexts[0] = DecodeComputeShader(
state,
gpuVa,
localSizeX,
@@ -80,11 +345,45 @@ namespace Ryujinx.Graphics.Gpu.Shader
localMemorySize,
sharedMemorySize);
- shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
+ bool isShaderCacheEnabled = _cacheManager != null;
+
+ byte[] programCode = null;
+ Hash128 programCodeHash = default;
+ GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
+
+ if (isShaderCacheEnabled)
+ {
+ // Compute hash and prepare data for shader disk cache comparison.
+ GetProgramInformations(null, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
+ }
+
+ ShaderBundle cpShader;
+
+ // Search for the program hash in loaded shaders.
+ if (!isShaderCacheEnabled || !_cpProgramsDiskCache.TryGetValue(programCodeHash, out cpShader))
+ {
+ if (isShaderCacheEnabled)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
+ }
+
+ // The shader isn't currently cached, translate it and compile it.
+ ShaderCodeHolder shader = TranslateShader(shaderContexts[0]);
+
+ shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
+
+ IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
- IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
+ byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
- ShaderBundle cpShader = new ShaderBundle(hostProgram, shader);
+ cpShader = new ShaderBundle(hostProgram, shader);
+
+ if (isShaderCacheEnabled)
+ {
+ _cpProgramsDiskCache.Add(programCodeHash, cpShader);
+ _cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, null), hostProgramBinary);
+ }
+ }
if (!isCached)
{
@@ -123,9 +422,9 @@ namespace Ryujinx.Graphics.Gpu.Shader
}
}
- ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
+ TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages];
- var tfd = GetTransformFeedbackDescriptors(state);
+ TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state);
TranslationFlags flags = DefaultFlags;
@@ -138,39 +437,79 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (addresses.VertexA != 0)
{
- shaders[0] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
+ shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
}
else
{
- shaders[0] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
+ shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
}
- shaders[1] = TranslateGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
- shaders[2] = TranslateGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
- shaders[3] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
- shaders[4] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
+ shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
+ shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
+ shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
+ shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
- List<IShader> hostShaders = new List<IShader>();
+ bool isShaderCacheEnabled = _cacheManager != null;
- for (int stage = 0; stage < Constants.ShaderStages; stage++)
+ byte[] programCode = null;
+ Hash128 programCodeHash = default;
+ GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
+
+ if (isShaderCacheEnabled)
{
- ShaderProgram program = shaders[stage]?.Program;
+ // Compute hash and prepare data for shader disk cache comparison.
+ GetProgramInformations(tfd, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
+ }
- if (program == null)
+ ShaderBundle gpShaders;
+
+ // Search for the program hash in loaded shaders.
+ if (!isShaderCacheEnabled || !_gpProgramsDiskCache.TryGetValue(programCodeHash, out gpShaders))
+ {
+ if (isShaderCacheEnabled)
{
- continue;
+ Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
}
- IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
+ // The shader isn't currently cached, translate it and compile it.
+ ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
- shaders[stage].HostShader = hostShader;
+ shaders[0] = TranslateShader(shaderContexts[0]);
+ shaders[1] = TranslateShader(shaderContexts[1]);
+ shaders[2] = TranslateShader(shaderContexts[2]);
+ shaders[3] = TranslateShader(shaderContexts[3]);
+ shaders[4] = TranslateShader(shaderContexts[4]);
- hostShaders.Add(hostShader);
- }
+ List<IShader> hostShaders = new List<IShader>();
- IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
+ for (int stage = 0; stage < Constants.ShaderStages; stage++)
+ {
+ ShaderProgram program = shaders[stage]?.Program;
- ShaderBundle gpShaders = new ShaderBundle(hostProgram, shaders);
+ if (program == null)
+ {
+ continue;
+ }
+
+ IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
+
+ shaders[stage].HostShader = hostShader;
+
+ hostShaders.Add(hostShader);
+ }
+
+ IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
+
+ byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
+
+ gpShaders = new ShaderBundle(hostProgram, shaders);
+
+ if (isShaderCacheEnabled)
+ {
+ _gpProgramsDiskCache.Add(programCodeHash, gpShaders);
+ _cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, tfd), hostProgramBinary);
+ }
+ }
if (!isCached)
{
@@ -286,7 +625,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
}
/// <summary>
- /// Translates the binary Maxwell shader code to something that the host API accepts.
+ /// Decode the binary Maxwell shader code to a translator context.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
@@ -295,8 +634,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <param name="localSizeZ">Local group size Z of the computer shader</param>
/// <param name="localMemorySize">Local memory size of the compute shader</param>
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
- /// <returns>Compiled compute shader code</returns>
- private ShaderCodeHolder TranslateComputeShader(
+ /// <returns>The generated translator context</returns>
+ private TranslatorContext DecodeComputeShader(
GpuState state,
ulong gpuVa,
int localSizeX,
@@ -312,25 +651,11 @@ namespace Ryujinx.Graphics.Gpu.Shader
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
- ShaderProgram program;
-
- program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
-
- byte[] code = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
-
- _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
-
- if (fullPath != null && codePath != null)
- {
- program.Prepend("// " + codePath);
- program.Prepend("// " + fullPath);
- }
-
- return new ShaderCodeHolder(program, code);
+ return Translator.CreateContext(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
}
/// <summary>
- /// Translates the binary Maxwell shader code to something that the host API accepts.
+ /// Decode the binary Maxwell shader code to a translator context.
/// </summary>
/// <remarks>
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
@@ -341,8 +666,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <param name="stage">Shader stage</param>
/// <param name="gpuVa">GPU virtual address of the shader code</param>
/// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
- /// <returns>Compiled graphics shader code</returns>
- private ShaderCodeHolder TranslateGraphicsShader(
+ /// <returns>The generated translator context</returns>
+ private TranslatorContext DecodeGraphicsShader(
GpuState state,
TranslationCounts counts,
TranslationFlags flags,
@@ -359,14 +684,36 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (gpuVaA != 0)
{
- ShaderProgram program = Translator.Translate(gpuVaA, gpuVa, gpuAccessor, flags, counts);
+ return Translator.CreateContext(gpuVaA, gpuVa, gpuAccessor, flags, counts);
+ }
+ else
+ {
+ return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
+ }
+ }
- byte[] codeA = _context.MemoryManager.GetSpan(gpuVaA, program.SizeA).ToArray();
- byte[] codeB = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
+ /// <summary>
+ /// Translates a previously generated translator context to something that the host API accepts.
+ /// </summary>
+ /// <param name="translatorContext">Current translator context to translate</param>
+ /// <returns>Compiled graphics shader code</returns>
+ private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext)
+ {
+ if (translatorContext == null)
+ {
+ return null;
+ }
+
+ if (translatorContext.AddressA != 0)
+ {
+ byte[] codeA = _context.MemoryManager.GetSpan(translatorContext.AddressA, translatorContext.SizeA).ToArray();
+ byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
_dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
+ ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
+
if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
{
program.Prepend("// " + codePathB);
@@ -375,23 +722,23 @@ namespace Ryujinx.Graphics.Gpu.Shader
program.Prepend("// " + fullPathA);
}
- return new ShaderCodeHolder(program, codeB, codeA);
+ return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
}
else
{
- ShaderProgram program = Translator.Translate(gpuVa, gpuAccessor, flags, counts);
-
- byte[] code = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
+ byte[] code = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
_dumper.Dump(code, compute: false, out string fullPath, out string codePath);
+ ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
+
if (fullPath != null && codePath != null)
{
program.Prepend("// " + codePath);
program.Prepend("// " + fullPath);
}
- return new ShaderCodeHolder(program, code);
+ return new ShaderCodeHolder(program, shaderProgramInfo, code);
}
}
@@ -416,6 +763,194 @@ namespace Ryujinx.Graphics.Gpu.Shader
bundle.Dispose();
}
}
+
+ _cacheManager?.Dispose();
+ }
+
+ /// <summary>
+ /// Create a guest shader program.
+ /// </summary>
+ /// <param name="programCode">The program code of the shader code</param>
+ /// <param name="shaderCacheEntries">The resulting guest shader entries header</param>
+ /// <param name="tfd">The transform feedback descriptors in use</param>
+ /// <returns>The resulting guest shader program</returns>
+ private static byte[] CreateGuestProgramDump(ReadOnlySpan<byte> programCode, GuestShaderCacheEntryHeader[] shaderCacheEntries, TransformFeedbackDescriptor[] tfd)
+ {
+ using (MemoryStream resultStream = new MemoryStream())
+ {
+ BinaryWriter resultStreamWriter = new BinaryWriter(resultStream);
+
+ byte transformFeedbackCount = 0;
+
+ if (tfd != null)
+ {
+ transformFeedbackCount = (byte)tfd.Length;
+ }
+
+ // Header
+ resultStreamWriter.WriteStruct(new GuestShaderCacheHeader((byte)shaderCacheEntries.Length, transformFeedbackCount));
+
+ // Write all entries header
+ foreach (GuestShaderCacheEntryHeader entry in shaderCacheEntries)
+ {
+ resultStreamWriter.WriteStruct(entry);
+ }
+
+ // Finally, write all program code and all transform feedback information.
+ resultStreamWriter.Write(programCode);
+
+ return resultStream.ToArray();
+ }
+ }
+
+ /// <summary>
+ /// Write transform feedback guest information to the given stream.
+ /// </summary>
+ /// <param name="stream">The stream to write data to</param>
+ /// <param name="tfd">The current transform feedback descriptors used</param>
+ private static void WriteTransformationFeedbackInformation(Stream stream, TransformFeedbackDescriptor[] tfd)
+ {
+ if (tfd != null)
+ {
+ BinaryWriter writer = new BinaryWriter(stream);
+
+ foreach (TransformFeedbackDescriptor transform in tfd)
+ {
+ writer.WriteStruct(new GuestShaderCacheTransformFeedbackHeader(transform.BufferIndex, transform.Stride, transform.VaryingLocations.Length));
+ writer.Write(transform.VaryingLocations);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Read transform feedback descriptors from guest.
+ /// </summary>
+ /// <param name="data">The raw guest transform feedback descriptors</param>
+ /// <param name="header">The guest shader program header</param>
+ /// <returns>The transform feedback descriptors read from guest</returns>
+ private static TransformFeedbackDescriptor[] ReadTransformationFeedbackInformations(ref ReadOnlySpan<byte> data, GuestShaderCacheHeader header)
+ {
+ if (header.TransformFeedbackCount != 0)
+ {
+ TransformFeedbackDescriptor[] result = new TransformFeedbackDescriptor[header.TransformFeedbackCount];
+
+ for (int i = 0; i < result.Length; i++)
+ {
+ GuestShaderCacheTransformFeedbackHeader feedbackHeader = MemoryMarshal.Read<GuestShaderCacheTransformFeedbackHeader>(data);
+
+ result[i] = new TransformFeedbackDescriptor(feedbackHeader.BufferIndex, feedbackHeader.Stride, data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>(), feedbackHeader.VaryingLocationsLength).ToArray());
+
+ data = data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>() + feedbackHeader.VaryingLocationsLength);
+ }
+
+ return result;
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Create a new instance of <see cref="GuestGpuAccessorHeader"/> from an gpu accessor.
+ /// </summary>
+ /// <param name="gpuAccessor">The gpu accessor</param>
+ /// <returns>a new instance of <see cref="GuestGpuAccessorHeader"/></returns>
+ private static GuestGpuAccessorHeader CreateGuestGpuAccessorCache(IGpuAccessor gpuAccessor)
+ {
+ return new GuestGpuAccessorHeader
+ {
+ ComputeLocalSizeX = gpuAccessor.QueryComputeLocalSizeX(),
+ ComputeLocalSizeY = gpuAccessor.QueryComputeLocalSizeY(),
+ ComputeLocalSizeZ = gpuAccessor.QueryComputeLocalSizeZ(),
+ ComputeLocalMemorySize = gpuAccessor.QueryComputeLocalMemorySize(),
+ ComputeSharedMemorySize = gpuAccessor.QueryComputeSharedMemorySize(),
+ PrimitiveTopology = gpuAccessor.QueryPrimitiveTopology(),
+ };
+ }
+
+ /// <summary>
+ /// Write the guest GpuAccessor informations to the given stream.
+ /// </summary>
+ /// <param name="stream">The stream to write the guest GpuAcessor</param>
+ /// <param name="shaderContext">The shader tranlator context in use</param>
+ /// <returns>The guest gpu accessor header</returns>
+ private static GuestGpuAccessorHeader WriteGuestGpuAccessorCache(Stream stream, TranslatorContext shaderContext)
+ {
+ BinaryWriter writer = new BinaryWriter(stream);
+
+ GuestGpuAccessorHeader header = CreateGuestGpuAccessorCache(shaderContext.GpuAccessor);
+
+ // If we have a full gpu accessor, cache textures descriptors
+ if (shaderContext.GpuAccessor is GpuAccessor gpuAccessor)
+ {
+ HashSet<int> textureHandlesInUse = shaderContext.TextureHandlesForCache;
+
+ header.TextureDescriptorCount = textureHandlesInUse.Count;
+
+ foreach (int textureHandle in textureHandlesInUse)
+ {
+ GuestTextureDescriptor textureDescriptor = gpuAccessor.GetTextureDescriptor(textureHandle).ToCache();
+
+ textureDescriptor.Handle = (uint)textureHandle;
+
+ writer.WriteStruct(textureDescriptor);
+ }
+ }
+
+ return header;
+ }
+
+ /// <summary>
+ /// Get the shader program information for use on the shader cache.
+ /// </summary>
+ /// <param name="tfd">The current transform feedback descriptors used</param>
+ /// <param name="shaderContexts">The shader translators context in use</param>
+ /// <param name="programCode">The resulting raw shader program code</param>
+ /// <param name="programCodeHash">The resulting raw shader program code hash</param>
+ /// <param name="entries">The resulting guest shader entries header</param>
+ private void GetProgramInformations(TransformFeedbackDescriptor[] tfd, ReadOnlySpan<TranslatorContext> shaderContexts, out byte[] programCode, out Hash128 programCodeHash, out GuestShaderCacheEntryHeader[] entries)
+ {
+ GuestShaderCacheEntryHeader ComputeStage(Stream stream, TranslatorContext context)
+ {
+ if (context == null)
+ {
+ return new GuestShaderCacheEntryHeader();
+ }
+
+ ReadOnlySpan<byte> data = _context.MemoryManager.GetSpan(context.Address, context.Size);
+
+ stream.Write(data);
+
+ int size = data.Length;
+ int sizeA = 0;
+
+ if (context.AddressA != 0)
+ {
+ data = _context.MemoryManager.GetSpan(context.AddressA, context.SizeA);
+
+ sizeA = data.Length;
+
+ stream.Write(data);
+ }
+
+ GuestGpuAccessorHeader gpuAccessorHeader = WriteGuestGpuAccessorCache(stream, context);
+
+ return new GuestShaderCacheEntryHeader(context.Stage, size, sizeA, gpuAccessorHeader);
+ }
+
+ entries = new GuestShaderCacheEntryHeader[shaderContexts.Length];
+
+ using (MemoryStream stream = new MemoryStream())
+ {
+ for (int i = 0; i < shaderContexts.Length; i++)
+ {
+ entries[i] = ComputeStage(stream, shaderContexts[i]);
+ }
+
+ WriteTransformationFeedbackInformation(stream, tfd);
+
+ programCode = stream.ToArray();
+ programCodeHash = _cacheManager.ComputeHash(programCode);
+ }
}
}
} \ No newline at end of file