Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 176db99

Browse files
committed
render/vulkan: get only available validation layers
1 parent fb393dd commit 176db99

File tree

1 file changed

+89
-8
lines changed

1 file changed

+89
-8
lines changed

render/vulkan/vulkan.c

+89-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,81 @@ static const char *find_extensions(const VkExtensionProperties *avail,
3333
return NULL;
3434
}
3535

36+
static char **get_validation_layers(uint32_t *layer_count) {
37+
// TODO request layer via env var
38+
static const char *layers[] = {
39+
"VK_LAYER_KHRONOS_validation",
40+
// "VK_LAYER_RENDERDOC_Capture",
41+
// "VK_LAYER_live_introspection",
42+
};
43+
44+
static const size_t layers_len = sizeof(layers) / sizeof(layers[0]);
45+
46+
VkLayerProperties *layer_props = NULL;
47+
char **found_layers = NULL;
48+
uint32_t found_layers_count = 0;
49+
50+
uint32_t count;
51+
vkEnumerateInstanceLayerProperties(&count, NULL);
52+
53+
if (count == 0) {
54+
wlr_log(WLR_DEBUG, "No validation layers found");
55+
goto layers_err;
56+
}
57+
wlr_log(WLR_DEBUG, "Found %"PRIu32" validation layers", count);
58+
59+
layer_props = calloc((size_t)count, sizeof(VkLayerProperties));
60+
if (layer_props == NULL) {
61+
wlr_log(WLR_ERROR, "Failed to allocate %"PRIu32" VkLayerProperties",
62+
count);
63+
goto layers_err;
64+
}
65+
66+
found_layers = calloc((size_t)count, sizeof(char*));
67+
if (found_layers == NULL) {
68+
wlr_log(WLR_ERROR, "Failed to allocate validation layers");
69+
goto layers_err;
70+
}
71+
72+
vkEnumerateInstanceLayerProperties(&count, layer_props);
73+
for (size_t i = 0; i < (size_t)count; ++i) {
74+
wlr_log(WLR_DEBUG, "Vulkan instance validation layer %s v%"PRIu32,
75+
layer_props[i].layerName, layer_props[i].implementationVersion);
76+
for (size_t j = 0; j < layers_len; ++j) {
77+
if (strcmp(layer_props[i].layerName, layers[j]) == 0) {
78+
found_layers[found_layers_count] = calloc(
79+
VK_MAX_EXTENSION_NAME_SIZE, sizeof(char));
80+
if (found_layers[found_layers_count] == NULL) {
81+
wlr_log(WLR_ERROR, "Failed to allocate validation layer");
82+
goto layers_err;
83+
}
84+
85+
strcpy(found_layers[found_layers_count], layers[j]);
86+
found_layers_count++;
87+
break;
88+
}
89+
}
90+
}
91+
92+
free(layer_props);
93+
94+
*layer_count = found_layers_count;
95+
return found_layers;
96+
97+
layers_err:
98+
free(layer_props);
99+
100+
if (found_layers) {
101+
for (uint32_t i = 0; i < found_layers_count; ++i) {
102+
free(found_layers[i]);
103+
}
104+
}
105+
106+
free(found_layers);
107+
*layer_count = 0;
108+
return NULL;
109+
}
110+
36111
static VkBool32 debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
37112
VkDebugUtilsMessageTypeFlagsEXT type,
38113
const VkDebugUtilsMessengerCallbackDataEXT *debug_data,
@@ -163,21 +238,21 @@ struct wlr_vk_instance *vulkan_instance_create(size_t ext_count,
163238
application_info.engineVersion = WLR_VERSION_NUM;
164239
application_info.apiVersion = VK_API_VERSION_1_1;
165240

166-
const char *layers[] = {
167-
"VK_LAYER_KHRONOS_validation",
168-
// "VK_LAYER_RENDERDOC_Capture",
169-
// "VK_LAYER_live_introspection",
170-
};
171-
172-
unsigned layer_count = debug * (sizeof(layers) / sizeof(layers[0]));
241+
uint32_t layer_count = 0;
242+
char **layers = get_validation_layers(&layer_count);
243+
wlr_log(WLR_DEBUG, "Using %"PRIu32" instance validation layers",
244+
layer_count);
245+
for (uint32_t i = 0; i < layer_count; ++i) {
246+
wlr_log(WLR_DEBUG, "%s", layers[i]);
247+
}
173248

174249
VkInstanceCreateInfo instance_info = {0};
175250
instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
176251
instance_info.pApplicationInfo = &application_info;
177252
instance_info.enabledExtensionCount = ini->extension_count;
178253
instance_info.ppEnabledExtensionNames = ini->extensions;
179254
instance_info.enabledLayerCount = layer_count;
180-
instance_info.ppEnabledLayerNames = layers;
255+
instance_info.ppEnabledLayerNames = (const char *const *)layers;
181256

182257
VkDebugUtilsMessageSeverityFlagsEXT severity =
183258
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
@@ -204,6 +279,12 @@ struct wlr_vk_instance *vulkan_instance_create(size_t ext_count,
204279
}
205280

206281
res = vkCreateInstance(&instance_info, NULL, &ini->instance);
282+
283+
for (size_t i = 0; i < layer_count; ++i) {
284+
free(layers[i]);
285+
}
286+
free(layers);
287+
207288
if (res != VK_SUCCESS) {
208289
wlr_vk_error("Could not create instance", res);
209290
goto error;

0 commit comments

Comments
 (0)