Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/vkcom/vulkan/vk_loader.cpp
16344 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "../../precomp.hpp"
9
#ifdef HAVE_VULKAN
10
#include <vulkan/vulkan.h>
11
#endif
12
#include "vk_functions.hpp"
13
14
#if defined(_WIN32)
15
#include <windows.h>
16
typedef HMODULE VulkanHandle;
17
#define DEFAULT_VK_LIBRARY_PATH "vulkan-1.dll"
18
#define LOAD_VK_LIBRARY(path) LoadLibrary(path)
19
#define FREE_VK_LIBRARY(handle) FreeLibrary(handle)
20
#define GET_VK_ENTRY_POINT(handle) \
21
(PFN_vkGetInstanceProcAddr)GetProcAddress(handle, "vkGetInstanceProcAddr");
22
#endif // _WIN32
23
24
#if defined(__linux__)
25
#include <dlfcn.h>
26
#include <stdio.h>
27
typedef void* VulkanHandle;
28
#define DEFAULT_VK_LIBRARY_PATH "libvulkan.so.1"
29
#define LOAD_VK_LIBRARY(path) dlopen(path, RTLD_LAZY | RTLD_GLOBAL)
30
#define FREE_VK_LIBRARY(handle) dlclose(handle)
31
#define GET_VK_ENTRY_POINT(handle) \
32
(PFN_vkGetInstanceProcAddr)dlsym(handle, "vkGetInstanceProcAddr");
33
#endif // __linux__
34
35
#ifndef DEFAULT_VK_LIBRARY_PATH
36
#define DEFAULT_VK_LIBRARY_PATH ""
37
#define LOAD_VK_LIBRARY(path) nullptr
38
#define FREE_VK_LIBRARY(handle)
39
#define GET_VK_ENTRY_POINT(handle) nullptr
40
#endif
41
42
namespace cv { namespace dnn { namespace vkcom {
43
44
#ifdef HAVE_VULKAN
45
static VulkanHandle handle = nullptr;
46
47
bool loadVulkanFunctions(VkInstance& instance)
48
{
49
#define VK_FUNC(fun) \
50
fun = (PFN_##fun)vkGetInstanceProcAddr(instance, #fun);
51
52
#define VK_FUNC_MANDATORY(fun) \
53
VK_FUNC(fun) \
54
if(!fun) \
55
{ \
56
fprintf(stderr, "Could not load Vulkan function: %s !\n", #fun); \
57
return false; \
58
}
59
60
#include "function_list.inl"
61
return true;
62
}
63
64
bool loadVulkanGlobalFunctions()
65
{
66
#define VK_GLOBAL_LEVEL_FUNC(fun) \
67
fun = (PFN_##fun)vkGetInstanceProcAddr(nullptr, #fun);
68
69
#define VK_GLOBAL_LEVEL_FUNC_MANDATORY(fun) \
70
VK_GLOBAL_LEVEL_FUNC(fun) \
71
if(!fun) \
72
{ \
73
fprintf(stderr, "Could not load global Vulkan function: %s !\n", #fun); \
74
return false; \
75
}
76
77
#include "function_list.inl"
78
return true;
79
}
80
81
bool loadVulkanEntry()
82
{
83
if (handle == nullptr)
84
return false;
85
86
vkGetInstanceProcAddr = GET_VK_ENTRY_POINT(handle);
87
if (!vkGetInstanceProcAddr)
88
{
89
fprintf(stderr, "Could not load Vulkan entry function: vkGetInstanceProcAddr!\n");
90
return false;
91
}
92
93
return true;
94
}
95
96
bool loadVulkanLibrary()
97
{
98
if (handle != nullptr)
99
return true;
100
101
const char* path;
102
const char* envPath = getenv("OPENCV_VULKAN_RUNTIME");
103
if (envPath)
104
{
105
path = envPath;
106
}
107
else
108
{
109
path = DEFAULT_VK_LIBRARY_PATH;
110
}
111
112
handle = LOAD_VK_LIBRARY(path);
113
if( handle == nullptr )
114
{
115
fprintf(stderr, "Could not load Vulkan library: %s!\n", path);
116
return false;
117
}
118
119
return true;
120
}
121
122
#endif // HAVE_VULKAN
123
124
}}} // namespace cv::dnn::vkcom
125
126