link statically with Vulkan

This commit is contained in:
PancakeTAS 2025-07-01 09:53:48 +02:00
parent 59b05ce220
commit eb1cee1355
No known key found for this signature in database
6 changed files with 10 additions and 217 deletions

View file

@ -20,7 +20,6 @@ add_subdirectory(lsfg-vk-gen)
file(GLOB SOURCES file(GLOB SOURCES
"src/loader/*.cpp" "src/loader/*.cpp"
"src/vulkan/*.cpp"
"src/*.cpp" "src/*.cpp"
) )

View file

@ -1,7 +1,7 @@
#ifndef HOOKS_HPP #ifndef HOOKS_HPP
#define HOOKS_HPP #define HOOKS_HPP
namespace Vulkan::Hooks { namespace Hooks {
/// ///
/// Install overrides for hooked Vulkan functions. /// Install overrides for hooked Vulkan functions.

View file

@ -1,67 +0,0 @@
#ifndef FUNCS_HPP
#define FUNCS_HPP
#include <vulkan/vulkan_core.h>
namespace Vulkan::Funcs {
///
/// Initialize the global Vulkan function pointers.
///
void initialize();
///
/// Initialize the instance Vulkan function pointers.
///
/// @param instance The Vulkan instance to initialize.
///
void initializeInstance(VkInstance instance);
///
/// Initialize the device Vulkan function pointers.
///
/// @param device The Vulkan device to initialize.
///
void initializeDevice(VkDevice device);
/// Call to the original vkCreateInstance function.
VkResult ovkCreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance
);
/// Call to the original vkDestroyInstance function.
void ovkDestroyInstance(
VkInstance instance,
const VkAllocationCallbacks* pAllocator
);
/// Call to the original vkCreateDevice function.
VkResult ovkCreateDevice(
VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice
);
/// Call to the original vkDestroyDevice function.
void ovkDestroyDevice(
VkDevice device,
const VkAllocationCallbacks* pAllocator
);
/// Call to the original vkCreateSwapchainKHR function.
VkResult ovkCreateSwapchainKHR(
VkDevice device,
const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchain
);
/// Call to the original vkDestroySwapchainKHR function.
void ovkDestroySwapchainKHR(
VkDevice device,
VkSwapchainKHR swapchain,
const VkAllocationCallbacks* pAllocator
);
}
#endif // FUNCS_HPP

View file

@ -1,5 +1,4 @@
#include "vulkan/hooks.hpp" #include "hooks.hpp"
#include "vulkan/funcs.hpp"
#include "loader/dl.hpp" #include "loader/dl.hpp"
#include "loader/vk.hpp" #include "loader/vk.hpp"
#include "application.hpp" #include "application.hpp"
@ -9,31 +8,18 @@
#include <optional> #include <optional>
using namespace Vulkan; using namespace Hooks;
namespace { namespace {
bool initialized{false}; bool initialized{false};
std::optional<Application> application; std::optional<Application> application;
VkResult myvkCreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance) {
auto res = Funcs::ovkCreateInstance(pCreateInfo, pAllocator, pInstance);
Funcs::initializeInstance(*pInstance);
return res;
}
VkResult myvkCreateDevice( VkResult myvkCreateDevice(
VkPhysicalDevice physicalDevice, VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo, const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice) { VkDevice* pDevice) {
auto res = Funcs::ovkCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice); auto res = vkCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
Funcs::initializeDevice(*pDevice);
// create the main application // create the main application
if (application.has_value()) { if (application.has_value()) {
@ -61,7 +47,7 @@ namespace {
const VkSwapchainCreateInfoKHR* pCreateInfo, const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator, const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchain) { VkSwapchainKHR* pSwapchain) {
auto res = Funcs::ovkCreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain); auto res = vkCreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
// add the swapchain to the application // add the swapchain to the application
if (!application.has_value()) { if (!application.has_value()) {
@ -120,7 +106,7 @@ namespace {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
Funcs::ovkDestroySwapchainKHR(device, swapchain, pAllocator); vkDestroySwapchainKHR(device, swapchain, pAllocator);
} }
void myvkDestroyDevice( void myvkDestroyDevice(
@ -134,7 +120,7 @@ namespace {
Log::warn("lsfg-vk(hooks): No application to destroy, continuing"); Log::warn("lsfg-vk(hooks): No application to destroy, continuing");
} }
Funcs::ovkDestroyDevice(device, pAllocator); vkDestroyDevice(device, pAllocator);
} }
} }
@ -146,8 +132,6 @@ void Hooks::initialize() {
} }
// register hooks to vulkan loader // register hooks to vulkan loader
Loader::VK::registerSymbol("vkCreateInstance",
reinterpret_cast<void*>(myvkCreateInstance));
Loader::VK::registerSymbol("vkCreateDevice", Loader::VK::registerSymbol("vkCreateDevice",
reinterpret_cast<void*>(myvkCreateDevice)); reinterpret_cast<void*>(myvkCreateDevice));
Loader::VK::registerSymbol("vkDestroyDevice", Loader::VK::registerSymbol("vkDestroyDevice",
@ -159,8 +143,6 @@ void Hooks::initialize() {
// register hooks to dynamic loader under libvulkan.so.1 // register hooks to dynamic loader under libvulkan.so.1
Loader::DL::File vk1("libvulkan.so.1"); Loader::DL::File vk1("libvulkan.so.1");
vk1.defineSymbol("vkCreateInstance",
reinterpret_cast<void*>(myvkCreateInstance));
vk1.defineSymbol("vkCreateDevice", vk1.defineSymbol("vkCreateDevice",
reinterpret_cast<void*>(myvkCreateDevice)); reinterpret_cast<void*>(myvkCreateDevice));
vk1.defineSymbol("vkDestroyDevice", vk1.defineSymbol("vkDestroyDevice",
@ -173,8 +155,6 @@ void Hooks::initialize() {
// register hooks to dynamic loader under libvulkan.so // register hooks to dynamic loader under libvulkan.so
Loader::DL::File vk2("libvulkan.so"); Loader::DL::File vk2("libvulkan.so");
vk2.defineSymbol("vkCreateInstance",
reinterpret_cast<void*>(myvkCreateInstance));
vk2.defineSymbol("vkCreateDevice", vk2.defineSymbol("vkCreateDevice",
reinterpret_cast<void*>(myvkCreateDevice)); reinterpret_cast<void*>(myvkCreateDevice));
vk2.defineSymbol("vkDestroyDevice", vk2.defineSymbol("vkDestroyDevice",

View file

@ -1,7 +1,6 @@
#include "loader/dl.hpp" #include "loader/dl.hpp"
#include "loader/vk.hpp" #include "loader/vk.hpp"
#include "vulkan/funcs.hpp" #include "hooks.hpp"
#include "vulkan/hooks.hpp"
#include "log.hpp" #include "log.hpp"
extern "C" void __attribute__((constructor)) init(); extern "C" void __attribute__((constructor)) init();
@ -14,9 +13,8 @@ void init() {
Loader::DL::initialize(); Loader::DL::initialize();
Loader::VK::initialize(); Loader::VK::initialize();
// setup vulkan stuff // setup hooks
Vulkan::Funcs::initialize(); Hooks::initialize();
Vulkan::Hooks::initialize();
Log::info("lsfg-vk: init() completed successfully"); Log::info("lsfg-vk: init() completed successfully");
} }

View file

@ -1,117 +0,0 @@
#include "vulkan/funcs.hpp"
#include "loader/dl.hpp"
#include "loader/vk.hpp"
#include "log.hpp"
#include <vulkan/vulkan_core.h>
using namespace Vulkan;
namespace {
PFN_vkCreateInstance vkCreateInstance_ptr{};
PFN_vkDestroyInstance vkDestroyInstance_ptr{};
PFN_vkCreateDevice vkCreateDevice_ptr{};
PFN_vkDestroyDevice vkDestroyDevice_ptr{};
PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR_ptr{};
PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR_ptr{};
}
void Funcs::initialize() {
if (vkCreateInstance_ptr || vkDestroyInstance_ptr) {
Log::warn("lsfg-vk(funcs): Global Vulkan functions already initialized, did you call it twice?");
return;
}
auto* handle = Loader::DL::odlopen("libvulkan.so.1", 0x2);
vkCreateInstance_ptr = reinterpret_cast<PFN_vkCreateInstance>(
Loader::DL::odlsym(handle, "vkCreateInstance"));
vkDestroyInstance_ptr = reinterpret_cast<PFN_vkDestroyInstance>(
Loader::DL::odlsym(handle, "vkDestroyInstance"));
if (!vkCreateInstance_ptr || !vkDestroyInstance_ptr) {
Log::error("lsfg-vk(funcs): Failed to initialize Vulkan functions, missing symbols");
exit(EXIT_FAILURE);
}
Log::debug("lsfg-vk(funcs): Initialized global Vulkan functions with original pointers");
}
void Funcs::initializeInstance(VkInstance instance) {
if (vkCreateDevice_ptr || vkDestroyDevice_ptr) {
Log::warn("lsfg-vk(funcs): Instance Vulkan functions already initialized, did you call it twice?");
return;
}
vkCreateDevice_ptr = reinterpret_cast<PFN_vkCreateDevice>(
Loader::VK::ovkGetInstanceProcAddr(instance, "vkCreateDevice"));
vkDestroyDevice_ptr = reinterpret_cast<PFN_vkDestroyDevice>(
Loader::VK::ovkGetInstanceProcAddr(instance, "vkDestroyDevice"));
if (!vkCreateDevice_ptr || !vkDestroyDevice_ptr) {
Log::error("lsfg-vk(funcs): Failed to initialize Vulkan instance functions, missing symbols");
exit(EXIT_FAILURE);
}
Log::debug("lsfg-vk(funcs): Initialized instance Vulkan functions with original pointers");
}
void Funcs::initializeDevice(VkDevice device) {
if (vkCreateSwapchainKHR_ptr || vkDestroySwapchainKHR_ptr) {
Log::warn("lsfg-vk(funcs): Device Vulkan functions already initialized, did you call it twice?");
return;
}
vkCreateSwapchainKHR_ptr = reinterpret_cast<PFN_vkCreateSwapchainKHR>(
Loader::VK::ovkGetDeviceProcAddr(device, "vkCreateSwapchainKHR"));
vkDestroySwapchainKHR_ptr = reinterpret_cast<PFN_vkDestroySwapchainKHR>(
Loader::VK::ovkGetDeviceProcAddr(device, "vkDestroySwapchainKHR"));
if (!vkCreateSwapchainKHR_ptr || !vkDestroySwapchainKHR_ptr) {
Log::error("lsfg-vk(funcs): Failed to initialize Vulkan device functions, missing symbols");
exit(EXIT_FAILURE);
}
Log::debug("lsfg-vk(funcs): Initialized device Vulkan functions with original pointers");
}
// original function calls
VkResult Funcs::ovkCreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance) {
return vkCreateInstance_ptr(pCreateInfo, pAllocator, pInstance);
}
void Funcs::ovkDestroyInstance(
VkInstance instance,
const VkAllocationCallbacks* pAllocator) {
vkDestroyInstance_ptr(instance, pAllocator);
}
VkResult Funcs::ovkCreateDevice(
VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice) {
return vkCreateDevice_ptr(physicalDevice, pCreateInfo, pAllocator, pDevice);
}
void Funcs::ovkDestroyDevice(
VkDevice device,
const VkAllocationCallbacks* pAllocator) {
vkDestroyDevice_ptr(device, pAllocator);
}
VkResult Funcs::ovkCreateSwapchainKHR(
VkDevice device,
const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchain) {
return vkCreateSwapchainKHR_ptr(device, pCreateInfo, pAllocator, pSwapchain);
}
void Funcs::ovkDestroySwapchainKHR(
VkDevice device,
VkSwapchainKHR swapchain,
const VkAllocationCallbacks* pAllocator) {
vkDestroySwapchainKHR_ptr(device, swapchain, pAllocator);
}