forked from vv/efemra
1
0
Fork 0
efemra/src/rendering/vulkan/sync.zig

69 lines
2.0 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const vk = @import("vulkan");
const Device = @import("device.zig").Device;
const Renderer = @import("Renderer.zig");
pub const Semaphore = struct {
const Self = @This();
handle: vk.Semaphore,
pub fn init(device: *const Device) !Self {
const handle = try device.dispatch.createSemaphore(device.handle, &.{
.s_type = .semaphore_create_info,
.flags = .{},
}, null);
return Self{
.handle = handle,
};
}
pub fn deinit(self: Self, device: *const Device) void {
device.dispatch.destroySemaphore(device.handle, self.handle, null);
}
};
pub const Fence = struct {
const Self = @This();
pub const State = enum(i32) {
signaled = @enumToInt(vk.Result.success),
unsignaled = @enumToInt(vk.Result.not_ready),
};
handle: vk.Fence,
pub fn init(device: *const Device, signaled: bool) !Self {
const handle = try device.dispatch.createFence(device.handle, &.{ .s_type = .fence_create_info, .flags = .{
.signaled_bit = if (signaled) true else false,
} }, null);
return Self{
.handle = handle,
};
}
pub fn deinit(self: *const Self, device: *const Device) void {
device.dispatch.destroyFence(device.handle, self.handle, null);
}
pub fn reset(self: *const Self, device: *const Device) !void {
try device.dispatch.resetFences(device.handle, 1, &self.handle);
}
pub fn wait(self: *const Self, device: *const Device) !void {
const timeout = std.math.maxInt(u64);
while ((try self.stat(device)) != .signaled) {
_ = try device.dispatch.waitForFences(device.handle, 1, @ptrCast([*]const vk.Fence, &self.handle), vk.FALSE, timeout);
}
}
pub fn stat(self: *const Self, device: *const Device) !State {
return @intToEnum(State, @enumToInt(try device.dispatch.getFenceStatus(device.handle, self.handle)));
}
};