-
Notifications
You must be signed in to change notification settings - Fork 34
/
fence.cc
79 lines (62 loc) · 2.09 KB
/
fence.cc
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
#include <vkpp/fence.hh>
#include <vkpp/device.hh>
#include <vkpp/debug_marker.hh>
#include <vkpp/exception.hh>
#include <utility>
#include <limits>
namespace vkpp {
Fence::~Fence() noexcept {
if (handle != VK_NULL_HANDLE) {
vkDestroyFence(device, handle, nullptr);
}
}
std::vector<Fence> Fence::create(Device& device, std::uint32_t amount, const char* name) {
std::vector<Fence> fences;
fences.reserve(amount);
for (std::uint32_t i { 0 }; i < amount; ++i) {
fences.emplace_back(device);
DebugMarker::object_name(device, fences.back(),
VK_OBJECT_TYPE_FENCE,
name);
} return fences;
}
Fence::Fence(Device& logical_device) : device { logical_device.get_handle() } {
VkFenceCreateInfo create_info;
create_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
create_info.pNext = nullptr;
create_info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
if (VkResult error = vkCreateFence(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create fence!" };
}
}
Fence::Fence(Fence&& fence) noexcept {
swap(*this, fence);
}
Fence& Fence::operator=(Fence&& fence) noexcept {
swap(*this, fence);
return *this;
}
void swap(Fence& lhs, Fence& rhs) {
using std::swap;
swap(lhs.handle, rhs.handle);
swap(lhs.device, rhs.device);
}
VkFence& Fence::get_handle() {
return handle;
}
bool Fence::wait(std::uint64_t time) {
return vkWaitForFences(device, 1, &handle, true, time) == VK_SUCCESS;
}
bool Fence::is_signaled() const {
return vkGetFenceStatus(device, handle) == VK_SUCCESS;
}
void Fence::reset() {
vkResetFences(device, 1, &handle);
}
bool Fence::wait_and_reset() {
auto forever { std::numeric_limits<std::uint64_t>::max() };
bool result { wait(forever) };
reset(); // Put into signaled.
return result;
}
}