Salieri: shader cache (#1701)
Here come Salieri, my implementation of a disk shader cache! "I'm sure you know why I named it that." "It doesn't really mean anything." This implementation collects shaders at runtime and cache them to be later compiled when starting a game.
This commit is contained in:
parent
7166e82c3c
commit
48f6570557
57 changed files with 3589 additions and 396 deletions
595
Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs
Normal file
595
Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs
Normal file
|
@ -0,0 +1,595 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a cache collection handling one shader cache.
|
||||
/// </summary>
|
||||
class CacheCollection : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Possible operation to do on the <see cref="_fileWriterWorkerQueue"/>.
|
||||
/// </summary>
|
||||
private enum CacheFileOperation
|
||||
{
|
||||
/// <summary>
|
||||
/// Save a new entry in the temp cache.
|
||||
/// </summary>
|
||||
SaveTempEntry,
|
||||
|
||||
/// <summary>
|
||||
/// Save the hash manifest.
|
||||
/// </summary>
|
||||
SaveManifest,
|
||||
|
||||
/// <summary>
|
||||
/// Flush temporary cache to archive.
|
||||
/// </summary>
|
||||
FlushToArchive,
|
||||
|
||||
/// <summary>
|
||||
/// Signal when hitting this point. This is useful to know if all previous operations were performed.
|
||||
/// </summary>
|
||||
Synchronize
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represent an operation to perform on the <see cref="_fileWriterWorkerQueue"/>.
|
||||
/// </summary>
|
||||
private class CacheFileOperationTask
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of operation to perform.
|
||||
/// </summary>
|
||||
public CacheFileOperation Type;
|
||||
|
||||
/// <summary>
|
||||
/// The data associated to this operation or null.
|
||||
/// </summary>
|
||||
public object Data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data associated to the <see cref="CacheFileOperation.SaveTempEntry"/> operation.
|
||||
/// </summary>
|
||||
private class CacheFileSaveEntryTaskData
|
||||
{
|
||||
/// <summary>
|
||||
/// The key of the entry to cache.
|
||||
/// </summary>
|
||||
public Hash128 Key;
|
||||
|
||||
/// <summary>
|
||||
/// The value of the entry to cache.
|
||||
/// </summary>
|
||||
public byte[] Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The directory of the shader cache.
|
||||
/// </summary>
|
||||
private readonly string _cacheDirectory;
|
||||
|
||||
/// <summary>
|
||||
/// The version of the cache.
|
||||
/// </summary>
|
||||
private readonly ulong _version;
|
||||
|
||||
/// <summary>
|
||||
/// The hash type of the cache.
|
||||
/// </summary>
|
||||
private readonly CacheHashType _hashType;
|
||||
|
||||
/// <summary>
|
||||
/// The graphics API of the cache.
|
||||
/// </summary>
|
||||
private readonly CacheGraphicsApi _graphicsApi;
|
||||
|
||||
/// <summary>
|
||||
/// The table of all the hash registered in the cache.
|
||||
/// </summary>
|
||||
private HashSet<Hash128> _hashTable;
|
||||
|
||||
/// <summary>
|
||||
/// The queue of operations to be performed by the file writer worker.
|
||||
/// </summary>
|
||||
private AsyncWorkQueue<CacheFileOperationTask> _fileWriterWorkerQueue;
|
||||
|
||||
/// <summary>
|
||||
/// Main storage of the cache collection.
|
||||
/// </summary>
|
||||
private ZipArchive _cacheArchive;
|
||||
|
||||
/// <summary>
|
||||
/// Immutable copy of the hash table.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<Hash128> HashTable => _hashTable.ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Get the temp path to the cache data directory.
|
||||
/// </summary>
|
||||
/// <returns>The temp path to the cache data directory</returns>
|
||||
private string GetCacheTempDataPath() => Path.Combine(_cacheDirectory, "temp");
|
||||
|
||||
/// <summary>
|
||||
/// The path to the cache archive file.
|
||||
/// </summary>
|
||||
/// <returns>The path to the cache archive file</returns>
|
||||
private string GetArchivePath() => Path.Combine(_cacheDirectory, "cache.zip");
|
||||
|
||||
/// <summary>
|
||||
/// The path to the cache manifest file.
|
||||
/// </summary>
|
||||
/// <returns>The path to the cache manifest file</returns>
|
||||
private string GetManifestPath() => Path.Combine(_cacheDirectory, "cache.info");
|
||||
|
||||
/// <summary>
|
||||
/// Create a new temp path to the given cached file via its hash.
|
||||
/// </summary>
|
||||
/// <param name="key">The hash of the cached data</param>
|
||||
/// <returns>New path to the given cached file</returns>
|
||||
private string GenCacheTempFilePath(Hash128 key) => Path.Combine(GetCacheTempDataPath(), key.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Create a new cache collection.
|
||||
/// </summary>
|
||||
/// <param name="baseCacheDirectory">The directory of the shader cache</param>
|
||||
/// <param name="hashType">The hash type of the shader cache</param>
|
||||
/// <param name="graphicsApi">The graphics api of the shader cache</param>
|
||||
/// <param name="shaderProvider">The shader provider name of the shader cache</param>
|
||||
/// <param name="cacheName">The name of the cache</param>
|
||||
/// <param name="version">The version of the cache</param>
|
||||
public CacheCollection(string baseCacheDirectory, CacheHashType hashType, CacheGraphicsApi graphicsApi, string shaderProvider, string cacheName, ulong version)
|
||||
{
|
||||
if (hashType != CacheHashType.XxHash128)
|
||||
{
|
||||
throw new NotImplementedException($"{hashType}");
|
||||
}
|
||||
|
||||
_cacheDirectory = GenerateCachePath(baseCacheDirectory, graphicsApi, shaderProvider, cacheName);
|
||||
_graphicsApi = graphicsApi;
|
||||
_hashType = hashType;
|
||||
_version = version;
|
||||
_hashTable = new HashSet<Hash128>();
|
||||
|
||||
Load();
|
||||
|
||||
_fileWriterWorkerQueue = new AsyncWorkQueue<CacheFileOperationTask>(HandleCacheTask, $"CacheCollection.Worker.{cacheName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the cache manifest file and recreate it if invalid.
|
||||
/// </summary>
|
||||
private void Load()
|
||||
{
|
||||
bool isInvalid = false;
|
||||
|
||||
if (!Directory.Exists(_cacheDirectory))
|
||||
{
|
||||
isInvalid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
string manifestPath = GetManifestPath();
|
||||
|
||||
if (File.Exists(manifestPath))
|
||||
{
|
||||
Memory<byte> rawManifest = File.ReadAllBytes(manifestPath);
|
||||
|
||||
if (MemoryMarshal.TryRead(rawManifest.Span, out CacheManifestHeader manifestHeader))
|
||||
{
|
||||
Memory<byte> hashTableRaw = rawManifest.Slice(Unsafe.SizeOf<CacheManifestHeader>());
|
||||
|
||||
isInvalid = !manifestHeader.IsValid(_version, _graphicsApi, _hashType, hashTableRaw.Span);
|
||||
|
||||
if (!isInvalid)
|
||||
{
|
||||
ReadOnlySpan<Hash128> hashTable = MemoryMarshal.Cast<byte, Hash128>(hashTableRaw.Span);
|
||||
|
||||
foreach (Hash128 hash in hashTable)
|
||||
{
|
||||
_hashTable.Add(hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
isInvalid = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isInvalid)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Gpu, $"Shader collection \"{_cacheDirectory}\" got invalidated, cache will need to be rebuilt.");
|
||||
|
||||
if (Directory.Exists(_cacheDirectory))
|
||||
{
|
||||
Directory.Delete(_cacheDirectory, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(_cacheDirectory);
|
||||
|
||||
SaveManifest();
|
||||
}
|
||||
|
||||
FlushToArchive();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove given entries from the manifest.
|
||||
/// </summary>
|
||||
/// <param name="entries">Entries to remove from the manifest</param>
|
||||
public void RemoveManifestEntries(HashSet<Hash128> entries)
|
||||
{
|
||||
lock (_hashTable)
|
||||
{
|
||||
foreach (Hash128 entry in entries)
|
||||
{
|
||||
_hashTable.Remove(entry);
|
||||
}
|
||||
|
||||
SaveManifest();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue a task to flush temporary files to the archive on the worker.
|
||||
/// </summary>
|
||||
public void FlushToArchiveAsync()
|
||||
{
|
||||
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
|
||||
{
|
||||
Type = CacheFileOperation.FlushToArchive
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wait for all tasks before this given point to be done.
|
||||
/// </summary>
|
||||
public void Synchronize()
|
||||
{
|
||||
using (ManualResetEvent evnt = new ManualResetEvent(false))
|
||||
{
|
||||
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
|
||||
{
|
||||
Type = CacheFileOperation.Synchronize,
|
||||
Data = evnt
|
||||
});
|
||||
|
||||
evnt.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush temporary files to the archive.
|
||||
/// </summary>
|
||||
/// <remarks>This dispose <see cref="_cacheArchive"/> if not null and reinstantiate it.</remarks>
|
||||
private void FlushToArchive()
|
||||
{
|
||||
EnsureArchiveUpToDate();
|
||||
|
||||
// Open the zip in readonly to avoid anyone modifying/corrupting it during normal operations.
|
||||
_cacheArchive = ZipFile.Open(GetArchivePath(), ZipArchiveMode.Read);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save temporary files not in archive.
|
||||
/// </summary>
|
||||
/// <remarks>This dispose <see cref="_cacheArchive"/> if not null.</remarks>
|
||||
public void EnsureArchiveUpToDate()
|
||||
{
|
||||
// First close previous opened instance if found.
|
||||
if (_cacheArchive != null)
|
||||
{
|
||||
_cacheArchive.Dispose();
|
||||
}
|
||||
|
||||
string archivePath = GetArchivePath();
|
||||
|
||||
// Open the zip in read/write.
|
||||
_cacheArchive = ZipFile.Open(archivePath, ZipArchiveMode.Update);
|
||||
|
||||
Logger.Info?.Print(LogClass.Gpu, $"Updating cache collection archive {archivePath}...");
|
||||
|
||||
// Update the content of the zip.
|
||||
lock (_hashTable)
|
||||
{
|
||||
foreach (Hash128 hash in _hashTable)
|
||||
{
|
||||
string cacheTempFilePath = GenCacheTempFilePath(hash);
|
||||
|
||||
if (File.Exists(cacheTempFilePath))
|
||||
{
|
||||
string cacheHash = $"{hash}";
|
||||
|
||||
ZipArchiveEntry entry = _cacheArchive.GetEntry(cacheHash);
|
||||
|
||||
entry?.Delete();
|
||||
|
||||
_cacheArchive.CreateEntryFromFile(cacheTempFilePath, cacheHash);
|
||||
File.Delete(cacheTempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Close the instance to force a flush.
|
||||
_cacheArchive.Dispose();
|
||||
_cacheArchive = null;
|
||||
|
||||
string cacheTempDataPath = GetCacheTempDataPath();
|
||||
|
||||
// Create the cache data path if missing.
|
||||
if (!Directory.Exists(cacheTempDataPath))
|
||||
{
|
||||
Directory.CreateDirectory(cacheTempDataPath);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Info?.Print(LogClass.Gpu, $"Updated cache collection archive {archivePath}.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the manifest file.
|
||||
/// </summary>
|
||||
private void SaveManifest()
|
||||
{
|
||||
CacheManifestHeader manifestHeader = new CacheManifestHeader(_version, _graphicsApi, _hashType);
|
||||
|
||||
byte[] data;
|
||||
|
||||
lock (_hashTable)
|
||||
{
|
||||
data = new byte[Unsafe.SizeOf<CacheManifestHeader>() + _hashTable.Count * Unsafe.SizeOf<Hash128>()];
|
||||
|
||||
// CacheManifestHeader has the same size as a Hash128.
|
||||
Span<Hash128> dataSpan = MemoryMarshal.Cast<byte, Hash128>(data.AsSpan()).Slice(1);
|
||||
|
||||
int i = 0;
|
||||
|
||||
foreach (Hash128 hash in _hashTable)
|
||||
{
|
||||
dataSpan[i++] = hash;
|
||||
}
|
||||
}
|
||||
|
||||
manifestHeader.UpdateChecksum(data.AsSpan().Slice(Unsafe.SizeOf<CacheManifestHeader>()));
|
||||
|
||||
MemoryMarshal.Write(data, ref manifestHeader);
|
||||
|
||||
File.WriteAllBytes(GetManifestPath(), data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate the path to the cache directory.
|
||||
/// </summary>
|
||||
/// <param name="baseCacheDirectory">The base of the cache directory</param>
|
||||
/// <param name="graphicsApi">The graphics api in use</param>
|
||||
/// <param name="shaderProvider">The name of the shader provider in use</param>
|
||||
/// <param name="cacheName">The name of the cache</param>
|
||||
/// <returns>The path to the cache directory</returns>
|
||||
private static string GenerateCachePath(string baseCacheDirectory, CacheGraphicsApi graphicsApi, string shaderProvider, string cacheName)
|
||||
{
|
||||
string graphicsApiName = graphicsApi switch
|
||||
{
|
||||
CacheGraphicsApi.OpenGL => "opengl",
|
||||
CacheGraphicsApi.OpenGLES => "opengles",
|
||||
CacheGraphicsApi.Vulkan => "vulkan",
|
||||
CacheGraphicsApi.DirectX => "directx",
|
||||
CacheGraphicsApi.Metal => "metal",
|
||||
CacheGraphicsApi.Guest => "guest",
|
||||
_ => throw new NotImplementedException(graphicsApi.ToString()),
|
||||
};
|
||||
|
||||
return Path.Combine(baseCacheDirectory, graphicsApiName, shaderProvider, cacheName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a cached file with the given hash.
|
||||
/// </summary>
|
||||
/// <param name="keyHash">The given hash</param>
|
||||
/// <returns>The cached file if present or null</returns>
|
||||
public byte[] GetValueRaw(ref Hash128 keyHash)
|
||||
{
|
||||
return GetValueRawFromArchive(ref keyHash) ?? GetValueRawFromFile(ref keyHash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a cached file with the given hash that is present in the archive.
|
||||
/// </summary>
|
||||
/// <param name="keyHash">The given hash</param>
|
||||
/// <returns>The cached file if present or null</returns>
|
||||
private byte[] GetValueRawFromArchive(ref Hash128 keyHash)
|
||||
{
|
||||
bool found;
|
||||
|
||||
lock (_hashTable)
|
||||
{
|
||||
found = _hashTable.Contains(keyHash);
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
ZipArchiveEntry archiveEntry = _cacheArchive.GetEntry($"{keyHash}");
|
||||
|
||||
if (archiveEntry != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] result = new byte[archiveEntry.Length];
|
||||
|
||||
using (Stream archiveStream = archiveEntry.Open())
|
||||
{
|
||||
archiveStream.Read(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.Gpu, $"Cannot load cache file {keyHash} from archive");
|
||||
Logger.Error?.Print(LogClass.Gpu, e.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a cached file with the given hash that is not present in the archive.
|
||||
/// </summary>
|
||||
/// <param name="keyHash">The given hash</param>
|
||||
/// <returns>The cached file if present or null</returns>
|
||||
private byte[] GetValueRawFromFile(ref Hash128 keyHash)
|
||||
{
|
||||
bool found;
|
||||
|
||||
lock (_hashTable)
|
||||
{
|
||||
found = _hashTable.Contains(keyHash);
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
string cacheTempFilePath = GenCacheTempFilePath(keyHash);
|
||||
|
||||
try
|
||||
{
|
||||
return File.ReadAllBytes(GenCacheTempFilePath(keyHash));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.Gpu, $"Cannot load cache file at {cacheTempFilePath}");
|
||||
Logger.Error?.Print(LogClass.Gpu, e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void HandleCacheTask(CacheFileOperationTask task)
|
||||
{
|
||||
switch (task.Type)
|
||||
{
|
||||
case CacheFileOperation.SaveTempEntry:
|
||||
SaveTempEntry((CacheFileSaveEntryTaskData)task.Data);
|
||||
break;
|
||||
case CacheFileOperation.SaveManifest:
|
||||
SaveManifest();
|
||||
break;
|
||||
case CacheFileOperation.FlushToArchive:
|
||||
FlushToArchive();
|
||||
break;
|
||||
case CacheFileOperation.Synchronize:
|
||||
((ManualResetEvent)task.Data).Set();
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException($"{task.Type}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save a new entry in the temp cache.
|
||||
/// </summary>
|
||||
/// <param name="entry">The entry to save in the temp cache</param>
|
||||
private void SaveTempEntry(CacheFileSaveEntryTaskData entry)
|
||||
{
|
||||
string tempPath = GenCacheTempFilePath(entry.Key);
|
||||
|
||||
File.WriteAllBytes(tempPath, entry.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a new value in the cache with a given hash.
|
||||
/// </summary>
|
||||
/// <param name="keyHash">The hash to use for the value in the cache</param>
|
||||
/// <param name="value">The value to cache</param>
|
||||
public void AddValue(ref Hash128 keyHash, byte[] value)
|
||||
{
|
||||
Debug.Assert(value != null);
|
||||
Debug.Assert(GetValueRaw(ref keyHash) != null);
|
||||
|
||||
bool isAlreadyPresent;
|
||||
|
||||
lock (_hashTable)
|
||||
{
|
||||
isAlreadyPresent = !_hashTable.Add(keyHash);
|
||||
}
|
||||
|
||||
if (isAlreadyPresent)
|
||||
{
|
||||
// NOTE: Used for debug
|
||||
File.WriteAllBytes(GenCacheTempFilePath(new Hash128()), value);
|
||||
|
||||
throw new InvalidOperationException($"Cache collision found on {GenCacheTempFilePath(keyHash)}");
|
||||
}
|
||||
|
||||
// Queue file change operations
|
||||
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
|
||||
{
|
||||
Type = CacheFileOperation.SaveTempEntry,
|
||||
Data = new CacheFileSaveEntryTaskData
|
||||
{
|
||||
Key = keyHash,
|
||||
Value = value
|
||||
}
|
||||
});
|
||||
|
||||
// Save the manifest changes
|
||||
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
|
||||
{
|
||||
Type = CacheFileOperation.SaveManifest,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace a value at the given hash in the cache.
|
||||
/// </summary>
|
||||
/// <param name="keyHash">The hash to use for the value in the cache</param>
|
||||
/// <param name="value">The value to cache</param>
|
||||
public void ReplaceValue(ref Hash128 keyHash, byte[] value)
|
||||
{
|
||||
Debug.Assert(value != null);
|
||||
|
||||
// Only queue file change operations
|
||||
_fileWriterWorkerQueue.Add(new CacheFileOperationTask
|
||||
{
|
||||
Type = CacheFileOperation.SaveTempEntry,
|
||||
Data = new CacheFileSaveEntryTaskData
|
||||
{
|
||||
Key = keyHash,
|
||||
Value = value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// Make sure all operations on _fileWriterWorkerQueue are done.
|
||||
Synchronize();
|
||||
|
||||
_fileWriterWorkerQueue.Dispose();
|
||||
EnsureArchiveUpToDate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
168
Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs
Normal file
168
Ryujinx.Graphics.Gpu/Shader/Cache/CacheManager.cs
Normal file
|
@ -0,0 +1,168 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.Common.Configuration;
|
||||
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// Global Manager of the shader cache.
|
||||
/// </summary>
|
||||
class CacheManager : IDisposable
|
||||
{
|
||||
private CacheGraphicsApi _graphicsApi;
|
||||
private CacheHashType _hashType;
|
||||
private string _shaderProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Cache storing raw Maxwell shaders as programs.
|
||||
/// </summary>
|
||||
private CacheCollection _guestProgramCache;
|
||||
|
||||
/// <summary>
|
||||
/// Cache storing raw host programs.
|
||||
/// </summary>
|
||||
private CacheCollection _hostProgramCache;
|
||||
|
||||
/// <summary>
|
||||
/// Version of the guest cache shader (to increment when guest cache structure change).
|
||||
/// </summary>
|
||||
private const ulong GuestCacheVersion = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new cache manager instance
|
||||
/// </summary>
|
||||
/// <param name="graphicsApi">The graphics api in use</param>
|
||||
/// <param name="hashType">The hash type in use for the cache</param>
|
||||
/// <param name="shaderProvider">The name of the codegen provider</param>
|
||||
/// <param name="titleId">The guest application title ID</param>
|
||||
/// <param name="shaderCodeGenVersion">Version of the codegen</param>
|
||||
public CacheManager(CacheGraphicsApi graphicsApi, CacheHashType hashType, string shaderProvider, string titleId, ulong shaderCodeGenVersion)
|
||||
{
|
||||
_graphicsApi = graphicsApi;
|
||||
_hashType = hashType;
|
||||
_shaderProvider = shaderProvider;
|
||||
|
||||
string baseCacheDirectory = Path.Combine(AppDataManager.GamesDirPath, titleId, "cache", "shader");
|
||||
|
||||
_guestProgramCache = new CacheCollection(baseCacheDirectory, _hashType, CacheGraphicsApi.Guest, "", "program", GuestCacheVersion);
|
||||
_hostProgramCache = new CacheCollection(baseCacheDirectory, _hashType, _graphicsApi, _shaderProvider, "host", shaderCodeGenVersion);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Entries to remove from the manifest.
|
||||
/// </summary>
|
||||
/// <param name="entries">Entries to remove from the manifest of all caches</param>
|
||||
public void RemoveManifestEntries(HashSet<Hash128> entries)
|
||||
{
|
||||
_guestProgramCache.RemoveManifestEntries(entries);
|
||||
_hostProgramCache.RemoveManifestEntries(entries);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue a task to flush temporary files to the archives.
|
||||
/// </summary>
|
||||
public void FlushToArchive()
|
||||
{
|
||||
_guestProgramCache.FlushToArchiveAsync();
|
||||
_hostProgramCache.FlushToArchiveAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wait for all tasks before this given point to be done.
|
||||
/// </summary>
|
||||
public void Synchronize()
|
||||
{
|
||||
_guestProgramCache.Synchronize();
|
||||
_hostProgramCache.Synchronize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the hash of some data using the current cache hashing algorithm.
|
||||
/// </summary>
|
||||
/// <param name="data">Some data to generate a hash for.</param>
|
||||
/// <returns>The hash of some data using the current hashing algorithm of the cache</returns>
|
||||
public Hash128 ComputeHash(ReadOnlySpan<byte> data)
|
||||
{
|
||||
return XXHash128.ComputeHash(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save a shader program not present in the program cache.
|
||||
/// </summary>
|
||||
/// <param name="programCodeHash">Target program code hash</param>
|
||||
/// <param name="guestProgram">Guest program raw data</param>
|
||||
/// <param name="hostProgram">Host program raw data</param>
|
||||
public void SaveProgram(ref Hash128 programCodeHash, byte[] guestProgram, byte[] hostProgram)
|
||||
{
|
||||
_guestProgramCache.AddValue(ref programCodeHash, guestProgram);
|
||||
_hostProgramCache.AddValue(ref programCodeHash, hostProgram);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a host shader program not present in the program cache.
|
||||
/// </summary>
|
||||
/// <param name="programCodeHash">Target program code hash</param>
|
||||
/// <param name="data">Host program raw data</param>
|
||||
public void AddHostProgram(ref Hash128 programCodeHash, byte[] data)
|
||||
{
|
||||
_hostProgramCache.AddValue(ref programCodeHash, data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace a host shader program present in the program cache.
|
||||
/// </summary>
|
||||
/// <param name="programCodeHash">Target program code hash</param>
|
||||
/// <param name="data">Host program raw data</param>
|
||||
public void ReplaceHostProgram(ref Hash128 programCodeHash, byte[] data)
|
||||
{
|
||||
_hostProgramCache.ReplaceValue(ref programCodeHash, data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all guest program hashes.
|
||||
/// </summary>
|
||||
/// <returns>All guest program hashes</returns>
|
||||
public ReadOnlySpan<Hash128> GetGuestProgramList()
|
||||
{
|
||||
return _guestProgramCache.HashTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a host program by hash.
|
||||
/// </summary>
|
||||
/// <param name="hash">The given hash</param>
|
||||
/// <returns>The host program if present or null</returns>
|
||||
public byte[] GetHostProgramByHash(ref Hash128 hash)
|
||||
{
|
||||
return _hostProgramCache.GetValueRaw(ref hash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a guest program by hash.
|
||||
/// </summary>
|
||||
/// <param name="hash">The given hash</param>
|
||||
/// <returns>The guest program if present or null</returns>
|
||||
public byte[] GetGuestProgramByHash(ref Hash128 hash)
|
||||
{
|
||||
return _guestProgramCache.GetValueRaw(ref hash);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_guestProgramCache.Dispose();
|
||||
_hostProgramCache.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Graphics API type accepted by the shader cache.
|
||||
/// </summary>
|
||||
enum CacheGraphicsApi : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// OpenGL Core
|
||||
/// </summary>
|
||||
OpenGL,
|
||||
|
||||
/// <summary>
|
||||
/// OpenGL ES
|
||||
/// </summary>
|
||||
OpenGLES,
|
||||
|
||||
/// <summary>
|
||||
/// Vulkan
|
||||
/// </summary>
|
||||
Vulkan,
|
||||
|
||||
/// <summary>
|
||||
/// DirectX
|
||||
/// </summary>
|
||||
DirectX,
|
||||
|
||||
/// <summary>
|
||||
/// Metal
|
||||
/// </summary>
|
||||
Metal,
|
||||
|
||||
/// <summary>
|
||||
/// Guest, used to cache games raw shader programs.
|
||||
/// </summary>
|
||||
Guest
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Hash algorithm accepted by the shader cache.
|
||||
/// </summary>
|
||||
enum CacheHashType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// xxHash128
|
||||
/// </summary>
|
||||
XxHash128
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header of the shader cache manifest.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
|
||||
struct CacheManifestHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The version of the cache.
|
||||
/// </summary>
|
||||
public ulong Version;
|
||||
|
||||
/// <summary>
|
||||
/// The graphics api used for this cache.
|
||||
/// </summary>
|
||||
public CacheGraphicsApi GraphicsApi;
|
||||
|
||||
/// <summary>
|
||||
/// The hash type used for this cache.
|
||||
/// </summary>
|
||||
public CacheHashType HashType;
|
||||
|
||||
/// <summary>
|
||||
/// CRC-16 checksum over the data in the file.
|
||||
/// </summary>
|
||||
public ushort TableChecksum;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new cache manifest header.
|
||||
/// </summary>
|
||||
/// <param name="version">The version of the cache</param>
|
||||
/// <param name="graphicsApi">The graphics api used for this cache</param>
|
||||
/// <param name="hashType">The hash type used for this cache</param>
|
||||
public CacheManifestHeader(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType)
|
||||
{
|
||||
Version = version;
|
||||
GraphicsApi = graphicsApi;
|
||||
HashType = hashType;
|
||||
TableChecksum = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the checksum in the header.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to perform the checksum on</param>
|
||||
public void UpdateChecksum(ReadOnlySpan<byte> data)
|
||||
{
|
||||
TableChecksum = CalculateCrc16(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate a CRC-16 over data.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to perform the CRC-16 on</param>
|
||||
/// <returns>A CRC-16 over data</returns>
|
||||
private static ushort CalculateCrc16(ReadOnlySpan<byte> data)
|
||||
{
|
||||
int crc = 0;
|
||||
|
||||
const ushort poly = 0x1021;
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
crc ^= data[i] << 8;
|
||||
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
crc <<= 1;
|
||||
|
||||
if ((crc & 0x10000) != 0)
|
||||
{
|
||||
crc = (crc ^ poly) & 0xFFFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (ushort)crc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check the validity of the header.
|
||||
/// </summary>
|
||||
/// <param name="version">The target version in use</param>
|
||||
/// <param name="graphicsApi">The target graphics api in use</param>
|
||||
/// <param name="hashType">The target hash type in use</param>
|
||||
/// <param name="data">The data after this header</param>
|
||||
/// <returns>True if the header is valid</returns>
|
||||
public bool IsValid(ulong version, CacheGraphicsApi graphicsApi, CacheHashType hashType, ReadOnlySpan<byte> data)
|
||||
{
|
||||
return Version == version && GraphicsApi == graphicsApi && HashType == hashType && TableChecksum == CalculateCrc16(data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
using Ryujinx.Graphics.Shader;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header of a cached guest gpu accessor.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = 1)]
|
||||
struct GuestGpuAccessorHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of texture descriptors.
|
||||
/// </summary>
|
||||
public int TextureDescriptorCount;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size X for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeX;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size Y for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeY;
|
||||
|
||||
/// <summary>
|
||||
/// Local Size Z for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalSizeZ;
|
||||
|
||||
/// <summary>
|
||||
/// Local Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeLocalMemorySize;
|
||||
|
||||
/// <summary>
|
||||
/// Shared Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
public int ComputeSharedMemorySize;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public int Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Current primitive topology for geometry shaders.
|
||||
/// </summary>
|
||||
public InputTopology PrimitiveTopology;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent a cached shader entry in a guest shader program.
|
||||
/// </summary>
|
||||
class GuestShaderCacheEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of the cached shader entry.
|
||||
/// </summary>
|
||||
public GuestShaderCacheEntryHeader Header { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The code of this shader.
|
||||
/// </summary>
|
||||
/// <remarks>If a Vertex A is present, this also contains the code 2 section.</remarks>
|
||||
public byte[] Code { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The textures descriptors used for this shader.
|
||||
/// </summary>
|
||||
public Dictionary<int, GuestTextureDescriptor> TextureDescriptors { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="GuestShaderCacheEntry"/>.
|
||||
/// </summary>
|
||||
/// <param name="header">The header of the cached shader entry</param>
|
||||
/// <param name="code">The code of this shader</param>
|
||||
private GuestShaderCacheEntry(GuestShaderCacheEntryHeader header, byte[] code)
|
||||
{
|
||||
Header = header;
|
||||
Code = code;
|
||||
TextureDescriptors = new Dictionary<int, GuestTextureDescriptor>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a raw cached user shader program into an array of shader cache entry.
|
||||
/// </summary>
|
||||
/// <param name="data">The raw cached user shader program</param>
|
||||
/// <param name="fileHeader">The user shader program header</param>
|
||||
/// <returns>An array of shader cache entry</returns>
|
||||
public static GuestShaderCacheEntry[] Parse(ref ReadOnlySpan<byte> data, out GuestShaderCacheHeader fileHeader)
|
||||
{
|
||||
fileHeader = MemoryMarshal.Read<GuestShaderCacheHeader>(data);
|
||||
|
||||
data = data.Slice(Unsafe.SizeOf<GuestShaderCacheHeader>());
|
||||
|
||||
ReadOnlySpan<GuestShaderCacheEntryHeader> entryHeaders = MemoryMarshal.Cast<byte, GuestShaderCacheEntryHeader>(data.Slice(0, fileHeader.Count * Unsafe.SizeOf<GuestShaderCacheEntryHeader>()));
|
||||
|
||||
data = data.Slice(fileHeader.Count * Unsafe.SizeOf<GuestShaderCacheEntryHeader>());
|
||||
|
||||
GuestShaderCacheEntry[] result = new GuestShaderCacheEntry[fileHeader.Count];
|
||||
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
GuestShaderCacheEntryHeader header = entryHeaders[i];
|
||||
|
||||
// Ignore empty entries
|
||||
if (header.Size == 0 && header.SizeA == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] code = data.Slice(0, header.Size + header.SizeA).ToArray();
|
||||
|
||||
data = data.Slice(header.Size + header.SizeA);
|
||||
|
||||
result[i] = new GuestShaderCacheEntry(header, code);
|
||||
|
||||
ReadOnlySpan<GuestTextureDescriptor> textureDescriptors = MemoryMarshal.Cast<byte, GuestTextureDescriptor>(data.Slice(0, header.GpuAccessorHeader.TextureDescriptorCount * Unsafe.SizeOf<GuestTextureDescriptor>()));
|
||||
|
||||
foreach (GuestTextureDescriptor textureDescriptor in textureDescriptors)
|
||||
{
|
||||
result[i].TextureDescriptors.Add((int)textureDescriptor.Handle, textureDescriptor);
|
||||
}
|
||||
|
||||
data = data.Slice(header.GpuAccessorHeader.TextureDescriptorCount * Unsafe.SizeOf<GuestTextureDescriptor>());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using Ryujinx.Graphics.Shader;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a guest shader entry in a guest shader program.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x30)]
|
||||
struct GuestShaderCacheEntryHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The stage of this shader.
|
||||
/// </summary>
|
||||
public ShaderStage Stage;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved3;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the code section.
|
||||
/// </summary>
|
||||
public int Size;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the code2 section if present. (Vertex A)
|
||||
/// </summary>
|
||||
public int SizeA;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public int Reserved4;
|
||||
|
||||
/// <summary>
|
||||
/// The header of the cached gpu accessor.
|
||||
/// </summary>
|
||||
public GuestGpuAccessorHeader GpuAccessorHeader;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new guest shader entry header.
|
||||
/// </summary>
|
||||
/// <param name="stage">The stage of this shader</param>
|
||||
/// <param name="size">The size of the code section</param>
|
||||
/// <param name="sizeA">The size of the code2 section if present (Vertex A)</param>
|
||||
/// <param name="gpuAccessorHeader">The header of the cached gpu accessor</param>
|
||||
public GuestShaderCacheEntryHeader(ShaderStage stage, int size, int sizeA, GuestGpuAccessorHeader gpuAccessorHeader) : this()
|
||||
{
|
||||
Stage = stage;
|
||||
Size = size;
|
||||
SizeA = sizeA;
|
||||
GpuAccessorHeader = gpuAccessorHeader;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a shader program in the guest cache.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x10)]
|
||||
struct GuestShaderCacheHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of shaders defining this program.
|
||||
/// </summary>
|
||||
public byte Count;
|
||||
|
||||
/// <summary>
|
||||
/// The count of transform feedback data used in this program.
|
||||
/// </summary>
|
||||
public byte TransformFeedbackCount;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ulong Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new guest shader cache header.
|
||||
/// </summary>
|
||||
/// <param name="count">The count of shaders defining this program</param>
|
||||
/// <param name="transformFeedbackCount">The count of transform feedback data used in this program</param>
|
||||
public GuestShaderCacheHeader(byte count, byte transformFeedbackCount) : this()
|
||||
{
|
||||
Count = count;
|
||||
TransformFeedbackCount = transformFeedbackCount;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Header for transform feedback.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
|
||||
struct GuestShaderCacheTransformFeedbackHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The buffer index of the transform feedback.
|
||||
/// </summary>
|
||||
public int BufferIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The stride of the transform feedback.
|
||||
/// </summary>
|
||||
public int Stride;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the varying location buffer of the transform feedback.
|
||||
/// </summary>
|
||||
public int VaryingLocationsLength;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved/unused.
|
||||
/// </summary>
|
||||
public int Reserved1;
|
||||
|
||||
public GuestShaderCacheTransformFeedbackHeader(int bufferIndex, int stride, int varyingLocationsLength) : this()
|
||||
{
|
||||
BufferIndex = bufferIndex;
|
||||
Stride = stride;
|
||||
VaryingLocationsLength = varyingLocationsLength;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using Ryujinx.Graphics.Gpu.Image;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Mostly identical to TextureDescriptor from <see cref="Image"/> but we don't store the address of the texture and store its handle instead.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = 1)]
|
||||
struct GuestTextureDescriptor
|
||||
{
|
||||
public uint Handle;
|
||||
internal TextureDescriptor Descriptor;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Host shader entry used for binding information.
|
||||
/// </summary>
|
||||
class HostShaderCacheEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of the cached shader entry.
|
||||
/// </summary>
|
||||
public HostShaderCacheEntryHeader Header { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached constant buffers.
|
||||
/// </summary>
|
||||
public BufferDescriptor[] CBuffers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached storage buffers.
|
||||
/// </summary>
|
||||
public BufferDescriptor[] SBuffers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached texture descriptors.
|
||||
/// </summary>
|
||||
public TextureDescriptor[] Textures { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached image descriptors.
|
||||
/// </summary>
|
||||
public TextureDescriptor[] Images { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="HostShaderCacheEntry"/>.
|
||||
/// </summary>
|
||||
/// <param name="header">The header of the cached shader entry</param>
|
||||
/// <param name="cBuffers">Cached constant buffers</param>
|
||||
/// <param name="sBuffers">Cached storage buffers</param>
|
||||
/// <param name="textures">Cached texture descriptors</param>
|
||||
/// <param name="images">Cached image descriptors</param>
|
||||
private HostShaderCacheEntry(
|
||||
HostShaderCacheEntryHeader header,
|
||||
BufferDescriptor[] cBuffers,
|
||||
BufferDescriptor[] sBuffers,
|
||||
TextureDescriptor[] textures,
|
||||
TextureDescriptor[] images)
|
||||
{
|
||||
Header = header;
|
||||
CBuffers = cBuffers;
|
||||
SBuffers = sBuffers;
|
||||
Textures = textures;
|
||||
Images = images;
|
||||
}
|
||||
|
||||
private HostShaderCacheEntry()
|
||||
{
|
||||
Header = new HostShaderCacheEntryHeader();
|
||||
CBuffers = new BufferDescriptor[0];
|
||||
SBuffers = new BufferDescriptor[0];
|
||||
Textures = new TextureDescriptor[0];
|
||||
Images = new TextureDescriptor[0];
|
||||
}
|
||||
|
||||
private HostShaderCacheEntry(ShaderProgramInfo programInfo)
|
||||
{
|
||||
Header = new HostShaderCacheEntryHeader(programInfo.CBuffers.Count,
|
||||
programInfo.SBuffers.Count,
|
||||
programInfo.Textures.Count,
|
||||
programInfo.Images.Count,
|
||||
programInfo.UsesInstanceId);
|
||||
CBuffers = programInfo.CBuffers.ToArray();
|
||||
SBuffers = programInfo.SBuffers.ToArray();
|
||||
Textures = programInfo.Textures.ToArray();
|
||||
Images = programInfo.Images.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the host shader entry to a <see cref="ShaderProgramInfo"/>.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="ShaderProgramInfo"/> from this instance</returns>
|
||||
internal ShaderProgramInfo ToShaderProgramInfo()
|
||||
{
|
||||
return new ShaderProgramInfo(CBuffers, SBuffers, Textures, Images, Header.UsesInstanceId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a raw cached user shader program into an array of shader cache entry.
|
||||
/// </summary>
|
||||
/// <param name="data">The raw cached host shader</param>
|
||||
/// <param name="programCode">The host shader program</param>
|
||||
/// <returns>An array of shader cache entry</returns>
|
||||
internal static HostShaderCacheEntry[] Parse(ReadOnlySpan<byte> data, out ReadOnlySpan<byte> programCode)
|
||||
{
|
||||
HostShaderCacheHeader fileHeader = MemoryMarshal.Read<HostShaderCacheHeader>(data);
|
||||
|
||||
data = data.Slice(Unsafe.SizeOf<HostShaderCacheHeader>());
|
||||
|
||||
ReadOnlySpan<HostShaderCacheEntryHeader> entryHeaders = MemoryMarshal.Cast<byte, HostShaderCacheEntryHeader>(data.Slice(0, fileHeader.Count * Unsafe.SizeOf<HostShaderCacheEntryHeader>()));
|
||||
|
||||
data = data.Slice(fileHeader.Count * Unsafe.SizeOf<HostShaderCacheEntryHeader>());
|
||||
|
||||
HostShaderCacheEntry[] result = new HostShaderCacheEntry[fileHeader.Count];
|
||||
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
HostShaderCacheEntryHeader header = entryHeaders[i];
|
||||
|
||||
if (!header.InUse)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int cBufferDescriptorsSize = header.CBuffersCount * Unsafe.SizeOf<BufferDescriptor>();
|
||||
int sBufferDescriptorsSize = header.SBuffersCount * Unsafe.SizeOf<BufferDescriptor>();
|
||||
int textureDescriptorsSize = header.TexturesCount * Unsafe.SizeOf<TextureDescriptor>();
|
||||
int imageDescriptorsSize = header.ImagesCount * Unsafe.SizeOf<TextureDescriptor>();
|
||||
|
||||
ReadOnlySpan<BufferDescriptor> cBuffers = MemoryMarshal.Cast<byte, BufferDescriptor>(data.Slice(0, cBufferDescriptorsSize));
|
||||
data = data.Slice(cBufferDescriptorsSize);
|
||||
|
||||
ReadOnlySpan<BufferDescriptor> sBuffers = MemoryMarshal.Cast<byte, BufferDescriptor>(data.Slice(0, sBufferDescriptorsSize));
|
||||
data = data.Slice(sBufferDescriptorsSize);
|
||||
|
||||
ReadOnlySpan<TextureDescriptor> textureDescriptors = MemoryMarshal.Cast<byte, TextureDescriptor>(data.Slice(0, textureDescriptorsSize));
|
||||
data = data.Slice(textureDescriptorsSize);
|
||||
|
||||
ReadOnlySpan<TextureDescriptor> imageDescriptors = MemoryMarshal.Cast<byte, TextureDescriptor>(data.Slice(0, imageDescriptorsSize));
|
||||
data = data.Slice(imageDescriptorsSize);
|
||||
|
||||
result[i] = new HostShaderCacheEntry(header, cBuffers.ToArray(), sBuffers.ToArray(), textureDescriptors.ToArray(), imageDescriptors.ToArray());
|
||||
}
|
||||
|
||||
programCode = data.Slice(0, fileHeader.CodeSize);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new host shader cache file.
|
||||
/// </summary>
|
||||
/// <param name="programCode">The host shader program</param>
|
||||
/// <param name="codeHolders">The shaders code holder</param>
|
||||
/// <returns>Raw data of a new host shader cache file</returns>
|
||||
internal static byte[] Create(ReadOnlySpan<byte> programCode, ShaderCodeHolder[] codeHolders)
|
||||
{
|
||||
HostShaderCacheHeader header = new HostShaderCacheHeader((byte)codeHolders.Length, programCode.Length);
|
||||
|
||||
HostShaderCacheEntry[] entries = new HostShaderCacheEntry[codeHolders.Length];
|
||||
|
||||
for (int i = 0; i < codeHolders.Length; i++)
|
||||
{
|
||||
if (codeHolders[i] == null)
|
||||
{
|
||||
entries[i] = new HostShaderCacheEntry();
|
||||
}
|
||||
else
|
||||
{
|
||||
entries[i] = new HostShaderCacheEntry(codeHolders[i].Info);
|
||||
}
|
||||
}
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(stream);
|
||||
|
||||
writer.WriteStruct(header);
|
||||
|
||||
foreach (HostShaderCacheEntry entry in entries)
|
||||
{
|
||||
writer.WriteStruct(entry.Header);
|
||||
}
|
||||
|
||||
foreach (HostShaderCacheEntry entry in entries)
|
||||
{
|
||||
foreach (BufferDescriptor cBuffer in entry.CBuffers)
|
||||
{
|
||||
writer.WriteStruct(cBuffer);
|
||||
}
|
||||
|
||||
foreach (BufferDescriptor sBuffer in entry.SBuffers)
|
||||
{
|
||||
writer.WriteStruct(sBuffer);
|
||||
}
|
||||
|
||||
foreach (TextureDescriptor texture in entry.Textures)
|
||||
{
|
||||
writer.WriteStruct(texture);
|
||||
}
|
||||
|
||||
foreach (TextureDescriptor image in entry.Images)
|
||||
{
|
||||
writer.WriteStruct(image);
|
||||
}
|
||||
}
|
||||
|
||||
writer.Write(programCode);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// Host shader entry header used for binding information.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x14)]
|
||||
struct HostShaderCacheEntryHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// Count of constant buffer descriptors.
|
||||
/// </summary>
|
||||
public int CBuffersCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of storage buffer descriptors.
|
||||
/// </summary>
|
||||
public int SBuffersCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of texture descriptors.
|
||||
/// </summary>
|
||||
public int TexturesCount;
|
||||
|
||||
/// <summary>
|
||||
/// Count of image descriptors.
|
||||
/// </summary>
|
||||
public int ImagesCount;
|
||||
|
||||
/// <summary>
|
||||
/// Set to true if the shader uses instance id.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool UsesInstanceId;
|
||||
|
||||
/// <summary>
|
||||
/// Set to true if this entry is in use.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool InUse;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved / unused.
|
||||
/// </summary>
|
||||
public short Reserved;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new host shader cache entry header.
|
||||
/// </summary>
|
||||
/// <param name="cBuffersCount">Count of constant buffer descriptors</param>
|
||||
/// <param name="sBuffersCount">Count of storage buffer descriptors</param>
|
||||
/// <param name="texturesCount">Count of texture descriptors</param>
|
||||
/// <param name="imagesCount">Count of image descriptors</param>
|
||||
/// <param name="usesInstanceId">Set to true if the shader uses instance id</param>
|
||||
public HostShaderCacheEntryHeader(int cBuffersCount, int sBuffersCount, int texturesCount, int imagesCount, bool usesInstanceId) : this()
|
||||
{
|
||||
CBuffersCount = cBuffersCount;
|
||||
SBuffersCount = sBuffersCount;
|
||||
TexturesCount = texturesCount;
|
||||
ImagesCount = imagesCount;
|
||||
UsesInstanceId = usesInstanceId;
|
||||
InUse = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader.Cache.Definition
|
||||
{
|
||||
/// <summary>
|
||||
/// The header of a shader program in the guest cache.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0x1, Size = 0x10)]
|
||||
struct HostShaderCacheHeader
|
||||
{
|
||||
/// <summary>
|
||||
/// The count of shaders defining this program.
|
||||
/// </summary>
|
||||
public byte Count;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public byte Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// Unused/reserved.
|
||||
/// </summary>
|
||||
public ushort Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// Size of the shader binary.
|
||||
/// </summary>
|
||||
public int CodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new host shader cache header.
|
||||
/// </summary>
|
||||
/// <param name="count">The count of shaders defining this program</param>
|
||||
/// <param name="codeSize">The size of the shader binary</param>
|
||||
public HostShaderCacheHeader(byte count, int codeSize) : this()
|
||||
{
|
||||
Count = count;
|
||||
CodeSize = codeSize;
|
||||
}
|
||||
}
|
||||
}
|
154
Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs
Normal file
154
Ryujinx.Graphics.Gpu/Shader/CachedGpuAccessor.cs
Normal file
|
@ -0,0 +1,154 @@
|
|||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader
|
||||
{
|
||||
class CachedGpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
|
||||
{
|
||||
private readonly GpuContext _context;
|
||||
private readonly ReadOnlyMemory<byte> _data;
|
||||
private readonly GuestGpuAccessorHeader _header;
|
||||
private readonly Dictionary<int, Image.TextureDescriptor> _textureDescriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the cached GPU state accessor for shader translation.
|
||||
/// </summary>
|
||||
/// <param name="context">GPU context</param>
|
||||
/// <param name="data">The data of the shader</param>
|
||||
/// <param name="header">The cache of the GPU accessor</param>
|
||||
/// <param name="guestTextureDescriptors">The cache of the texture descriptors</param>
|
||||
public CachedGpuAccessor(GpuContext context, ReadOnlyMemory<byte> data, GuestGpuAccessorHeader header, Dictionary<int, GuestTextureDescriptor> guestTextureDescriptors)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_header = header;
|
||||
_textureDescriptors = new Dictionary<int, Image.TextureDescriptor>();
|
||||
|
||||
foreach (KeyValuePair<int, GuestTextureDescriptor> guestTextureDescriptor in guestTextureDescriptors)
|
||||
{
|
||||
_textureDescriptors.Add(guestTextureDescriptor.Key, guestTextureDescriptor.Value.Descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prints a log message.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to print</param>
|
||||
public void Log(string message)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads data from GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the data to be read</typeparam>
|
||||
/// <param name="address">GPU virtual address of the data</param>
|
||||
/// <returns>Data at the memory location</returns>
|
||||
public override T MemoryRead<T>(ulong address)
|
||||
{
|
||||
return MemoryMarshal.Cast<byte, T>(_data.Span.Slice((int)address))[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a given memory address is mapped.
|
||||
/// </summary>
|
||||
/// <param name="address">GPU virtual address to be checked</param>
|
||||
/// <returns>True if the address is mapped, false otherwise</returns>
|
||||
public bool MemoryMapped(ulong address)
|
||||
{
|
||||
return address < (ulong)_data.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries Local Size X for compute shaders.
|
||||
/// </summary>
|
||||
/// <returns>Local Size X</returns>
|
||||
public int QueryComputeLocalSizeX()
|
||||
{
|
||||
return _header.ComputeLocalSizeX;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries Local Size Y for compute shaders.
|
||||
/// </summary>
|
||||
/// <returns>Local Size Y</returns>
|
||||
public int QueryComputeLocalSizeY()
|
||||
{
|
||||
return _header.ComputeLocalSizeY;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries Local Size Z for compute shaders.
|
||||
/// </summary>
|
||||
/// <returns>Local Size Z</returns>
|
||||
public int QueryComputeLocalSizeZ()
|
||||
{
|
||||
return _header.ComputeLocalSizeZ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries Local Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
/// <returns>Local Memory size in bytes</returns>
|
||||
public int QueryComputeLocalMemorySize()
|
||||
{
|
||||
return _header.ComputeLocalMemorySize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries Shared Memory size in bytes for compute shaders.
|
||||
/// </summary>
|
||||
/// <returns>Shared Memory size in bytes</returns>
|
||||
public int QueryComputeSharedMemorySize()
|
||||
{
|
||||
return _header.ComputeSharedMemorySize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries current primitive topology for geometry shaders.
|
||||
/// </summary>
|
||||
/// <returns>Current primitive topology</returns>
|
||||
public InputTopology QueryPrimitiveTopology()
|
||||
{
|
||||
return _header.PrimitiveTopology;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries host storage buffer alignment required.
|
||||
/// </summary>
|
||||
/// <returns>Host storage buffer alignment in bytes</returns>
|
||||
public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
|
||||
|
||||
/// <summary>
|
||||
/// Queries host support for readable images without a explicit format declaration on the shader.
|
||||
/// </summary>
|
||||
/// <returns>True if formatted image load is supported, false otherwise</returns>
|
||||
public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
|
||||
|
||||
/// <summary>
|
||||
/// Queries host GPU non-constant texture offset support.
|
||||
/// </summary>
|
||||
/// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
|
||||
public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the texture descriptor for a given texture on the pool.
|
||||
/// </summary>
|
||||
/// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
|
||||
/// <returns>Texture descriptor</returns>
|
||||
public override Image.TextureDescriptor GetTextureDescriptor(int handle)
|
||||
{
|
||||
if (!_textureDescriptors.TryGetValue(handle, out Image.TextureDescriptor textureDescriptor))
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
return textureDescriptor;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.GAL;
|
||||
using Ryujinx.Graphics.Gpu.Image;
|
||||
using Ryujinx.Graphics.Gpu.State;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
|
||||
|
@ -9,7 +8,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// <summary>
|
||||
/// Represents a GPU state and memory accessor.
|
||||
/// </summary>
|
||||
class GpuAccessor : IGpuAccessor
|
||||
class GpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
|
||||
{
|
||||
private readonly GpuContext _context;
|
||||
private readonly GpuState _state;
|
||||
|
@ -78,7 +77,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// <typeparam name="T">Type of the data to be read</typeparam>
|
||||
/// <param name="address">GPU virtual address of the data</param>
|
||||
/// <returns>Data at the memory location</returns>
|
||||
public T MemoryRead<T>(ulong address) where T : unmanaged
|
||||
public override T MemoryRead<T>(ulong address)
|
||||
{
|
||||
return _context.MemoryManager.Read<T>(address);
|
||||
}
|
||||
|
@ -134,33 +133,6 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
: _context.Methods.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture target information.
|
||||
/// </summary>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>True if the texture is a buffer texture, false otherwise</returns>
|
||||
public bool QueryIsTextureBuffer(int handle)
|
||||
{
|
||||
return GetTextureDescriptor(handle).UnpackTextureTarget() == TextureTarget.TextureBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture target information.
|
||||
/// </summary>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>True if the texture is a rectangle texture, false otherwise</returns>
|
||||
public bool QueryIsTextureRectangle(int handle)
|
||||
{
|
||||
var descriptor = GetTextureDescriptor(handle);
|
||||
|
||||
TextureTarget target = descriptor.UnpackTextureTarget();
|
||||
|
||||
bool is2DTexture = target == TextureTarget.Texture2D ||
|
||||
target == TextureTarget.Texture2DRect;
|
||||
|
||||
return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries current primitive topology for geometry shaders.
|
||||
/// </summary>
|
||||
|
@ -208,76 +180,12 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
|
||||
public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture format information, for shaders using image load or store.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only returns non-compressed color formats.
|
||||
/// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
|
||||
/// </remarks>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>Color format of the non-compressed texture</returns>
|
||||
public TextureFormat QueryTextureFormat(int handle)
|
||||
{
|
||||
var descriptor = GetTextureDescriptor(handle);
|
||||
|
||||
if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
|
||||
{
|
||||
return TextureFormat.Unknown;
|
||||
}
|
||||
|
||||
return formatInfo.Format switch
|
||||
{
|
||||
Format.R8Unorm => TextureFormat.R8Unorm,
|
||||
Format.R8Snorm => TextureFormat.R8Snorm,
|
||||
Format.R8Uint => TextureFormat.R8Uint,
|
||||
Format.R8Sint => TextureFormat.R8Sint,
|
||||
Format.R16Float => TextureFormat.R16Float,
|
||||
Format.R16Unorm => TextureFormat.R16Unorm,
|
||||
Format.R16Snorm => TextureFormat.R16Snorm,
|
||||
Format.R16Uint => TextureFormat.R16Uint,
|
||||
Format.R16Sint => TextureFormat.R16Sint,
|
||||
Format.R32Float => TextureFormat.R32Float,
|
||||
Format.R32Uint => TextureFormat.R32Uint,
|
||||
Format.R32Sint => TextureFormat.R32Sint,
|
||||
Format.R8G8Unorm => TextureFormat.R8G8Unorm,
|
||||
Format.R8G8Snorm => TextureFormat.R8G8Snorm,
|
||||
Format.R8G8Uint => TextureFormat.R8G8Uint,
|
||||
Format.R8G8Sint => TextureFormat.R8G8Sint,
|
||||
Format.R16G16Float => TextureFormat.R16G16Float,
|
||||
Format.R16G16Unorm => TextureFormat.R16G16Unorm,
|
||||
Format.R16G16Snorm => TextureFormat.R16G16Snorm,
|
||||
Format.R16G16Uint => TextureFormat.R16G16Uint,
|
||||
Format.R16G16Sint => TextureFormat.R16G16Sint,
|
||||
Format.R32G32Float => TextureFormat.R32G32Float,
|
||||
Format.R32G32Uint => TextureFormat.R32G32Uint,
|
||||
Format.R32G32Sint => TextureFormat.R32G32Sint,
|
||||
Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
|
||||
Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
|
||||
Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
|
||||
Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
|
||||
Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
|
||||
Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
|
||||
Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
|
||||
Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
|
||||
Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
|
||||
Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
|
||||
Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
|
||||
Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
|
||||
Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
|
||||
Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
|
||||
Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
|
||||
Format.R11G11B10Float => TextureFormat.R11G11B10Float,
|
||||
_ => TextureFormat.Unknown
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the texture descriptor for a given texture on the pool.
|
||||
/// </summary>
|
||||
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
|
||||
/// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
|
||||
/// <returns>Texture descriptor</returns>
|
||||
private Image.TextureDescriptor GetTextureDescriptor(int handle)
|
||||
public override Image.TextureDescriptor GetTextureDescriptor(int handle)
|
||||
{
|
||||
if (_compute)
|
||||
{
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
|
||||
foreach (ShaderCodeHolder holder in Shaders)
|
||||
{
|
||||
holder?.HostShader.Dispose();
|
||||
holder?.HostShader?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.GAL;
|
||||
using Ryujinx.Graphics.Gpu.Shader.Cache;
|
||||
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
||||
using Ryujinx.Graphics.Gpu.State;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader
|
||||
{
|
||||
|
@ -21,6 +29,16 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
|
||||
private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
|
||||
|
||||
private CacheManager _cacheManager;
|
||||
|
||||
private Dictionary<Hash128, ShaderBundle> _gpProgramsDiskCache;
|
||||
private Dictionary<Hash128, ShaderBundle> _cpProgramsDiskCache;
|
||||
|
||||
/// <summary>
|
||||
/// Version of the codegen (to be incremented when codegen changes).
|
||||
/// </summary>
|
||||
private const ulong ShaderCodeGenVersion = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the shader cache.
|
||||
/// </summary>
|
||||
|
@ -33,6 +51,251 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
|
||||
_cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
|
||||
_gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
|
||||
_gpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
|
||||
_cpProgramsDiskCache = new Dictionary<Hash128, ShaderBundle>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the cache.
|
||||
/// </summary>
|
||||
internal void Initialize()
|
||||
{
|
||||
if (GraphicsConfig.EnableShaderCache && GraphicsConfig.TitleId != null)
|
||||
{
|
||||
_cacheManager = new CacheManager(CacheGraphicsApi.OpenGL, CacheHashType.XxHash128, "glsl", GraphicsConfig.TitleId, ShaderCodeGenVersion);
|
||||
|
||||
HashSet<Hash128> invalidEntries = new HashSet<Hash128>();
|
||||
|
||||
ReadOnlySpan<Hash128> guestProgramList = _cacheManager.GetGuestProgramList();
|
||||
|
||||
for (int programIndex = 0; programIndex < guestProgramList.Length; programIndex++)
|
||||
{
|
||||
Hash128 key = guestProgramList[programIndex];
|
||||
|
||||
Logger.Info?.Print(LogClass.Gpu, $"Compiling shader {key} ({programIndex + 1} / {guestProgramList.Length})");
|
||||
|
||||
byte[] hostProgramBinary = _cacheManager.GetHostProgramByHash(ref key);
|
||||
bool hasHostCache = hostProgramBinary != null;
|
||||
|
||||
IProgram hostProgram = null;
|
||||
|
||||
// If the program sources aren't in the cache, compile from saved guest program.
|
||||
byte[] guestProgram = _cacheManager.GetGuestProgramByHash(ref key);
|
||||
|
||||
if (guestProgram == null)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.Gpu, $"Ignoring orphan shader hash {key} in cache (is the cache incomplete?)");
|
||||
|
||||
// Should not happen, but if someone messed with the cache it's better to catch it.
|
||||
invalidEntries.Add(key);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> guestProgramReadOnlySpan = guestProgram;
|
||||
|
||||
ReadOnlySpan<GuestShaderCacheEntry> cachedShaderEntries = GuestShaderCacheEntry.Parse(ref guestProgramReadOnlySpan, out GuestShaderCacheHeader fileHeader);
|
||||
|
||||
if (cachedShaderEntries[0].Header.Stage == ShaderStage.Compute)
|
||||
{
|
||||
Debug.Assert(cachedShaderEntries.Length == 1);
|
||||
|
||||
GuestShaderCacheEntry entry = cachedShaderEntries[0];
|
||||
|
||||
HostShaderCacheEntry[] hostShaderEntries = null;
|
||||
|
||||
// Try loading host shader binary.
|
||||
if (hasHostCache)
|
||||
{
|
||||
hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
|
||||
hostProgramBinary = hostProgramBinarySpan.ToArray();
|
||||
hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
|
||||
}
|
||||
|
||||
bool isHostProgramValid = hostProgram != null;
|
||||
|
||||
ShaderProgram program;
|
||||
ShaderProgramInfo shaderProgramInfo;
|
||||
|
||||
// Reconstruct code holder.
|
||||
if (isHostProgramValid)
|
||||
{
|
||||
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
|
||||
shaderProgramInfo = hostShaderEntries[0].ToShaderProgramInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
|
||||
|
||||
program = Translator.CreateContext(0, gpuAccessor, DefaultFlags | TranslationFlags.Compute).Translate(out shaderProgramInfo);
|
||||
}
|
||||
|
||||
ShaderCodeHolder shader = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
|
||||
|
||||
// If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
|
||||
if (hostProgram == null)
|
||||
{
|
||||
Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
|
||||
|
||||
// Compile shader and create program as the shader program binary got invalidated.
|
||||
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
|
||||
hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
|
||||
|
||||
// As the host program was invalidated, save the new entry in the cache.
|
||||
hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
|
||||
|
||||
if (hasHostCache)
|
||||
{
|
||||
_cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
|
||||
|
||||
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
||||
}
|
||||
}
|
||||
|
||||
_cpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shader));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Assert(cachedShaderEntries.Length == Constants.ShaderStages);
|
||||
|
||||
ShaderCodeHolder[] shaders = new ShaderCodeHolder[cachedShaderEntries.Length];
|
||||
List<ShaderProgram> shaderPrograms = new List<ShaderProgram>();
|
||||
|
||||
TransformFeedbackDescriptor[] tfd = ReadTransformationFeedbackInformations(ref guestProgramReadOnlySpan, fileHeader);
|
||||
|
||||
TranslationFlags flags = DefaultFlags;
|
||||
|
||||
if (tfd != null)
|
||||
{
|
||||
flags = TranslationFlags.Feedback;
|
||||
}
|
||||
|
||||
TranslationCounts counts = new TranslationCounts();
|
||||
|
||||
HostShaderCacheEntry[] hostShaderEntries = null;
|
||||
|
||||
// Try loading host shader binary.
|
||||
if (hasHostCache)
|
||||
{
|
||||
hostShaderEntries = HostShaderCacheEntry.Parse(hostProgramBinary, out ReadOnlySpan<byte> hostProgramBinarySpan);
|
||||
hostProgramBinary = hostProgramBinarySpan.ToArray();
|
||||
hostProgram = _context.Renderer.LoadProgramBinary(hostProgramBinary);
|
||||
}
|
||||
|
||||
bool isHostProgramValid = hostProgram != null;
|
||||
|
||||
// Reconstruct code holder.
|
||||
for (int i = 0; i < cachedShaderEntries.Length; i++)
|
||||
{
|
||||
GuestShaderCacheEntry entry = cachedShaderEntries[i];
|
||||
|
||||
if (entry == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ShaderProgram program;
|
||||
|
||||
if (entry.Header.SizeA != 0)
|
||||
{
|
||||
ShaderProgramInfo shaderProgramInfo;
|
||||
|
||||
if (isHostProgramValid)
|
||||
{
|
||||
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
|
||||
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
|
||||
|
||||
program = Translator.CreateContext((ulong)entry.Header.Size, 0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
|
||||
}
|
||||
|
||||
// NOTE: Vertex B comes first in the shader cache.
|
||||
byte[] code = entry.Code.AsSpan().Slice(0, entry.Header.Size).ToArray();
|
||||
byte[] code2 = entry.Code.AsSpan().Slice(entry.Header.Size, entry.Header.SizeA).ToArray();
|
||||
|
||||
shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, code, code2);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShaderProgramInfo shaderProgramInfo;
|
||||
|
||||
if (isHostProgramValid)
|
||||
{
|
||||
program = new ShaderProgram(entry.Header.Stage, "", entry.Header.Size, entry.Header.SizeA);
|
||||
shaderProgramInfo = hostShaderEntries[i].ToShaderProgramInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
IGpuAccessor gpuAccessor = new CachedGpuAccessor(_context, entry.Code, entry.Header.GpuAccessorHeader, entry.TextureDescriptors);
|
||||
|
||||
program = Translator.CreateContext(0, gpuAccessor, flags, counts).Translate(out shaderProgramInfo);
|
||||
}
|
||||
|
||||
shaders[i] = new ShaderCodeHolder(program, shaderProgramInfo, entry.Code);
|
||||
}
|
||||
|
||||
shaderPrograms.Add(program);
|
||||
}
|
||||
|
||||
// If the host program was rejected by the gpu driver or isn't in cache, try to build from program sources again.
|
||||
if (!isHostProgramValid)
|
||||
{
|
||||
Logger.Info?.Print(LogClass.Gpu, $"Host shader {key} got invalidated, rebuilding from guest...");
|
||||
|
||||
List<IShader> hostShaders = new List<IShader>();
|
||||
|
||||
// Compile shaders and create program as the shader program binary got invalidated.
|
||||
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
||||
{
|
||||
ShaderProgram program = shaders[stage]?.Program;
|
||||
|
||||
if (program == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
|
||||
|
||||
shaders[stage].HostShader = hostShader;
|
||||
|
||||
hostShaders.Add(hostShader);
|
||||
}
|
||||
|
||||
hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
|
||||
|
||||
// As the host program was invalidated, save the new entry in the cache.
|
||||
hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
|
||||
|
||||
if (hasHostCache)
|
||||
{
|
||||
_cacheManager.ReplaceHostProgram(ref key, hostProgramBinary);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Gpu, $"Add missing host shader {key} in cache (is the cache incomplete?)");
|
||||
|
||||
_cacheManager.AddHostProgram(ref key, hostProgramBinary);
|
||||
}
|
||||
}
|
||||
|
||||
_gpProgramsDiskCache.Add(key, new ShaderBundle(hostProgram, shaders));
|
||||
}
|
||||
}
|
||||
|
||||
// Remove entries that are broken in the cache
|
||||
_cacheManager.RemoveManifestEntries(invalidEntries);
|
||||
_cacheManager.FlushToArchive();
|
||||
_cacheManager.Synchronize();
|
||||
|
||||
Logger.Info?.Print(LogClass.Gpu, "Shader cache loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -71,7 +334,9 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
}
|
||||
}
|
||||
|
||||
ShaderCodeHolder shader = TranslateComputeShader(
|
||||
TranslatorContext[] shaderContexts = new TranslatorContext[1];
|
||||
|
||||
shaderContexts[0] = DecodeComputeShader(
|
||||
state,
|
||||
gpuVa,
|
||||
localSizeX,
|
||||
|
@ -80,11 +345,45 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
localMemorySize,
|
||||
sharedMemorySize);
|
||||
|
||||
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
|
||||
bool isShaderCacheEnabled = _cacheManager != null;
|
||||
|
||||
IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
|
||||
byte[] programCode = null;
|
||||
Hash128 programCodeHash = default;
|
||||
GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
|
||||
|
||||
ShaderBundle cpShader = new ShaderBundle(hostProgram, shader);
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
// Compute hash and prepare data for shader disk cache comparison.
|
||||
GetProgramInformations(null, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
|
||||
}
|
||||
|
||||
ShaderBundle cpShader;
|
||||
|
||||
// Search for the program hash in loaded shaders.
|
||||
if (!isShaderCacheEnabled || !_cpProgramsDiskCache.TryGetValue(programCodeHash, out cpShader))
|
||||
{
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
|
||||
}
|
||||
|
||||
// The shader isn't currently cached, translate it and compile it.
|
||||
ShaderCodeHolder shader = TranslateShader(shaderContexts[0]);
|
||||
|
||||
shader.HostShader = _context.Renderer.CompileShader(ShaderStage.Compute, shader.Program.Code);
|
||||
|
||||
IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }, null);
|
||||
|
||||
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), new ShaderCodeHolder[] { shader });
|
||||
|
||||
cpShader = new ShaderBundle(hostProgram, shader);
|
||||
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
_cpProgramsDiskCache.Add(programCodeHash, cpShader);
|
||||
_cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, null), hostProgramBinary);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isCached)
|
||||
{
|
||||
|
@ -123,9 +422,9 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
}
|
||||
}
|
||||
|
||||
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
|
||||
TranslatorContext[] shaderContexts = new TranslatorContext[Constants.ShaderStages];
|
||||
|
||||
var tfd = GetTransformFeedbackDescriptors(state);
|
||||
TransformFeedbackDescriptor[] tfd = GetTransformFeedbackDescriptors(state);
|
||||
|
||||
TranslationFlags flags = DefaultFlags;
|
||||
|
||||
|
@ -138,40 +437,80 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
|
||||
if (addresses.VertexA != 0)
|
||||
{
|
||||
shaders[0] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
|
||||
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
|
||||
}
|
||||
else
|
||||
{
|
||||
shaders[0] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
|
||||
shaderContexts[0] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Vertex, addresses.Vertex);
|
||||
}
|
||||
|
||||
shaders[1] = TranslateGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
|
||||
shaders[2] = TranslateGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
|
||||
shaders[3] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
|
||||
shaders[4] = TranslateGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
|
||||
shaderContexts[1] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationControl, addresses.TessControl);
|
||||
shaderContexts[2] = DecodeGraphicsShader(state, counts, flags, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
|
||||
shaderContexts[3] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Geometry, addresses.Geometry);
|
||||
shaderContexts[4] = DecodeGraphicsShader(state, counts, flags, ShaderStage.Fragment, addresses.Fragment);
|
||||
|
||||
List<IShader> hostShaders = new List<IShader>();
|
||||
bool isShaderCacheEnabled = _cacheManager != null;
|
||||
|
||||
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
||||
byte[] programCode = null;
|
||||
Hash128 programCodeHash = default;
|
||||
GuestShaderCacheEntryHeader[] shaderCacheEntries = null;
|
||||
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
ShaderProgram program = shaders[stage]?.Program;
|
||||
// Compute hash and prepare data for shader disk cache comparison.
|
||||
GetProgramInformations(tfd, shaderContexts, out programCode, out programCodeHash, out shaderCacheEntries);
|
||||
}
|
||||
|
||||
if (program == null)
|
||||
ShaderBundle gpShaders;
|
||||
|
||||
// Search for the program hash in loaded shaders.
|
||||
if (!isShaderCacheEnabled || !_gpProgramsDiskCache.TryGetValue(programCodeHash, out gpShaders))
|
||||
{
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
continue;
|
||||
Logger.Debug?.Print(LogClass.Gpu, $"Shader {programCodeHash} not in cache, compiling!");
|
||||
}
|
||||
|
||||
IShader hostShader = _context.Renderer.CompileShader(program.Stage, program.Code);
|
||||
// The shader isn't currently cached, translate it and compile it.
|
||||
ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
|
||||
|
||||
shaders[stage].HostShader = hostShader;
|
||||
shaders[0] = TranslateShader(shaderContexts[0]);
|
||||
shaders[1] = TranslateShader(shaderContexts[1]);
|
||||
shaders[2] = TranslateShader(shaderContexts[2]);
|
||||
shaders[3] = TranslateShader(shaderContexts[3]);
|
||||
shaders[4] = TranslateShader(shaderContexts[4]);
|
||||
|
||||
hostShaders.Add(hostShader);
|
||||
List<IShader> hostShaders = new List<IShader>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
|
||||
|
||||
byte[] hostProgramBinary = HostShaderCacheEntry.Create(hostProgram.GetBinary(), shaders);
|
||||
|
||||
gpShaders = new ShaderBundle(hostProgram, shaders);
|
||||
|
||||
if (isShaderCacheEnabled)
|
||||
{
|
||||
_gpProgramsDiskCache.Add(programCodeHash, gpShaders);
|
||||
_cacheManager.SaveProgram(ref programCodeHash, CreateGuestProgramDump(programCode, shaderCacheEntries, tfd), hostProgramBinary);
|
||||
}
|
||||
}
|
||||
|
||||
IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray(), tfd);
|
||||
|
||||
ShaderBundle gpShaders = new ShaderBundle(hostProgram, shaders);
|
||||
|
||||
if (!isCached)
|
||||
{
|
||||
list = new List<ShaderBundle>();
|
||||
|
@ -286,7 +625,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translates the binary Maxwell shader code to something that the host API accepts.
|
||||
/// Decode the binary Maxwell shader code to a translator context.
|
||||
/// </summary>
|
||||
/// <param name="state">Current GPU state</param>
|
||||
/// <param name="gpuVa">GPU virtual address of the binary shader code</param>
|
||||
|
@ -295,8 +634,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// <param name="localSizeZ">Local group size Z of the computer shader</param>
|
||||
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
||||
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
||||
/// <returns>Compiled compute shader code</returns>
|
||||
private ShaderCodeHolder TranslateComputeShader(
|
||||
/// <returns>The generated translator context</returns>
|
||||
private TranslatorContext DecodeComputeShader(
|
||||
GpuState state,
|
||||
ulong gpuVa,
|
||||
int localSizeX,
|
||||
|
@ -312,25 +651,11 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
|
||||
GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
|
||||
|
||||
ShaderProgram program;
|
||||
|
||||
program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
|
||||
|
||||
byte[] code = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
|
||||
|
||||
_dumper.Dump(code, compute: true, out string fullPath, out string codePath);
|
||||
|
||||
if (fullPath != null && codePath != null)
|
||||
{
|
||||
program.Prepend("// " + codePath);
|
||||
program.Prepend("// " + fullPath);
|
||||
}
|
||||
|
||||
return new ShaderCodeHolder(program, code);
|
||||
return Translator.CreateContext(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translates the binary Maxwell shader code to something that the host API accepts.
|
||||
/// Decode the binary Maxwell shader code to a translator context.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
|
||||
|
@ -341,8 +666,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// <param name="stage">Shader stage</param>
|
||||
/// <param name="gpuVa">GPU virtual address of the shader code</param>
|
||||
/// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
|
||||
/// <returns>Compiled graphics shader code</returns>
|
||||
private ShaderCodeHolder TranslateGraphicsShader(
|
||||
/// <returns>The generated translator context</returns>
|
||||
private TranslatorContext DecodeGraphicsShader(
|
||||
GpuState state,
|
||||
TranslationCounts counts,
|
||||
TranslationFlags flags,
|
||||
|
@ -359,14 +684,36 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
|
||||
if (gpuVaA != 0)
|
||||
{
|
||||
ShaderProgram program = Translator.Translate(gpuVaA, gpuVa, gpuAccessor, flags, counts);
|
||||
return Translator.CreateContext(gpuVaA, gpuVa, gpuAccessor, flags, counts);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Translator.CreateContext(gpuVa, gpuAccessor, flags, counts);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] codeA = _context.MemoryManager.GetSpan(gpuVaA, program.SizeA).ToArray();
|
||||
byte[] codeB = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
|
||||
/// <summary>
|
||||
/// Translates a previously generated translator context to something that the host API accepts.
|
||||
/// </summary>
|
||||
/// <param name="translatorContext">Current translator context to translate</param>
|
||||
/// <returns>Compiled graphics shader code</returns>
|
||||
private ShaderCodeHolder TranslateShader(TranslatorContext translatorContext)
|
||||
{
|
||||
if (translatorContext == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (translatorContext.AddressA != 0)
|
||||
{
|
||||
byte[] codeA = _context.MemoryManager.GetSpan(translatorContext.AddressA, translatorContext.SizeA).ToArray();
|
||||
byte[] codeB = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
|
||||
|
||||
_dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
|
||||
_dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
|
||||
|
||||
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
|
||||
|
||||
if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
|
||||
{
|
||||
program.Prepend("// " + codePathB);
|
||||
|
@ -375,23 +722,23 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
program.Prepend("// " + fullPathA);
|
||||
}
|
||||
|
||||
return new ShaderCodeHolder(program, codeB, codeA);
|
||||
return new ShaderCodeHolder(program, shaderProgramInfo, codeB, codeA);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShaderProgram program = Translator.Translate(gpuVa, gpuAccessor, flags, counts);
|
||||
|
||||
byte[] code = _context.MemoryManager.GetSpan(gpuVa, program.Size).ToArray();
|
||||
byte[] code = _context.MemoryManager.GetSpan(translatorContext.Address, translatorContext.Size).ToArray();
|
||||
|
||||
_dumper.Dump(code, compute: false, out string fullPath, out string codePath);
|
||||
|
||||
ShaderProgram program = translatorContext.Translate(out ShaderProgramInfo shaderProgramInfo);
|
||||
|
||||
if (fullPath != null && codePath != null)
|
||||
{
|
||||
program.Prepend("// " + codePath);
|
||||
program.Prepend("// " + fullPath);
|
||||
}
|
||||
|
||||
return new ShaderCodeHolder(program, code);
|
||||
return new ShaderCodeHolder(program, shaderProgramInfo, code);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -416,6 +763,194 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
bundle.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
_cacheManager?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a guest shader program.
|
||||
/// </summary>
|
||||
/// <param name="programCode">The program code of the shader code</param>
|
||||
/// <param name="shaderCacheEntries">The resulting guest shader entries header</param>
|
||||
/// <param name="tfd">The transform feedback descriptors in use</param>
|
||||
/// <returns>The resulting guest shader program</returns>
|
||||
private static byte[] CreateGuestProgramDump(ReadOnlySpan<byte> programCode, GuestShaderCacheEntryHeader[] shaderCacheEntries, TransformFeedbackDescriptor[] tfd)
|
||||
{
|
||||
using (MemoryStream resultStream = new MemoryStream())
|
||||
{
|
||||
BinaryWriter resultStreamWriter = new BinaryWriter(resultStream);
|
||||
|
||||
byte transformFeedbackCount = 0;
|
||||
|
||||
if (tfd != null)
|
||||
{
|
||||
transformFeedbackCount = (byte)tfd.Length;
|
||||
}
|
||||
|
||||
// Header
|
||||
resultStreamWriter.WriteStruct(new GuestShaderCacheHeader((byte)shaderCacheEntries.Length, transformFeedbackCount));
|
||||
|
||||
// Write all entries header
|
||||
foreach (GuestShaderCacheEntryHeader entry in shaderCacheEntries)
|
||||
{
|
||||
resultStreamWriter.WriteStruct(entry);
|
||||
}
|
||||
|
||||
// Finally, write all program code and all transform feedback information.
|
||||
resultStreamWriter.Write(programCode);
|
||||
|
||||
return resultStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write transform feedback guest information to the given stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to write data to</param>
|
||||
/// <param name="tfd">The current transform feedback descriptors used</param>
|
||||
private static void WriteTransformationFeedbackInformation(Stream stream, TransformFeedbackDescriptor[] tfd)
|
||||
{
|
||||
if (tfd != null)
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(stream);
|
||||
|
||||
foreach (TransformFeedbackDescriptor transform in tfd)
|
||||
{
|
||||
writer.WriteStruct(new GuestShaderCacheTransformFeedbackHeader(transform.BufferIndex, transform.Stride, transform.VaryingLocations.Length));
|
||||
writer.Write(transform.VaryingLocations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read transform feedback descriptors from guest.
|
||||
/// </summary>
|
||||
/// <param name="data">The raw guest transform feedback descriptors</param>
|
||||
/// <param name="header">The guest shader program header</param>
|
||||
/// <returns>The transform feedback descriptors read from guest</returns>
|
||||
private static TransformFeedbackDescriptor[] ReadTransformationFeedbackInformations(ref ReadOnlySpan<byte> data, GuestShaderCacheHeader header)
|
||||
{
|
||||
if (header.TransformFeedbackCount != 0)
|
||||
{
|
||||
TransformFeedbackDescriptor[] result = new TransformFeedbackDescriptor[header.TransformFeedbackCount];
|
||||
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
GuestShaderCacheTransformFeedbackHeader feedbackHeader = MemoryMarshal.Read<GuestShaderCacheTransformFeedbackHeader>(data);
|
||||
|
||||
result[i] = new TransformFeedbackDescriptor(feedbackHeader.BufferIndex, feedbackHeader.Stride, data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>(), feedbackHeader.VaryingLocationsLength).ToArray());
|
||||
|
||||
data = data.Slice(Unsafe.SizeOf<GuestShaderCacheTransformFeedbackHeader>() + feedbackHeader.VaryingLocationsLength);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="GuestGpuAccessorHeader"/> from an gpu accessor.
|
||||
/// </summary>
|
||||
/// <param name="gpuAccessor">The gpu accessor</param>
|
||||
/// <returns>a new instance of <see cref="GuestGpuAccessorHeader"/></returns>
|
||||
private static GuestGpuAccessorHeader CreateGuestGpuAccessorCache(IGpuAccessor gpuAccessor)
|
||||
{
|
||||
return new GuestGpuAccessorHeader
|
||||
{
|
||||
ComputeLocalSizeX = gpuAccessor.QueryComputeLocalSizeX(),
|
||||
ComputeLocalSizeY = gpuAccessor.QueryComputeLocalSizeY(),
|
||||
ComputeLocalSizeZ = gpuAccessor.QueryComputeLocalSizeZ(),
|
||||
ComputeLocalMemorySize = gpuAccessor.QueryComputeLocalMemorySize(),
|
||||
ComputeSharedMemorySize = gpuAccessor.QueryComputeSharedMemorySize(),
|
||||
PrimitiveTopology = gpuAccessor.QueryPrimitiveTopology(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the guest GpuAccessor informations to the given stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to write the guest GpuAcessor</param>
|
||||
/// <param name="shaderContext">The shader tranlator context in use</param>
|
||||
/// <returns>The guest gpu accessor header</returns>
|
||||
private static GuestGpuAccessorHeader WriteGuestGpuAccessorCache(Stream stream, TranslatorContext shaderContext)
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(stream);
|
||||
|
||||
GuestGpuAccessorHeader header = CreateGuestGpuAccessorCache(shaderContext.GpuAccessor);
|
||||
|
||||
// If we have a full gpu accessor, cache textures descriptors
|
||||
if (shaderContext.GpuAccessor is GpuAccessor gpuAccessor)
|
||||
{
|
||||
HashSet<int> textureHandlesInUse = shaderContext.TextureHandlesForCache;
|
||||
|
||||
header.TextureDescriptorCount = textureHandlesInUse.Count;
|
||||
|
||||
foreach (int textureHandle in textureHandlesInUse)
|
||||
{
|
||||
GuestTextureDescriptor textureDescriptor = gpuAccessor.GetTextureDescriptor(textureHandle).ToCache();
|
||||
|
||||
textureDescriptor.Handle = (uint)textureHandle;
|
||||
|
||||
writer.WriteStruct(textureDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
return header;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the shader program information for use on the shader cache.
|
||||
/// </summary>
|
||||
/// <param name="tfd">The current transform feedback descriptors used</param>
|
||||
/// <param name="shaderContexts">The shader translators context in use</param>
|
||||
/// <param name="programCode">The resulting raw shader program code</param>
|
||||
/// <param name="programCodeHash">The resulting raw shader program code hash</param>
|
||||
/// <param name="entries">The resulting guest shader entries header</param>
|
||||
private void GetProgramInformations(TransformFeedbackDescriptor[] tfd, ReadOnlySpan<TranslatorContext> shaderContexts, out byte[] programCode, out Hash128 programCodeHash, out GuestShaderCacheEntryHeader[] entries)
|
||||
{
|
||||
GuestShaderCacheEntryHeader ComputeStage(Stream stream, TranslatorContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
return new GuestShaderCacheEntryHeader();
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> data = _context.MemoryManager.GetSpan(context.Address, context.Size);
|
||||
|
||||
stream.Write(data);
|
||||
|
||||
int size = data.Length;
|
||||
int sizeA = 0;
|
||||
|
||||
if (context.AddressA != 0)
|
||||
{
|
||||
data = _context.MemoryManager.GetSpan(context.AddressA, context.SizeA);
|
||||
|
||||
sizeA = data.Length;
|
||||
|
||||
stream.Write(data);
|
||||
}
|
||||
|
||||
GuestGpuAccessorHeader gpuAccessorHeader = WriteGuestGpuAccessorCache(stream, context);
|
||||
|
||||
return new GuestShaderCacheEntryHeader(context.Stage, size, sizeA, gpuAccessorHeader);
|
||||
}
|
||||
|
||||
entries = new GuestShaderCacheEntryHeader[shaderContexts.Length];
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
for (int i = 0; i < shaderContexts.Length; i++)
|
||||
{
|
||||
entries[i] = ComputeStage(stream, shaderContexts[i]);
|
||||
}
|
||||
|
||||
WriteTransformationFeedbackInformation(stream, tfd);
|
||||
|
||||
programCode = stream.ToArray();
|
||||
programCodeHash = _cacheManager.ComputeHash(programCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,9 +13,15 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// </summary>
|
||||
public ShaderProgram Program { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Shader program information.
|
||||
/// </summary>
|
||||
public ShaderProgramInfo Info { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Host shader object.
|
||||
/// </summary>
|
||||
/// <remarks>Null if the host shader program cache is in use.</remarks>
|
||||
public IShader HostShader { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -32,11 +38,13 @@ namespace Ryujinx.Graphics.Gpu.Shader
|
|||
/// Creates a new instace of the shader code holder.
|
||||
/// </summary>
|
||||
/// <param name="program">Shader program</param>
|
||||
/// <param name="info">Shader program information</param>
|
||||
/// <param name="code">Maxwell binary shader code</param>
|
||||
/// <param name="code2">Optional binary shader code of the "Vertex A" shader, when combined with "Vertex B"</param>
|
||||
public ShaderCodeHolder(ShaderProgram program, byte[] code, byte[] code2 = null)
|
||||
public ShaderCodeHolder(ShaderProgram program, ShaderProgramInfo info, byte[] code, byte[] code2 = null)
|
||||
{
|
||||
Program = program;
|
||||
Info = info;
|
||||
Code = code;
|
||||
Code2 = code2;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
using Ryujinx.Graphics.GAL;
|
||||
using Ryujinx.Graphics.Gpu.Image;
|
||||
using Ryujinx.Graphics.Shader;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Shader
|
||||
{
|
||||
abstract class TextureDescriptorCapableGpuAccessor : IGpuAccessor
|
||||
{
|
||||
public abstract T MemoryRead<T>(ulong address) where T : unmanaged;
|
||||
|
||||
public abstract Image.TextureDescriptor GetTextureDescriptor(int handle);
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture format information, for shaders using image load or store.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only returns non-compressed color formats.
|
||||
/// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
|
||||
/// </remarks>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>Color format of the non-compressed texture</returns>
|
||||
public TextureFormat QueryTextureFormat(int handle)
|
||||
{
|
||||
var descriptor = GetTextureDescriptor(handle);
|
||||
|
||||
if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
|
||||
{
|
||||
return TextureFormat.Unknown;
|
||||
}
|
||||
|
||||
return formatInfo.Format switch
|
||||
{
|
||||
Format.R8Unorm => TextureFormat.R8Unorm,
|
||||
Format.R8Snorm => TextureFormat.R8Snorm,
|
||||
Format.R8Uint => TextureFormat.R8Uint,
|
||||
Format.R8Sint => TextureFormat.R8Sint,
|
||||
Format.R16Float => TextureFormat.R16Float,
|
||||
Format.R16Unorm => TextureFormat.R16Unorm,
|
||||
Format.R16Snorm => TextureFormat.R16Snorm,
|
||||
Format.R16Uint => TextureFormat.R16Uint,
|
||||
Format.R16Sint => TextureFormat.R16Sint,
|
||||
Format.R32Float => TextureFormat.R32Float,
|
||||
Format.R32Uint => TextureFormat.R32Uint,
|
||||
Format.R32Sint => TextureFormat.R32Sint,
|
||||
Format.R8G8Unorm => TextureFormat.R8G8Unorm,
|
||||
Format.R8G8Snorm => TextureFormat.R8G8Snorm,
|
||||
Format.R8G8Uint => TextureFormat.R8G8Uint,
|
||||
Format.R8G8Sint => TextureFormat.R8G8Sint,
|
||||
Format.R16G16Float => TextureFormat.R16G16Float,
|
||||
Format.R16G16Unorm => TextureFormat.R16G16Unorm,
|
||||
Format.R16G16Snorm => TextureFormat.R16G16Snorm,
|
||||
Format.R16G16Uint => TextureFormat.R16G16Uint,
|
||||
Format.R16G16Sint => TextureFormat.R16G16Sint,
|
||||
Format.R32G32Float => TextureFormat.R32G32Float,
|
||||
Format.R32G32Uint => TextureFormat.R32G32Uint,
|
||||
Format.R32G32Sint => TextureFormat.R32G32Sint,
|
||||
Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
|
||||
Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
|
||||
Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
|
||||
Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
|
||||
Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
|
||||
Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
|
||||
Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
|
||||
Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
|
||||
Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
|
||||
Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
|
||||
Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
|
||||
Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
|
||||
Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
|
||||
Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
|
||||
Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
|
||||
Format.R11G11B10Float => TextureFormat.R11G11B10Float,
|
||||
_ => TextureFormat.Unknown
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture target information.
|
||||
/// </summary>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>True if the texture is a buffer texture, false otherwise</returns>
|
||||
public bool QueryIsTextureBuffer(int handle)
|
||||
{
|
||||
return GetTextureDescriptor(handle).UnpackTextureTarget() == TextureTarget.TextureBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries texture target information.
|
||||
/// </summary>
|
||||
/// <param name="handle">Texture handle</param>
|
||||
/// <returns>True if the texture is a rectangle texture, false otherwise</returns>
|
||||
public bool QueryIsTextureRectangle(int handle)
|
||||
{
|
||||
var descriptor = GetTextureDescriptor(handle);
|
||||
|
||||
TextureTarget target = descriptor.UnpackTextureTarget();
|
||||
|
||||
bool is2DTexture = target == TextureTarget.Texture2D ||
|
||||
target == TextureTarget.Texture2DRect;
|
||||
|
||||
return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue