aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Vulkan/ResourceArray.cs
blob: 0880a10f072d61d8e338ae148132937a8612872f (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
using Silk.NET.Vulkan;
using System;
using System.Diagnostics;

namespace Ryujinx.Graphics.Vulkan
{
    class ResourceArray : IDisposable
    {
        private DescriptorSet[] _cachedDescriptorSets;

        private ShaderCollection _cachedDscProgram;
        private int _cachedDscSetIndex;
        private int _cachedDscIndex;

        private int _bindCount;

        protected void SetDirty(VulkanRenderer gd)
        {
            ReleaseDescriptorSet();

            if (_bindCount != 0)
            {
                gd.PipelineInternal.ForceTextureDirty();
            }
        }

        public bool TryGetCachedDescriptorSets(CommandBufferScoped cbs, ShaderCollection program, int setIndex, out DescriptorSet[] sets)
        {
            if (_cachedDescriptorSets != null)
            {
                _cachedDscProgram.UpdateManualDescriptorSetCollectionOwnership(cbs, _cachedDscSetIndex, _cachedDscIndex);

                sets = _cachedDescriptorSets;

                return true;
            }

            var dsc = program.GetNewManualDescriptorSetCollection(cbs, setIndex, out _cachedDscIndex).Get(cbs);

            sets = dsc.GetSets();

            _cachedDescriptorSets = sets;
            _cachedDscProgram = program;
            _cachedDscSetIndex = setIndex;

            return false;
        }

        public void IncrementBindCount()
        {
            _bindCount++;
        }

        public void DecrementBindCount()
        {
            int newBindCount = --_bindCount;
            Debug.Assert(newBindCount >= 0);
        }

        private void ReleaseDescriptorSet()
        {
            if (_cachedDescriptorSets != null)
            {
                _cachedDscProgram.ReleaseManualDescriptorSetCollection(_cachedDscSetIndex, _cachedDscIndex);
                _cachedDescriptorSets = null;
            }
        }

        public void Dispose()
        {
            ReleaseDescriptorSet();
        }
    }
}