aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Vulkan/FenceHolder.cs
blob: 1c1e624073c1f2fe8ba6250d460cd4bb3f0cc289 (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
using Silk.NET.Vulkan;
using System;
using System.Threading;

namespace Ryujinx.Graphics.Vulkan
{
    class FenceHolder : IDisposable
    {
        private readonly Vk _api;
        private readonly Device _device;
        private Fence _fence;
        private int _referenceCount;
        private bool _disposed;

        public unsafe FenceHolder(Vk api, Device device)
        {
            _api = api;
            _device = device;

            var fenceCreateInfo = new FenceCreateInfo()
            {
                SType = StructureType.FenceCreateInfo
            };

            api.CreateFence(device, in fenceCreateInfo, null, out _fence).ThrowOnError();

            _referenceCount = 1;
        }

        public Fence GetUnsafe()
        {
            return _fence;
        }

        public Fence Get()
        {
            Interlocked.Increment(ref _referenceCount);
            return _fence;
        }

        public void Put()
        {
            if (Interlocked.Decrement(ref _referenceCount) == 0)
            {
                _api.DestroyFence(_device, _fence, Span<AllocationCallbacks>.Empty);
                _fence = default;
            }
        }

        public void Wait()
        {
            Span<Fence> fences = stackalloc Fence[]
            {
                _fence
            };

            FenceHelper.WaitAllIndefinitely(_api, _device, fences);
        }

        public bool IsSignaled()
        {
            Span<Fence> fences = stackalloc Fence[]
            {
                _fence
            };

            return FenceHelper.AllSignaled(_api, _device, fences);
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                Put();
                _disposed = true;
            }
        }
    }
}