#include <Vk/Fence/Fence.hpp>
#include <Vk/Device/Device.hpp>
#include <Utilities/Utilities.hpp>

Vk::Fence::Fence(std::shared_ptr<Vk::Device> device, VkFenceCreateFlags flags) : m_device(device)
{
    VkFenceCreateInfo fenceInfo{};
    fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    fenceInfo.flags = flags;

    if (vkCreateFence(device->inner(), &fenceInfo, nullptr, &m_fence) != VK_SUCCESS)
    {
        crash("Failed to create a Vulkan fence!");
    }
}

Vk::Fence::~Fence()
{
    vkDestroyFence(m_device->inner(), m_fence, nullptr);
}

VkFence Vk::Fence::inner()
{
    return m_fence;
}

bool Vk::Fence::wait(uint64_t timeout)
{
    return vkWaitForFences(m_device->inner(), 1, &m_fence, VK_TRUE, timeout) == VK_SUCCESS;
}

void Vk::Fence::reset()
{
    vkResetFences(m_device->inner(), 1, &m_fence);
}