1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-21 16:12:10 +00:00
Ryujinx/Ryujinx.Graphics.Gpu/Shader/HashTable/SmartDataAccessor.cs
gdkchan 43ebd7a9bb
New shader cache implementation (#3194)
* New shader cache implementation

* Remove some debug code

* Take transform feedback varying count into account

* Create shader cache directory if it does not exist + fragment output map related fixes

* Remove debug code

* Only check texture descriptors if the constant buffer is bound

* Also check CPU VA on GetSpanMapped

* Remove more unused code and move cache related code

* XML docs + remove more unused methods

* Better codegen for TransformFeedbackDescriptor.AsSpan

* Support migration from old cache format, remove more unused code

Shader cache rebuild now also rewrites the shared toc and data files

* Fix migration error with BRX shaders

* Add a limit to the async translation queue

 Avoid async translation threads not being able to keep up and the queue growing very large

* Re-create specialization state on recompile

This might be required if a new version of the shader translator requires more or less state, or if there is a bug related to the GPU state access

* Make shader cache more error resilient

* Add some missing XML docs and move GpuAccessor docs to the interface/use inheritdoc

* Address early PR feedback

* Fix rebase

* Remove IRenderer.CompileShader and IShader interface, replace with new ShaderSource struct passed to CreateProgram directly

* Handle some missing exceptions

* Make shader cache purge delete both old and new shader caches

* Register textures on new specialization state

* Translate and compile shaders in forward order (eliminates diffs due to different binding numbers)

* Limit in-flight shader compilation to the maximum number of compilation threads

* Replace ParallelDiskCacheLoader state changed event with a callback function

* Better handling for invalid constant buffer 1 data length

* Do not create the old cache directory structure if the old cache does not exist

* Constant buffer use should be per-stage. This change will invalidate existing new caches (file format version was incremented)

* Replace rectangle texture with just coordinate normalization

* Skip incompatible shaders that are missing texture information, instead of crashing

This is required if we, for example, support new texture instruction to the shader translator, and then they allow access to textures that were not accessed before. In this scenario, the old cache entry is no longer usable

* Fix coordinates normalization on cubemap textures

* Check if title ID is null before combining shader cache path

* More robust constant buffer address validation on spec state

* More robust constant buffer address validation on spec state (2)

* Regenerate shader cache with one stream, rather than one per shader.

* Only create shader cache directory during initialization

* Logging improvements

* Proper shader program disposal

* PR feedback, and add a comment on serialized structs

* XML docs for RegisterTexture

Co-authored-by: riperiperi <rhy3756547@hotmail.com>
2022-04-10 10:49:44 -03:00

96 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Gpu.Shader.HashTable
{
/// <summary>
/// Smart data accessor that can cache data and hashes to avoid reading and re-hashing the same memory regions.
/// </summary>
ref struct SmartDataAccessor
{
private readonly IDataAccessor _dataAccessor;
private ReadOnlySpan<byte> _data;
private readonly SortedList<int, HashState> _cachedHashes;
/// <summary>
/// Creates a new smart data accessor.
/// </summary>
/// <param name="dataAccessor">Data accessor</param>
public SmartDataAccessor(IDataAccessor dataAccessor)
{
_dataAccessor = dataAccessor;
_data = ReadOnlySpan<byte>.Empty;
_cachedHashes = new SortedList<int, HashState>();
}
/// <summary>
/// Get a spans of a given size.
/// </summary>
/// <remarks>
/// The actual length of the span returned depends on the <see cref="IDataAccessor"/>
/// and might be less than requested.
/// </remarks>
/// <param name="length">Size in bytes</param>
/// <returns>Span with the requested size</returns>
public ReadOnlySpan<byte> GetSpan(int length)
{
if (_data.Length < length)
{
_data = _dataAccessor.GetSpan(0, length);
}
else if (_data.Length > length)
{
return _data.Slice(0, length);
}
return _data;
}
/// <summary>
/// Gets a span of the requested size, and a hash of its data.
/// </summary>
/// <param name="length">Length of the span</param>
/// <param name="hash">Hash of the span data</param>
/// <returns>Span of data</returns>
public ReadOnlySpan<byte> GetSpanAndHash(int length, out uint hash)
{
ReadOnlySpan<byte> data = GetSpan(length);
hash = data.Length == length ? CalcHashCached(data) : 0;
return data;
}
/// <summary>
/// Calculates the hash for a requested span.
/// This will try to use a cached hash if the data was already accessed before, to avoid re-hashing.
/// </summary>
/// <param name="data">Data to be hashed</param>
/// <returns>Hash of the data</returns>
private uint CalcHashCached(ReadOnlySpan<byte> data)
{
HashState state = default;
bool found = false;
for (int i = _cachedHashes.Count - 1; i >= 0; i--)
{
int cachedHashSize = _cachedHashes.Keys[i];
if (cachedHashSize < data.Length)
{
state = _cachedHashes.Values[i];
found = true;
break;
}
}
if (!found)
{
state = new HashState();
state.Initialize();
}
state.Continue(data);
_cachedHashes[data.Length & ~7] = state;
return state.Finalize(data);
}
}
}