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<vk::PhysicalDevice> physicalDevices = mgr.listDevices();
uint32_t deviceCount = physicalDevices.size();
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)
return results;
for (uint32_t i = 0; i < deviceCount; i++) {
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(physicalDevices.at(i), &properties);
VkPhysicalDeviceMemoryProperties memoryProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevices.at(i), &memoryProperties);
VkPhysicalDeviceProperties properties = physicalDevices.at(i).getProperties();
VkPhysicalDeviceMemoryProperties memoryProperties = physicalDevices.at(i).getMemoryProperties();
const uint32_t major = VK_VERSION_MAJOR(properties.apiVersion);
const uint32_t minor = VK_VERSION_MINOR(properties.apiVersion);
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(const ggml_vk_device &device);
bool ggml_vk_init_device(int device);
bool ggml_vk_has_vulkan();
bool ggml_vk_has_device();
ggml_vk_device ggml_vk_current_device();
struct ggml_kompute_context * ggml_vk_init(void);

View File

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

View File

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

View File

@ -10,7 +10,6 @@
#include "kompute/Core.hpp"
#if VK_USE_PLATFORM_ANDROID_KHR
#ifndef 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
#endif // !KOMPUTE_VK_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#endif // VK_USE_PLATFORM_ANDROID_KHR
namespace kp {
} // namespace kp

View File

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

View File

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