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

Commit

Permalink
render/vulkan: get only available validation layers
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ckb0ne committed Oct 31, 2021
1 parent fb393dd commit 9372b1e
Showing 1 changed file with 87 additions and 8 deletions.
95 changes: 87 additions & 8 deletions render/vulkan/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,79 @@ static const char *find_extensions(const VkExtensionProperties *avail,
return NULL;
}

static char **get_validation_layers(uint32_t *layer_count) {
// TODO request layer via env var
static const char *layers[] = {
"VK_LAYER_KHRONOS_validation",
// "VK_LAYER_RENDERDOC_Capture",
// "VK_LAYER_live_introspection",
};

static const size_t layers_len = sizeof(layers) / sizeof(layers[0]);

uint32_t count;
vkEnumerateInstanceLayerProperties(&count, NULL);

if (count == 0) {
wlr_log(WLR_DEBUG, "No validation layers found");
goto layers_err;
}
wlr_log(WLR_DEBUG, "Found %"PRIu32" validation layers", count);

VkLayerProperties *layer_props = calloc((size_t)count,
sizeof(VkLayerProperties));
if (layer_props == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate %"PRIu32" VkLayerProperties",
count);
goto layers_err;
}

uint32_t found_layers_count = 0;
char **found_layers = calloc((size_t)count, sizeof(char*));
if (found_layers == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate validation layers");
goto layers_err;
}

vkEnumerateInstanceLayerProperties(&count, layer_props);
for (size_t i = 0; i < (size_t)count; ++i) {
wlr_log(WLR_DEBUG, "Vulkan instance validation layer %s v%"PRIu32,
layer_props[i].layerName, layer_props[i].implementationVersion);
for (size_t j = 0; j < layers_len; ++j) {
if (strcmp(layer_props[i].layerName, layers[j]) == 0) {
found_layers[found_layers_count] = calloc(
VK_MAX_EXTENSION_NAME_SIZE, sizeof(char));
if (found_layers[found_layers_count] == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate validation layer");
goto layers_err;
}

strcpy(found_layers[found_layers_count], layers[j]);
found_layers_count++;
break;
}
}
}

free(layer_props);

*layer_count = found_layers_count;
return found_layers;

layers_err:
free(layer_props);

if (found_layers) {
for (uint32_t i = 0; i < found_layers_count; ++i) {
free(found_layers[i]);
}
}

free(found_layers);
*layer_count = 0;
return NULL;
}

static VkBool32 debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
VkDebugUtilsMessageTypeFlagsEXT type,
const VkDebugUtilsMessengerCallbackDataEXT *debug_data,
Expand Down Expand Up @@ -163,21 +236,21 @@ struct wlr_vk_instance *vulkan_instance_create(size_t ext_count,
application_info.engineVersion = WLR_VERSION_NUM;
application_info.apiVersion = VK_API_VERSION_1_1;

const char *layers[] = {
"VK_LAYER_KHRONOS_validation",
// "VK_LAYER_RENDERDOC_Capture",
// "VK_LAYER_live_introspection",
};

unsigned layer_count = debug * (sizeof(layers) / sizeof(layers[0]));
uint32_t layer_count = 0;
char **layers = get_validation_layers(&layer_count);
wlr_log(WLR_DEBUG, "Using %"PRIu32" instance validation layers",
layer_count);
for (uint32_t i = 0; i < layer_count; ++i) {
wlr_log(WLR_DEBUG, "%s", layers[i]);
}

VkInstanceCreateInfo instance_info = {0};
instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instance_info.pApplicationInfo = &application_info;
instance_info.enabledExtensionCount = ini->extension_count;
instance_info.ppEnabledExtensionNames = ini->extensions;
instance_info.enabledLayerCount = layer_count;
instance_info.ppEnabledLayerNames = layers;
instance_info.ppEnabledLayerNames = (const char *const *)layers;

VkDebugUtilsMessageSeverityFlagsEXT severity =
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
Expand All @@ -204,6 +277,12 @@ struct wlr_vk_instance *vulkan_instance_create(size_t ext_count,
}

res = vkCreateInstance(&instance_info, NULL, &ini->instance);

for (size_t i = 0; i < layer_count; ++i) {
free(layers[i]);
}
free(layers);

if (res != VK_SUCCESS) {
wlr_vk_error("Could not create instance", res);
goto error;
Expand Down

0 comments on commit 9372b1e

Please sign in to comment.