Skip to content

Commit 6d62664

Browse files
committed
Improve debug printf setup and handle missing validation layer gracefully
Switch to boolean `printf_enable` layer setting (removing string-based `enables`), fix indentation, and make validation layer optional in fallback path by checking availability before adding to enabled layers list.
1 parent 93dd405 commit 6d62664

2 files changed

Lines changed: 37 additions & 33 deletions

File tree

samples/extensions/shader_relaxed_extended_instruction/README.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ The feature is exposed via `VkPhysicalDeviceShaderRelaxedExtendedInstructionFeat
5757
- Instance extension for feature chaining: `VK_KHR_get_physical_device_properties2` (the framework enables this; the sample requests it explicitly).
5858
- Device feature (required): `VkPhysicalDeviceShaderRelaxedExtendedInstructionFeaturesKHR::shaderRelaxedExtendedInstruction = VK_TRUE`
5959

60+
NOTE: This sample relies on the Vulkan Validation Layers (VVL) to capture and display `debugPrintfEXT` output from the non‑semantic instruction set. Ensure that VVL is enabled when running the sample. Debug builds of Vulkan‑Samples automatically enable validation; on Release builds you may need to enable validation explicitly via your environment or runtime settings.
61+
6062
Code excerpt:
6163
[source,cpp]
6264
----

samples/extensions/shader_relaxed_extended_instruction/shader_relaxed_extended_instruction.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,22 @@ ShaderRelaxedExtendedInstruction::ShaderRelaxedExtendedInstruction()
4343

4444
// Instance prerequisite for feature chaining and layer settings (optional)
4545
add_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
46-
add_instance_extension(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME, /*optional*/ true);
4746

4847
// Device extensions used by this demo
4948
add_device_extension(VK_KHR_SHADER_RELAXED_EXTENDED_INSTRUCTION_EXTENSION_NAME);
5049
// Non-semantic info is the SPIR-V mechanism for non-semantic extended instruction sets
5150
add_device_extension(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME);
5251

53-
// Optionally, enable debug printf so shaders using debugPrintfEXT will print via VVL
52+
// Enable debug printf so shaders using debugPrintfEXT will print via VVL
5453
{
55-
static const char *enables[] = {
56-
"VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT",
57-
};
54+
static const VkBool32 printf_enable = VK_TRUE;
5855

5956
VkLayerSettingEXT layer_setting{};
6057
layer_setting.pLayerName = "VK_LAYER_KHRONOS_validation";
61-
layer_setting.pSettingName = "enables";
62-
layer_setting.type = VK_LAYER_SETTING_TYPE_STRING_EXT;
63-
layer_setting.valueCount = static_cast<uint32_t>(std::size(enables));
64-
layer_setting.pValues = enables;
58+
layer_setting.pSettingName = "printf_enable";
59+
layer_setting.type = VK_LAYER_SETTING_TYPE_BOOL32_EXT;
60+
layer_setting.valueCount = 1;
61+
layer_setting.pValues = &printf_enable;
6562
add_layer_setting(layer_setting);
6663
}
6764
}
@@ -142,25 +139,26 @@ std::unique_ptr<vkb::core::InstanceC> ShaderRelaxedExtendedInstruction::create_i
142139
return strcmp(p.layerName, validation_layer_name) == 0;
143140
});
144141

145-
if (vvl_properties != layer_properties.end())
146-
{
147-
// Does VVL advertise VK_EXT_layer_settings?
148-
uint32_t vvl_extension_count = 0;
149-
VK_CHECK(vkEnumerateInstanceExtensionProperties(validation_layer_name, &vvl_extension_count, nullptr));
150-
std::vector<VkExtensionProperties> vvl_instance_extensions(vvl_extension_count);
151-
VK_CHECK(vkEnumerateInstanceExtensionProperties(validation_layer_name, &vvl_extension_count, vvl_instance_extensions.data()));
152-
153-
bool has_layer_settings = std::ranges::any_of(vvl_instance_extensions, [](const VkExtensionProperties &e) {
154-
return strcmp(e.extensionName, VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) == 0;
155-
});
156-
157-
if (has_layer_settings)
158-
{
159-
set_api_version(debugprintf_api_version);
160-
// Use the base implementation which will chain our pre-added layer settings
161-
return VulkanSample::create_instance();
162-
}
163-
}
142+
const bool validation_layer_available = (vvl_properties != layer_properties.end());
143+
if (validation_layer_available)
144+
{
145+
// Does VVL advertise VK_EXT_layer_settings?
146+
uint32_t vvl_extension_count = 0;
147+
VK_CHECK(vkEnumerateInstanceExtensionProperties(validation_layer_name, &vvl_extension_count, nullptr));
148+
std::vector<VkExtensionProperties> vvl_instance_extensions(vvl_extension_count);
149+
VK_CHECK(vkEnumerateInstanceExtensionProperties(validation_layer_name, &vvl_extension_count, vvl_instance_extensions.data()));
150+
151+
bool has_layer_settings = std::ranges::any_of(vvl_instance_extensions, [](const VkExtensionProperties &e) {
152+
return strcmp(e.extensionName, VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) == 0;
153+
});
154+
155+
if (has_layer_settings)
156+
{
157+
set_api_version(debugprintf_api_version);
158+
// Use the base implementation which will chain our pre-added layer settings
159+
return VulkanSample::create_instance();
160+
}
161+
}
164162

165163
// Fallback: use VK_EXT_validation_features to enable debugPrintf without layer settings
166164
std::vector<const char *> enabled_extensions;
@@ -187,15 +185,19 @@ std::unique_ptr<vkb::core::InstanceC> ShaderRelaxedExtendedInstruction::create_i
187185
}
188186
#endif
189187

190-
// Enable validation features to activate debugPrintf
191-
enabled_extensions.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
188+
// Enable validation features to activate debugPrintf (works when validation layer is present)
189+
enabled_extensions.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
192190

193191
VkApplicationInfo app_info{VK_STRUCTURE_TYPE_APPLICATION_INFO};
194192
app_info.pApplicationName = "Shader relaxed extended instruction";
195193
app_info.pEngineName = "Vulkan Samples";
196194
app_info.apiVersion = debugprintf_api_version;
197195

198-
std::vector<const char *> validation_layers = {validation_layer_name};
196+
std::vector<const char *> validation_layers;
197+
if (validation_layer_available)
198+
{
199+
validation_layers.push_back(validation_layer_name);
200+
}
199201

200202
std::vector<VkValidationFeatureEnableEXT> validation_feature_enables = {VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT};
201203
VkValidationFeaturesEXT validation_features{VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT};
@@ -206,8 +208,8 @@ std::unique_ptr<vkb::core::InstanceC> ShaderRelaxedExtendedInstruction::create_i
206208
instance_create_info.ppEnabledExtensionNames = enabled_extensions.data();
207209
instance_create_info.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size());
208210
instance_create_info.pApplicationInfo = &app_info;
209-
instance_create_info.ppEnabledLayerNames = validation_layers.data();
210-
instance_create_info.enabledLayerCount = static_cast<uint32_t>(validation_layers.size());
211+
instance_create_info.ppEnabledLayerNames = validation_layers.empty() ? nullptr : validation_layers.data();
212+
instance_create_info.enabledLayerCount = static_cast<uint32_t>(validation_layers.size());
211213
#if (defined(VKB_ENABLE_PORTABILITY))
212214
if (portability_enumeration_available)
213215
{

0 commit comments

Comments
 (0)