Skip to content

Commit

Permalink
Improve Vulkan fence
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Aug 28, 2024
1 parent b509da7 commit ee8c2ed
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#pragma once

#include <expected> // expected
#include <system_error> // error_code
#include <expected> // expected

#include <vulkan/vulkan.h>

Expand All @@ -18,6 +17,11 @@ namespace tactile {
class TACTILE_VULKAN_API VulkanFence final
{
public:
TACTILE_DECLARE_MOVE(VulkanFence);
TACTILE_DELETE_COPY(VulkanFence);

VulkanFence() = default;

/**
* Creates a Vulkan fence from existing resources.
*
Expand All @@ -26,16 +30,8 @@ class TACTILE_VULKAN_API VulkanFence final
*/
VulkanFence(VkDevice device, VkFence fence);

VulkanFence(VulkanFence&& other) noexcept;

VulkanFence(const VulkanFence&) = delete;

~VulkanFence() noexcept;

auto operator=(VulkanFence&& other) noexcept -> VulkanFence&;

auto operator=(const VulkanFence&) -> VulkanFence& = delete;

/**
* Creates a Vulkan fence.
*
Expand All @@ -50,20 +46,20 @@ class TACTILE_VULKAN_API VulkanFence final
VkFenceCreateFlags flags) -> std::expected<VulkanFence, VkResult>;

[[nodiscard]]
auto device() noexcept -> VkDevice
{
return mDevice;
}
auto reset() -> VkResult;

[[nodiscard]]
auto wait() -> VkResult;

[[nodiscard]]
auto device() -> VkDevice;

[[nodiscard]]
auto get() noexcept -> VkFence
{
return mFence;
}
auto get() -> VkFence;

private:
VkDevice mDevice {VK_NULL_HANDLE};
VkFence mFence {VK_NULL_HANDLE};
VkDevice m_device {VK_NULL_HANDLE};
VkFence m_fence {VK_NULL_HANDLE};

void _destroy() noexcept;
};
Expand Down
48 changes: 37 additions & 11 deletions source/vulkan_renderer/lib/src/vulkan_fence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

#include "tactile/vulkan_renderer/vulkan_fence.hpp"

#include <cstdint> // uint64_t
#include <limits> // numeric_limits
#include <utility> // exchange

#include "tactile/runtime/logging.hpp"
#include "tactile/vulkan_renderer/vulkan_util.hpp"

namespace tactile {

VulkanFence::VulkanFence(VkDevice device, VkFence fence) :
mDevice {device},
mFence {fence}
VulkanFence::VulkanFence(VkDevice device, VkFence fence)
: m_device {device},
m_fence {fence}
{}

VulkanFence::VulkanFence(VulkanFence&& other) noexcept :
mDevice {std::exchange(other.mDevice, VK_NULL_HANDLE)},
mFence {std::exchange(other.mFence, VK_NULL_HANDLE)}
VulkanFence::VulkanFence(VulkanFence&& other) noexcept
: m_device {std::exchange(other.m_device, VK_NULL_HANDLE)},
m_fence {std::exchange(other.m_fence, VK_NULL_HANDLE)}
{}

VulkanFence::~VulkanFence() noexcept
Expand All @@ -26,9 +28,9 @@ VulkanFence::~VulkanFence() noexcept

void VulkanFence::_destroy() noexcept
{
if (mFence != VK_NULL_HANDLE) {
vkDestroyFence(mDevice, mFence, nullptr);
mFence = VK_NULL_HANDLE;
if (m_fence != VK_NULL_HANDLE) {
vkDestroyFence(m_device, m_fence, nullptr);
m_fence = VK_NULL_HANDLE;
}
}

Expand All @@ -37,8 +39,8 @@ auto VulkanFence::operator=(VulkanFence&& other) noexcept -> VulkanFence&
if (this != &other) {
_destroy();

mDevice = std::exchange(other.mDevice, VK_NULL_HANDLE);
mFence = std::exchange(other.mFence, VK_NULL_HANDLE);
m_device = std::exchange(other.m_device, VK_NULL_HANDLE);
m_fence = std::exchange(other.m_fence, VK_NULL_HANDLE);
}

return *this;
Expand All @@ -64,4 +66,28 @@ auto VulkanFence::create(VkDevice device, const VkFenceCreateFlags flags)
return VulkanFence {device, fence};
}

auto VulkanFence::reset() -> VkResult
{
return vkResetFences(m_device, 1, &m_fence);
}

auto VulkanFence::wait() -> VkResult
{
return vkWaitForFences(m_device,
1,
&m_fence,
VK_TRUE,
std::numeric_limits<std::uint64_t>::max());
}

auto VulkanFence::device() -> VkDevice
{
return m_device;
}

auto VulkanFence::get() -> VkFence
{
return m_fence;
}

} // namespace tactile

0 comments on commit ee8c2ed

Please sign in to comment.