Halide 22.0.0 Halide compiler and libraries

vulkan_internal.h
Go to the documentation of this file.
1#ifndef HALIDE_RUNTIME_VULKAN_INTERNAL_H
2#define HALIDE_RUNTIME_VULKAN_INTERNAL_H
3
4#include "gpu_context_common.h"
5#include "printer.h"
6#include "runtime_internal.h"
7#include "scoped_spin_lock.h"
8
9#include "internal/block_storage.h"
10#include "internal/linked_list.h"
11#include "internal/memory_arena.h"
12#include "internal/string_storage.h"
13#include "internal/string_table.h"
14
15#include "vulkan_interface.h"
16
17// --
18
19namespace Halide {
20namespace Runtime {
21namespace Internal {
22namespace Vulkan {
23
24// Declarations
25class VulkanMemoryAllocator;
26struct VulkanShaderBinding;
27struct VulkanCompiledShaderModule;
28struct VulkanCompilationCacheEntry;
29
30// --------------------------------------------------------------------------
31
32namespace { // internalize
33
34// --------------------------------------------------------------------------
35// Memory
36// --------------------------------------------------------------------------
37void *vk_host_malloc(void *user_context, size_t size, size_t alignment, VkSystemAllocationScope scope, const VkAllocationCallbacks *callbacks = nullptr);
38void vk_host_free(void *user_context, void *ptr, const VkAllocationCallbacks *callbacks = nullptr);
39int vk_device_crop_from_offset(void *user_context, const struct halide_buffer_t *src, int64_t offset, struct halide_buffer_t *dst);
40VulkanMemoryAllocator *vk_create_memory_allocator(void *user_context, VkDevice device, VkPhysicalDevice physical_device,
41 const VkAllocationCallbacks *alloc_callbacks);
42
43int vk_destroy_memory_allocator(void *user_context, VulkanMemoryAllocator *allocator);
44int vk_clear_device_buffer(void *user_context,
45 VulkanMemoryAllocator *allocator,
46 VkCommandBuffer command_buffer,
47 VkQueue command_queue,
48 VkBuffer device_buffer);
49// --------------------------------------------------------------------------
50// Context
51// --------------------------------------------------------------------------
52
53int vk_create_context(
54 void *user_context,
55 VulkanMemoryAllocator **allocator,
56 VkInstance *instance,
57 VkDevice *device,
58 VkPhysicalDevice *physical_device,
59 VkQueue *queue, uint32_t *queue_family_index);
60
61int vk_destroy_context(
62 void *user_context,
63 VulkanMemoryAllocator *allocator,
64 VkInstance instance,
65 VkDevice device,
66 VkPhysicalDevice physical_device,
67 VkQueue queue);
68
69int vk_find_compute_capability(void *user_context, int *major, int *minor);
70
71int vk_create_instance(void *user_context, const StringTable &requested_layers, VkInstance *instance, const VkAllocationCallbacks *alloc_callbacks);
72int vk_destroy_instance(void *user_context, VkInstance instance, const VkAllocationCallbacks *alloc_callbacks);
73
74int vk_select_device_for_context(void *user_context,
75 VkInstance *instance, VkDevice *device,
76 VkPhysicalDevice *physical_device,
77 uint32_t *queue_family_index);
78
79int vk_create_device(void *user_context, const StringTable &requested_layers, VkInstance *instance, VkDevice *device, VkQueue *queue,
80 VkPhysicalDevice *physical_device, uint32_t *queue_family_index, const VkAllocationCallbacks *alloc_callbacks);
81
82// --------------------------------------------------------------------------
83// Extensions
84// --------------------------------------------------------------------------
85 uint32_t vk_get_requested_layers(void *user_context, StringTable &layer_table);
86 uint32_t vk_get_required_instance_extensions(void *user_context, StringTable &ext_table);
87 uint32_t vk_get_optional_instance_extensions(void *user_context, StringTable &ext_table);
88 uint32_t vk_get_supported_instance_extensions(void *user_context, StringTable &ext_table);
89 uint32_t vk_get_required_device_extensions(void *user_context, uint32_t major_version, uint32_t minor_version, StringTable &ext_table);
90 uint32_t vk_get_optional_device_extensions(void *user_context, uint32_t major_version, uint32_t minor_version, StringTable &ext_table);
91 uint32_t vk_get_supported_device_extensions(void *user_context, VkPhysicalDevice physical_device, StringTable &ext_table);
92bool vk_validate_required_extension_support(void *user_context,
93 const StringTable &required_extensions,
94 const StringTable &supported_extensions);
95
96// --------------------------------------------------------------------------
97// Resources
98// --------------------------------------------------------------------------
99
100// -- Command Pool
101int vk_create_command_pool(void *user_context, VulkanMemoryAllocator *allocator, uint32_t queue_index, VkCommandPool *command_pool);
102int vk_destroy_command_pool(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool);
103
104// -- Command Buffer
105int vk_create_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool pool, VkCommandBuffer *command_buffer);
106int vk_destroy_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool, VkCommandBuffer command_buffer);
107
108struct ScopedVulkanCommandBufferAndPool;
109
110int vk_fill_command_buffer_with_dispatch_call(void *user_context,
111 VkDevice device,
112 VkCommandBuffer command_buffer,
113 VkPipeline compute_pipeline,
114 VkPipelineLayout pipeline_layout,
115 VkDescriptorSet descriptor_set,
116 uint32_t descriptor_set_index,
117 int blocksX, int blocksY, int blocksZ);
118
119int vk_submit_command_buffer(void *user_context, VkQueue queue, VkCommandBuffer command_buffer);
120
121// -- Scalar Uniform Buffer
122bool vk_needs_scalar_uniform_buffer(void *user_context,
123 size_t arg_sizes[],
124 void *args[],
125 int8_t arg_is_buffer[]);
126
127size_t vk_estimate_scalar_uniform_buffer_size(void *user_context,
128 size_t arg_sizes[],
129 void *args[],
130 int8_t arg_is_buffer[]);
131
132 MemoryRegion *vk_create_scalar_uniform_buffer(void *user_context,
133 VulkanMemoryAllocator *allocator,
134 size_t scalar_buffer_size);
135
136int vk_update_scalar_uniform_buffer(void *user_context,
137 VulkanMemoryAllocator *allocator,
138 MemoryRegion *region,
139 size_t arg_sizes[],
140 void *args[],
141 int8_t arg_is_buffer[]);
142
143int vk_destroy_scalar_uniform_buffer(void *user_context, VulkanMemoryAllocator *allocator,
144 MemoryRegion *scalar_args_region);
145// -- Descriptor Pool
146int vk_create_descriptor_pool(void *user_context,
147 VulkanMemoryAllocator *allocator,
148 uint32_t uniform_buffer_count,
149 uint32_t storage_buffer_count,
150 VkDescriptorPool *descriptor_pool);
151
152int vk_destroy_descriptor_pool(void *user_context,
153 VulkanMemoryAllocator *allocator,
154 VkDescriptorPool descriptor_pool);
155
156// -- Descriptor Set Layout
157 uint32_t vk_count_bindings_for_descriptor_set(void *user_context,
158 size_t arg_sizes[],
159 void *args[],
160 int8_t arg_is_buffer[]);
161
162int vk_create_descriptor_set_layout(void *user_context,
163 VulkanMemoryAllocator *allocator,
164 uint32_t uniform_buffer_count,
165 uint32_t storage_buffer_count,
166 VkDescriptorSetLayout *layout);
167
168int vk_destroy_descriptor_set_layout(void *user_context,
169 VulkanMemoryAllocator *allocator,
170 VkDescriptorSetLayout descriptor_set_layout);
171
172// -- Descriptor Set
173int vk_create_descriptor_set(void *user_context,
174 VulkanMemoryAllocator *allocator,
175 VkDescriptorSetLayout descriptor_set_layout,
176 VkDescriptorPool descriptor_pool,
177 VkDescriptorSet *descriptor_set);
178
179int vk_update_descriptor_set(void *user_context,
180 VulkanMemoryAllocator *allocator,
181 VkBuffer *scalar_args_buffer,
182 size_t uniform_buffer_count,
183 size_t storage_buffer_count,
184 size_t arg_sizes[],
185 void *args[],
186 int8_t arg_is_buffer[],
187 VkDescriptorSet descriptor_set);
188
189// -- Pipeline Layout
190int vk_create_pipeline_layout(void *user_context,
191 VulkanMemoryAllocator *allocator,
192 uint32_t descriptor_set_count,
193 VkDescriptorSetLayout *descriptor_set_layouts,
194 VkPipelineLayout *pipeline_layout);
195
196int vk_destroy_pipeline_layout(void *user_context,
197 VulkanMemoryAllocator *allocator,
198 VkPipelineLayout pipeline_layout);
199// -- Compute Pipeline
200int vk_create_compute_pipeline(void *user_context,
201 VulkanMemoryAllocator *allocator,
202 const char *pipeline_name,
203 VkShaderModule shader_module,
204 VkPipelineLayout pipeline_layout,
205 VkSpecializationInfo *specialization_info,
206 VkPipeline *compute_pipeline);
207
208int vk_setup_compute_pipeline(void *user_context,
209 VulkanMemoryAllocator *allocator,
210 VulkanShaderBinding *shader_bindings,
211 VkShaderModule shader_module,
212 VkPipelineLayout pipeline_layout,
213 VkPipeline *compute_pipeline);
214
215int vk_destroy_compute_pipeline(void *user_context,
216 VulkanMemoryAllocator *allocator,
217 VkPipeline compute_pipeline);
218
219// -- Kernel Module
220VulkanCompilationCacheEntry *vk_compile_kernel_module(void *user_context, VulkanMemoryAllocator *allocator,
221 const char *ptr, int size);
222
223// -- Shader Module
224VulkanShaderBinding *vk_decode_shader_bindings(void *user_context, VulkanMemoryAllocator *allocator,
225 const uint32_t *module_ptr, uint32_t module_size);
226
227VulkanCompiledShaderModule *vk_compile_shader_module(void *user_context, VulkanMemoryAllocator *allocator,
228 const char *src, int size);
229
230int vk_destroy_shader_modules(void *user_context, VulkanMemoryAllocator *allocator);
231
232// -- Copy Buffer
233int vk_do_multidimensional_copy(void *user_context, VkCommandBuffer command_buffer,
234 const device_copy &c, uint64_t src_offset, uint64_t dst_offset,
235 int d, bool from_host, bool to_host);
236
237// --------------------------------------------------------------------------
238// Debug & Errors
239// --------------------------------------------------------------------------
240
241VkResult vk_create_debug_utils_messenger(void *user_context, VkInstance instance, VulkanMemoryAllocator *allocator, VkDebugUtilsMessengerEXT *messenger);
242void vk_destroy_debug_utils_messenger(void *user_context, VkInstance instance, VulkanMemoryAllocator *allocator, VkDebugUtilsMessengerEXT messenger);
243
244// Returns the corresponding string for a given vulkan error code
245const char *vk_get_error_name(VkResult error) {
246 switch (error) {
247 case VK_SUCCESS:
248 return "VK_SUCCESS";
249 case VK_NOT_READY:
250 return "VK_NOT_READY";
251 case VK_TIMEOUT:
252 return "VK_TIMEOUT";
253 case VK_EVENT_SET:
254 return "VK_EVENT_SET";
255 case VK_EVENT_RESET:
256 return "VK_EVENT_RESET";
257 case VK_INCOMPLETE:
258 return "VK_INCOMPLETE";
259 case VK_ERROR_OUT_OF_HOST_MEMORY:
260 return "VK_ERROR_OUT_OF_HOST_MEMORY";
261 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
262 return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
263 case VK_ERROR_INITIALIZATION_FAILED:
264 return "VK_ERROR_INITIALIZATION_FAILED";
265 case VK_ERROR_DEVICE_LOST:
266 return "VK_ERROR_DEVICE_LOST";
267 case VK_ERROR_MEMORY_MAP_FAILED:
268 return "VK_ERROR_MEMORY_MAP_FAILED";
269 case VK_ERROR_LAYER_NOT_PRESENT:
270 return "VK_ERROR_LAYER_NOT_PRESENT";
271 case VK_ERROR_EXTENSION_NOT_PRESENT:
272 return "VK_ERROR_EXTENSION_NOT_PRESENT";
273 case VK_ERROR_FEATURE_NOT_PRESENT:
274 return "VK_ERROR_FEATURE_NOT_PRESENT";
275 case VK_ERROR_INCOMPATIBLE_DRIVER:
276 return "VK_ERROR_INCOMPATIBLE_DRIVER";
277 case VK_ERROR_TOO_MANY_OBJECTS:
278 return "VK_ERROR_TOO_MANY_OBJECTS";
279 case VK_ERROR_FORMAT_NOT_SUPPORTED:
280 return "VK_ERROR_FORMAT_NOT_SUPPORTED";
281 case VK_ERROR_FRAGMENTED_POOL:
282 return "VK_ERROR_FRAGMENTED_POOL";
283 case VK_ERROR_SURFACE_LOST_KHR:
284 return "VK_ERROR_SURFACE_LOST_KHR";
285 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
286 return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
287 case VK_SUBOPTIMAL_KHR:
288 return "VK_SUBOPTIMAL_KHR";
289 case VK_ERROR_OUT_OF_DATE_KHR:
290 return "VK_ERROR_OUT_OF_DATE_KHR";
291 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
292 return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
293 case VK_ERROR_VALIDATION_FAILED_EXT:
294 return "VK_ERROR_VALIDATION_FAILED_EXT";
295 case VK_ERROR_INVALID_SHADER_NV:
296 return "VK_ERROR_INVALID_SHADER_NV";
297 case VK_ERROR_OUT_OF_POOL_MEMORY_KHR:
298 return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
299 case VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR:
300 return "VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR";
301 default:
302 return "<Unknown Vulkan Result Code>";
303 }
304}
305
306// --------------------------------------------------------------------------
307
308} // namespace
309} // namespace Vulkan
310} // namespace Internal
311} // namespace Runtime
312} // namespace Halide
313
314#endif // HALIDE_RUNTIME_VULKAN_INTERNAL_H
block_storage.h
Halide::Runtime::Internal::StringTable
Definition: string_table.h:15
gpu_context_common.h
linked_list.h
memory_arena.h
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::DeviceAPI::Vulkan
@ Vulkan
printer.h
runtime_internal.h
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:23
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:22
uint32_t
unsigned __INT32_TYPE__ uint32_t
Definition: runtime_internal.h:25
int8_t
signed __INT8_TYPE__ int8_t
Definition: runtime_internal.h:28
scoped_spin_lock.h
string_storage.h
string_table.h
Halide::Runtime::Internal::MemoryRegion
Definition: memory_resources.h:79
Halide::Runtime::Internal::Vulkan::VulkanCompilationCacheEntry::allocator
VulkanMemoryAllocator * allocator
Definition: vulkan_resources.h:70
Halide::Runtime::Internal::device_copy
Definition: device_buffer_utils.h:33
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition: HalideRuntime.h:1569
vulkan_interface.h
command_pool
VkCommandPool command_pool
Definition: vulkan_resources.h:180
user_context
void * user_context
Definition: vulkan_resources.h:178
command_buffer
VkCommandBuffer command_buffer
Definition: vulkan_resources.h:181