diff --git a/ggml-vulkan.cpp b/ggml-vulkan.cpp index 9b5c01f68..055b1124d 100644 --- a/ggml-vulkan.cpp +++ b/ggml-vulkan.cpp @@ -123,21 +123,20 @@ static std::string ggml_vk_getVendorName(uint32_t vendorID) { } std::vector ggml_vk_available_devices(size_t memoryRequired) { - std::vector physicalDevices = mgr.listDevices(); - uint32_t deviceCount = physicalDevices.size(); std::vector results; + if (!mgr.hasVulkan()) + return results; + + std::vector 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) diff --git a/ggml-vulkan.h b/ggml-vulkan.h index ad8b41e4d..d13ed4184 100644 --- a/ggml-vulkan.h +++ b/ggml-vulkan.h @@ -40,6 +40,7 @@ std::vector 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); diff --git a/kompute/CMakeLists.txt b/kompute/CMakeLists.txt index f89e13d1d..aa228653a 100644 --- a/kompute/CMakeLists.txt +++ b/kompute/CMakeLists.txt @@ -158,6 +158,8 @@ else() find_package(fmt REQUIRED) endif() +add_compile_definitions(VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1) + # #################################################### # Preprocessor Macros # #################################################### diff --git a/kompute/src/CMakeLists.txt b/kompute/src/CMakeLists.txt index 5f02ce12c..4179a81f2 100644 --- a/kompute/src/CMakeLists.txt +++ b/kompute/src/CMakeLists.txt @@ -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) diff --git a/kompute/src/Core.cpp b/kompute/src/Core.cpp index 60849a3ec..9b0483232 100644 --- a/kompute/src/Core.cpp +++ b/kompute/src/Core.cpp @@ -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 diff --git a/kompute/src/Manager.cpp b/kompute/src/Manager.cpp index 07514ed9a..2c86b6e10 100644 --- a/kompute/src/Manager.cpp +++ b/kompute/src/Manager.cpp @@ -223,20 +223,21 @@ Manager::createInstance() } #endif -#if VK_USE_PLATFORM_ANDROID_KHR - vk::DynamicLoader dl; + try { + mDynamicLoader = std::make_shared(); + } catch (const std::exception & err) { + return; + } + PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = - dl.getProcAddress("vkGetInstanceProcAddr"); + mDynamicLoader->getProcAddress("vkGetInstanceProcAddr"); VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr); -#endif // VK_USE_PLATFORM_ANDROID_KHR this->mInstance = std::make_shared(); 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"); diff --git a/kompute/src/include/kompute/Manager.hpp b/kompute/src/include/kompute/Manager.hpp index 8fda58f84..42336f4e8 100644 --- a/kompute/src/include/kompute/Manager.hpp +++ b/kompute/src/include/kompute/Manager.hpp @@ -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 mPhysicalDevice = nullptr; std::shared_ptr mDevice = nullptr; + std::shared_ptr mDynamicLoader = nullptr; bool mFreeDevice = false; // -------------- ALWAYS OWNED RESOURCES