Halide 22.0.0 Halide compiler and libraries

Loading...
Searching...
No Matches
vulkan_interface.h
Go to the documentation of this file.
1#ifndef HALIDE_RUNTIME_VULKAN_INTERFACE_H
2#define HALIDE_RUNTIME_VULKAN_INTERFACE_H
3
4#include "runtime_internal.h"
5
6// --------------------------------------------------------------------------
7// Vulkan Specific Definitions
8// --------------------------------------------------------------------------
9
10// Environment variable string delimiter
11#ifdef WINDOWS
12#define HL_VK_ENV_DELIM ";"
13#else
14#define HL_VK_ENV_DELIM ":"
15#endif
16
17// Prototypes for the subset of the Vulkan API we need
18#define VK_NO_PROTOTYPES
19// NOLINTNEXTLINE
20#include <vulkan/vulkan.h>
21
22// --------------------------------------------------------------------------
23// Vulkan API Definition
24// --------------------------------------------------------------------------
25
26 namespace Halide {
27 namespace Runtime {
28 namespace Internal {
29 namespace Vulkan {
30
31// --------------------------------------------------------------------------
32
33// Halide device interface struct for runtime specific function table
34 extern WEAK halide_device_interface_t vulkan_device_interface;
35
36// --------------------------------------------------------------------------
37
38// The default implementation of halide_vulkan_get_symbol attempts to load
39// the Vulkan loader shared library/DLL, and then get the symbol from it.
40 WEAK void *lib_vulkan = nullptr;
41
42 extern "C" WEAK void *halide_vulkan_get_symbol(void *user_context, const char *name) {
43 // Only try to load the library if the library isn't already
44 // loaded, or we can't load the symbol from the process already.
45 void *symbol = halide_get_library_symbol(lib_vulkan, name);
46 if (symbol) {
47 return symbol;
48 }
49
50 const char *lib_names[] = {
51#ifdef WINDOWS
52 "vulkan-1.dll",
53#else
54 "libvulkan.so.1",
55 "libvulkan.1.dylib",
56#endif
57 };
58 for (auto &lib_name : lib_names) {
59 lib_vulkan = halide_load_library(lib_name);
60 if (lib_vulkan) {
61 debug(user_context) << " Loaded Vulkan loader library: " << lib_name << "\n";
62 break;
63 } else {
64 debug(user_context) << " Missing Vulkan loader library: " << lib_name << "\n";
65 }
66 }
67
68 return halide_get_library_symbol(lib_vulkan, name);
69}
70
71// Declare all the function pointers for the Vulkan API methods that will be resolved dynamically
72
73#define VULKAN_FN(fn) WEAK PFN_##fn fn = nullptr;
74#define HL_USE_VULKAN_LOADER_FNS
75#define HL_USE_VULKAN_INSTANCE_FNS
76#define HL_USE_VULKAN_DEVICE_FNS
77#include "vulkan_functions.h"
78#undef HL_USE_VULKAN_DEVICE_FNS
79#undef HL_USE_VULKAN_INSTANCE_FNS
80#undef HL_USE_VULKAN_LOADER_FNS
81#undef VULKAN_FN
82
83// Get the function pointers to the Vulkan loader (to find all available instances)
84 void WEAK vk_load_vulkan_loader_functions(void *user_context) {
85 debug(user_context) << " vk_load_vulkan_loader_functions (user_context: " << user_context << ")\n";
86#define VULKAN_FN(fn) fn = (PFN_##fn)halide_vulkan_get_symbol(user_context, #fn);
87#define HL_USE_VULKAN_LOADER_FNS
88#include "vulkan_functions.h"
89#undef HL_USE_VULKAN_LOADER_FNS
90#undef VULKAN_FN
91}
92
93// Get the function pointers from the Vulkan loader for the resolved instance API methods.
94 void WEAK vk_load_vulkan_instance_functions(void *user_context, VkInstance instance) {
95 debug(user_context) << " vk_load_vulkan_instance_functions (user_context: " << user_context << ")\n";
96#define VULKAN_FN(fn) fn = (PFN_##fn)vkGetInstanceProcAddr(instance, #fn);
97#define HL_USE_VULKAN_INSTANCE_FNS
98#include "vulkan_functions.h"
99#undef HL_USE_VULKAN_INSTANCE_FNS
100#undef VULKAN_FN
101}
102
103// Reset the instance function pointers
104 void WEAK vk_unload_vulkan_instance_functions(void *user_context) {
105#define VULKAN_FN(fn) fn = (PFN_##fn)(nullptr);
106#define HL_USE_VULKAN_INSTANCE_FNS
107#include "vulkan_functions.h"
108#undef HL_USE_VULKAN_INSTANCE_FNS
109#undef VULKAN_FN
110}
111
112// Get the function pointers from the Vulkan instance for the resolved driver API methods.
113 void WEAK vk_load_vulkan_device_functions(void *user_context, VkDevice device) {
114 debug(user_context) << " vk_load_vulkan_device_functions (user_context: " << user_context << ")\n";
115#define VULKAN_FN(fn) fn = (PFN_##fn)vkGetDeviceProcAddr(device, #fn);
116#define HL_USE_VULKAN_DEVICE_FNS
117#include "vulkan_functions.h"
118#undef HL_USE_VULKAN_DEVICE_FNS
119#undef VULKAN_FN
120}
121
122// Reset the device function pointers
123 void WEAK vk_unload_vulkan_device_functions(void *user_context) {
124#define VULKAN_FN(fn) fn = (PFN_##fn)(nullptr);
125#define HL_USE_VULKAN_DEVICE_FNS
126#include "vulkan_functions.h"
127#undef HL_USE_VULKAN_DEVICE_FNS
128#undef VULKAN_FN
129}
130
131// --------------------------------------------------------------------------
132
133} // namespace Vulkan
134} // namespace Internal
135} // namespace Runtime
136} // namespace Halide
137
138#endif // HALIDE_RUNTIME_VULKAN_INTERFACE_H
debug
#define debug(n)
For optional debugging during codegen, use the debug macro as follows:
Definition Debug.h:52
halide_get_library_symbol
void * halide_get_library_symbol(void *lib, const char *name)
halide_load_library
void * halide_load_library(const char *name)
Halide::Runtime::Internal::Vulkan::halide_vulkan_get_symbol
WEAK void * halide_vulkan_get_symbol(void *user_context, const char *name)
Definition vulkan_interface.h:42
Halide::Runtime::Internal::Vulkan::vk_unload_vulkan_device_functions
void WEAK vk_unload_vulkan_device_functions(void *user_context)
Definition vulkan_interface.h:123
Halide::Runtime::Internal::Vulkan::vk_unload_vulkan_instance_functions
void WEAK vk_unload_vulkan_instance_functions(void *user_context)
Definition vulkan_interface.h:104
Halide::Runtime::Internal::Vulkan::vk_load_vulkan_loader_functions
void WEAK vk_load_vulkan_loader_functions(void *user_context)
Definition vulkan_interface.h:84
Halide::Runtime::Internal::Vulkan::vk_load_vulkan_device_functions
void WEAK vk_load_vulkan_device_functions(void *user_context, VkDevice device)
Definition vulkan_interface.h:113
Halide::Runtime::Internal::Vulkan::vk_load_vulkan_instance_functions
void WEAK vk_load_vulkan_instance_functions(void *user_context, VkInstance instance)
Definition vulkan_interface.h:94
Halide::Runtime::Internal::Vulkan::lib_vulkan
WEAK void * lib_vulkan
Definition vulkan_interface.h:40
Halide::Runtime::Internal::Vulkan::vulkan_device_interface
WEAK halide_device_interface_t vulkan_device_interface
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::cast
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
runtime_internal.h
WEAK
#define WEAK
Definition runtime_internal.h:52
halide_device_interface_t
Each GPU API provides a halide_device_interface_t struct pointing to the code that manages device all...
Definition HalideRuntime.h:797
vulkan_functions.h
user_context
void * user_context
Definition vulkan_resources.h:178