Skip to content

Commit

Permalink
Adjust hello_triangle samples to warn instead of throw when there's n…
Browse files Browse the repository at this point in the history
…o validation layer
  • Loading branch information
asuessenbach committed Jan 21, 2025
1 parent 87849d4 commit 64b561a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 181 deletions.
63 changes: 14 additions & 49 deletions samples/api/hello_triangle/hello_triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,6 @@ bool HelloTriangle::validate_extensions(const std::vector<const char *>
return true;
}

/**
* @brief Validates a list of required layers, comparing it with the available ones.
*
* @param required A vector containing required layer names.
* @param available A VkLayerProperties object containing available layers.
* @return true if all required extensions are available
* @return false otherwise
*/
bool HelloTriangle::validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available)
{
for (auto extension : required)
{
bool found = false;
for (auto &available_extension : available)
{
if (strcmp(available_extension.layerName, extension) == 0)
{
found = true;
break;
}
}

if (!found)
{
return false;
}
}

return true;
}

/**
* @brief Find the vulkan shader stage for a given a string.
*
Expand Down Expand Up @@ -226,30 +194,27 @@ void HelloTriangle::init_instance()
throw std::runtime_error("Required instance extensions are missing.");
}

uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));
std::vector<const char *> requested_instance_layers{};

std::vector<VkLayerProperties> supported_validation_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_validation_layers.data()));
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
char const *validationLayer = "VK_LAYER_KHRONOS_validation";

std::vector<const char *> requested_validation_layers{};
uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
requested_validation_layers.push_back("VK_LAYER_KHRONOS_validation");
#endif
std::vector<VkLayerProperties> supported_instance_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_instance_layers.data()));

if (validate_layers(requested_validation_layers, supported_validation_layers))
if (std::any_of(supported_instance_layers.begin(), supported_instance_layers.end(), [&validationLayer](auto const &lp) { return strcmp(lp.layerName, validationLayer) == 0; }))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : requested_validation_layers)
{
LOGI(" \t{}", layer);
}
requested_instance_layers.push_back(validationLayer);
LOGI("Enabled Validation Layer {}", validationLayer);
}
else
{
throw std::runtime_error("Required validation layers are missing.");
LOGW("Validation Layer {} is not available", validationLayer);
}
#endif

VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO};
app.pApplicationName = "Hello Triangle";
Expand All @@ -260,8 +225,8 @@ void HelloTriangle::init_instance()
instance_info.pApplicationInfo = &app;
instance_info.enabledExtensionCount = vkb::to_u32(required_instance_extensions.size());
instance_info.ppEnabledExtensionNames = required_instance_extensions.data();
instance_info.enabledLayerCount = vkb::to_u32(requested_validation_layers.size());
instance_info.ppEnabledLayerNames = requested_validation_layers.data();
instance_info.enabledLayerCount = vkb::to_u32(requested_instance_layers.size());
instance_info.ppEnabledLayerNames = requested_instance_layers.data();

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
// Validation layers help finding wrong api usage, we enable them when explicitly requested or in debug builds
Expand Down
3 changes: 0 additions & 3 deletions samples/api/hello_triangle/hello_triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ class HelloTriangle : public vkb::Application
bool validate_extensions(const std::vector<const char *> &required,
const std::vector<VkExtensionProperties> &available);

bool validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available);

VkShaderStageFlagBits find_shader_stage(const std::string &ext);

void init_instance();
Expand Down
66 changes: 14 additions & 52 deletions samples/api/hello_triangle_1_3/hello_triangle_1_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,6 @@ bool HelloTriangleV13::validate_extensions(const std::vector<const char *>
return all_found;
}

/**
* @brief Validates a list of required layers, comparing it with the available ones.
*
* @param required A vector containing required layer names.
* @param available A VkLayerProperties object containing available layers.
* @return true if all required extensions are available
* @return false otherwise
*/
bool HelloTriangleV13::validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available)
{
bool all_found = true;

for (const auto *layer_name : required)
{
bool found = false;
for (const auto &available_layer : available)
{
if (strcmp(available_layer.layerName, layer_name) == 0)
{
found = true;
break;
}
}

if (!found)
{
LOGE("Error: Required layer not found: {}", layer_name);
all_found = false;
}
}

return all_found;
}

/**
* @brief Initializes the Vulkan instance.
*/
Expand Down Expand Up @@ -188,30 +153,27 @@ void HelloTriangleV13::init_instance()
throw std::runtime_error("Required instance extensions are missing.");
}

uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));
std::vector<const char *> requested_instance_layers{};

std::vector<VkLayerProperties> supported_validation_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_validation_layers.data()));
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
char const *validationLayer = "VK_LAYER_KHRONOS_validation";

std::vector<const char *> requested_validation_layers{};
uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
requested_validation_layers.push_back("VK_LAYER_KHRONOS_validation");
#endif
std::vector<VkLayerProperties> supported_instance_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_instance_layers.data()));

if (validate_layers(requested_validation_layers, supported_validation_layers))
if (std::any_of(supported_instance_layers.begin(), supported_instance_layers.end(), [&validationLayer](auto const &lp) { return strcmp(lp.layerName, validationLayer) == 0; }))
{
LOGI("Enabled Validation Layers:");
for (const auto &layer : requested_validation_layers)
{
LOGI(" \t{}", layer);
}
requested_instance_layers.push_back(validationLayer);
LOGI("Enabled Validation Layer {}", validationLayer);
}
else
{
throw std::runtime_error("Required validation layers are missing.");
LOGW("Validation Layer {} is not available", validationLayer);
}
#endif

VkApplicationInfo app{
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Expand All @@ -222,8 +184,8 @@ void HelloTriangleV13::init_instance()
VkInstanceCreateInfo instance_info{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app,
.enabledLayerCount = vkb::to_u32(requested_validation_layers.size()),
.ppEnabledLayerNames = requested_validation_layers.data(),
.enabledLayerCount = vkb::to_u32(requested_instance_layers.size()),
.ppEnabledLayerNames = requested_instance_layers.data(),
.enabledExtensionCount = vkb::to_u32(required_instance_extensions.size()),
.ppEnabledExtensionNames = required_instance_extensions.data()};

Expand Down
3 changes: 0 additions & 3 deletions samples/api/hello_triangle_1_3/hello_triangle_1_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ class HelloTriangleV13 : public vkb::Application
bool validate_extensions(const std::vector<const char *> &required,
const std::vector<VkExtensionProperties> &available);

bool validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available);

void init_instance();

void init_device();
Expand Down
44 changes: 10 additions & 34 deletions samples/api/hpp_hello_triangle/hpp_hello_triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,6 @@ bool validate_extensions(const std::vector<const char *> &required,
}) == required.end();
}

bool validate_layers(const std::vector<const char *> &required,
const std::vector<vk::LayerProperties> &available)
{
// inner find_if returns true if the layer was not found
// outer find_if returns iterator to the not found layer, if any
auto requiredButNotFoundIt = std::find_if(required.begin(),
required.end(),
[&available](auto layer) {
return std::find_if(available.begin(),
available.end(),
[&layer](auto const &lp) {
return strcmp(lp.layerName, layer) == 0;
}) == available.end();
});
if (requiredButNotFoundIt != required.end())
{
LOGE("Validation Layer {} not found", *requiredButNotFoundIt);
}
return (requiredButNotFoundIt == required.end());
}

HPPHelloTriangle::HPPHelloTriangle()
{
}
Expand Down Expand Up @@ -441,30 +420,27 @@ vk::Instance HPPHelloTriangle::create_instance(std::vector<const char *> const &
throw std::runtime_error("Required instance extensions are missing.");
}

std::vector<vk::LayerProperties> supported_validation_layers = vk::enumerateInstanceLayerProperties();
std::vector<const char *> requested_instance_layers(required_validation_layers);

std::vector<const char *> requested_validation_layers(required_validation_layers);
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
char const *validationLayer = "VK_LAYER_KHRONOS_validation";

#ifdef VKB_VALIDATION_LAYERS
requested_validation_layers.push_back("VK_LAYER_KHRONOS_validation");
#endif
std::vector<vk::LayerProperties> supported_instance_layers = vk::enumerateInstanceLayerProperties();

if (validate_layers(requested_validation_layers, supported_validation_layers))
if (std::any_of(supported_instance_layers.begin(), supported_instance_layers.end(), [&validationLayer](auto const &lp) { return strcmp(lp.layerName, validationLayer) == 0; }))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : requested_validation_layers)
{
LOGI(" \t{}", layer);
}
requested_instance_layers.push_back(validationLayer);
LOGI("Enabled Validation Layer {}", validationLayer);
}
else
{
throw std::runtime_error("Required validation layers are missing.");
LOGW("Validation Layer {} is not available", validationLayer);
}
#endif

vk::ApplicationInfo app("HPP Hello Triangle", {}, "Vulkan Samples", {}, VK_MAKE_VERSION(1, 0, 0));

vk::InstanceCreateInfo instance_info({}, &app, requested_validation_layers, active_instance_extensions);
vk::InstanceCreateInfo instance_info({}, &app, requested_instance_layers, active_instance_extensions);

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
debug_utils_create_info =
Expand Down
54 changes: 15 additions & 39 deletions samples/extensions/full_screen_exclusive/full_screen_exclusive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,6 @@ bool FullScreenExclusive::validate_extensions(const std::vector<const char *> &r
return true;
}

bool FullScreenExclusive::validate_layers(const std::vector<const char *> &required, const std::vector<VkLayerProperties> &available)
{
for (auto extension : required)
{
bool found = false;

for (auto &available_extension : available)
{
if (strcmp(available_extension.layerName, extension) == 0)
{
found = true;
break;
}
}

if (!found)
{
return false;
}
}

return true;
}

VkShaderStageFlagBits FullScreenExclusive::find_shader_stage(const std::string &ext)
{
if (ext == "vert")
Expand Down Expand Up @@ -161,28 +137,28 @@ void FullScreenExclusive::init_instance(const std::vector<const char *> &require
{
throw std::runtime_error("Required instance extensions are missing.");
}

std::vector<const char *> requested_instance_layers(required_validation_layers);

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
char const *validationLayer = "VK_LAYER_KHRONOS_validation";

uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));
std::vector<VkLayerProperties> supported_validation_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_validation_layers.data()));
std::vector<const char *> requested_validation_layers(required_validation_layers);

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
requested_validation_layers.push_back("VK_LAYER_KHRONOS_validation");
#endif
std::vector<VkLayerProperties> supported_instance_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_instance_layers.data()));

if (validate_layers(requested_validation_layers, supported_validation_layers))
if (std::any_of(supported_instance_layers.begin(), supported_instance_layers.end(), [&validationLayer](auto const &lp) { return strcmp(lp.layerName, validationLayer) == 0; }))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : requested_validation_layers)
{
LOGI(" \t{}", layer)
}
requested_instance_layers.push_back(validationLayer);
LOGI("Enabled Validation Layer {}", validationLayer);
}
else
{
throw std::runtime_error("Required validation layers are missing.");
LOGW("Validation Layer {} is not available", validationLayer);
}
#endif

VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO};
app.pApplicationName = "Full Screen Exclusive";
Expand All @@ -193,8 +169,8 @@ void FullScreenExclusive::init_instance(const std::vector<const char *> &require
instance_info.pApplicationInfo = &app;
instance_info.enabledExtensionCount = vkb::to_u32(active_instance_extensions.size());
instance_info.ppEnabledExtensionNames = active_instance_extensions.data();
instance_info.enabledLayerCount = vkb::to_u32(requested_validation_layers.size());
instance_info.ppEnabledLayerNames = requested_validation_layers.data();
instance_info.enabledLayerCount = vkb::to_u32(requested_instance_layers.size());
instance_info.ppEnabledLayerNames = requested_instance_layers.data();

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
VkDebugUtilsMessengerCreateInfoEXT debug_utils_create_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class FullScreenExclusive : public vkb::Application
void update(float delta_time) override;
bool resize(uint32_t width, uint32_t height) override;
static bool validate_extensions(const std::vector<const char *> &required, const std::vector<VkExtensionProperties> &available);
static bool validate_layers(const std::vector<const char *> &required, const std::vector<VkLayerProperties> &available);
static VkShaderStageFlagBits find_shader_stage(const std::string &ext);
void init_instance(const std::vector<const char *> &required_instance_extensions, const std::vector<const char *> &required_validation_layers);
void init_device(const std::vector<const char *> &required_device_extensions);
Expand Down
2 changes: 2 additions & 0 deletions samples/extensions/shader_debugprintf/shader_debugprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ ShaderDebugPrintf::ShaderDebugPrintf()
// If layer settings available, use it to configure validation layer for debugPrintfEXT
add_instance_extension(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME, /*optional*/ true);

add_instance_layer("VK_LAYER_KHRONOS_shader_object");

VkLayerSettingEXT layerSetting;
layerSetting.pLayerName = "VK_LAYER_KHRONOS_validation";
layerSetting.pSettingName = "enables";
Expand Down

0 comments on commit 64b561a

Please sign in to comment.