2020-11-13 00:15:34 +01:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
2019-11-14 19:26:40 +01:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2021-07-11 22:20:40 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Engine.Threed;
|
2021-06-29 19:32:02 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2020-11-13 00:15:34 +01:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader.Cache;
|
|
|
|
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
2019-11-14 19:26:40 +01:00
|
|
|
using Ryujinx.Graphics.Shader;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-11-13 00:15:34 +01:00
|
|
|
using System.Diagnostics;
|
2021-07-11 22:20:40 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2021-03-03 01:39:36 +01:00
|
|
|
using System.Threading;
|
2021-03-29 22:52:25 +02:00
|
|
|
using System.Threading.Tasks;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
|
|
|
{
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Memory cache of shader code.
|
|
|
|
/// </summary>
|
2019-12-31 23:09:49 +01:00
|
|
|
class ShaderCache : IDisposable
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2019-12-16 05:59:46 +01:00
|
|
|
private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
private readonly GpuContext _context;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
private readonly ShaderDumper _dumper;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
|
|
|
|
private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
private CacheManager _cacheManager;
|
|
|
|
|
|
|
|
private Dictionary<Hash128, ShaderBundle> _gpProgramsDiskCache;
|
|
|
|
private Dictionary<Hash128, ShaderBundle> _cpProgramsDiskCache;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-17 22:20:17 +01:00
|
|
|
/// Version of the codegen (to be changed when codegen or guest format change).
|
2020-11-13 00:15:34 +01:00
|
|
|
/// </summary>
|
2021-08-11 23:56:59 +02:00
|
|
|
private const ulong ShaderCodeGenVersion = 2542;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-03 01:39:36 +01:00
|
|
|
// Progress reporting helpers
|
2021-03-22 19:40:07 +01:00
|
|
|
private volatile int _shaderCount;
|
|
|
|
private volatile int _totalShaderCount;
|
|
|
|
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
2021-03-03 01:39:36 +01:00
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the shader cache.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context that the shader cache belongs to</param>
|
2019-11-14 19:26:40 +01:00
|
|
|
public ShaderCache(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
_dumper = new ShaderDumper();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
_cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
|
|
|
|
_gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
|
2020-11-13 00:15:34 +01:00
|
|
|
_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);
|
|
|
|
|
2020-12-13 08:46:07 +01:00
|
|
|
bool isReadOnly = _cacheManager.IsReadOnly;
|
|
|
|
|
|
|
|
HashSet<Hash128> invalidEntries = null;
|
|
|
|
|
|
|
|
if (isReadOnly)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Loading shader cache in read-only mode (cache in use by another program!)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
invalidEntries = new HashSet<Hash128>();
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
ReadOnlySpan<Hash128> guestProgramList = _cacheManager.GetGuestProgramList();
|
|
|
|
|
2021-03-22 19:40:07 +01:00
|
|
|
using AutoResetEvent progressReportEvent = new AutoResetEvent(false);
|
|
|
|
|
2021-03-03 01:39:36 +01:00
|
|
|
_shaderCount = 0;
|
2021-03-22 19:40:07 +01:00
|
|
|
_totalShaderCount = guestProgramList.Length;
|
2021-03-03 01:39:36 +01:00
|
|
|
|
2021-03-22 19:40:07 +01:00
|
|
|
ShaderCacheStateChanged?.Invoke(ShaderCacheState.Start, _shaderCount, _totalShaderCount);
|
|
|
|
Thread progressReportThread = null;
|
|
|
|
|
|
|
|
if (guestProgramList.Length > 0)
|
|
|
|
{
|
|
|
|
progressReportThread = new Thread(ReportProgress)
|
|
|
|
{
|
|
|
|
Name = "ShaderCache.ProgressReporter",
|
|
|
|
Priority = ThreadPriority.Lowest,
|
|
|
|
IsBackground = true
|
|
|
|
};
|
|
|
|
|
|
|
|
progressReportThread.Start(progressReportEvent);
|
|
|
|
}
|
2021-03-03 01:39:36 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Make sure these are initialized before doing compilation.
|
|
|
|
Capabilities caps = _context.Capabilities;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
int maxTaskCount = Math.Min(Environment.ProcessorCount, 8);
|
|
|
|
int programIndex = 0;
|
|
|
|
List<ShaderCompileTask> activeTasks = new List<ShaderCompileTask>();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-08-11 20:59:42 +02:00
|
|
|
using AutoResetEvent taskDoneEvent = new AutoResetEvent(false);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// 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.
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
while (programIndex < guestProgramList.Length || activeTasks.Count > 0)
|
|
|
|
{
|
|
|
|
if (activeTasks.Count < maxTaskCount && programIndex < guestProgramList.Length)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
// Begin a new shader compilation.
|
|
|
|
Hash128 key = guestProgramList[programIndex];
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
byte[] hostProgramBinary = _cacheManager.GetHostProgramByHash(ref key);
|
|
|
|
bool hasHostCache = hostProgramBinary != null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
IProgram hostProgram = null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// If the program sources aren't in the cache, compile from saved guest program.
|
|
|
|
byte[] guestProgram = _cacheManager.GetGuestProgramByHash(ref key);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (guestProgram == null)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
Logger.Error?.Print(LogClass.Gpu, $"Ignoring orphan shader hash {key} in cache (is the cache incomplete?)");
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Should not happen, but if someone messed with the cache it's better to catch it.
|
|
|
|
invalidEntries?.Add(key);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-04-15 00:15:25 +02:00
|
|
|
_shaderCount = ++programIndex;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
continue;
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
ReadOnlySpan<byte> guestProgramReadOnlySpan = guestProgram;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (cachedShaderEntries[0].Header.Stage == ShaderStage.Compute)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
Debug.Assert(cachedShaderEntries.Length == 1);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
GuestShaderCacheEntry entry = cachedShaderEntries[0];
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
HostShaderCacheEntry[] hostShaderEntries = null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Try loading host shader binary.
|
|
|
|
if (hasHostCache)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
|
|
|
|
hostProgramBinary = hostProgramBinarySpan.ToArray();
|
|
|
|
hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 00:05:55 +02:00
|
|
|
ShaderCompileTask task = new ShaderCompileTask(taskDoneEvent);
|
2021-03-29 22:52:25 +02:00
|
|
|
activeTasks.Add(task);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
task.OnCompiled(hostProgram, (bool isHostProgramValid, ShaderCompileTask task) =>
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
ShaderProgram program = null;
|
|
|
|
ShaderProgramInfo shaderProgramInfo = null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
if (isHostProgramValid)
|
2020-12-13 08:46:07 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
// Reconstruct code holder.
|
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
program = new ShaderProgram(entry.Header.Stage, "");
|
|
|
|
shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-12 20:59:24 +02:00
|
|
|
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray();
|
|
|
|
|
|
|
|
ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, code);
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
_cpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shader));
|
|
|
|
|
|
|
|
return true;
|
2020-12-13 08:46:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
// If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
Task compileTask = Task.Run(() =>
|
|
|
|
{
|
2021-08-11 20:59:42 +02:00
|
|
|
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);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, DefaultFlags | TranslationFlags.Compute);
|
|
|
|
program = Translator.CreateContext(0, gpuAccessor, options).Translate(out shaderProgramInfo);
|
2021-04-04 15:01:33 +02:00
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
task.OnTask(compileTask, (bool _, ShaderCompileTask task) =>
|
2021-03-29 22:52:25 +02:00
|
|
|
{
|
2021-08-11 20:59:42 +02:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2021-08-12 20:59:24 +02:00
|
|
|
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray();
|
|
|
|
|
|
|
|
ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, code);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Compile shader and create program as the shader program binary got invalidated.
|
2021-08-11 20:59:42 +02:00
|
|
|
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, program.Code);
|
2021-03-29 22:52:25 +02:00
|
|
|
hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
task.OnCompiled(hostProgram, (bool isNewProgramValid, ShaderCompileTask task) =>
|
|
|
|
{
|
|
|
|
// As the host program was invalidated, save the new entry in the cache.
|
|
|
|
hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (!isReadOnly)
|
|
|
|
{
|
|
|
|
if (hasHostCache)
|
|
|
|
{
|
|
|
|
_cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
_cpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shader));
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
return true;
|
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
return false; // Not finished: still need to compile the host program.
|
2021-04-04 15:01:33 +02:00
|
|
|
});
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
return false; // Not finished: translating the program.
|
|
|
|
}
|
2021-03-29 22:52:25 +02:00
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
2021-03-29 22:52:25 +02:00
|
|
|
else
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
Debug.Assert(cachedShaderEntries.Length == Constants.ShaderStages);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
|
|
|
|
List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformFeedbackInformation(ref guestProgramReadOnlySpan, fileHeader);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
TranslationFlags flags = DefaultFlags;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (tfd != null)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
flags |= TranslationFlags.Feedback;
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
TranslationCounts counts = new TranslationCounts();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
HostShaderCacheEntry[] hostShaderEntries = null;
|
2021-02-08 00:42:17 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Try loading host shader binary.
|
|
|
|
if (hasHostCache)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-03-29 22:52:25 +02:00
|
|
|
hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
|
|
|
|
hostProgramBinary = hostProgramBinarySpan.ToArray();
|
|
|
|
hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 00:05:55 +02:00
|
|
|
ShaderCompileTask task = new ShaderCompileTask(taskDoneEvent);
|
2021-03-29 22:52:25 +02:00
|
|
|
activeTasks.Add(task);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
GuestShaderCacheEntry[] entries = cachedShaderEntries.ToArray();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
task.OnCompiled(hostProgram, (bool isHostProgramValid, ShaderCompileTask task) =>
|
|
|
|
{
|
|
|
|
Task compileTask = Task.Run(() =>
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
TranslatorContext[] shaderContexts = null;
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (!isHostProgramValid)
|
|
|
|
{
|
|
|
|
shaderContexts = new TranslatorContext[1 + entries.Length];
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
for (int i = 0; i < entries.Length; i++)
|
2021-03-29 22:52:25 +02:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
GuestShaderCacheEntry entry = entries[i];
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (entry == null)
|
2021-03-29 22:52:25 +02:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
continue;
|
2021-03-29 22:52:25 +02:00
|
|
|
}
|
2021-08-11 20:59:42 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
var binaryCode = new Memory<byte>(entry.Code);
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
shaderContexts[i + 1] = Translator.CreateContext(0, gpuAccessor, options, counts);
|
2021-07-06 21:20:06 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (entry.Header.SizeA != 0)
|
|
|
|
{
|
|
|
|
var options2 = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags | TranslationFlags.VertexA);
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
shaderContexts[0] = Translator.CreateContext((ulong)entry.Header.Size, gpuAccessor, options2, counts);
|
2021-03-29 22:52:25 +02:00
|
|
|
}
|
2021-08-11 22:27:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
// Reconstruct code holder.
|
|
|
|
for (int i = 0; i < entries.Length; i++)
|
|
|
|
{
|
|
|
|
GuestShaderCacheEntry entry = entries[i];
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (entry == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderProgram program;
|
|
|
|
ShaderProgramInfo shaderProgramInfo;
|
|
|
|
|
|
|
|
if (isHostProgramValid)
|
|
|
|
{
|
|
|
|
program = new ShaderProgram(entry.Header.Stage, "");
|
|
|
|
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
|
2021-03-29 22:52:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
int stageIndex = i + 1;
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
TranslatorContext currentStage = shaderContexts[stageIndex];
|
|
|
|
TranslatorContext nextStage = GetNextStageContext(shaderContexts, stageIndex);
|
|
|
|
TranslatorContext vertexA = stageIndex == 1 ? shaderContexts[0] : null;
|
2021-08-11 20:59:42 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
program = currentStage.Translate(out shaderProgramInfo, nextStage, vertexA);
|
|
|
|
}
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
// NOTE: Vertex B comes first in the shader cache.
|
|
|
|
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size - entry.Header.Cb1DataSize).ToArray();
|
|
|
|
byte[] code2 = entry.Header.SizeA != 0 ? entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray() : null;
|
2021-03-29 22:52:25 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2);
|
2021-03-29 22:52:25 +02:00
|
|
|
|
|
|
|
shaderPrograms.Add(program);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
task.OnTask(compileTask, (bool _, ShaderCompileTask task) =>
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-08-11 20:59:42 +02:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// 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...");
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
List<IShader> hostShaders = new List<IShader>();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// 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;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (program == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
shaders[stage].HostShader = hostShader;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
hostShaders.Add(hostShader);
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
task.OnCompiled(hostProgram, (bool isNewProgramValid, ShaderCompileTask task) =>
|
|
|
|
{
|
|
|
|
// As the host program was invalidated, save the new entry in the cache.
|
|
|
|
hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (!isReadOnly)
|
|
|
|
{
|
|
|
|
if (hasHostCache)
|
|
|
|
{
|
|
|
|
_cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
_gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
return true;
|
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
return false; // Not finished: still need to compile the host program.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-04-04 15:01:33 +02:00
|
|
|
return false; // Not finished: translating the program.
|
2021-03-29 22:52:25 +02:00
|
|
|
});
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
_shaderCount = ++programIndex;
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
// Process the queue.
|
|
|
|
for (int i = 0; i < activeTasks.Count; i++)
|
|
|
|
{
|
|
|
|
ShaderCompileTask task = activeTasks[i];
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (task.IsDone())
|
|
|
|
{
|
|
|
|
activeTasks.RemoveAt(i--);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 01:39:36 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
if (activeTasks.Count == maxTaskCount)
|
|
|
|
{
|
2021-04-03 00:05:55 +02:00
|
|
|
// Wait for a task to be done, or for 1ms.
|
2021-05-20 20:12:15 +02:00
|
|
|
// Host shader compilation cannot signal when it is done,
|
2021-04-03 00:05:55 +02:00
|
|
|
// so the 1ms timeout is required to poll status.
|
|
|
|
|
|
|
|
taskDoneEvent.WaitOne(1);
|
2021-03-29 22:52:25 +02:00
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-13 08:46:07 +01:00
|
|
|
if (!isReadOnly)
|
|
|
|
{
|
|
|
|
// Remove entries that are broken in the cache
|
|
|
|
_cacheManager.RemoveManifestEntries(invalidEntries);
|
|
|
|
_cacheManager.FlushToArchive();
|
|
|
|
_cacheManager.Synchronize();
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-22 19:40:07 +01:00
|
|
|
progressReportEvent.Set();
|
|
|
|
progressReportThread?.Join();
|
|
|
|
|
|
|
|
ShaderCacheStateChanged?.Invoke(ShaderCacheState.Loaded, _shaderCount, _totalShaderCount);
|
2021-03-03 01:39:36 +01:00
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Gpu, $"Shader cache loaded {_shaderCount} entries.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-03-22 19:40:07 +01:00
|
|
|
/// Raises ShaderCacheStateChanged events periodically.
|
2021-03-03 01:39:36 +01:00
|
|
|
/// </summary>
|
2021-03-22 19:40:07 +01:00
|
|
|
private void ReportProgress(object state)
|
2021-03-03 01:39:36 +01:00
|
|
|
{
|
2021-03-22 19:40:07 +01:00
|
|
|
const int refreshRate = 50; // ms
|
|
|
|
|
|
|
|
AutoResetEvent endEvent = (AutoResetEvent)state;
|
|
|
|
|
|
|
|
int count = 0;
|
2021-03-03 01:39:36 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-03-22 19:40:07 +01:00
|
|
|
int newCount = _shaderCount;
|
|
|
|
|
|
|
|
if (count != newCount)
|
|
|
|
{
|
|
|
|
ShaderCacheStateChanged?.Invoke(ShaderCacheState.Loading, newCount, _totalShaderCount);
|
|
|
|
count = newCount;
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
2021-03-22 19:40:07 +01:00
|
|
|
while (!endEvent.WaitOne(refreshRate));
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a compute shader from the cache.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2021-07-08 01:56:06 +02:00
|
|
|
/// <param name="channel">GPU channel</param>
|
|
|
|
/// <param name="gas">GPU accessor state</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
|
|
|
|
/// <param name="localSizeX">Local group size X of the computer shader</param>
|
|
|
|
/// <param name="localSizeY">Local group size Y of the computer shader</param>
|
|
|
|
/// <param name="localSizeZ">Local group size Z of the computer shader</param>
|
2020-02-02 04:25:52 +01:00
|
|
|
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
|
|
|
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <returns>Compiled compute shader code</returns>
|
2020-05-06 03:02:28 +02:00
|
|
|
public ShaderBundle GetComputeShader(
|
2021-07-08 01:56:06 +02:00
|
|
|
GpuChannel channel,
|
|
|
|
GpuAccessorState gas,
|
2020-02-02 04:25:52 +01:00
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ShaderBundle> list);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (ShaderBundle cachedCpShader in list)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
if (IsShaderEqual(channel.MemoryManager, cachedCpShader, gpuVa))
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
return cachedCpShader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
TranslatorContext[] shaderContexts = new TranslatorContext[1];
|
|
|
|
|
|
|
|
shaderContexts[0] = DecodeComputeShader(
|
2021-07-08 01:56:06 +02:00
|
|
|
channel,
|
|
|
|
gas,
|
2020-02-02 04:25:52 +01:00
|
|
|
gpuVa,
|
|
|
|
localSizeX,
|
|
|
|
localSizeY,
|
|
|
|
localSizeZ,
|
|
|
|
localMemorySize,
|
|
|
|
sharedMemorySize);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
bool isShaderCacheEnabled = _cacheManager != null;
|
2020-12-13 08:46:07 +01:00
|
|
|
bool isShaderCacheReadOnly = false;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
Hash128 programCodeHash = default;
|
2020-12-01 22:48:31 +01:00
|
|
|
GuestShaderCacheEntry[] shaderCacheEntries = null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-27 00:50:26 +01:00
|
|
|
// Current shader cache doesn't support bindless textures
|
|
|
|
if (shaderContexts[0].UsedFeatures.HasFlag(FeatureFlags.Bindless))
|
|
|
|
{
|
|
|
|
isShaderCacheEnabled = false;
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
if (isShaderCacheEnabled)
|
|
|
|
{
|
2020-12-13 08:46:07 +01:00
|
|
|
isShaderCacheReadOnly = _cacheManager.IsReadOnly;
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
// Compute hash and prepare data for shader disk cache comparison.
|
2021-08-11 20:59:42 +02:00
|
|
|
shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel, shaderContexts);
|
2020-12-01 22:48:31 +01:00
|
|
|
programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-08-11 22:27:00 +02:00
|
|
|
ShaderCodeHolder shader = TranslateShader(_dumper, channel.MemoryManager, shaderContexts[0], null, null);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
|
|
|
|
|
|
|
|
IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
hostProgram.CheckProgramLink(true);
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
cpShader = new ShaderBundle(hostProgram, shader);
|
|
|
|
|
2021-03-27 00:50:26 +01:00
|
|
|
if (isShaderCacheEnabled)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
|
|
|
_cpProgramsDiskCache.Add(programCodeHash, cpShader);
|
2020-12-13 08:46:07 +01:00
|
|
|
|
|
|
|
if (!isShaderCacheReadOnly)
|
|
|
|
{
|
|
|
|
_cacheManager.SaveProgram(ref programCodeHash, CacheHelper.CreateGuestProgramDump(shaderCacheEntries), hostProgramBinary);
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
list = new List<ShaderBundle>();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
_cpPrograms.Add(gpuVa, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(cpShader);
|
|
|
|
|
|
|
|
return cpShader;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a graphics shader program from the shader cache.
|
|
|
|
/// This includes all the specified shader stages.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2021-07-11 22:20:40 +02:00
|
|
|
/// <param name="state">GPU state</param>
|
|
|
|
/// <param name="channel">GPU channel</param>
|
|
|
|
/// <param name="gas">GPU accessor state</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="addresses">Addresses of the shaders for each stage</param>
|
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2021-07-11 22:20:40 +02:00
|
|
|
public ShaderBundle GetGraphicsShader(ref ThreedClassState state, GpuChannel channel, GpuAccessorState gas, ShaderAddresses addresses)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
bool isCached = _gpPrograms.TryGetValue(addresses, out List<ShaderBundle> list);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (ShaderBundle cachedGpShaders in list)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
if (IsShaderEqual(channel.MemoryManager, cachedGpShaders, addresses))
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
return cachedGpShaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 00:42:17 +01:00
|
|
|
TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages + 1];
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(ref state);
|
2020-07-29 00:01:11 +02:00
|
|
|
|
|
|
|
TranslationFlags flags = DefaultFlags;
|
|
|
|
|
|
|
|
if (tfd != null)
|
|
|
|
{
|
|
|
|
flags |= TranslationFlags.Feedback;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
TranslationCounts counts = new TranslationCounts();
|
|
|
|
|
2019-11-14 19:26:40 +01:00
|
|
|
if (addresses.VertexA != 0)
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
shaderContexts[0] = DecodeGraphicsShader(channel, gas, counts, flags | TranslationFlags.VertexA, ShaderStage.Vertex, addresses.VertexA);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
shaderContexts[1] = DecodeGraphicsShader(channel, gas, counts, flags, ShaderStage.Vertex, addresses.Vertex);
|
|
|
|
shaderContexts[2] = DecodeGraphicsShader(channel, gas, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
|
|
|
|
shaderContexts[3] = DecodeGraphicsShader(channel, gas, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
|
|
|
|
shaderContexts[4] = DecodeGraphicsShader(channel, gas, counts, flags, ShaderStage.Geometry, addresses.Geometry);
|
|
|
|
shaderContexts[5] = DecodeGraphicsShader(channel, gas, counts, flags, ShaderStage.Fragment, addresses.Fragment);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
bool isShaderCacheEnabled = _cacheManager != null;
|
2020-12-13 08:46:07 +01:00
|
|
|
bool isShaderCacheReadOnly = false;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
Hash128 programCodeHash = default;
|
2020-12-01 22:48:31 +01:00
|
|
|
GuestShaderCacheEntry[] shaderCacheEntries = null;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-03-27 00:50:26 +01:00
|
|
|
// Current shader cache doesn't support bindless textures
|
|
|
|
for (int i = 0; i < shaderContexts.Length; i++)
|
|
|
|
{
|
|
|
|
if (shaderContexts[i] != null && shaderContexts[i].UsedFeatures.HasFlag(FeatureFlags.Bindless))
|
|
|
|
{
|
|
|
|
isShaderCacheEnabled = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
if (isShaderCacheEnabled)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-12-13 08:46:07 +01:00
|
|
|
isShaderCacheReadOnly = _cacheManager.IsReadOnly;
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
// Compute hash and prepare data for shader disk cache comparison.
|
2021-08-11 20:59:42 +02:00
|
|
|
shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(channel, shaderContexts);
|
2020-12-01 22:48:31 +01:00
|
|
|
programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries, tfd);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
ShaderBundle gpShaders;
|
|
|
|
|
|
|
|
// Search for the program hash in loaded shaders.
|
|
|
|
if (!isShaderCacheEnabled || !_gpProgramsDiskCache.TryGetValue(programCodeHash, out gpShaders))
|
|
|
|
{
|
|
|
|
if (isShaderCacheEnabled)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-11-13 00:15:34 +01:00
|
|
|
Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
// The shader isn't currently cached, translate it and compile it.
|
|
|
|
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
for (int stageIndex = 0; stageIndex < Constants.ShaderStages; stageIndex++)
|
|
|
|
{
|
|
|
|
shaders[stageIndex] = TranslateShader(_dumper, channel.MemoryManager, shaderContexts, stageIndex + 1);
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
List<IShader> hostShaders = new List<IShader>();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
|
|
|
{
|
|
|
|
ShaderProgram program = shaders[stage]?.Program;
|
2020-05-06 03:02:28 +02:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
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);
|
|
|
|
|
2021-03-29 22:52:25 +02:00
|
|
|
hostProgram.CheckProgramLink(true);
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
|
|
|
|
|
|
|
|
gpShaders = new ShaderBundle(hostProgram, shaders);
|
|
|
|
|
2021-03-27 00:50:26 +01:00
|
|
|
if (isShaderCacheEnabled)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
|
|
|
_gpProgramsDiskCache.Add(programCodeHash, gpShaders);
|
2020-12-13 08:46:07 +01:00
|
|
|
|
|
|
|
if (!isShaderCacheReadOnly)
|
|
|
|
{
|
|
|
|
_cacheManager.SaveProgram(ref programCodeHash, CacheHelper.CreateGuestProgramDump(shaderCacheEntries, tfd), hostProgramBinary);
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
list = new List<ShaderBundle>();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
_gpPrograms.Add(addresses, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(gpShaders);
|
|
|
|
|
|
|
|
return gpShaders;
|
|
|
|
}
|
|
|
|
|
2020-07-15 05:01:10 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets transform feedback state from the current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <returns>Four transform feedback descriptors for the enabled TFBs, or null if TFB is disabled</returns>
|
2021-07-11 22:20:40 +02:00
|
|
|
private static TransformFeedbackDescriptor[] GetTransformFeedbackDescriptors(ref ThreedClassState state)
|
2020-07-15 05:01:10 +02:00
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
bool tfEnable = state.TfEnable;
|
2020-07-15 05:01:10 +02:00
|
|
|
|
|
|
|
if (!tfEnable)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransformFeedbackDescriptor[] descs = new TransformFeedbackDescriptor[Constants.TotalTransformFeedbackBuffers];
|
|
|
|
|
|
|
|
for (int i = 0; i < Constants.TotalTransformFeedbackBuffers; i++)
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
var tf = state.TfState[i];
|
2020-07-15 05:01:10 +02:00
|
|
|
|
|
|
|
int length = (int)Math.Min((uint)tf.VaryingsCount, 0x80);
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
var varyingLocations = MemoryMarshal.Cast<uint, byte>(state.TfVaryingLocations[i].ToSpan()).Slice(0, length);
|
2020-07-15 05:01:10 +02:00
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
descs[i] = new TransformFeedbackDescriptor(tf.BufferIndex, tf.Stride, varyingLocations.ToArray());
|
2020-07-15 05:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return descs;
|
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
2020-05-06 03:02:28 +02:00
|
|
|
/// Checks if compute shader code in memory is equal to the cached shader.
|
2019-12-31 05:46:57 +01:00
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="cpShader">Cached compute shader</param>
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
|
|
|
|
/// <returns>True if the code is different, false otherwise</returns>
|
2021-06-29 19:32:02 +02:00
|
|
|
private static bool IsShaderEqual(MemoryManager memoryManager, ShaderBundle cpShader, ulong gpuVa)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
return IsShaderEqual(memoryManager, cpShader.Shaders[0], gpuVa);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
2020-05-06 03:02:28 +02:00
|
|
|
/// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
|
2019-12-31 05:46:57 +01:00
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="gpShaders">Cached graphics shaders</param>
|
|
|
|
/// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
|
|
|
|
/// <returns>True if the code is different, false otherwise</returns>
|
2021-06-29 19:32:02 +02:00
|
|
|
private static bool IsShaderEqual(MemoryManager memoryManager, ShaderBundle gpShaders, ShaderAddresses addresses)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
ShaderCodeHolder shader = gpShaders.Shaders[stage];
|
2019-11-14 19:26:40 +01:00
|
|
|
|
|
|
|
ulong gpuVa = 0;
|
|
|
|
|
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case 0: gpuVa = addresses.Vertex; break;
|
|
|
|
case 1: gpuVa = addresses.TessControl; break;
|
|
|
|
case 2: gpuVa = addresses.TessEvaluation; break;
|
|
|
|
case 3: gpuVa = addresses.Geometry; break;
|
|
|
|
case 4: gpuVa = addresses.Fragment; break;
|
|
|
|
}
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
if (!IsShaderEqual(memoryManager, shader, gpuVa, addresses.VertexA))
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
return false;
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
return true;
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the code of the specified cached shader is different from the code in memory.
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="shader">Cached shader to compare with</param>
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
|
2020-05-06 03:02:28 +02:00
|
|
|
/// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" binary shader code</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <returns>True if the code is different, false otherwise</returns>
|
2021-06-29 19:32:02 +02:00
|
|
|
private static bool IsShaderEqual(MemoryManager memoryManager, ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
if (shader == null)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
return true;
|
2020-01-05 17:40:21 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
ReadOnlySpan<byte> memoryCode = memoryManager.GetSpan(gpuVa, shader.Code.Length);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
bool equals = memoryCode.SequenceEqual(shader.Code);
|
|
|
|
|
|
|
|
if (equals && shader.Code2 != null)
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
memoryCode = memoryManager.GetSpan(gpuVaA, shader.Code2.Length);
|
2020-05-06 03:02:28 +02:00
|
|
|
|
|
|
|
equals = memoryCode.SequenceEqual(shader.Code2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return equals;
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
2020-11-13 00:15:34 +01:00
|
|
|
/// Decode the binary Maxwell shader code to a translator context.
|
2019-12-31 05:46:57 +01:00
|
|
|
/// </summary>
|
2021-07-08 01:56:06 +02:00
|
|
|
/// <param name="channel">GPU channel</param>
|
|
|
|
/// <param name="gas">GPU accessor state</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
|
|
|
|
/// <param name="localSizeX">Local group size X of the computer shader</param>
|
|
|
|
/// <param name="localSizeY">Local group size Y of the computer shader</param>
|
|
|
|
/// <param name="localSizeZ">Local group size Z of the computer shader</param>
|
2020-02-02 04:25:52 +01:00
|
|
|
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
|
|
|
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
2020-11-13 00:15:34 +01:00
|
|
|
/// <returns>The generated translator context</returns>
|
|
|
|
private TranslatorContext DecodeComputeShader(
|
2021-07-08 01:56:06 +02:00
|
|
|
GpuChannel channel,
|
|
|
|
GpuAccessorState gas,
|
2020-02-02 04:25:52 +01:00
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-08 01:56:06 +02:00
|
|
|
GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gas, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
|
2019-12-16 05:59:46 +01:00
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, DefaultFlags | TranslationFlags.Compute);
|
|
|
|
return Translator.CreateContext(gpuVa, gpuAccessor, options);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <summary>
|
2020-11-13 00:15:34 +01:00
|
|
|
/// Decode the binary Maxwell shader code to a translator context.
|
2019-12-31 05:46:57 +01:00
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
|
|
|
|
/// </remarks>
|
2021-07-11 22:20:40 +02:00
|
|
|
/// <param name="channel">GPU channel</param>
|
|
|
|
/// <param name="gas">GPU accessor state</param>
|
2020-11-08 12:10:00 +01:00
|
|
|
/// <param name="counts">Cumulative shader resource counts</param>
|
2020-07-29 00:01:11 +02:00
|
|
|
/// <param name="flags">Flags that controls shader translation</param>
|
2019-12-31 05:46:57 +01:00
|
|
|
/// <param name="stage">Shader stage</param>
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the shader code</param>
|
2020-11-13 00:15:34 +01:00
|
|
|
/// <returns>The generated translator context</returns>
|
|
|
|
private TranslatorContext DecodeGraphicsShader(
|
2021-07-11 22:20:40 +02:00
|
|
|
GpuChannel channel,
|
|
|
|
GpuAccessorState gas,
|
2020-11-08 12:10:00 +01:00
|
|
|
TranslationCounts counts,
|
|
|
|
TranslationFlags flags,
|
|
|
|
ShaderStage stage,
|
2021-02-08 00:42:17 +01:00
|
|
|
ulong gpuVa)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
return null;
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gas, (int)stage - 1);
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags);
|
|
|
|
return Translator.CreateContext(gpuVa, gpuAccessor, options, counts);
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Translates a previously generated translator context to something that the host API accepts.
|
|
|
|
/// </summary>
|
2021-08-11 22:27:00 +02:00
|
|
|
/// <param name="dumper">Optional shader code dumper</param>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
|
2021-08-11 22:27:00 +02:00
|
|
|
/// <param name="stages">Translator context of all available shader stages</param>
|
|
|
|
/// <param name="stageIndex">Index on the stages array to translate</param>
|
2020-11-13 00:15:34 +01:00
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2021-08-11 22:27:00 +02:00
|
|
|
private static ShaderCodeHolder TranslateShader(
|
|
|
|
ShaderDumper dumper,
|
2021-06-29 19:32:02 +02:00
|
|
|
MemoryManager memoryManager,
|
2021-08-11 22:27:00 +02:00
|
|
|
TranslatorContext[] stages,
|
|
|
|
int stageIndex)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
TranslatorContext currentStage = stages[stageIndex];
|
|
|
|
TranslatorContext nextStage = GetNextStageContext(stages, stageIndex);
|
|
|
|
TranslatorContext vertexA = stageIndex == 1 ? stages[0] : null;
|
|
|
|
|
|
|
|
return TranslateShader(dumper, memoryManager, currentStage, nextStage, vertexA);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the next shader stage context, from an array of contexts and index of the current stage.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stages">Translator context of all available shader stages</param>
|
|
|
|
/// <param name="stageIndex">Index on the stages array to translate</param>
|
|
|
|
/// <returns>The translator context of the next stage, or null if inexistent</returns>
|
|
|
|
private static TranslatorContext GetNextStageContext(TranslatorContext[] stages, int stageIndex)
|
|
|
|
{
|
|
|
|
for (int nextStageIndex = stageIndex + 1; nextStageIndex < stages.Length; nextStageIndex++)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
if (stages[nextStageIndex] != null)
|
|
|
|
{
|
|
|
|
return stages[nextStageIndex];
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Translates a previously generated translator context to something that the host API accepts.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dumper">Optional shader code dumper</param>
|
|
|
|
/// <param name="memoryManager">Memory manager used to access the GPU memory where the shader is located</param>
|
|
|
|
/// <param name="currentStage">Translator context of the stage to be translated</param>
|
|
|
|
/// <param name="nextStage">Translator context of the next active stage, if existent</param>
|
|
|
|
/// <param name="vertexA">Optional translator context of the shader that should be combined</param>
|
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
|
|
|
private static ShaderCodeHolder TranslateShader(
|
|
|
|
ShaderDumper dumper,
|
|
|
|
MemoryManager memoryManager,
|
|
|
|
TranslatorContext currentStage,
|
|
|
|
TranslatorContext nextStage,
|
|
|
|
TranslatorContext vertexA)
|
|
|
|
{
|
|
|
|
if (currentStage == null)
|
2020-11-13 00:15:34 +01:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
return null;
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (vertexA != null)
|
|
|
|
{
|
|
|
|
byte[] codeA = memoryManager.GetSpan(vertexA.Address, vertexA.Size).ToArray();
|
|
|
|
byte[] codeB = memoryManager.GetSpan(currentStage.Address, currentStage.Size).ToArray();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
ShaderDumpPaths pathsA = default;
|
|
|
|
ShaderDumpPaths pathsB = default;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (dumper != null)
|
2019-11-14 19:26:40 +01:00
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
pathsA = dumper.Dump(codeA, compute: false);
|
|
|
|
pathsB = dumper.Dump(codeB, compute: false);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
2020-05-06 03:02:28 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
ShaderProgram program = currentStage.Translate(out ShaderProgramInfo shaderProgramInfo, nextStage, vertexA);
|
|
|
|
|
|
|
|
pathsB.Prepend(program);
|
|
|
|
pathsA.Prepend(program);
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-11 22:27:00 +02:00
|
|
|
byte[] code = memoryManager.GetSpan(currentStage.Address, currentStage.Size).ToArray();
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
ShaderDumpPaths paths = dumper?.Dump(code, currentStage.Stage == ShaderStage.Compute) ?? default;
|
2019-11-14 19:26:40 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
ShaderProgram program = currentStage.Translate(out ShaderProgramInfo shaderProgramInfo, nextStage);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
paths.Prepend(program);
|
2019-12-16 05:59:46 +01:00
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
return new ShaderCodeHolder(program, shaderProgramInfo, code);
|
2020-04-22 01:35:28 +02:00
|
|
|
}
|
2019-12-01 03:53:09 +01:00
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes the shader cache, deleting all the cached shaders.
|
|
|
|
/// It's an error to use the shader cache after disposal.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (List<ShaderBundle> list in _cpPrograms.Values)
|
2019-12-31 23:09:49 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (ShaderBundle bundle in list)
|
2019-12-31 23:09:49 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
bundle.Dispose();
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (List<ShaderBundle> list in _gpPrograms.Values)
|
2019-12-31 23:09:49 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
foreach (ShaderBundle bundle in list)
|
2019-12-31 23:09:49 +01:00
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
bundle.Dispose();
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
_cacheManager?.Dispose();
|
|
|
|
}
|
2019-11-14 19:26:40 +01:00
|
|
|
}
|
2021-06-15 02:09:59 +02:00
|
|
|
}
|