Skip to content

Commit 396421a

Browse files
committed
Vulkan: add context callback setters
Weak symbol overrides are not reliable in every embedding environment. Add setter functions for the Vulkan acquire/release context callbacks, while keeping the existing weak symbols as the public runtime entry points. Passing null restores Halide's default context management.
1 parent f2afbd8 commit 396421a

3 files changed

Lines changed: 81 additions & 21 deletions

File tree

src/runtime/HalideRuntimeVulkan.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ extern int halide_vulkan_release_context(void *user_context,
106106
VkQueue queue,
107107
VkDebugUtilsMessengerEXT messenger);
108108

109+
typedef int (*halide_vulkan_acquire_context_t)(void *user_context,
110+
struct halide_vulkan_memory_allocator **allocator,
111+
VkInstance *instance,
112+
VkDevice *device,
113+
VkPhysicalDevice *physical_device,
114+
VkQueue *queue,
115+
uint32_t *queue_family_index,
116+
VkDebugUtilsMessengerEXT *messenger,
117+
bool create);
118+
typedef int (*halide_vulkan_release_context_t)(void *user_context,
119+
VkInstance instance,
120+
VkDevice device,
121+
VkQueue queue,
122+
VkDebugUtilsMessengerEXT messenger);
123+
124+
/** Override the Vulkan context acquisition callback. Returns the previous
125+
* handler. If unset, Halide uses its built-in Vulkan context management.
126+
*/
127+
extern halide_vulkan_acquire_context_t halide_set_vulkan_acquire_context(halide_vulkan_acquire_context_t handler);
128+
129+
/** Override the Vulkan context release callback. Returns the previous handler. */
130+
extern halide_vulkan_release_context_t halide_set_vulkan_release_context(halide_vulkan_release_context_t handler);
131+
109132
/** Ensure a Halide Vulkan memory allocator exists for an externally-managed
110133
* Vulkan context. Intended for embedders that override
111134
* halide_vulkan_acquire_context()/halide_vulkan_release_context().

src/runtime/runtime_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ extern "C" __attribute__((used)) void *halide_runtime_api_functions[] = {
219219
(void *)&halide_vulkan_release_memory_allocator,
220220
(void *)&halide_vulkan_release_context,
221221
(void *)&halide_vulkan_run,
222+
(void *)&halide_set_vulkan_acquire_context,
223+
(void *)&halide_set_vulkan_release_context,
222224
(void *)&halide_webgpu_device_interface,
223225
(void *)&halide_webgpu_initialize_kernels,
224226
(void *)&halide_webgpu_finalize_kernels,

src/runtime/vulkan.cpp

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ ALWAYS_INLINE int vk_load_external_context_functions(void *user_context, VkInsta
4242
return halide_error_code_success;
4343
}
4444

45-
} // namespace Vulkan
46-
} // namespace Internal
47-
} // namespace Runtime
48-
} // namespace Halide
49-
50-
// --------------------------------------------------------------------------
51-
52-
extern "C" {
53-
54-
// --------------------------------------------------------------------------
55-
5645
// The default implementation of halide_acquire_vulkan_context uses
5746
// the global pointers above, and serializes access with a spin lock.
5847
// Overriding implementations of acquire/release must implement the
@@ -65,15 +54,15 @@ extern "C" {
6554
// call to halide_release_vulkan_context. halide_acquire_vulkan_context
6655
// should block while a previous call (if any) has not yet been
6756
// released via halide_release_vulkan_context.
68-
WEAK int halide_vulkan_acquire_context(void *user_context,
69-
halide_vulkan_memory_allocator **allocator,
70-
VkInstance *instance,
71-
VkDevice *device,
72-
VkPhysicalDevice *physical_device,
73-
VkQueue *queue,
74-
uint32_t *queue_family_index,
75-
VkDebugUtilsMessengerEXT *messenger,
76-
bool create) {
57+
WEAK int default_vulkan_acquire_context(void *user_context,
58+
halide_vulkan_memory_allocator **allocator,
59+
VkInstance *instance,
60+
VkDevice *device,
61+
VkPhysicalDevice *physical_device,
62+
VkQueue *queue,
63+
uint32_t *queue_family_index,
64+
VkDebugUtilsMessengerEXT *messenger,
65+
bool create) {
7766
#ifdef DEBUG_RUNTIME
7867
halide_start_clock(user_context);
7968
#endif
@@ -110,11 +99,57 @@ WEAK int halide_vulkan_acquire_context(void *user_context,
11099
return halide_error_code_success;
111100
}
112101

113-
WEAK int halide_vulkan_release_context(void *user_context, VkInstance instance, VkDevice device, VkQueue queue, VkDebugUtilsMessengerEXT messenger) {
102+
WEAK int default_vulkan_release_context(void *user_context, VkInstance instance, VkDevice device, VkQueue queue, VkDebugUtilsMessengerEXT messenger) {
114103
halide_mutex_unlock(&thread_lock);
115104
return halide_error_code_success;
116105
}
117106

107+
WEAK halide_vulkan_acquire_context_t vulkan_acquire_context_handler =
108+
default_vulkan_acquire_context;
109+
WEAK halide_vulkan_release_context_t vulkan_release_context_handler =
110+
default_vulkan_release_context;
111+
112+
} // namespace Vulkan
113+
} // namespace Internal
114+
} // namespace Runtime
115+
} // namespace Halide
116+
117+
// --------------------------------------------------------------------------
118+
119+
extern "C" {
120+
121+
// --------------------------------------------------------------------------
122+
123+
WEAK int halide_vulkan_acquire_context(void *user_context,
124+
halide_vulkan_memory_allocator **allocator,
125+
VkInstance *instance,
126+
VkDevice *device,
127+
VkPhysicalDevice *physical_device,
128+
VkQueue *queue,
129+
uint32_t *queue_family_index,
130+
VkDebugUtilsMessengerEXT *messenger,
131+
bool create) {
132+
return vulkan_acquire_context_handler(user_context, allocator, instance, device,
133+
physical_device, queue, queue_family_index,
134+
messenger, create);
135+
}
136+
137+
WEAK int halide_vulkan_release_context(void *user_context, VkInstance instance, VkDevice device, VkQueue queue, VkDebugUtilsMessengerEXT messenger) {
138+
return vulkan_release_context_handler(user_context, instance, device, queue, messenger);
139+
}
140+
141+
WEAK halide_vulkan_acquire_context_t halide_set_vulkan_acquire_context(halide_vulkan_acquire_context_t handler) {
142+
halide_vulkan_acquire_context_t result = vulkan_acquire_context_handler;
143+
vulkan_acquire_context_handler = handler ? handler : default_vulkan_acquire_context;
144+
return result;
145+
}
146+
147+
WEAK halide_vulkan_release_context_t halide_set_vulkan_release_context(halide_vulkan_release_context_t handler) {
148+
halide_vulkan_release_context_t result = vulkan_release_context_handler;
149+
vulkan_release_context_handler = handler ? handler : default_vulkan_release_context;
150+
return result;
151+
}
152+
118153
WEAK int halide_vulkan_acquire_memory_allocator(void *user_context,
119154
halide_vulkan_memory_allocator **allocator,
120155
VkInstance instance,

0 commit comments

Comments
 (0)