-
Notifications
You must be signed in to change notification settings - Fork 789
Add sample demonstrating VK_EXT_layer_settings usage #1419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gpx1000
wants to merge
10
commits into
KhronosGroup:main
Choose a base branch
from
gpx1000:VK_EXT_layer_settings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a131c2
Add sample demonstrating VK_EXT_layer_settings usage
gpx1000 9097014
- Update README with GitHub repository link for sample
gpx1000 54f808c
Add local debug messenger and UI overlay for validation messages in L…
gpx1000 7fb2566
Adjust code formatting in LayerSettingsSample for improved readability
gpx1000 0856f47
Merge branch 'main' into VK_EXT_layer_settings
gpx1000 d46c09d
Add interactive validation scenarios with toggleable UI and message c…
gpx1000 04c9d5c
Update copyright year to 2026
gpx1000 7bd25b2
Refactor scenario state tracking from array to unordered_map and mode…
gpx1000 c229456
Fix indentation to use tabs consistently throughout layer_settings files
gpx1000 50dda0f
Replace std::format with fmt::format and refactor loop variable naming
gpx1000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright (c) 2025, Holochip Inc. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 the "License"; | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) | ||
| get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH) | ||
| get_filename_component(CATEGORY_NAME ${PARENT_DIR} NAME) | ||
|
|
||
| add_sample_with_tags( | ||
| ID ${FOLDER_NAME} | ||
| CATEGORY ${CATEGORY_NAME} | ||
| AUTHOR "Holochip" | ||
| NAME "Layer settings" | ||
| DESCRIPTION "Demonstrates using VK_EXT_layer_settings to configure validation at runtime" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| //// | ||
| - Copyright (c) 2025, Holochip Inc | ||
| - | ||
| - SPDX-License-Identifier: Apache-2.0 | ||
| - | ||
| - Licensed under the Apache License, Version 2.0 the "License"; | ||
| - you may not use this file except in compliance with the License. | ||
| - You may obtain a copy of the License at | ||
| - | ||
| - http://www.apache.org/licenses/LICENSE-2.0 | ||
| - | ||
| - Unless required by applicable law or agreed to in writing, software | ||
| - distributed under the License is distributed on an "AS IS" BASIS, | ||
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| - See the License for the specific language governing permissions and | ||
| - limitations under the License. | ||
| - | ||
| //// | ||
| = VK_EXT_layer_settings — Configure validation layers programmatically | ||
|
|
||
| This sample demonstrates how to use VK_EXT_layer_settings to configure Vulkan layers (in particular `VK_LAYER_KHRONOS_validation`) directly from your application. Instead of relying on environment variables or JSON files, you can enable features like Best Practices and `debugPrintfEXT`, tune message filtering, or change output settings via code. | ||
|
|
||
| == Why use layer settings? | ||
|
|
||
| - Portability and reproducibility: ship settings with the app, not the environment. | ||
| - Fine-grained control: enable/disable individual validation features and categories. | ||
| - Better UX for samples and tools: turn on useful validation features without asking users to edit configs. | ||
|
|
||
| VK_EXT_layer_settings supersedes the older `VK_EXT_validation_features` and `VK_EXT_validation_flags` by offering a general, extensible way to pass settings to any Vulkan layer (not just validation). | ||
|
|
||
| == What this sample does | ||
|
|
||
| - Enables the instance extension `VK_EXT_layer_settings` (optionally). | ||
| - Adds settings for the Khronos validation layer before the Vulkan instance is created: | ||
| - Enables Best Practices checks. | ||
| - Enables `debugPrintfEXT` support (so shader `debugPrintf` messages are emitted via validation). | ||
asuessenbach marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Runs a minimal frame loop that presents without validation errors (uses a small per‑frame command buffer that transitions the swapchain image to `PRESENT`). | ||
|
|
||
| == When to use it vs. alternatives | ||
| - Prefer VK_EXT_layer_settings whenever you need to configure validation from within your app (development builds, samples, tools). | ||
| - `VK_EXT_validation_features` and `VK_EXT_validation_flags` are deprecated by layer settings; use those only for legacy purposes. | ||
|
|
||
| == Using VK_EXT_layer_settings | ||
| If you are integrating layer settings directly into your own Vulkan app, the flow is simple: | ||
|
|
||
| 1. Enable validation layers (e.g., "VK_LAYER_KHRONOS_validation"). | ||
| 2. Check that the validation layer advertises the instance extension VK_EXT_layer_settings. | ||
| 3. If available, add VK_EXT_layer_settings to your instance extension list. | ||
| 4. Create one or more VkLayerSettingEXT entries (for example, to enable Best Practices and debugPrintfEXT). | ||
| 5. Chain a VkLayerSettingsCreateInfoEXT with those settings into VkInstanceCreateInfo::pNext. | ||
| 6. Call vkCreateInstance as usual. The layer consumes the settings at instance creation time. | ||
| 7. Optional: Create a VkDebugUtilsMessengerEXT so you can see validation output. | ||
|
|
||
| === Minimal C/C++ example (C API shown) | ||
| [source,cpp] | ||
| ---- | ||
| #include <vulkan/vulkan.h> | ||
| #include <vector> | ||
| #include <cstring> | ||
|
|
||
| static const char *kValidationLayer = "VK_LAYER_KHRONOS_validation"; | ||
|
|
||
| // Helper to check if VK_EXT_layer_settings is advertised by the validation layer | ||
| bool layer_settings_supported() | ||
| { | ||
| uint32_t count = 0; | ||
| vkEnumerateInstanceExtensionProperties(kValidationLayer, &count, nullptr); | ||
| std::vector<VkExtensionProperties> exts(count); | ||
| vkEnumerateInstanceExtensionProperties(kValidationLayer, &count, exts.data()); | ||
| for (auto &e : exts) | ||
| { | ||
| if (strcmp(e.extensionName, VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) == 0) | ||
| return true; | ||
| } | ||
| return false; | ||
gpx1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| VkInstance create_instance_with_layer_settings() | ||
| { | ||
| std::vector<const char *> instance_exts; | ||
| instance_exts.push_back(VK_KHR_SURFACE_EXTENSION_NAME); | ||
| // Add your platform surface extension(s), e.g. VK_KHR_xcb_surface, VK_KHR_win32_surface, etc. | ||
|
|
||
| const char *layers[] = {kValidationLayer}; | ||
|
|
||
| // Prepare layer settings (strings) we want to enable | ||
| static const char *enables[] = { | ||
| "VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT", | ||
| "VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT", | ||
| }; | ||
|
|
||
| // One setting object that enables both features above | ||
| VkLayerSettingEXT setting{}; | ||
| setting.pLayerName = kValidationLayer; | ||
| setting.pSettingName = "enables"; // well-known name used by VVL | ||
| setting.type = VK_LAYER_SETTING_TYPE_STRING_EXT; // string list | ||
| setting.valueCount = static_cast<uint32_t>(std::size(enables)); | ||
| setting.pValues = enables; // const char* array | ||
|
|
||
| VkLayerSettingsCreateInfoEXT layer_settings_ci{VK_STRUCTURE_TYPE_LAYER_SETTINGS_CREATE_INFO_EXT}; | ||
| layer_settings_ci.settingCount = 1; | ||
| layer_settings_ci.pSettings = &setting; | ||
|
|
||
| // Build instance create info and chain layer settings if supported | ||
| VkInstanceCreateInfo ici{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; | ||
| ici.ppEnabledLayerNames = layers; | ||
| ici.enabledLayerCount = 1; | ||
| ici.ppEnabledExtensionNames = instance_exts.data(); | ||
| ici.enabledExtensionCount = static_cast<uint32_t>(instance_exts.size()); | ||
|
|
||
| const bool has_layer_settings = layer_settings_supported(); | ||
| if (has_layer_settings) | ||
| { | ||
| // You also need to add VK_EXT_layer_settings to the enabled instance extensions | ||
| instance_exts.push_back(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME); | ||
| ici.ppEnabledExtensionNames = instance_exts.data(); | ||
| ici.enabledExtensionCount = static_cast<uint32_t>(instance_exts.size()); | ||
| ici.pNext = &layer_settings_ci; | ||
| } | ||
| else | ||
| { | ||
| // Fallback for older SDKs: use VK_EXT_validation_features | ||
gpx1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static const VkValidationFeatureEnableEXT vfe[] = { | ||
| VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT, | ||
| VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT, | ||
| }; | ||
| static VkValidationFeaturesEXT validation_features{VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT}; | ||
| validation_features.enabledValidationFeatureCount = static_cast<uint32_t>(std::size(vfe)); | ||
| validation_features.pEnabledValidationFeatures = vfe; | ||
| ici.pNext = &validation_features; // no VK_EXT_layer_settings needed for this path | ||
| } | ||
|
|
||
| VkInstance instance = VK_NULL_HANDLE; | ||
| VkResult result = vkCreateInstance(&ici, nullptr, &instance); | ||
| if (result != VK_SUCCESS) | ||
| { | ||
| // Handle error (missing extensions/layers, etc.) | ||
| return VK_NULL_HANDLE; | ||
| } | ||
|
|
||
| return instance; | ||
| } | ||
| ---- | ||
|
|
||
| TIP: debugPrintfEXT only produces messages when your shaders call debugPrintf. See the shader_debugprintf sample for a complete pipeline + shader example that emits messages through validation. | ||
|
|
||
| NOTE: VK_EXT_layer_settings is an instance extension provided by the layer. On some systems it may not be present; the fallback above using VK_EXT_validation_features enables the same categories in a more limited, legacy way. | ||
|
|
||
| CAUTION: Layer settings only affect layers that recognize them. Enabling layer settings without enabling the corresponding layer (e.g., VK_LAYER_KHRONOS_validation) has no effect. | ||
asuessenbach marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* Copyright (c) 2025, Holochip Inc. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 the "License"; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "layer_settings.h" | ||
|
|
||
| #include "common/vk_common.h" | ||
| #include "common/vk_initializers.h" | ||
|
|
||
| #include <array> | ||
|
|
||
| LayerSettingsSample::LayerSettingsSample() | ||
| { | ||
| title = "Layer settings (VK_EXT_layer_settings)"; | ||
|
|
||
| // Configure the Khronos validation layer using layer settings. These settings are | ||
| // consumed by the validation layer at instance creation time. | ||
| // | ||
| // Note: The settings only take effect if the layer is enabled (e.g. by building | ||
| // with validation layers on, or enabling them via the application's options). | ||
| // | ||
| // 1) Enable Best Practices (generic + vendor-specific) | ||
| { | ||
| static const char *enables[] = { | ||
| "VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT", | ||
| }; | ||
|
|
||
| VkLayerSettingEXT layer_setting{}; | ||
| layer_setting.pLayerName = "VK_LAYER_KHRONOS_validation"; | ||
| layer_setting.pSettingName = "enables"; | ||
| layer_setting.type = VK_LAYER_SETTING_TYPE_STRING_EXT; | ||
| layer_setting.valueCount = static_cast<uint32_t>(std::size(enables)); | ||
| layer_setting.pValues = enables; | ||
| add_layer_setting(layer_setting); | ||
| } | ||
|
|
||
| // 2) Optionally enable debug printf so shaders using debugPrintfEXT will print via VVL | ||
| { | ||
| static const char *enables[] = { | ||
| "VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT", | ||
| }; | ||
|
|
||
| VkLayerSettingEXT layer_setting{}; | ||
| layer_setting.pLayerName = "VK_LAYER_KHRONOS_validation"; | ||
| layer_setting.pSettingName = "enables"; | ||
| layer_setting.type = VK_LAYER_SETTING_TYPE_STRING_EXT; | ||
| layer_setting.valueCount = static_cast<uint32_t>(std::size(enables)); | ||
| layer_setting.pValues = enables; | ||
| add_layer_setting(layer_setting); | ||
| } | ||
|
|
||
| // 3) Demonstrate disabling a known verbose message category (example) | ||
| // This shows how to filter messages with the "disables" setting. | ||
| // Replace with a concrete message enable/disable as needed in your environment. | ||
| { | ||
| static const char *disables[] = { | ||
| // Example: Treat performance warnings as disabled (users can customize) | ||
| "VK_VALIDATION_FEATURE_DISABLE_ALL_EXT" // users can comment this out; kept as illustration | ||
| }; | ||
|
|
||
| VkLayerSettingEXT layer_setting{}; | ||
| layer_setting.pLayerName = "VK_LAYER_KHRONOS_validation"; | ||
| layer_setting.pSettingName = "disables"; | ||
| layer_setting.type = VK_LAYER_SETTING_TYPE_STRING_EXT; | ||
| layer_setting.valueCount = static_cast<uint32_t>(std::size(disables)); | ||
| layer_setting.pValues = disables; | ||
| // Do not add this by default as it disables all validation. Leave as commented example. | ||
| // add_layer_setting(layer_setting); | ||
| } | ||
| } | ||
|
|
||
| bool LayerSettingsSample::prepare(const vkb::ApplicationOptions &options) | ||
| { | ||
| if (!ApiVulkanSample::prepare(options)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Build once; we record per-frame minimal CBs in render(). | ||
| build_command_buffers(); | ||
|
|
||
| prepared = true; | ||
| return true; | ||
| } | ||
|
|
||
| void LayerSettingsSample::record_minimal_present_cmd(VkCommandBuffer cmd, uint32_t image_index) | ||
| { | ||
| VkCommandBufferBeginInfo begin_info = vkb::initializers::command_buffer_begin_info(); | ||
| VK_CHECK(vkBeginCommandBuffer(cmd, &begin_info)); | ||
|
|
||
| // Transition the acquired swapchain image to PRESENT so vkQueuePresentKHR is valid. | ||
| VkImageSubresourceRange subresource_range{}; | ||
| subresource_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| subresource_range.baseMipLevel = 0; | ||
| subresource_range.levelCount = 1; | ||
| subresource_range.baseArrayLayer = 0; | ||
| subresource_range.layerCount = 1; | ||
|
|
||
| vkb::image_layout_transition(cmd, | ||
| swapchain_buffers[image_index].image, | ||
| VK_IMAGE_LAYOUT_UNDEFINED, | ||
| VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | ||
| subresource_range); | ||
|
|
||
| // Note: UI rendering requires an active render pass. This minimal sample | ||
| // does not start one, so we intentionally skip draw_ui(cmd) to avoid draw calls | ||
| // outside a render pass. | ||
| VK_CHECK(vkEndCommandBuffer(cmd)); | ||
| } | ||
|
|
||
| void LayerSettingsSample::render(float /*delta_time*/) | ||
| { | ||
| if (!prepared) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| prepare_frame(); | ||
|
|
||
| // Recreate and record the command buffer for the current swapchain image | ||
| recreate_current_command_buffer(); | ||
| auto &cmd = draw_cmd_buffers[current_buffer]; | ||
| record_minimal_present_cmd(cmd, current_buffer); | ||
|
|
||
| // Submit: wait on the acquire semaphore and signal render_complete for present | ||
| VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; | ||
|
|
||
| VkSubmitInfo submit_info{}; | ||
| submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; | ||
| submit_info.waitSemaphoreCount = 1; | ||
| submit_info.pWaitSemaphores = &semaphores.acquired_image_ready; | ||
| submit_info.pWaitDstStageMask = &wait_stage; | ||
| submit_info.commandBufferCount = 1; | ||
| submit_info.pCommandBuffers = &cmd; | ||
| submit_info.signalSemaphoreCount = 1; | ||
| submit_info.pSignalSemaphores = &semaphores.render_complete; | ||
|
|
||
| VK_CHECK(vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE)); | ||
|
|
||
| submit_frame(); | ||
| } | ||
|
|
||
| std::unique_ptr<vkb::Application> create_layer_settings() | ||
| { | ||
| return std::make_unique<LayerSettingsSample>(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.