aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.GAL/SupportBufferUpdater.cs
blob: 84936e5475e53aad7120669fcbc7ed00e26cede3 (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
97
98
99
100
101
102
using Ryujinx.Graphics.Shader;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.Graphics.GAL
{
    public class SupportBufferUpdater : IDisposable
    {
        public SupportBuffer Data;
        public BufferHandle Handle;

        private readonly IRenderer _renderer;
        private int _startOffset = -1;
        private int _endOffset = -1;

        public SupportBufferUpdater(IRenderer renderer)
        {
            _renderer = renderer;
            Handle = renderer.CreateBuffer(SupportBuffer.RequiredSize);
            renderer.Pipeline.ClearBuffer(Handle, 0, SupportBuffer.RequiredSize, 0);
        }

        private void MarkDirty(int startOffset, int byteSize)
        {
            int endOffset = startOffset + byteSize;

            if (_startOffset == -1)
            {
                _startOffset = startOffset;
                _endOffset = endOffset;
            }
            else
            {
                if (startOffset < _startOffset)
                {
                    _startOffset = startOffset;
                }

                if (endOffset > _endOffset)
                {
                    _endOffset = endOffset;
                }
            }
        }

        public void UpdateFragmentRenderScaleCount(int count)
        {
            if (Data.FragmentRenderScaleCount.X != count)
            {
                Data.FragmentRenderScaleCount.X = count;

                MarkDirty(SupportBuffer.FragmentRenderScaleCountOffset, sizeof(int));
            }
        }

        private void UpdateGenericField<T>(int baseOffset, ReadOnlySpan<T> data, Span<T> target, int offset, int count) where T : unmanaged
        {
            data[..count].CopyTo(target[offset..]);

            int elemSize = Unsafe.SizeOf<T>();

            MarkDirty(baseOffset + offset * elemSize, count * elemSize);
        }

        public void UpdateRenderScale(ReadOnlySpan<Vector4<float>> data, int offset, int count)
        {
            UpdateGenericField(SupportBuffer.GraphicsRenderScaleOffset, data, Data.RenderScale.AsSpan(), offset, count);
        }

        public void UpdateFragmentIsBgra(ReadOnlySpan<Vector4<int>> data, int offset, int count)
        {
            UpdateGenericField(SupportBuffer.FragmentIsBgraOffset, data, Data.FragmentIsBgra.AsSpan(), offset, count);
        }

        public void UpdateViewportInverse(Vector4<float> data)
        {
            Data.ViewportInverse = data;

            MarkDirty(SupportBuffer.ViewportInverseOffset, SupportBuffer.FieldSize);
        }

        public void Commit()
        {
            if (_startOffset != -1)
            {
                ReadOnlySpan<byte> data = MemoryMarshal.Cast<SupportBuffer, byte>(MemoryMarshal.CreateSpan(ref Data, 1));

                _renderer.SetBufferData(Handle, _startOffset, data[_startOffset.._endOffset]);

                _startOffset = -1;
                _endOffset = -1;
            }
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            _renderer.DeleteBuffer(Handle);
        }
    }
}