Created
November 5, 2019 08:55
-
-
Save Warpten/c813efc065b84094b78655abca72dbb6 to your computer and use it in GitHub Desktop.
vk::DynamicLoader without pulling Windows.h everywhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "vk.hpp" | |
#include <Windows.h> | |
namespace vk | |
{ | |
DynamicLoader::DynamicLoader() : m_success(false) | |
{ | |
#if defined(__linux__) | |
m_library = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL); | |
#elif defined(__APPLE__) | |
m_library = dlopen("libvulkan.dylib", RTLD_NOW | RTLD_LOCAL); | |
#elif defined(_WIN32) | |
m_library = LoadLibrary(TEXT("vulkan-1.dll")); | |
#endif | |
m_success = m_library != 0; | |
if (!m_success) | |
{ | |
// NOTE there should be an InitializationFailedError, but msvc insists on the symbol does not exist within the scope of this function. | |
throw std::runtime_error("Failed to load vulkan library!"); | |
} | |
} | |
DynamicLoader::~DynamicLoader() noexcept | |
{ | |
if (m_library) | |
{ | |
#if defined(__linux__) || defined(__APPLE__) | |
dlclose(m_library); | |
#elif defined(_WIN32) | |
FreeLibrary(m_library); | |
#endif | |
} | |
} | |
void* DynamicLoader::getProcAddressImpl(const char* function) noexcept | |
{ | |
#if defined(__linux__) || defined(__APPLE__) | |
return reinterpret_cast<void*>(dlsym(m_library, function)); | |
#elif defined(_WIN32) | |
return reinterpret_cast<void*>(GetProcAddress(m_library, function)); | |
#endif | |
} | |
DispatchLoaderDynamic defaultDispatcherDynamic; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
namespace vk | |
{ | |
class DynamicLoader | |
{ | |
public: | |
DynamicLoader(); | |
~DynamicLoader() noexcept; | |
template <typename T> | |
T getProcAddress(const char* function) const noexcept | |
{ | |
return reinterpret_cast<T>(getProcAddressImpl(function)); | |
} | |
bool success() const noexcept { return m_success; } | |
private: | |
void* getProcAddressImpl(const char* function) noexcept; | |
private: | |
bool m_success; | |
#if defined(__linux__) || defined(__APPLE__) | |
void* m_library; | |
#elif defined(_WIN32) | |
HMODULE m_library; | |
#else | |
#error unsupported platform | |
#endif | |
}; | |
} | |
#define VULKAN_HPP_DEFAULT_DISPATCHER vk::defaultDispatcherDynamic | |
namespace vk { | |
extern DispatchLoaderDynamic defaultDispatcherDynamic; | |
} | |
// Kill the default implementation and provide our own, which is a verbatim copy of the provided one, | |
// but does not expose GetProcAddress in its header (by forcing Windows.h everywhere) | |
#define VULKAN_HPP_ENABLE_DYNAMIC_LOADER_TOOL 0 | |
#include <vulkan/vulkan.hpp> | |
#include "vk_mem_alloc.hpp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment