aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Vulkan/SemaphoreHolder.cs
blob: aa1b0eafeebbedfb6f781014d22f1e06628a240f (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
using Silk.NET.Vulkan;
using System;
using System.Threading;
using VkSemaphore = Silk.NET.Vulkan.Semaphore;

namespace Ryujinx.Graphics.Vulkan
{
    class SemaphoreHolder : IDisposable
    {
        private readonly Vk _api;
        private readonly Device _device;
        private VkSemaphore _semaphore;
        private int _referenceCount;
        public bool _disposed;

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

            var semaphoreCreateInfo = new SemaphoreCreateInfo()
            {
                SType = StructureType.SemaphoreCreateInfo
            };

            api.CreateSemaphore(device, in semaphoreCreateInfo, null, out _semaphore).ThrowOnError();

            _referenceCount = 1;
        }

        public VkSemaphore GetUnsafe()
        {
            return _semaphore;
        }

        public VkSemaphore Get()
        {
            Interlocked.Increment(ref _referenceCount);
            return _semaphore;
        }

        public unsafe void Put()
        {
            if (Interlocked.Decrement(ref _referenceCount) == 0)
            {
                _api.DestroySemaphore(_device, _semaphore, null);
                _semaphore = default;
            }
        }

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