aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.GAL/Multithreading/Resources/ThreadedTexture.cs
blob: 9ad9e645482a742466286980cd314c7d22e00f10 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.GAL.Multithreading.Commands.Texture;
using Ryujinx.Graphics.GAL.Multithreading.Model;

namespace Ryujinx.Graphics.GAL.Multithreading.Resources
{
    /// <summary>
    /// Threaded representation of a texture.
    /// </summary>
    class ThreadedTexture : ITexture
    {
        private readonly ThreadedRenderer _renderer;
        private readonly TextureCreateInfo _info;
        public ITexture Base;

        public int Width => _info.Width;

        public int Height => _info.Height;

        public ThreadedTexture(ThreadedRenderer renderer, TextureCreateInfo info)
        {
            _renderer = renderer;
            _info = info;
        }

        private TableRef<T> Ref<T>(T reference)
        {
            return new TableRef<T>(_renderer, reference);
        }

        public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
        {
            _renderer.New<TextureCopyToCommand>().Set(Ref(this), Ref((ThreadedTexture)destination), firstLayer, firstLevel);
            _renderer.QueueCommand();
        }

        public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
        {
            _renderer.New<TextureCopyToSliceCommand>().Set(Ref(this), Ref((ThreadedTexture)destination), srcLayer, dstLayer, srcLevel, dstLevel);
            _renderer.QueueCommand();
        }

        public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
        {
            ThreadedTexture dest = (ThreadedTexture)destination;

            if (_renderer.IsGpuThread())
            {
                _renderer.New<TextureCopyToScaledCommand>().Set(Ref(this), Ref(dest), srcRegion, dstRegion, linearFilter);
                _renderer.QueueCommand();
            }
            else
            {
                // Scaled copy can happen on another thread for a res scale flush.
                ThreadedHelpers.SpinUntilNonNull(ref Base);
                ThreadedHelpers.SpinUntilNonNull(ref dest.Base);

                Base.CopyTo(dest.Base, srcRegion, dstRegion, linearFilter);
            }
        }

        public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
        {
            ThreadedTexture newTex = new(_renderer, info);
            _renderer.New<TextureCreateViewCommand>().Set(Ref(this), Ref(newTex), info, firstLayer, firstLevel);
            _renderer.QueueCommand();

            return newTex;
        }

        public PinnedSpan<byte> GetData()
        {
            if (_renderer.IsGpuThread())
            {
                ResultBox<PinnedSpan<byte>> box = new();
                _renderer.New<TextureGetDataCommand>().Set(Ref(this), Ref(box));
                _renderer.InvokeCommand();

                return box.Result;
            }
            else
            {
                ThreadedHelpers.SpinUntilNonNull(ref Base);

                return Base.GetData();
            }
        }

        public PinnedSpan<byte> GetData(int layer, int level)
        {
            if (_renderer.IsGpuThread())
            {
                ResultBox<PinnedSpan<byte>> box = new();
                _renderer.New<TextureGetDataSliceCommand>().Set(Ref(this), Ref(box), layer, level);
                _renderer.InvokeCommand();

                return box.Result;
            }
            else
            {
                ThreadedHelpers.SpinUntilNonNull(ref Base);

                return Base.GetData(layer, level);
            }
        }

        public void CopyTo(BufferRange range, int layer, int level, int stride)
        {
            _renderer.New<TextureCopyToBufferCommand>().Set(Ref(this), range, layer, level, stride);
            _renderer.QueueCommand();
        }

        public void SetData(SpanOrArray<byte> data)
        {
            _renderer.New<TextureSetDataCommand>().Set(Ref(this), Ref(data.ToArray()));
            _renderer.QueueCommand();
        }

        public void SetData(SpanOrArray<byte> data, int layer, int level)
        {
            _renderer.New<TextureSetDataSliceCommand>().Set(Ref(this), Ref(data.ToArray()), layer, level);
            _renderer.QueueCommand();
        }

        public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
        {
            _renderer.New<TextureSetDataSliceRegionCommand>().Set(Ref(this), Ref(data.ToArray()), layer, level, region);
            _renderer.QueueCommand();
        }

        public void SetStorage(BufferRange buffer)
        {
            _renderer.New<TextureSetStorageCommand>().Set(Ref(this), buffer);
            _renderer.QueueCommand();
        }

        public void Release()
        {
            _renderer.New<TextureReleaseCommand>().Set(Ref(this));
            _renderer.QueueCommand();
        }
    }
}