Switch to a dynamic dispatch table instead of linking hard against libvulkan.

This commit is contained in:
Adam Treat 2023-09-12 12:39:38 -04:00 committed by Cebtenzzre
parent 8563fa001f
commit 45c8778b49
7 changed files with 23 additions and 17 deletions

View File

@ -123,21 +123,20 @@ static std::string ggml_vk_getVendorName(uint32_t vendorID) {
} }
std::vector<ggml_vk_device> ggml_vk_available_devices(size_t memoryRequired) { std::vector<ggml_vk_device> ggml_vk_available_devices(size_t memoryRequired) {
std::vector<vk::PhysicalDevice> physicalDevices = mgr.listDevices();
uint32_t deviceCount = physicalDevices.size();
std::vector<ggml_vk_device> results; std::vector<ggml_vk_device> results;
if (!mgr.hasVulkan())
return results;
std::vector<vk::PhysicalDevice> physicalDevices = mgr.listDevices();
uint32_t deviceCount = physicalDevices.size();
if (deviceCount == 0) if (deviceCount == 0)
return results; return results;
for (uint32_t i = 0; i < deviceCount; i++) { for (uint32_t i = 0; i < deviceCount; i++) {
VkPhysicalDeviceProperties properties; VkPhysicalDeviceProperties properties = physicalDevices.at(i).getProperties();
vkGetPhysicalDeviceProperties(physicalDevices.at(i), &properties); VkPhysicalDeviceMemoryProperties memoryProperties = physicalDevices.at(i).getMemoryProperties();
VkPhysicalDeviceMemoryProperties memoryProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevices.at(i), &memoryProperties);
const uint32_t major = VK_VERSION_MAJOR(properties.apiVersion); const uint32_t major = VK_VERSION_MAJOR(properties.apiVersion);
const uint32_t minor = VK_VERSION_MINOR(properties.apiVersion); const uint32_t minor = VK_VERSION_MINOR(properties.apiVersion);
if (major < 1 || minor < 2) if (major < 1 || minor < 2)

View File

@ -40,6 +40,7 @@ std::vector<ggml_vk_device> ggml_vk_available_devices(size_t memoryRequired);
bool ggml_vk_init_device(size_t memoryRequired, const std::string &device); bool ggml_vk_init_device(size_t memoryRequired, const std::string &device);
bool ggml_vk_init_device(const ggml_vk_device &device); bool ggml_vk_init_device(const ggml_vk_device &device);
bool ggml_vk_init_device(int device); bool ggml_vk_init_device(int device);
bool ggml_vk_has_vulkan();
bool ggml_vk_has_device(); bool ggml_vk_has_device();
ggml_vk_device ggml_vk_current_device(); ggml_vk_device ggml_vk_current_device();
struct ggml_kompute_context * ggml_vk_init(void); struct ggml_kompute_context * ggml_vk_init(void);

View File

@ -158,6 +158,8 @@ else()
find_package(fmt REQUIRED) find_package(fmt REQUIRED)
endif() endif()
add_compile_definitions(VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
# #################################################### # ####################################################
# Preprocessor Macros # Preprocessor Macros
# #################################################### # ####################################################

View File

@ -59,7 +59,7 @@ if(KOMPUTE_OPT_ANDROID_BUILD)
kp_shader kp_shader
fmt::fmt-header-only) fmt::fmt-header-only)
else() else()
target_link_libraries(kompute PUBLIC Vulkan::Vulkan target_link_libraries(kompute PUBLIC
kp_logger kp_logger
kp_shader kp_shader
fmt::fmt-header-only) fmt::fmt-header-only)

View File

@ -10,7 +10,6 @@
#include "kompute/Core.hpp" #include "kompute/Core.hpp"
#if VK_USE_PLATFORM_ANDROID_KHR
#ifndef KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE #ifndef KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#define KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE #define KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
/** /**
@ -21,7 +20,6 @@
**/ **/
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#endif // !KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE #endif // !KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#endif // VK_USE_PLATFORM_ANDROID_KHR
namespace kp { namespace kp {
} // namespace kp } // namespace kp

View File

@ -223,20 +223,21 @@ Manager::createInstance()
} }
#endif #endif
#if VK_USE_PLATFORM_ANDROID_KHR try {
vk::DynamicLoader dl; mDynamicLoader = std::make_shared<vk::DynamicLoader>();
} catch (const std::exception & err) {
return;
}
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
dl.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr"); mDynamicLoader->getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr); VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
#endif // VK_USE_PLATFORM_ANDROID_KHR
this->mInstance = std::make_shared<vk::Instance>(); this->mInstance = std::make_shared<vk::Instance>();
vk::createInstance( vk::createInstance(
&computeInstanceCreateInfo, nullptr, this->mInstance.get()); &computeInstanceCreateInfo, nullptr, this->mInstance.get());
#if VK_USE_PLATFORM_ANDROID_KHR
VULKAN_HPP_DEFAULT_DISPATCHER.init(*this->mInstance); VULKAN_HPP_DEFAULT_DISPATCHER.init(*this->mInstance);
#endif // VK_USE_PLATFORM_ANDROID_KHR
KP_LOG_DEBUG("Kompute Manager Instance Created"); KP_LOG_DEBUG("Kompute Manager Instance Created");

View File

@ -43,6 +43,10 @@ class Manager
return this->mDevice.get(); return this->mDevice.get();
} }
bool hasVulkan() const {
return this->mDynamicLoader.get();
}
/** /**
* Initialize a device. * Initialize a device.
* *
@ -240,6 +244,7 @@ class Manager
bool mFreeInstance = false; bool mFreeInstance = false;
std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice = nullptr; std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice = nullptr;
std::shared_ptr<vk::Device> mDevice = nullptr; std::shared_ptr<vk::Device> mDevice = nullptr;
std::shared_ptr<vk::DynamicLoader> mDynamicLoader = nullptr;
bool mFreeDevice = false; bool mFreeDevice = false;
// -------------- ALWAYS OWNED RESOURCES // -------------- ALWAYS OWNED RESOURCES