2020-11-12 18:15:34 -05:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
2019-11-14 13:26:40 -05:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-11-12 18:15:34 -05:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader.Cache;
|
|
|
|
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
2019-11-14 13:26:40 -05:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using Ryujinx.Graphics.Shader;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-11-12 18:15:34 -05:00
|
|
|
using System.Diagnostics;
|
2021-03-02 19:39:36 -05:00
|
|
|
using System.Threading;
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
|
|
|
{
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Memory cache of shader code.
|
|
|
|
/// </summary>
|
2019-12-31 17:09:49 -05:00
|
|
|
class ShaderCache : IDisposable
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2019-12-15 23:59:46 -05:00
|
|
|
private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
private readonly GpuContext _context;
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
private readonly ShaderDumper _dumper;
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
|
|
|
|
private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
private CacheManager _cacheManager;
|
|
|
|
|
|
|
|
private Dictionary<Hash128, ShaderBundle> _gpProgramsDiskCache;
|
|
|
|
private Dictionary<Hash128, ShaderBundle> _cpProgramsDiskCache;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-17 16:20:17 -05:00
|
|
|
/// Version of the codegen (to be changed when codegen or guest format change).
|
2020-11-12 18:15:34 -05:00
|
|
|
/// </summary>
|
2021-04-20 06:33:54 -04:00
|
|
|
private const ulong ShaderCodeGenVersion = 2217;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
2021-03-02 19:39:36 -05:00
|
|
|
// Progress reporting helpers
|
2021-03-22 14:40:07 -04:00
|
|
|
private volatile int _shaderCount;
|
|
|
|
private volatile int _totalShaderCount;
|
|
|
|
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
2021-03-02 19:39:36 -05:00
|
|
|
|
2019-12-30 23:46:57 -05: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 13:26:40 -05:00
|
|
|
public ShaderCache(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
_dumper = new ShaderDumper();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
_cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
|
|
|
|
_gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
|
2020-11-12 18:15:34 -05: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 02:46:07 -05: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-12 18:15:34 -05:00
|
|
|
|
|
|
|
ReadOnlySpan<Hash128> guestProgramList = _cacheManager.GetGuestProgramList();
|
|
|
|
|
2021-03-22 14:40:07 -04:00
|
|
|
using AutoResetEvent progressReportEvent = new AutoResetEvent(false);
|
|
|
|
|
2021-03-02 19:39:36 -05:00
|
|
|
_shaderCount = 0;
|
2021-03-22 14:40:07 -04:00
|
|
|
_totalShaderCount = guestProgramList.Length;
|
2021-03-02 19:39:36 -05:00
|
|
|
|
2021-03-22 14:40:07 -04: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-02 19:39:36 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
for (int programIndex = 0; programIndex < guestProgramList.Length; programIndex++)
|
|
|
|
{
|
|
|
|
Hash128 key = guestProgramList[programIndex];
|
|
|
|
|
|
|
|
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.
|
2020-12-13 02:46:07 -05:00
|
|
|
invalidEntries?.Add(key);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
program = new ShaderProgram(entry.Header.Stage, "");
|
2020-11-12 18:15:34 -05:00
|
|
|
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 });
|
|
|
|
|
2020-12-13 02:46:07 -05:00
|
|
|
if (!isReadOnly)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
2020-12-13 02:46:07 -05:00
|
|
|
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-12 18:15:34 -05:00
|
|
|
|
2020-12-13 02:46:07 -05:00
|
|
|
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_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>();
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
TransformFeedbackDescriptor[] tfd = CacheHelper.ReadTransformFeedbackInformation(ref guestProgramReadOnlySpan, fileHeader);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
|
|
|
TranslationFlags flags = DefaultFlags;
|
|
|
|
|
|
|
|
if (tfd != null)
|
|
|
|
{
|
2021-03-19 15:07:37 -04:00
|
|
|
flags |= TranslationFlags.Feedback;
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
program = new ShaderProgram(entry.Header.Stage, "");
|
2020-11-12 18:15:34 -05:00
|
|
|
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
TranslatorContext translatorContext = Translator.CreateContext(0, gpuAccessor, flags, counts);
|
|
|
|
TranslatorContext translatorContext2 = Translator.CreateContext((ulong)entry.Header.Size, gpuAccessor, flags | TranslationFlags.VertexA, counts);
|
|
|
|
|
|
|
|
program = translatorContext.Translate(out shaderProgramInfo, translatorContext2);
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
program = new ShaderProgram(entry.Header.Stage, "");
|
2020-11-12 18:15:34 -05:00
|
|
|
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);
|
|
|
|
|
2020-12-13 02:46:07 -05:00
|
|
|
if (!isReadOnly)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
2020-12-13 02:46:07 -05:00
|
|
|
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-12 18:15:34 -05:00
|
|
|
|
2020-12-13 02:46:07 -05:00
|
|
|
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
|
|
|
|
}
|
2021-03-02 19:39:36 -05:00
|
|
|
|
2021-03-22 14:40:07 -04:00
|
|
|
_shaderCount = programIndex + 1;
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
|
2020-12-13 02:46:07 -05:00
|
|
|
if (!isReadOnly)
|
|
|
|
{
|
|
|
|
// Remove entries that are broken in the cache
|
|
|
|
_cacheManager.RemoveManifestEntries(invalidEntries);
|
|
|
|
_cacheManager.FlushToArchive();
|
|
|
|
_cacheManager.Synchronize();
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
|
2021-03-22 14:40:07 -04:00
|
|
|
progressReportEvent.Set();
|
|
|
|
progressReportThread?.Join();
|
|
|
|
|
|
|
|
ShaderCacheStateChanged?.Invoke(ShaderCacheState.Loaded, _shaderCount, _totalShaderCount);
|
2021-03-02 19:39:36 -05:00
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Gpu, $"Shader cache loaded {_shaderCount} entries.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-03-22 14:40:07 -04:00
|
|
|
/// Raises ShaderCacheStateChanged events periodically.
|
2021-03-02 19:39:36 -05:00
|
|
|
/// </summary>
|
2021-03-22 14:40:07 -04:00
|
|
|
private void ReportProgress(object state)
|
2021-03-02 19:39:36 -05:00
|
|
|
{
|
2021-03-22 14:40:07 -04:00
|
|
|
const int refreshRate = 50; // ms
|
|
|
|
|
|
|
|
AutoResetEvent endEvent = (AutoResetEvent)state;
|
|
|
|
|
|
|
|
int count = 0;
|
2021-03-02 19:39:36 -05:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-03-22 14:40:07 -04:00
|
|
|
int newCount = _shaderCount;
|
|
|
|
|
|
|
|
if (count != newCount)
|
|
|
|
{
|
|
|
|
ShaderCacheStateChanged?.Invoke(ShaderCacheState.Loading, newCount, _totalShaderCount);
|
|
|
|
count = newCount;
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
2021-03-22 14:40:07 -04:00
|
|
|
while (!endEvent.WaitOne(refreshRate));
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a compute shader from the cache.
|
|
|
|
/// </summary>
|
2020-01-01 10:39:09 -05:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2020-04-21 19:35:28 -04:00
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-12-30 23:46:57 -05: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-01 22:25:52 -05: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-30 23:46:57 -05:00
|
|
|
/// <returns>Compiled compute shader code</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
public ShaderBundle GetComputeShader(
|
2020-04-21 19:35:28 -04:00
|
|
|
GpuState state,
|
2020-02-01 22:25:52 -05:00
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ShaderBundle> list);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
foreach (ShaderBundle cachedCpShader in list)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
if (IsShaderEqual(cachedCpShader, gpuVa))
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
|
|
|
return cachedCpShader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
TranslatorContext[] shaderContexts = new TranslatorContext[1];
|
|
|
|
|
|
|
|
shaderContexts[0] = DecodeComputeShader(
|
2020-04-21 19:35:28 -04:00
|
|
|
state,
|
2020-02-01 22:25:52 -05:00
|
|
|
gpuVa,
|
|
|
|
localSizeX,
|
|
|
|
localSizeY,
|
|
|
|
localSizeZ,
|
|
|
|
localMemorySize,
|
|
|
|
sharedMemorySize);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
bool isShaderCacheEnabled = _cacheManager != null;
|
2020-12-13 02:46:07 -05:00
|
|
|
bool isShaderCacheReadOnly = false;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
|
|
|
Hash128 programCodeHash = default;
|
2020-12-01 16:48:31 -05:00
|
|
|
GuestShaderCacheEntry[] shaderCacheEntries = null;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
2021-03-26 19:50:26 -04:00
|
|
|
// Current shader cache doesn't support bindless textures
|
|
|
|
if (shaderContexts[0].UsedFeatures.HasFlag(FeatureFlags.Bindless))
|
|
|
|
{
|
|
|
|
isShaderCacheEnabled = false;
|
|
|
|
}
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
if (isShaderCacheEnabled)
|
|
|
|
{
|
2020-12-13 02:46:07 -05:00
|
|
|
isShaderCacheReadOnly = _cacheManager.IsReadOnly;
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
// Compute hash and prepare data for shader disk cache comparison.
|
2020-12-01 16:48:31 -05:00
|
|
|
shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(_context.MemoryManager, shaderContexts);
|
|
|
|
programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries);
|
2020-11-12 18:15:34 -05: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.
|
|
|
|
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);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
cpShader = new ShaderBundle(hostProgram, shader);
|
|
|
|
|
2021-03-26 19:50:26 -04:00
|
|
|
if (isShaderCacheEnabled)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
|
|
|
_cpProgramsDiskCache.Add(programCodeHash, cpShader);
|
2020-12-13 02:46:07 -05:00
|
|
|
|
|
|
|
if (!isShaderCacheReadOnly)
|
|
|
|
{
|
|
|
|
_cacheManager.SaveProgram(ref programCodeHash, CacheHelper.CreateGuestProgramDump(shaderCacheEntries), hostProgramBinary);
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
list = new List<ShaderBundle>();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
_cpPrograms.Add(gpuVa, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(cpShader);
|
|
|
|
|
|
|
|
return cpShader;
|
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a graphics shader program from the shader cache.
|
|
|
|
/// This includes all the specified shader stages.
|
|
|
|
/// </summary>
|
2020-01-01 10:39:09 -05:00
|
|
|
/// <remarks>
|
|
|
|
/// This automatically translates, compiles and adds the code to the cache if not present.
|
|
|
|
/// </remarks>
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="addresses">Addresses of the shaders for each stage</param>
|
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
public ShaderBundle GetGraphicsShader(GpuState state, ShaderAddresses addresses)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
bool isCached = _gpPrograms.TryGetValue(addresses, out List<ShaderBundle> list);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
if (isCached)
|
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
foreach (ShaderBundle cachedGpShaders in list)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
if (IsShaderEqual(cachedGpShaders, addresses))
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
|
|
|
return cachedGpShaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages + 1];
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state);
|
2020-07-28 18:01:11 -04:00
|
|
|
|
|
|
|
TranslationFlags flags = DefaultFlags;
|
|
|
|
|
|
|
|
if (tfd != null)
|
|
|
|
{
|
|
|
|
flags |= TranslationFlags.Feedback;
|
|
|
|
}
|
|
|
|
|
2020-11-08 06:10:00 -05:00
|
|
|
TranslationCounts counts = new TranslationCounts();
|
|
|
|
|
2019-11-14 13:26:40 -05:00
|
|
|
if (addresses.VertexA != 0)
|
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags | TranslationFlags.VertexA, ShaderStage.Vertex, addresses.VertexA);
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
|
|
|
|
shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
|
|
|
|
shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
|
|
|
|
shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
|
|
|
|
shaderContexts[5] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
bool isShaderCacheEnabled = _cacheManager != null;
|
2020-12-13 02:46:07 -05:00
|
|
|
bool isShaderCacheReadOnly = false;
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
Hash128 programCodeHash = default;
|
2020-12-01 16:48:31 -05:00
|
|
|
GuestShaderCacheEntry[] shaderCacheEntries = null;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
2021-03-26 19:50:26 -04: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-12 18:15:34 -05:00
|
|
|
if (isShaderCacheEnabled)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-12-13 02:46:07 -05:00
|
|
|
isShaderCacheReadOnly = _cacheManager.IsReadOnly;
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
// Compute hash and prepare data for shader disk cache comparison.
|
2020-12-01 16:48:31 -05:00
|
|
|
shaderCacheEntries = CacheHelper.CreateShaderCacheEntries(_context.MemoryManager, shaderContexts);
|
|
|
|
programCodeHash = CacheHelper.ComputeGuestHashFromCache(shaderCacheEntries, tfd);
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
ShaderBundle gpShaders;
|
|
|
|
|
|
|
|
// Search for the program hash in loaded shaders.
|
|
|
|
if (!isShaderCacheEnabled || !_gpProgramsDiskCache.TryGetValue(programCodeHash, out gpShaders))
|
|
|
|
{
|
|
|
|
if (isShaderCacheEnabled)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-11-12 18:15:34 -05:00
|
|
|
Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
// The shader isn't currently cached, translate it and compile it.
|
|
|
|
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
shaders[0] = TranslateShader(shaderContexts[1], shaderContexts[0]);
|
|
|
|
shaders[1] = TranslateShader(shaderContexts[2]);
|
|
|
|
shaders[2] = TranslateShader(shaderContexts[3]);
|
|
|
|
shaders[3] = TranslateShader(shaderContexts[4]);
|
|
|
|
shaders[4] = TranslateShader(shaderContexts[5]);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
List<IShader> hostShaders = new List<IShader>();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
|
|
|
{
|
|
|
|
ShaderProgram program = shaders[stage]?.Program;
|
2020-05-05 21:02:28 -04:00
|
|
|
|
2020-11-12 18:15:34 -05: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);
|
|
|
|
|
|
|
|
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
|
|
|
|
|
|
|
|
gpShaders = new ShaderBundle(hostProgram, shaders);
|
|
|
|
|
2021-03-26 19:50:26 -04:00
|
|
|
if (isShaderCacheEnabled)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
|
|
|
_gpProgramsDiskCache.Add(programCodeHash, gpShaders);
|
2020-12-13 02:46:07 -05:00
|
|
|
|
|
|
|
if (!isShaderCacheReadOnly)
|
|
|
|
{
|
|
|
|
_cacheManager.SaveProgram(ref programCodeHash, CacheHelper.CreateGuestProgramDump(shaderCacheEntries, tfd), hostProgramBinary);
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
if (!isCached)
|
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
list = new List<ShaderBundle>();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
_gpPrograms.Add(addresses, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(gpShaders);
|
|
|
|
|
|
|
|
return gpShaders;
|
|
|
|
}
|
|
|
|
|
2020-07-14 23:01:10 -04: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>
|
|
|
|
private TransformFeedbackDescriptor[] GetTransformFeedbackDescriptors(GpuState state)
|
|
|
|
{
|
|
|
|
bool tfEnable = state.Get<Boolean32>(MethodOffset.TfEnable);
|
|
|
|
|
|
|
|
if (!tfEnable)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransformFeedbackDescriptor[] descs = new TransformFeedbackDescriptor[Constants.TotalTransformFeedbackBuffers];
|
|
|
|
|
|
|
|
for (int i = 0; i < Constants.TotalTransformFeedbackBuffers; i++)
|
|
|
|
{
|
|
|
|
var tf = state.Get<TfState>(MethodOffset.TfState, i);
|
|
|
|
|
|
|
|
int length = (int)Math.Min((uint)tf.VaryingsCount, 0x80);
|
|
|
|
|
|
|
|
var varyingLocations = state.GetSpan(MethodOffset.TfVaryingLocations + i * 0x80, length).ToArray();
|
|
|
|
|
|
|
|
descs[i] = new TransformFeedbackDescriptor(tf.BufferIndex, tf.Stride, varyingLocations);
|
|
|
|
}
|
|
|
|
|
|
|
|
return descs;
|
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
2020-05-05 21:02:28 -04:00
|
|
|
/// Checks if compute shader code in memory is equal to the cached shader.
|
2019-12-30 23:46:57 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <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>
|
2020-05-05 21:02:28 -04:00
|
|
|
private bool IsShaderEqual(ShaderBundle cpShader, ulong gpuVa)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
return IsShaderEqual(cpShader.Shaders[0], gpuVa);
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
2020-05-05 21:02:28 -04:00
|
|
|
/// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
|
2019-12-30 23:46:57 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <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>
|
2020-05-05 21:02:28 -04:00
|
|
|
private bool IsShaderEqual(ShaderBundle gpShaders, ShaderAddresses addresses)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2019-12-31 17:09:49 -05:00
|
|
|
for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
ShaderCodeHolder shader = gpShaders.Shaders[stage];
|
2019-11-14 13:26:40 -05: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;
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
if (!IsShaderEqual(shader, gpuVa, addresses.VertexA))
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
return false;
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
return true;
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the code of the specified cached shader is different from the code in memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="shader">Cached shader to compare with</param>
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
|
2020-05-05 21:02:28 -04:00
|
|
|
/// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" binary shader code</param>
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <returns>True if the code is different, false otherwise</returns>
|
2020-05-05 21:02:28 -04:00
|
|
|
private bool IsShaderEqual(ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
2020-01-05 11:40:21 -05:00
|
|
|
if (shader == null)
|
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
return true;
|
2020-01-05 11:40:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-25 02:39:45 -04:00
|
|
|
ReadOnlySpan<byte> memoryCode = _context.MemoryManager.GetSpan(gpuVa, shader.Code.Length);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
bool equals = memoryCode.SequenceEqual(shader.Code);
|
|
|
|
|
|
|
|
if (equals && shader.Code2 != null)
|
|
|
|
{
|
2020-07-25 02:39:45 -04:00
|
|
|
memoryCode = _context.MemoryManager.GetSpan(gpuVaA, shader.Code2.Length);
|
2020-05-05 21:02:28 -04:00
|
|
|
|
|
|
|
equals = memoryCode.SequenceEqual(shader.Code2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return equals;
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
2020-11-12 18:15:34 -05:00
|
|
|
/// Decode the binary Maxwell shader code to a translator context.
|
2019-12-30 23:46:57 -05:00
|
|
|
/// </summary>
|
2020-04-21 19:35:28 -04:00
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-12-30 23:46:57 -05: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-01 22:25:52 -05: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-12 18:15:34 -05:00
|
|
|
/// <returns>The generated translator context</returns>
|
|
|
|
private TranslatorContext DecodeComputeShader(
|
2020-04-21 19:35:28 -04:00
|
|
|
GpuState state,
|
2020-02-01 22:25:52 -05:00
|
|
|
ulong gpuVa,
|
|
|
|
int localSizeX,
|
|
|
|
int localSizeY,
|
|
|
|
int localSizeZ,
|
|
|
|
int localMemorySize,
|
|
|
|
int sharedMemorySize)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
|
2019-12-15 23:59:46 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
return Translator.CreateContext(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <summary>
|
2020-11-12 18:15:34 -05:00
|
|
|
/// Decode the binary Maxwell shader code to a translator context.
|
2019-12-30 23:46:57 -05:00
|
|
|
/// </summary>
|
2020-01-01 10:39:09 -05:00
|
|
|
/// <remarks>
|
|
|
|
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
|
|
|
|
/// </remarks>
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <param name="state">Current GPU state</param>
|
2020-11-08 06:10:00 -05:00
|
|
|
/// <param name="counts">Cumulative shader resource counts</param>
|
2020-07-28 18:01:11 -04:00
|
|
|
/// <param name="flags">Flags that controls shader translation</param>
|
2019-12-30 23:46:57 -05:00
|
|
|
/// <param name="stage">Shader stage</param>
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the shader code</param>
|
2020-11-12 18:15:34 -05:00
|
|
|
/// <returns>The generated translator context</returns>
|
|
|
|
private TranslatorContext DecodeGraphicsShader(
|
2020-11-08 06:10:00 -05:00
|
|
|
GpuState state,
|
|
|
|
TranslationCounts counts,
|
|
|
|
TranslationFlags flags,
|
|
|
|
ShaderStage stage,
|
2021-02-07 18:42:17 -05:00
|
|
|
ulong gpuVa)
|
2019-11-14 13:26:40 -05:00
|
|
|
{
|
|
|
|
if (gpuVa == 0)
|
|
|
|
{
|
2020-01-05 11:40:21 -05:00
|
|
|
return null;
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
|
2020-11-12 18:15:34 -05:00
|
|
|
}
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Translates a previously generated translator context to something that the host API accepts.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="translatorContext">Current translator context to translate</param>
|
2021-02-07 18:42:17 -05:00
|
|
|
/// <param name="translatorContext2">Optional translator context of the shader that should be combined</param>
|
2020-11-12 18:15:34 -05:00
|
|
|
/// <returns>Compiled graphics shader code</returns>
|
2021-02-07 18:42:17 -05:00
|
|
|
private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext, TranslatorContext translatorContext2 = null)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
|
|
|
if (translatorContext == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
if (translatorContext2 != null)
|
2020-11-12 18:15:34 -05:00
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
byte[] codeA = _context.MemoryManager.GetSpan(translatorContext2.Address, translatorContext2.Size).ToArray();
|
2020-11-12 18:15:34 -05:00
|
|
|
byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
|
|
|
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
|
|
|
|
_dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo, translatorContext2);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
2019-11-14 13:26:40 -05:00
|
|
|
if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
|
|
|
|
{
|
|
|
|
program.Prepend("// " + codePathB);
|
|
|
|
program.Prepend("// " + fullPathB);
|
|
|
|
program.Prepend("// " + codePathA);
|
|
|
|
program.Prepend("// " + fullPathA);
|
|
|
|
}
|
2020-05-05 21:02:28 -04:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-12 18:15:34 -05:00
|
|
|
byte[] code = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2021-01-26 12:27:18 -05:00
|
|
|
_dumper.Dump(code, translatorContext.Stage == ShaderStage.Compute, out string fullPath, out string codePath);
|
2019-11-14 13:26:40 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
|
|
|
|
|
2019-11-14 13:26:40 -05:00
|
|
|
if (fullPath != null && codePath != null)
|
|
|
|
{
|
|
|
|
program.Prepend("// " + codePath);
|
|
|
|
program.Prepend("// " + fullPath);
|
|
|
|
}
|
2019-12-15 23:59:46 -05:00
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
return new ShaderCodeHolder(program, shaderProgramInfo, code);
|
2020-04-21 19:35:28 -04:00
|
|
|
}
|
2019-11-30 21:53:09 -05:00
|
|
|
}
|
2019-12-31 17:09:49 -05: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-05 21:02:28 -04:00
|
|
|
foreach (List<ShaderBundle> list in _cpPrograms.Values)
|
2019-12-31 17:09:49 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
foreach (ShaderBundle bundle in list)
|
2019-12-31 17:09:49 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
bundle.Dispose();
|
2019-12-31 17:09:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:02:28 -04:00
|
|
|
foreach (List<ShaderBundle> list in _gpPrograms.Values)
|
2019-12-31 17:09:49 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
foreach (ShaderBundle bundle in list)
|
2019-12-31 17:09:49 -05:00
|
|
|
{
|
2020-05-05 21:02:28 -04:00
|
|
|
bundle.Dispose();
|
2019-12-31 17:09:49 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 18:15:34 -05:00
|
|
|
|
|
|
|
_cacheManager?.Dispose();
|
|
|
|
}
|
2019-11-14 13:26:40 -05:00
|
|
|
}
|
|
|
|
}
|