aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Gpu/Shader/HashTable/SmartDataAccessor.cs
blob: d0eabba286ac135b25c26e2db51866adc150638d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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[..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 readonly 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);
        }
    }
}