Halide  17.0.2
Halide compiler and libraries
vulkan_extensions.h
Go to the documentation of this file.
1 #ifndef HALIDE_RUNTIME_VULKAN_EXTENSIONS_H
2 #define HALIDE_RUNTIME_VULKAN_EXTENSIONS_H
3 
4 #include "vulkan_internal.h"
5 
6 // --------------------------------------------------------------------------
7 
8 namespace Halide {
9 namespace Runtime {
10 namespace Internal {
11 namespace Vulkan {
12 
13 // --------------------------------------------------------------------------
14 
15 WEAK char layer_names[1024];
18 
19 WEAK char extension_names[1024];
22 
23 WEAK char device_type[256];
26 
27 WEAK char build_options[1024];
30 
31 WEAK char alloc_config[1024];
34 
35 // --------------------------------------------------------------------------
36 namespace {
37 
38 void vk_set_layer_names_internal(const char *n) {
39  if (n) {
40  size_t buffer_size = sizeof(layer_names) / sizeof(layer_names[0]);
41  StringUtils::copy_up_to(layer_names, n, buffer_size);
42  } else {
43  layer_names[0] = 0;
44  }
46 }
47 
48 const char *vk_get_layer_names_internal(void *user_context) {
50  const char *value = getenv("HL_VK_LAYERS");
51  if (value == nullptr) {
52  value = getenv("VK_INSTANCE_LAYERS");
53  }
54  vk_set_layer_names_internal(value);
55  }
56  return layer_names;
57 }
58 
59 void vk_set_extension_names_internal(const char *n) {
60  if (n) {
61  size_t buffer_size = sizeof(extension_names) / sizeof(extension_names[0]);
63  } else {
64  extension_names[0] = 0;
65  }
67 }
68 
69 const char *vk_get_extension_names_internal(void *user_context) {
71  const char *name = getenv("HL_VK_EXTENSIONS");
72  vk_set_extension_names_internal(name);
73  }
74  return extension_names;
75 }
76 
77 void vk_set_device_type_internal(const char *n) {
78  if (n) {
79  size_t buffer_size = sizeof(device_type) / sizeof(device_type[0]);
80  StringUtils::copy_up_to(device_type, n, buffer_size);
81  } else {
82  device_type[0] = 0;
83  }
85 }
86 
87 const char *vk_get_device_type_internal(void *user_context) {
89  const char *name = getenv("HL_VK_DEVICE_TYPE");
90  vk_set_device_type_internal(name);
91  }
92  return device_type;
93 }
94 
95 void vk_set_build_options_internal(const char *n) {
96  if (n) {
97  size_t buffer_size = sizeof(build_options) / sizeof(build_options[0]);
98  StringUtils::copy_up_to(build_options, n, buffer_size);
99  } else {
100  build_options[0] = 0;
101  }
103 }
104 
105 const char *vk_get_build_options_internal(void *user_context) {
107  const char *name = getenv("HL_VK_BUILD_OPTIONS");
108  vk_set_build_options_internal(name);
109  }
110  return build_options;
111 }
112 
113 void vk_set_alloc_config_internal(const char *n) {
114  if (n) {
115  size_t buffer_size = sizeof(alloc_config) / sizeof(alloc_config[0]);
116  StringUtils::copy_up_to(alloc_config, n, buffer_size);
117  } else {
118  alloc_config[0] = 0;
119  }
121 }
122 
123 const char *vk_get_alloc_config_internal(void *user_context) {
125  const char *name = getenv("HL_VK_ALLOC_CONFIG");
126  vk_set_alloc_config_internal(name);
127  }
128  return alloc_config;
129 }
130 
131 // --------------------------------------------------------------------------
132 
133 uint32_t vk_get_requested_layers(void *user_context, StringTable &layer_table) {
135  const char *layer_names = vk_get_layer_names_internal(user_context);
136  return layer_table.parse(user_context, layer_names, HL_VK_ENV_DELIM);
137 }
138 
139 uint32_t vk_get_required_instance_extensions(void *user_context, StringTable &ext_table) {
140  const char *required_ext_table[] = {"VK_KHR_get_physical_device_properties2"};
141  const uint32_t required_ext_count = sizeof(required_ext_table) / sizeof(required_ext_table[0]);
142  ext_table.fill(user_context, (const char **)required_ext_table, required_ext_count);
143  return required_ext_count;
144 }
145 
146 uint32_t vk_get_supported_instance_extensions(void *user_context, StringTable &ext_table) {
147 
149  vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceExtensionProperties");
150 
152  debug(user_context) << "Vulkan: Missing vkEnumerateInstanceExtensionProperties proc address! Invalid loader?!\n";
153  return 0;
154  }
155 
156  debug(user_context) << "Vulkan: Checking vkEnumerateInstanceExtensionProperties for extensions ...\n";
157 
158  uint32_t avail_ext_count = 0;
159  vkEnumerateInstanceExtensionProperties(nullptr, &avail_ext_count, nullptr);
160 
161  if (avail_ext_count) {
162  BlockStorage::Config config;
163  config.entry_size = sizeof(VkExtensionProperties);
164  config.minimum_capacity = avail_ext_count;
165 
166  BlockStorage extension_properties(user_context, config);
167  extension_properties.resize(user_context, avail_ext_count);
168 
170  &avail_ext_count, static_cast<VkExtensionProperties *>(extension_properties.data()));
171 
172  for (uint32_t n = 0; n < avail_ext_count; ++n) {
173  const VkExtensionProperties *properties = static_cast<const VkExtensionProperties *>(extension_properties[n]);
174  debug(user_context) << " [" << n << "]: " << properties->extensionName << "\n";
175  }
176 
177  ext_table.resize(user_context, avail_ext_count);
178  for (uint32_t n = 0; n < avail_ext_count; ++n) {
179  const VkExtensionProperties *properties = static_cast<const VkExtensionProperties *>(extension_properties[n]);
180  ext_table.assign(user_context, n, properties->extensionName);
181  }
182  }
183  debug(user_context) << "Vulkan: vkEnumerateInstanceExtensionProperties found " << avail_ext_count << " extensions ...\n";
184  return avail_ext_count;
185 }
186 
187 uint32_t vk_get_required_device_extensions(void *user_context, StringTable &ext_table) {
188  const char *required_ext_table[] = {"VK_KHR_8bit_storage", "VK_KHR_storage_buffer_storage_class"};
189  const uint32_t required_ext_count = sizeof(required_ext_table) / sizeof(required_ext_table[0]);
190  ext_table.fill(user_context, (const char **)required_ext_table, required_ext_count);
191  return required_ext_count;
192 }
193 
194 uint32_t vk_get_optional_device_extensions(void *user_context, StringTable &ext_table) {
195  const char *optional_ext_table[] = {
196  "VK_KHR_portability_subset", //< necessary for running under Molten (aka Vulkan on Mac)
197  "VK_KHR_16bit_storage",
198  "VK_KHR_shader_float16_int8",
199  "VK_KHR_shader_float_controls"};
200  const uint32_t optional_ext_count = sizeof(optional_ext_table) / sizeof(optional_ext_table[0]);
201  ext_table.fill(user_context, (const char **)optional_ext_table, optional_ext_count);
202  return optional_ext_count;
203 }
204 
205 uint32_t vk_get_supported_device_extensions(void *user_context, VkPhysicalDevice physical_device, StringTable &ext_table) {
206  debug(user_context) << "vk_get_supported_device_extensions\n";
207  if (vkEnumerateDeviceExtensionProperties == nullptr) {
208  debug(user_context) << "Vulkan: Missing vkEnumerateDeviceExtensionProperties proc address! Invalid loader?!\n";
209  return 0;
210  }
211 
212  debug(user_context) << "Vulkan: Checking vkEnumerateDeviceExtensionProperties for extensions ...\n";
213 
214  uint32_t avail_ext_count = 0;
215  vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &avail_ext_count, nullptr);
216  if (avail_ext_count > 0) {
217  BlockStorage::Config config;
218  config.entry_size = sizeof(VkExtensionProperties);
219  config.minimum_capacity = avail_ext_count;
220 
221  BlockStorage extension_properties(user_context, config);
222  extension_properties.resize(user_context, avail_ext_count);
223 
224  vkEnumerateDeviceExtensionProperties(physical_device, nullptr,
225  &avail_ext_count, static_cast<VkExtensionProperties *>(extension_properties.data()));
226 
227  for (uint32_t n = 0; n < avail_ext_count; ++n) {
228  const VkExtensionProperties *properties = static_cast<const VkExtensionProperties *>(extension_properties[n]);
229  debug(user_context) << " [" << n << "]: " << properties->extensionName << "\n";
230  }
231 
232  ext_table.resize(user_context, avail_ext_count);
233  for (uint32_t n = 0; n < avail_ext_count; ++n) {
234  const VkExtensionProperties *properties = static_cast<const VkExtensionProperties *>(extension_properties[n]);
235  ext_table.assign(user_context, n, properties->extensionName);
236  }
237  }
238 
239  debug(user_context) << "Vulkan: vkEnumerateDeviceExtensionProperties found " << avail_ext_count << " extensions ...\n";
240  return avail_ext_count;
241 }
242 
243 bool vk_validate_required_extension_support(void *user_context,
244  const StringTable &required_extensions,
245  const StringTable &supported_extensions) {
246  debug(user_context) << "Vulkan: Validating " << uint32_t(required_extensions.size()) << " extensions ...\n";
247  bool validated = true;
248  for (uint32_t n = 0; n < required_extensions.size(); ++n) {
249  const char *extension = required_extensions[n];
250  if (!supported_extensions.contains(extension)) {
251  debug(user_context) << "Vulkan: Missing required extension: '" << extension << "'!\n";
252  validated = false;
253  }
254  }
255  return validated;
256 }
257 
258 // --------------------------------------------------------------------------
259 
260 } // namespace
261 } // namespace Vulkan
262 } // namespace Internal
263 } // namespace Runtime
264 } // namespace Halide
265 
266 // --------------------------------------------------------------------------
267 
268 using namespace Halide::Runtime::Internal::Vulkan;
269 
270 // --------------------------------------------------------------------------
271 
272 extern "C" {
273 
274 // --------------------------------------------------------------------------
275 
276 WEAK void halide_vulkan_set_layer_names(const char *n) {
278  vk_set_layer_names_internal(n);
279 }
280 
281 WEAK const char *halide_vulkan_get_layer_names(void *user_context) {
283  return vk_get_layer_names_internal(user_context);
284 }
285 
288  vk_set_extension_names_internal(n);
289 }
290 
291 WEAK const char *halide_vulkan_get_extension_names(void *user_context) {
293  return vk_get_extension_names_internal(user_context);
294 }
295 
296 WEAK void halide_vulkan_set_device_type(const char *n) {
298  vk_set_device_type_internal(n);
299 }
300 
301 WEAK const char *halide_vulkan_get_device_type(void *user_context) {
303  return vk_get_device_type_internal(user_context);
304 }
305 
308  vk_set_build_options_internal(n);
309 }
310 
311 WEAK const char *halide_vulkan_get_build_options(void *user_context) {
313  return vk_get_build_options_internal(user_context);
314 }
315 
318  vk_set_alloc_config_internal(n);
319 }
320 
321 WEAK const char *halide_vulkan_get_alloc_config(void *user_context) {
323  return vk_get_alloc_config_internal(user_context);
324 }
325 
326 // --------------------------------------------------------------------------
327 
328 } // extern "C"
329 
330 #endif // HALIDE_RUNTIME_VULKAN_EXTENSIONS_H
void resize(void *user_context, size_t entry_count, bool realloc=true)
size_t parse(void *user_context, const char *str, const char *delim)
Definition: string_table.h:159
void assign(void *user_context, size_t index, const char *str, size_t length=0)
Definition: string_table.h:134
void resize(void *user_context, size_t capacity)
Definition: string_table.h:89
void fill(void *user_context, const char **array, size_t count)
Definition: string_table.h:125
bool contains(const char *str) const
Definition: string_table.h:191
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *pName)
VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
VkResult(VKAPI_PTR * PFN_vkEnumerateInstanceExtensionProperties)(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
Definition: mini_vulkan.h:2517
struct VkExtensionProperties VkExtensionProperties
WEAK ScopedSpinLock::AtomicFlag alloc_config_lock
WEAK ScopedSpinLock::AtomicFlag extension_names_lock
WEAK ScopedSpinLock::AtomicFlag layer_names_lock
WEAK ScopedSpinLock::AtomicFlag build_options_lock
WEAK ScopedSpinLock::AtomicFlag device_type_lock
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
char * getenv(const char *)
unsigned __INT32_TYPE__ uint32_t
#define WEAK
static size_t copy_up_to(char *dst, const char *src, size_t max_chars)
char extensionName[VK_MAX_EXTENSION_NAME_SIZE]
Definition: mini_vulkan.h:1713
WEAK void halide_vulkan_set_layer_names(const char *n)
WEAK const char * halide_vulkan_get_layer_names(void *user_context)
WEAK void halide_vulkan_set_build_options(const char *n)
WEAK void halide_vulkan_set_extension_names(const char *n)
WEAK const char * halide_vulkan_get_alloc_config(void *user_context)
WEAK const char * halide_vulkan_get_device_type(void *user_context)
WEAK void halide_vulkan_set_device_type(const char *n)
WEAK const char * halide_vulkan_get_extension_names(void *user_context)
WEAK void halide_vulkan_set_alloc_config(const char *n)
WEAK const char * halide_vulkan_get_build_options(void *user_context)
#define HL_VK_ENV_DELIM