lots of fixes

This commit is contained in:
PancakeTAS 2025-07-01 11:18:05 +02:00
parent 969fcfadeb
commit 70bf51fa72
No known key found for this signature in database
8 changed files with 142 additions and 20 deletions

View file

@ -130,14 +130,12 @@ public:
/// Get the swapchain images.
[[nodiscard]] const std::vector<VkImage>& getImages() const { return this->images; }
// Non-copyable, trivially moveable
// Non-copyable, trivially moveable and destructible
SwapchainContext(const SwapchainContext&) = delete;
SwapchainContext& operator=(const SwapchainContext&) = delete;
SwapchainContext(SwapchainContext&&) = default;
SwapchainContext& operator=(SwapchainContext&&) = default;
/// Destructor, cleans up resources.
~SwapchainContext();
~SwapchainContext() = default;
private:
// (non-owned resources)
VkSwapchainKHR swapchain;
@ -147,7 +145,7 @@ private:
// (owned resources)
Mini::Image frame_0, frame_1;
int32_t lsfgId;
std::shared_ptr<int32_t> lsfgId;
};
#endif // APPLICATION_HPP

View file

@ -0,0 +1,53 @@
#ifndef SEMAPHORE_HPP
#define SEMAPHORE_HPP
#include <vulkan/vulkan_core.h>
#include <memory>
namespace Mini {
///
/// C++ wrapper class for a Vulkan semaphore.
///
/// This class manages the lifetime of a Vulkan semaphore.
///
class Semaphore {
public:
Semaphore() noexcept = default;
///
/// Create the semaphore.
///
/// @param device Vulkan device
///
/// @throws LSFG::vulkan_error if object creation fails.
///
Semaphore(VkDevice device);
///
/// Import a semaphore.
///
/// @param device Vulkan device
/// @param fd File descriptor to import the semaphore from.
///
/// @throws LSFG::vulkan_error if object creation fails.
///
Semaphore(VkDevice device, int* fd);
/// Get the Vulkan handle.
[[nodiscard]] auto handle() const { return *this->semaphore; }
// Trivially copyable, moveable and destructible
Semaphore(const Semaphore&) noexcept = default;
Semaphore& operator=(const Semaphore&) noexcept = default;
Semaphore(Semaphore&&) noexcept = default;
Semaphore& operator=(Semaphore&&) noexcept = default;
~Semaphore() = default;
private:
std::shared_ptr<VkSemaphore> semaphore;
};
}
#endif // SEMAPHORE_HPP

View file

@ -4,6 +4,7 @@
#include <cassert>
#include <format>
#include <optional>
using namespace LSFG;
@ -119,8 +120,8 @@ void Context::present(const Core::Device& device, int inSem, int outSem) {
cmdBuffer.end();
cmdBuffer.submit(device.getComputeQueue(), std::nullopt,
{ inSemaphore }, {},
{ outSemaphore }, {});
{ inSemaphore }, std::nullopt,
{ outSemaphore }, std::nullopt);
fc++;
}

View file

@ -83,10 +83,12 @@ void CommandBuffer::submit(VkQueue queue, std::optional<Fence> fence,
timelineInfo.pSignalSemaphoreValues = signalSemaphoreValues->data();
}
std::vector<VkSemaphore> waitSemaphoresHandles(waitSemaphores.size());
std::vector<VkSemaphore> waitSemaphoresHandles;
waitSemaphoresHandles.reserve(waitSemaphores.size());
for (const auto& semaphore : waitSemaphores)
waitSemaphoresHandles.push_back(semaphore.handle());
std::vector<VkSemaphore> signalSemaphoresHandles(signalSemaphores.size());
std::vector<VkSemaphore> signalSemaphoresHandles;
signalSemaphoresHandles.reserve(signalSemaphores.size());
for (const auto& semaphore : signalSemaphores)
signalSemaphoresHandles.push_back(semaphore.handle());

View file

@ -45,6 +45,9 @@ Semaphore::Semaphore(const Core::Device& device, int fd) {
throw LSFG::vulkan_error(res, "Unable to create semaphore");
// import semaphore from fd
auto vkImportSemaphoreFdKHR = reinterpret_cast<PFN_vkImportSemaphoreFdKHR>(
vkGetDeviceProcAddr(device.handle(), "vkImportSemaphoreFdKHR"));
const VkImportSemaphoreFdInfoKHR importInfo{
.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
.semaphore = semaphoreHandle,

View file

@ -1,8 +1,10 @@
#include "application.hpp"
#include "log.hpp"
#include "mini/image.hpp"
#include "mini/semaphore.hpp"
#include <lsfg.hpp>
#include <memory>
#include <vulkan/vulkan_core.h>
Application::Application(VkDevice device, VkPhysicalDevice physicalDevice,
@ -38,7 +40,13 @@ SwapchainContext::SwapchainContext(const Application& app, VkSwapchainKHR swapch
VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_ASPECT_COLOR_BIT, &frame1fd
);
this->lsfgId = LSFG::createContext(extent.width, extent.height, frame0fd, frame1fd);
auto id = LSFG::createContext(extent.width, extent.height, frame0fd, frame1fd);
this->lsfgId = std::shared_ptr<int32_t>(
new int32_t(id),
[](const int32_t* id) {
LSFG::deleteContext(*id);
}
);
}
void Application::presentSwapchain(VkSwapchainKHR handle, VkQueue queue,
@ -65,14 +73,6 @@ void SwapchainContext::present(const Application& app, VkQueue queue,
throw LSFG::vulkan_error(res, "Failed to present swapchain");
}
SwapchainContext::~SwapchainContext() {
try {
LSFG::deleteContext(this->lsfgId);
} catch (const std::exception&) {
return;
}
}
bool Application::removeSwapchain(VkSwapchainKHR handle) {
auto it = this->swapchains.find(handle);
if (it == this->swapchains.end())

View file

@ -7,6 +7,7 @@
#include <lsfg.hpp>
#include <optional>
#include <vulkan/vulkan_core.h>
using namespace Hooks;
@ -24,7 +25,8 @@ namespace {
const std::vector<const char*> requiredExtensions = {
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME
VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME
};
for (const auto& ext : requiredExtensions) {
auto it = std::ranges::find(extensions, ext);
@ -49,7 +51,9 @@ namespace {
const std::vector<const char*> requiredExtensions = {
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME
};
for (const auto& ext : requiredExtensions) {
auto it = std::ranges::find(extensions, ext);

61
src/mini/semaphore.cpp Normal file
View file

@ -0,0 +1,61 @@
#include "mini/semaphore.hpp"
#include "lsfg.hpp"
#include <vulkan/vulkan_core.h>
using namespace Mini;
Semaphore::Semaphore(VkDevice device) {
// create semaphore
const VkSemaphoreCreateInfo desc{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
};
VkSemaphore semaphoreHandle{};
auto res = vkCreateSemaphore(device, &desc, nullptr, &semaphoreHandle);
if (res != VK_SUCCESS || semaphoreHandle == VK_NULL_HANDLE)
throw LSFG::vulkan_error(res, "Unable to create semaphore");
// store semaphore in shared ptr
this->semaphore = std::shared_ptr<VkSemaphore>(
new VkSemaphore(semaphoreHandle),
[dev = device](VkSemaphore* semaphoreHandle) {
vkDestroySemaphore(dev, *semaphoreHandle, nullptr);
}
);
}
Semaphore::Semaphore(VkDevice device, int* fd) {
// create semaphore
const VkExportSemaphoreCreateInfo exportInfo{
.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
.handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
};
const VkSemaphoreCreateInfo desc{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = &exportInfo
};
VkSemaphore semaphoreHandle{};
auto res = vkCreateSemaphore(device, &desc, nullptr, &semaphoreHandle);
if (res != VK_SUCCESS || semaphoreHandle == VK_NULL_HANDLE)
throw LSFG::vulkan_error(res, "Unable to create semaphore");
// export semaphore to fd
auto vkGetSemaphoreFdKHR = reinterpret_cast<PFN_vkGetSemaphoreFdKHR>(
vkGetDeviceProcAddr(device, "vkGetSemaphoreFdKHR"));
const VkSemaphoreGetFdInfoKHR fdInfo{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
.semaphore = semaphoreHandle,
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
};
res = vkGetSemaphoreFdKHR(device, &fdInfo, fd);
if (res != VK_SUCCESS || *fd < 0)
throw LSFG::vulkan_error(res, "Unable to export semaphore to fd");
// store semaphore in shared ptr
this->semaphore = std::shared_ptr<VkSemaphore>(
new VkSemaphore(semaphoreHandle),
[dev = device](VkSemaphore* semaphoreHandle) {
vkDestroySemaphore(dev, *semaphoreHandle, nullptr);
}
);
}