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