Halide
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_find_compute_capability(void *user_context, int *major, int *minor);
62 
63 int vk_create_instance(void *user_context, const StringTable &requested_layers, VkInstance *instance, const VkAllocationCallbacks *alloc_callbacks);
64 int vk_destroy_instance(void *user_context, VkInstance instance, const VkAllocationCallbacks *alloc_callbacks);
65 
66 int vk_select_device_for_context(void *user_context,
67  VkInstance *instance, VkDevice *device,
68  VkPhysicalDevice *physical_device,
69  uint32_t *queue_family_index);
70 
71 int vk_create_device(void *user_context, const StringTable &requested_layers, VkInstance *instance, VkDevice *device, VkQueue *queue,
72  VkPhysicalDevice *physical_device, uint32_t *queue_family_index, const VkAllocationCallbacks *alloc_callbacks);
73 
74 // --------------------------------------------------------------------------
75 // Extensions
76 // --------------------------------------------------------------------------
77 uint32_t vk_get_requested_layers(void *user_context, StringTable &layer_table);
78 uint32_t vk_get_required_instance_extensions(void *user_context, StringTable &ext_table);
79 uint32_t vk_get_supported_instance_extensions(void *user_context, StringTable &ext_table);
80 uint32_t vk_get_required_device_extensions(void *user_context, StringTable &ext_table);
81 uint32_t vk_get_optional_device_extensions(void *user_context, StringTable &ext_table);
82 uint32_t vk_get_supported_device_extensions(void *user_context, VkPhysicalDevice physical_device, StringTable &ext_table);
83 bool vk_validate_required_extension_support(void *user_context,
84  const StringTable &required_extensions,
85  const StringTable &supported_extensions);
86 
87 // --------------------------------------------------------------------------
88 // Resources
89 // --------------------------------------------------------------------------
90 
91 // -- Command Pool
92 int vk_create_command_pool(void *user_context, VulkanMemoryAllocator *allocator, uint32_t queue_index, VkCommandPool *command_pool);
93 int vk_destroy_command_pool(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool);
94 
95 // -- Command Buffer
96 int vk_create_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool pool, VkCommandBuffer *command_buffer);
97 int vk_destroy_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool, VkCommandBuffer command_buffer);
98 
99 int vk_fill_command_buffer_with_dispatch_call(void *user_context,
100  VkDevice device,
101  VkCommandBuffer command_buffer,
102  VkPipeline compute_pipeline,
103  VkPipelineLayout pipeline_layout,
104  VkDescriptorSet descriptor_set,
105  uint32_t descriptor_set_index,
106  int blocksX, int blocksY, int blocksZ);
107 
108 int vk_submit_command_buffer(void *user_context, VkQueue queue, VkCommandBuffer command_buffer);
109 
110 // -- Scalar Uniform Buffer
111 bool vk_needs_scalar_uniform_buffer(void *user_context,
112  size_t arg_sizes[],
113  void *args[],
114  int8_t arg_is_buffer[]);
115 
116 size_t vk_estimate_scalar_uniform_buffer_size(void *user_context,
117  size_t arg_sizes[],
118  void *args[],
119  int8_t arg_is_buffer[]);
120 
121 MemoryRegion *vk_create_scalar_uniform_buffer(void *user_context,
122  VulkanMemoryAllocator *allocator,
123  size_t scalar_buffer_size);
124 
125 int vk_update_scalar_uniform_buffer(void *user_context,
126  VulkanMemoryAllocator *allocator,
127  MemoryRegion *region,
128  size_t arg_sizes[],
129  void *args[],
130  int8_t arg_is_buffer[]);
131 
132 int vk_destroy_scalar_uniform_buffer(void *user_context, VulkanMemoryAllocator *allocator,
133  MemoryRegion *scalar_args_region);
134 // -- Descriptor Pool
135 int vk_create_descriptor_pool(void *user_context,
136  VulkanMemoryAllocator *allocator,
137  uint32_t uniform_buffer_count,
138  uint32_t storage_buffer_count,
139  VkDescriptorPool *descriptor_pool);
140 
141 int vk_destroy_descriptor_pool(void *user_context,
142  VulkanMemoryAllocator *allocator,
143  VkDescriptorPool descriptor_pool);
144 
145 // -- Descriptor Set Layout
146 uint32_t vk_count_bindings_for_descriptor_set(void *user_context,
147  size_t arg_sizes[],
148  void *args[],
149  int8_t arg_is_buffer[]);
150 
151 int vk_create_descriptor_set_layout(void *user_context,
152  VulkanMemoryAllocator *allocator,
153  uint32_t uniform_buffer_count,
154  uint32_t storage_buffer_count,
155  VkDescriptorSetLayout *layout);
156 
157 int vk_destroy_descriptor_set_layout(void *user_context,
158  VulkanMemoryAllocator *allocator,
159  VkDescriptorSetLayout descriptor_set_layout);
160 
161 // -- Descriptor Set
162 int vk_create_descriptor_set(void *user_context,
163  VulkanMemoryAllocator *allocator,
164  VkDescriptorSetLayout descriptor_set_layout,
165  VkDescriptorPool descriptor_pool,
166  VkDescriptorSet *descriptor_set);
167 
168 int vk_update_descriptor_set(void *user_context,
169  VulkanMemoryAllocator *allocator,
170  VkBuffer *scalar_args_buffer,
171  size_t uniform_buffer_count,
172  size_t storage_buffer_count,
173  size_t arg_sizes[],
174  void *args[],
175  int8_t arg_is_buffer[],
176  VkDescriptorSet descriptor_set);
177 
178 // -- Pipeline Layout
179 int vk_create_pipeline_layout(void *user_context,
180  VulkanMemoryAllocator *allocator,
181  uint32_t descriptor_set_count,
182  VkDescriptorSetLayout *descriptor_set_layouts,
183  VkPipelineLayout *pipeline_layout);
184 
185 int vk_destroy_pipeline_layout(void *user_context,
186  VulkanMemoryAllocator *allocator,
187  VkPipelineLayout pipeline_layout);
188 // -- Compute Pipeline
189 int vk_create_compute_pipeline(void *user_context,
190  VulkanMemoryAllocator *allocator,
191  const char *pipeline_name,
192  VkShaderModule shader_module,
193  VkPipelineLayout pipeline_layout,
194  VkSpecializationInfo *specialization_info,
195  VkPipeline *compute_pipeline);
196 
197 int vk_setup_compute_pipeline(void *user_context,
198  VulkanMemoryAllocator *allocator,
199  VulkanShaderBinding *shader_bindings,
200  VkShaderModule shader_module,
201  VkPipelineLayout pipeline_layout,
202  VkPipeline *compute_pipeline);
203 
204 int vk_destroy_compute_pipeline(void *user_context,
205  VulkanMemoryAllocator *allocator,
206  VkPipeline compute_pipeline);
207 
208 // -- Shader Module
209 VulkanShaderBinding *vk_decode_shader_bindings(void *user_context, VulkanMemoryAllocator *allocator,
210  const uint32_t *module_ptr, uint32_t module_size);
211 
212 VulkanCompilationCacheEntry *vk_compile_shader_module(void *user_context, VulkanMemoryAllocator *allocator,
213  const char *src, int size);
214 
215 int vk_destroy_shader_modules(void *user_context, VulkanMemoryAllocator *allocator);
216 
217 // -- Copy Buffer
218 int vk_do_multidimensional_copy(void *user_context, VkCommandBuffer command_buffer,
219  const device_copy &c, uint64_t src_offset, uint64_t dst_offset,
220  int d, bool from_host, bool to_host);
221 
222 // --------------------------------------------------------------------------
223 // Errors
224 // --------------------------------------------------------------------------
225 
226 // Returns the corresponding string for a given vulkan error code
227 const char *vk_get_error_name(VkResult error) {
228  switch (error) {
229  case VK_SUCCESS:
230  return "VK_SUCCESS";
231  case VK_NOT_READY:
232  return "VK_NOT_READY";
233  case VK_TIMEOUT:
234  return "VK_TIMEOUT";
235  case VK_EVENT_SET:
236  return "VK_EVENT_SET";
237  case VK_EVENT_RESET:
238  return "VK_EVENT_RESET";
239  case VK_INCOMPLETE:
240  return "VK_INCOMPLETE";
242  return "VK_ERROR_OUT_OF_HOST_MEMORY";
244  return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
246  return "VK_ERROR_INITIALIZATION_FAILED";
248  return "VK_ERROR_DEVICE_LOST";
250  return "VK_ERROR_MEMORY_MAP_FAILED";
252  return "VK_ERROR_LAYER_NOT_PRESENT";
254  return "VK_ERROR_EXTENSION_NOT_PRESENT";
256  return "VK_ERROR_FEATURE_NOT_PRESENT";
258  return "VK_ERROR_INCOMPATIBLE_DRIVER";
260  return "VK_ERROR_TOO_MANY_OBJECTS";
262  return "VK_ERROR_FORMAT_NOT_SUPPORTED";
264  return "VK_ERROR_FRAGMENTED_POOL";
266  return "VK_ERROR_SURFACE_LOST_KHR";
268  return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
269  case VK_SUBOPTIMAL_KHR:
270  return "VK_SUBOPTIMAL_KHR";
272  return "VK_ERROR_OUT_OF_DATE_KHR";
274  return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
276  return "VK_ERROR_VALIDATION_FAILED_EXT";
278  return "VK_ERROR_INVALID_SHADER_NV";
280  return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
282  return "VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR";
283  default:
284  return "<Unknown Vulkan Result Code>";
285  }
286 }
287 
288 // --------------------------------------------------------------------------
289 
290 } // namespace
291 } // namespace Vulkan
292 } // namespace Internal
293 } // namespace Runtime
294 } // namespace Halide
295 
296 #endif // HALIDE_RUNTIME_VULKAN_INTERNAL_H
VK_ERROR_DEVICE_LOST
@ VK_ERROR_DEVICE_LOST
Definition: mini_vulkan.h:148
VK_ERROR_OUT_OF_POOL_MEMORY_KHR
@ VK_ERROR_OUT_OF_POOL_MEMORY_KHR
Definition: mini_vulkan.h:164
Halide::Runtime::Internal::device_copy
Definition: device_buffer_utils.h:33
VK_ERROR_MEMORY_MAP_FAILED
@ VK_ERROR_MEMORY_MAP_FAILED
Definition: mini_vulkan.h:149
string_table.h
VK_ERROR_VALIDATION_FAILED_EXT
@ VK_ERROR_VALIDATION_FAILED_EXT
Definition: mini_vulkan.h:162
VK_ERROR_OUT_OF_DATE_KHR
@ VK_ERROR_OUT_OF_DATE_KHR
Definition: mini_vulkan.h:160
VK_ERROR_NATIVE_WINDOW_IN_USE_KHR
@ VK_ERROR_NATIVE_WINDOW_IN_USE_KHR
Definition: mini_vulkan.h:158
VK_ERROR_OUT_OF_DEVICE_MEMORY
@ VK_ERROR_OUT_OF_DEVICE_MEMORY
Definition: mini_vulkan.h:146
VK_EVENT_SET
@ VK_EVENT_SET
Definition: mini_vulkan.h:142
int8_t
signed __INT8_TYPE__ int8_t
Definition: runtime_internal.h:28
Halide::Runtime::Internal::MemoryRegion
Definition: memory_resources.h:79
VK_ERROR_EXTENSION_NOT_PRESENT
@ VK_ERROR_EXTENSION_NOT_PRESENT
Definition: mini_vulkan.h:151
gpu_context_common.h
VK_ERROR_FRAGMENTED_POOL
@ VK_ERROR_FRAGMENTED_POOL
Definition: mini_vulkan.h:156
VK_ERROR_OUT_OF_HOST_MEMORY
@ VK_ERROR_OUT_OF_HOST_MEMORY
Definition: mini_vulkan.h:145
VK_INCOMPLETE
@ VK_INCOMPLETE
Definition: mini_vulkan.h:144
memory_arena.h
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:23
VK_EVENT_RESET
@ VK_EVENT_RESET
Definition: mini_vulkan.h:143
VK_NOT_READY
@ VK_NOT_READY
Definition: mini_vulkan.h:140
VK_ERROR_INCOMPATIBLE_DISPLAY_KHR
@ VK_ERROR_INCOMPATIBLE_DISPLAY_KHR
Definition: mini_vulkan.h:161
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
block_storage.h
VK_ERROR_LAYER_NOT_PRESENT
@ VK_ERROR_LAYER_NOT_PRESENT
Definition: mini_vulkan.h:150
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Runtime::Internal::Vulkan::VulkanCompilationCacheEntry::pipeline_layout
VkPipelineLayout pipeline_layout
Definition: vulkan_resources.h:63
VkResult
VkResult
Definition: mini_vulkan.h:138
scoped_spin_lock.h
Halide::Runtime::Internal::Vulkan::VulkanCompilationCacheEntry::descriptor_set_layouts
VkDescriptorSetLayout * descriptor_set_layouts
Definition: vulkan_resources.h:62
printer.h
VkSystemAllocationScope
VkSystemAllocationScope
Definition: mini_vulkan.h:365
VK_ERROR_INVALID_SHADER_NV
@ VK_ERROR_INVALID_SHADER_NV
Definition: mini_vulkan.h:163
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:22
string_storage.h
VK_ERROR_TOO_MANY_OBJECTS
@ VK_ERROR_TOO_MANY_OBJECTS
Definition: mini_vulkan.h:154
VK_ERROR_FORMAT_NOT_SUPPORTED
@ VK_ERROR_FORMAT_NOT_SUPPORTED
Definition: mini_vulkan.h:155
VkAllocationCallbacks
Definition: mini_vulkan.h:1449
VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
@ VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
Definition: mini_vulkan.h:165
linked_list.h
Halide::DeviceAPI::Vulkan
@ Vulkan
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition: HalideRuntime.h:1490
VK_SUBOPTIMAL_KHR
@ VK_SUBOPTIMAL_KHR
Definition: mini_vulkan.h:159
Halide::Runtime::Internal::Vulkan::VulkanCompilationCacheEntry::shader_bindings
VulkanShaderBinding * shader_bindings
Definition: vulkan_resources.h:65
VK_ERROR_SURFACE_LOST_KHR
@ VK_ERROR_SURFACE_LOST_KHR
Definition: mini_vulkan.h:157
VK_ERROR_INITIALIZATION_FAILED
@ VK_ERROR_INITIALIZATION_FAILED
Definition: mini_vulkan.h:147
Halide::Runtime::Internal::Vulkan::VulkanCompilationCacheEntry::shader_module
VkShaderModule shader_module
Definition: vulkan_resources.h:61
runtime_internal.h
VK_ERROR_INCOMPATIBLE_DRIVER
@ VK_ERROR_INCOMPATIBLE_DRIVER
Definition: mini_vulkan.h:153
uint32_t
unsigned __INT32_TYPE__ uint32_t
Definition: runtime_internal.h:25
Halide::Runtime::Internal::StringTable
Definition: string_table.h:15
VK_ERROR_FEATURE_NOT_PRESENT
@ VK_ERROR_FEATURE_NOT_PRESENT
Definition: mini_vulkan.h:152
VK_TIMEOUT
@ VK_TIMEOUT
Definition: mini_vulkan.h:141
vulkan_interface.h
VK_SUCCESS
@ VK_SUCCESS
Definition: mini_vulkan.h:139
VkSpecializationInfo
Definition: mini_vulkan.h:1955