Path: blob/master/modules/dnn/src/vkcom/vulkan/vk_loader.cpp
16344 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018, Intel Corporation, all rights reserved.5// Third party copyrights are property of their respective owners.67#include "../../precomp.hpp"8#ifdef HAVE_VULKAN9#include <vulkan/vulkan.h>10#endif11#include "vk_functions.hpp"1213#if defined(_WIN32)14#include <windows.h>15typedef HMODULE VulkanHandle;16#define DEFAULT_VK_LIBRARY_PATH "vulkan-1.dll"17#define LOAD_VK_LIBRARY(path) LoadLibrary(path)18#define FREE_VK_LIBRARY(handle) FreeLibrary(handle)19#define GET_VK_ENTRY_POINT(handle) \20(PFN_vkGetInstanceProcAddr)GetProcAddress(handle, "vkGetInstanceProcAddr");21#endif // _WIN322223#if defined(__linux__)24#include <dlfcn.h>25#include <stdio.h>26typedef void* VulkanHandle;27#define DEFAULT_VK_LIBRARY_PATH "libvulkan.so.1"28#define LOAD_VK_LIBRARY(path) dlopen(path, RTLD_LAZY | RTLD_GLOBAL)29#define FREE_VK_LIBRARY(handle) dlclose(handle)30#define GET_VK_ENTRY_POINT(handle) \31(PFN_vkGetInstanceProcAddr)dlsym(handle, "vkGetInstanceProcAddr");32#endif // __linux__3334#ifndef DEFAULT_VK_LIBRARY_PATH35#define DEFAULT_VK_LIBRARY_PATH ""36#define LOAD_VK_LIBRARY(path) nullptr37#define FREE_VK_LIBRARY(handle)38#define GET_VK_ENTRY_POINT(handle) nullptr39#endif4041namespace cv { namespace dnn { namespace vkcom {4243#ifdef HAVE_VULKAN44static VulkanHandle handle = nullptr;4546bool loadVulkanFunctions(VkInstance& instance)47{48#define VK_FUNC(fun) \49fun = (PFN_##fun)vkGetInstanceProcAddr(instance, #fun);5051#define VK_FUNC_MANDATORY(fun) \52VK_FUNC(fun) \53if(!fun) \54{ \55fprintf(stderr, "Could not load Vulkan function: %s !\n", #fun); \56return false; \57}5859#include "function_list.inl"60return true;61}6263bool loadVulkanGlobalFunctions()64{65#define VK_GLOBAL_LEVEL_FUNC(fun) \66fun = (PFN_##fun)vkGetInstanceProcAddr(nullptr, #fun);6768#define VK_GLOBAL_LEVEL_FUNC_MANDATORY(fun) \69VK_GLOBAL_LEVEL_FUNC(fun) \70if(!fun) \71{ \72fprintf(stderr, "Could not load global Vulkan function: %s !\n", #fun); \73return false; \74}7576#include "function_list.inl"77return true;78}7980bool loadVulkanEntry()81{82if (handle == nullptr)83return false;8485vkGetInstanceProcAddr = GET_VK_ENTRY_POINT(handle);86if (!vkGetInstanceProcAddr)87{88fprintf(stderr, "Could not load Vulkan entry function: vkGetInstanceProcAddr!\n");89return false;90}9192return true;93}9495bool loadVulkanLibrary()96{97if (handle != nullptr)98return true;99100const char* path;101const char* envPath = getenv("OPENCV_VULKAN_RUNTIME");102if (envPath)103{104path = envPath;105}106else107{108path = DEFAULT_VK_LIBRARY_PATH;109}110111handle = LOAD_VK_LIBRARY(path);112if( handle == nullptr )113{114fprintf(stderr, "Could not load Vulkan library: %s!\n", path);115return false;116}117118return true;119}120121#endif // HAVE_VULKAN122123}}} // namespace cv::dnn::vkcom124125126