Halide  19.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 VulkanCompilationCacheEntry;
28 
29 // --------------------------------------------------------------------------
30 
31 namespace { // internalize
32 
33 // --------------------------------------------------------------------------
34 // Memory
35 // --------------------------------------------------------------------------
36 void *vk_host_malloc(void *user_context, size_t size, size_t alignment, VkSystemAllocationScope scope, const VkAllocationCallbacks *callbacks = nullptr);
37 void vk_host_free(void *user_context, void *ptr, const VkAllocationCallbacks *callbacks = nullptr);
38 int vk_device_crop_from_offset(void *user_context, const struct halide_buffer_t *src, int64_t offset, struct halide_buffer_t *dst);
39 VulkanMemoryAllocator *vk_create_memory_allocator(void *user_context, VkDevice device, VkPhysicalDevice physical_device,
40  const VkAllocationCallbacks *alloc_callbacks);
41 
42 int vk_destroy_memory_allocator(void *user_context, VulkanMemoryAllocator *allocator);
43 int vk_clear_device_buffer(void *user_context,
44  VulkanMemoryAllocator *allocator,
45  VkCommandPool command_pool,
46  VkQueue command_queue,
47  VkBuffer device_buffer);
48 // --------------------------------------------------------------------------
49 // Context
50 // --------------------------------------------------------------------------
51 
52 int vk_create_context(
53  void *user_context,
54  VulkanMemoryAllocator **allocator,
55  VkInstance *instance,
56  VkDevice *device,
57  VkPhysicalDevice *physical_device,
58  VkCommandPool *command_pool,
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  VkCommandPool command_pool,
68  VkQueue queue);
69 
70 int vk_find_compute_capability(void *user_context, int *major, int *minor);
71 
72 int vk_create_instance(void *user_context, const StringTable &requested_layers, VkInstance *instance, const VkAllocationCallbacks *alloc_callbacks);
73 int vk_destroy_instance(void *user_context, VkInstance instance, const VkAllocationCallbacks *alloc_callbacks);
74 
75 int vk_select_device_for_context(void *user_context,
76  VkInstance *instance, VkDevice *device,
77  VkPhysicalDevice *physical_device,
78  uint32_t *queue_family_index);
79 
80 int vk_create_device(void *user_context, const StringTable &requested_layers, VkInstance *instance, VkDevice *device, VkQueue *queue,
81  VkPhysicalDevice *physical_device, uint32_t *queue_family_index, const VkAllocationCallbacks *alloc_callbacks);
82 
83 // --------------------------------------------------------------------------
84 // Extensions
85 // --------------------------------------------------------------------------
86 uint32_t vk_get_requested_layers(void *user_context, StringTable &layer_table);
87 uint32_t vk_get_required_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, StringTable &ext_table);
90 uint32_t vk_get_optional_device_extensions(void *user_context, StringTable &ext_table);
91 uint32_t vk_get_supported_device_extensions(void *user_context, VkPhysicalDevice physical_device, StringTable &ext_table);
92 bool 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
101 int vk_create_command_pool(void *user_context, VulkanMemoryAllocator *allocator, uint32_t queue_index, VkCommandPool *command_pool);
102 int vk_destroy_command_pool(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool);
103 
104 // Command pools are uint64_t and zero may be valid, so use this as a sentinel for invalid
105 const VkCommandPool VkInvalidCommandPool(uint64_t(-1));
106 
107 // -- Command Buffer
108 int vk_create_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool pool, VkCommandBuffer *command_buffer);
109 int vk_destroy_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool, VkCommandBuffer command_buffer);
110 
111 int vk_fill_command_buffer_with_dispatch_call(void *user_context,
112  VkDevice device,
113  VkCommandBuffer command_buffer,
114  VkPipeline compute_pipeline,
115  VkPipelineLayout pipeline_layout,
116  VkDescriptorSet descriptor_set,
117  uint32_t descriptor_set_index,
118  int blocksX, int blocksY, int blocksZ);
119 
120 int vk_submit_command_buffer(void *user_context, VkQueue queue, VkCommandBuffer command_buffer);
121 
122 // -- Scalar Uniform Buffer
123 bool vk_needs_scalar_uniform_buffer(void *user_context,
124  size_t arg_sizes[],
125  void *args[],
126  int8_t arg_is_buffer[]);
127 
128 size_t vk_estimate_scalar_uniform_buffer_size(void *user_context,
129  size_t arg_sizes[],
130  void *args[],
131  int8_t arg_is_buffer[]);
132 
133 MemoryRegion *vk_create_scalar_uniform_buffer(void *user_context,
134  VulkanMemoryAllocator *allocator,
135  size_t scalar_buffer_size);
136 
137 int vk_update_scalar_uniform_buffer(void *user_context,
138  VulkanMemoryAllocator *allocator,
139  MemoryRegion *region,
140  size_t arg_sizes[],
141  void *args[],
142  int8_t arg_is_buffer[]);
143 
144 int vk_destroy_scalar_uniform_buffer(void *user_context, VulkanMemoryAllocator *allocator,
145  MemoryRegion *scalar_args_region);
146 // -- Descriptor Pool
147 int vk_create_descriptor_pool(void *user_context,
148  VulkanMemoryAllocator *allocator,
149  uint32_t uniform_buffer_count,
150  uint32_t storage_buffer_count,
151  VkDescriptorPool *descriptor_pool);
152 
153 int vk_destroy_descriptor_pool(void *user_context,
154  VulkanMemoryAllocator *allocator,
155  VkDescriptorPool descriptor_pool);
156 
157 // -- Descriptor Set Layout
158 uint32_t vk_count_bindings_for_descriptor_set(void *user_context,
159  size_t arg_sizes[],
160  void *args[],
161  int8_t arg_is_buffer[]);
162 
163 int vk_create_descriptor_set_layout(void *user_context,
164  VulkanMemoryAllocator *allocator,
165  uint32_t uniform_buffer_count,
166  uint32_t storage_buffer_count,
167  VkDescriptorSetLayout *layout);
168 
169 int vk_destroy_descriptor_set_layout(void *user_context,
170  VulkanMemoryAllocator *allocator,
171  VkDescriptorSetLayout descriptor_set_layout);
172 
173 // -- Descriptor Set
174 int vk_create_descriptor_set(void *user_context,
175  VulkanMemoryAllocator *allocator,
176  VkDescriptorSetLayout descriptor_set_layout,
177  VkDescriptorPool descriptor_pool,
178  VkDescriptorSet *descriptor_set);
179 
180 int vk_update_descriptor_set(void *user_context,
181  VulkanMemoryAllocator *allocator,
182  VkBuffer *scalar_args_buffer,
183  size_t uniform_buffer_count,
184  size_t storage_buffer_count,
185  size_t arg_sizes[],
186  void *args[],
187  int8_t arg_is_buffer[],
188  VkDescriptorSet descriptor_set);
189 
190 // -- Pipeline Layout
191 int vk_create_pipeline_layout(void *user_context,
192  VulkanMemoryAllocator *allocator,
193  uint32_t descriptor_set_count,
194  VkDescriptorSetLayout *descriptor_set_layouts,
195  VkPipelineLayout *pipeline_layout);
196 
197 int vk_destroy_pipeline_layout(void *user_context,
198  VulkanMemoryAllocator *allocator,
199  VkPipelineLayout pipeline_layout);
200 // -- Compute Pipeline
201 int vk_create_compute_pipeline(void *user_context,
202  VulkanMemoryAllocator *allocator,
203  const char *pipeline_name,
204  VkShaderModule shader_module,
205  VkPipelineLayout pipeline_layout,
206  VkSpecializationInfo *specialization_info,
207  VkPipeline *compute_pipeline);
208 
209 int vk_setup_compute_pipeline(void *user_context,
210  VulkanMemoryAllocator *allocator,
211  VulkanShaderBinding *shader_bindings,
212  VkShaderModule shader_module,
213  VkPipelineLayout pipeline_layout,
214  VkPipeline *compute_pipeline);
215 
216 int vk_destroy_compute_pipeline(void *user_context,
217  VulkanMemoryAllocator *allocator,
218  VkPipeline compute_pipeline);
219 
220 // -- Shader Module
221 VulkanShaderBinding *vk_decode_shader_bindings(void *user_context, VulkanMemoryAllocator *allocator,
222  const uint32_t *module_ptr, uint32_t module_size);
223 
224 VulkanCompilationCacheEntry *vk_compile_shader_module(void *user_context, VulkanMemoryAllocator *allocator,
225  const char *src, int size);
226 
227 int vk_destroy_shader_modules(void *user_context, VulkanMemoryAllocator *allocator);
228 
229 // -- Copy Buffer
230 int vk_do_multidimensional_copy(void *user_context, VkCommandBuffer command_buffer,
231  const device_copy &c, uint64_t src_offset, uint64_t dst_offset,
232  int d, bool from_host, bool to_host);
233 
234 // --------------------------------------------------------------------------
235 // Errors
236 // --------------------------------------------------------------------------
237 
238 // Returns the corresponding string for a given vulkan error code
239 const char *vk_get_error_name(VkResult error) {
240  switch (error) {
241  case VK_SUCCESS:
242  return "VK_SUCCESS";
243  case VK_NOT_READY:
244  return "VK_NOT_READY";
245  case VK_TIMEOUT:
246  return "VK_TIMEOUT";
247  case VK_EVENT_SET:
248  return "VK_EVENT_SET";
249  case VK_EVENT_RESET:
250  return "VK_EVENT_RESET";
251  case VK_INCOMPLETE:
252  return "VK_INCOMPLETE";
254  return "VK_ERROR_OUT_OF_HOST_MEMORY";
256  return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
258  return "VK_ERROR_INITIALIZATION_FAILED";
260  return "VK_ERROR_DEVICE_LOST";
262  return "VK_ERROR_MEMORY_MAP_FAILED";
264  return "VK_ERROR_LAYER_NOT_PRESENT";
266  return "VK_ERROR_EXTENSION_NOT_PRESENT";
268  return "VK_ERROR_FEATURE_NOT_PRESENT";
270  return "VK_ERROR_INCOMPATIBLE_DRIVER";
272  return "VK_ERROR_TOO_MANY_OBJECTS";
274  return "VK_ERROR_FORMAT_NOT_SUPPORTED";
276  return "VK_ERROR_FRAGMENTED_POOL";
278  return "VK_ERROR_SURFACE_LOST_KHR";
280  return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
281  case VK_SUBOPTIMAL_KHR:
282  return "VK_SUBOPTIMAL_KHR";
284  return "VK_ERROR_OUT_OF_DATE_KHR";
286  return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
288  return "VK_ERROR_VALIDATION_FAILED_EXT";
290  return "VK_ERROR_INVALID_SHADER_NV";
292  return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
294  return "VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR";
295  default:
296  return "<Unknown Vulkan Result Code>";
297  }
298 }
299 
300 // --------------------------------------------------------------------------
301 
302 } // namespace
303 } // namespace Vulkan
304 } // namespace Internal
305 } // namespace Runtime
306 } // namespace Halide
307 
308 #endif // HALIDE_RUNTIME_VULKAN_INTERNAL_H
VkSystemAllocationScope
Definition: mini_vulkan.h:365
VkResult
Definition: mini_vulkan.h:138
@ VK_ERROR_VALIDATION_FAILED_EXT
Definition: mini_vulkan.h:162
@ VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
Definition: mini_vulkan.h:165
@ VK_ERROR_INVALID_SHADER_NV
Definition: mini_vulkan.h:163
@ VK_SUBOPTIMAL_KHR
Definition: mini_vulkan.h:159
@ VK_ERROR_EXTENSION_NOT_PRESENT
Definition: mini_vulkan.h:151
@ VK_INCOMPLETE
Definition: mini_vulkan.h:144
@ VK_ERROR_DEVICE_LOST
Definition: mini_vulkan.h:148
@ VK_SUCCESS
Definition: mini_vulkan.h:139
@ VK_EVENT_SET
Definition: mini_vulkan.h:142
@ VK_EVENT_RESET
Definition: mini_vulkan.h:143
@ VK_ERROR_OUT_OF_POOL_MEMORY_KHR
Definition: mini_vulkan.h:164
@ VK_ERROR_OUT_OF_HOST_MEMORY
Definition: mini_vulkan.h:145
@ VK_ERROR_INITIALIZATION_FAILED
Definition: mini_vulkan.h:147
@ VK_ERROR_INCOMPATIBLE_DISPLAY_KHR
Definition: mini_vulkan.h:161
@ VK_ERROR_OUT_OF_DEVICE_MEMORY
Definition: mini_vulkan.h:146
@ VK_ERROR_NATIVE_WINDOW_IN_USE_KHR
Definition: mini_vulkan.h:158
@ VK_ERROR_OUT_OF_DATE_KHR
Definition: mini_vulkan.h:160
@ VK_TIMEOUT
Definition: mini_vulkan.h:141
@ VK_ERROR_FORMAT_NOT_SUPPORTED
Definition: mini_vulkan.h:155
@ VK_ERROR_FRAGMENTED_POOL
Definition: mini_vulkan.h:156
@ VK_ERROR_SURFACE_LOST_KHR
Definition: mini_vulkan.h:157
@ VK_NOT_READY
Definition: mini_vulkan.h:140
@ VK_ERROR_FEATURE_NOT_PRESENT
Definition: mini_vulkan.h:152
@ VK_ERROR_TOO_MANY_OBJECTS
Definition: mini_vulkan.h:154
@ VK_ERROR_MEMORY_MAP_FAILED
Definition: mini_vulkan.h:149
@ VK_ERROR_LAYER_NOT_PRESENT
Definition: mini_vulkan.h:150
@ VK_ERROR_INCOMPATIBLE_DRIVER
Definition: mini_vulkan.h:153
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
VkDescriptorSetLayout * descriptor_set_layouts
VulkanShaderBinding * shader_bindings
VkPipelineLayout pipeline_layout
VkShaderModule shader_module
The raw representation of an image passed around by generated Halide code.