1 module sbylib.wrapper.vulkan.fence;
2 
3 import erupted;
4 import sbylib.wrapper.vulkan.device;
5 import sbylib.wrapper.vulkan.enums;
6 import sbylib.wrapper.vulkan.util;
7 
8 class Fence {
9     static struct CreateInfo {
10         @vkProp() {
11             immutable VkFenceCreateFlags flags;
12         }
13 
14         const mixin VkTo!(VkFenceCreateInfo);
15     }
16 
17     private Device device;
18     package VkFence fence;
19 
20     mixin ImplNameSetter!(device, fence, DebugReportObjectType.Fence);
21 
22     this(Device device, CreateInfo _info) {
23         import std.exception : enforce;
24 
25         this.device = device;
26 
27         auto info = _info.vkTo();
28 
29         enforceVK(vkCreateFence(device.device, &info, null, &fence));
30         enforce(fence != VK_NULL_HANDLE);
31     }
32 
33     ~this() {
34         vkDestroyFence(device.device, fence, null);
35     }
36 
37     auto signaled() {
38         auto result = vkGetFenceStatus(device.device, fence);
39         switch (result) {
40             case VK_SUCCESS: return true;
41             case VK_NOT_READY: return false;
42             default: assert(false);
43         }
44     }
45 
46     static void wait(uint N)(Fence[N] _fences, bool waitAll, ulong timeout) {
47         VkFence[N] fences;
48         static foreach (i; 0 .. N)
49             fences[i] = _fences[i].fence;
50         enforceVK(vkWaitForFences(_fences[0].device.device, N, fences.ptr, waitAll, timeout));
51     }
52 
53     static void reset(uint N)(Fence[N] _fences) {
54         VkFence[N] fences;
55         static foreach (i; 0 .. N)
56             fences[i] = _fences[i].fence;
57         enforceVK(vkResetFences(_fences[0].device.device, N, fences.ptr));
58     }
59 
60     static void wait(Fence[] _fences, bool waitAll, ulong timeout) {
61         VkFence[256] fences;
62         foreach (i; 0.._fences.length)
63             fences[i] = _fences[i].fence;
64         enforceVK(vkWaitForFences(_fences[0].device.device, cast(uint)_fences.length, fences.ptr, waitAll, timeout));
65     }
66 
67     static void reset(Fence[] _fences) {
68         VkFence[256] fences;
69         foreach (i; 0.._fences.length)
70             fences[i] = _fences[i].fence;
71         enforceVK(vkResetFences(_fences[0].device.device, cast(uint)_fences.length, fences.ptr));
72     }
73 }