Skip to content

Commit

Permalink
layer: Add minimum reflection on layer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe-lunarg committed Aug 15, 2023
1 parent 79d5fb1 commit e36ed76
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 79 deletions.
17 changes: 5 additions & 12 deletions include/vulkan/layer/vk_layer_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ extern "C" {

#include "vk_layer_settings_ext.h"

VK_DEFINE_HANDLE(VlLayerSettingSet)

typedef void *(*VL_LAYER_SETTING_LOG_CALLBACK)(const char *pSettingName, const char *pMessage);

// Create a layer setting set. If 'pCallback' is set to NULL, the messages are outputed to stderr.
VkResult vlCreateLayerSettingSet(const char *pLayerName, const VkLayerSettingsCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VL_LAYER_SETTING_LOG_CALLBACK pCallback, VlLayerSettingSet *pLayerSettingSet);

void vlDestroyLayerSettingSet(VlLayerSettingSet layerSettingSet, const VkAllocationCallbacks *pAllocator);
// Configure layer settings. If 'pCallback' is set to NULL, the messages are outputed to stderr.
VkResult vlRegisterLayerSettings(VkInstance instance, const char *pLayerName,
uint32_t settingCount, VkLayerSettingPropertiesEXT *pSettings, const VkAllocationCallbacks *pAllocator);

// Check whether a setting was set either programmatically, from vk_layer_settings.txt or an environment variable
VkBool32 vlHasLayerSetting(VlLayerSettingSet layerSettingSet, const char *pSettingName);
VkBool32 vlHasLayerSetting(VkInstance instance, const char *pLayerName, const char *pSettingName);

// Query setting values
VkResult vlGetLayerSettingValues(VlLayerSettingSet layerSettingSet, const char *pSettingName, VkLayerSettingTypeEXT type,
VkResult vlGetLayerSettingValues(VkInstance instance, const char *pLayerName, const char *pSettingName, VkLayerSettingTypeEXT type,
uint32_t *pValueCount, void *pValues);

const VkLayerSettingsCreateInfoEXT *vlFindLayerSettingsCreateInfo(const VkInstanceCreateInfo *pCreateInfo);
Expand Down
51 changes: 11 additions & 40 deletions include/vulkan/layer/vk_layer_settings_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,14 @@ extern "C" {

#include <vulkan/vk_layer.h>

// VK_EXT_layer_settings
//
// Name String
// VK_EXT_layer_settings
//
// Extension Type
// Instance extension
//
// Revision
// 1
//
// Extension and Version Dependencies
// Requires Vulkan 1.0
//
// Contact
// Christophe Riccio christophe-lunarg
//
// Contributors
// Christophe Riccio
// Mark Lobodzinski
//
// Description
// This extension provides a mechanism for configuring programmatically through
// the Vulkan API the behavior of layers.
//
// This extension provides the [VkLayerSettingsCreateInfoEXT] struct that can be
// included in the [pNext] chain of the [VkInstanceCreateInfo]
// structure passed as the [pCreateInfo] parameter of [vkCreateInstance].
//
// The structure contains an array of [VkLayerSettingEXT] structure
// values that configure specific features of layers.
//
// Note
// The [VK_EXT_layer_settings] extension subsumes all the functionality provided in the [VK_EXT_validation_flags] extension
// and the [VK_EXT_validation_features] extension.

#define VK_EXT_layer_settings 1
#define VK_EXT_LAYER_SETTINGS_SPEC_VERSION 1
#define VK_EXT_LAYER_SETTINGS_EXTENSION_NAME "VK_EXT_layer_settings"

// This extension is exclusively used by VVL, and is NOT intended as a deliverable.
// The value of the VK_STRUCTURE_TYPE is arbitrary. The only requirement,
// is that it must not conflict with existing sTypes.
//
// NOTE: VK_STRUCTURE_TYPE_MAX_ENUM - 1 is used by the intel driver.
// NOTE: VK_STRUCTURE_TYPE_MAX_ENUM - 42 is used by the validation layers
#define VK_STRUCTURE_TYPE_LAYER_SETTINGS_EXT ((VkStructureType)(VK_STRUCTURE_TYPE_MAX_ENUM - 43))
#define VK_STRUCTURE_TYPE_LAYER_SETTING_PROPERTIES_EXT ((VkStructureType)(VK_STRUCTURE_TYPE_MAX_ENUM - 44))

typedef enum VkLayerSettingTypeEXT {
VK_LAYER_SETTING_TYPE_BOOL32_EXT = 0,
Expand Down Expand Up @@ -96,6 +57,16 @@ typedef struct VkLayerSettingsCreateInfoEXT {
const VkLayerSettingEXT *pSettings;
} VkLayerSettingsCreateInfoEXT;

typedef struct VkLayerSettingPropertiesEXT {
VkStructureType sType;
const void *pNext;
char key[VK_MAX_DESCRIPTION_SIZE];
VkLayerSettingTypeEXT type;
} VkLayerSettingPropertiesEXT;

VkResult vkEnumerateInstanceLayerSettingsEXT(VkInstance instance, const char *pLayerName, uint32_t *pPropertyCount,
VkLayerSettingPropertiesEXT *pProperties);

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions src/layer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_compile_features(VulkanLayerSettings PRIVATE cxx_std_17)
target_sources(VulkanLayerSettings PRIVATE
vk_layer_settings.cpp
vk_layer_settings_helper.cpp
vk_layer_settings_interface.cpp
layer_settings_manager.cpp
layer_settings_manager.hpp
layer_settings_util.cpp
Expand Down
10 changes: 8 additions & 2 deletions src/layer/layer_settings_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ static void AddWorkaroundLayerNames(std::vector<std::string> &layer_names) {

namespace vl {

LayerSettings::LayerSettings(const char *pLayerName, const VkLayerSettingsCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VL_LAYER_SETTING_LOG_CALLBACK callback)
LayerSettings::LayerSettings(
const char *pLayerName,
uint32_t settingCount, VkLayerSettingPropertiesEXT *pSettings,
const VkLayerSettingsCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VL_LAYER_SETTING_LOG_CALLBACK callback)
: layer_name(pLayerName), create_info(pCreateInfo), callback(callback) {
(void)pAllocator;
assert(pLayerName != nullptr);

this->settings.resize(settingCount);
memcpy(&this->settings[0], pSettings, sizeof(VkLayerSettingPropertiesEXT) * settingCount);

std::string settings_file = this->FindSettingsFile();
this->ParseSettingsFile(settings_file.c_str());
}
Expand Down
11 changes: 7 additions & 4 deletions src/layer/layer_settings_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
namespace vl {
class LayerSettings {
public:
LayerSettings(const char *pLayerName, const VkLayerSettingsCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VL_LAYER_SETTING_LOG_CALLBACK callback);
LayerSettings(const char *pLayerName, uint32_t settingCount, VkLayerSettingPropertiesEXT *pSettings,
const VkAllocationCallbacks *pAllocator);
~LayerSettings();

bool HasEnvSetting(const char *pSettingName);
Expand All @@ -40,8 +40,13 @@ namespace vl {
std::vector<std::string> &GetSettingCache(const std::string &pSettingName);

private:
LayerSettings(const LayerSettings &) = delete;
LayerSettings& operator=(const LayerSettings &) = delete;

const VkLayerSettingEXT *FindLayerSettingValue(const char *pSettingName);

std::vector<VkLayerSettingPropertiesEXT> settings;

std::map<std::string, std::string> setting_file_values;
std::map<std::string, std::vector<std::string>> string_setting_cache;

Expand All @@ -52,8 +57,6 @@ namespace vl {
void ParseSettingsFile(const char *filename);

std::string layer_name;
const VkLayerSettingsCreateInfoEXT *create_info{nullptr};
VL_LAYER_SETTING_LOG_CALLBACK callback{nullptr};
};
}// namespace vl

1 change: 1 addition & 0 deletions src/layer/layer_settings_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// Author(s):
// - Christophe Riccio <christophe@lunarg.com>

#include "layer_settings_util.hpp"

#include <sstream>
Expand Down
39 changes: 18 additions & 21 deletions src/layer/vk_layer_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// Author(s):
// - Christophe Riccio <christophe@lunarg.com>

#include "vulkan/layer/vk_layer_settings.h"
#include "layer_settings_util.hpp"
#include "layer_settings_manager.hpp"
Expand All @@ -17,38 +18,34 @@
#include <cctype>
#include <cstring>
#include <cstdint>
#include <unordered_map>

static std::unordered_map<VkInstance, vl::LayerSettings> layer_setting_sets;

// This is used only for unit tests in test_layer_setting_file
void test_helper_SetLayerSetting(VlLayerSettingSet layerSettingSet, const char *pSettingName, const char *pValue) {
assert(layerSettingSet != VK_NULL_HANDLE);
void test_helper_SetLayerSetting(VkInstance instance, const char *pSettingName, const char *pValue) {
assert(instance != VK_NULL_HANDLE);
assert(pSettingName != nullptr);
assert(pValue != nullptr);

vl::LayerSettings *layer_setting_set = (vl::LayerSettings *)layerSettingSet;
vl::LayerSettings& layer_setting_set = ::layer_setting_sets[instance];

layer_setting_set->SetFileSetting(pSettingName, pValue);
layer_setting_set.SetFileSetting(pSettingName, pValue);
}

VkResult vlCreateLayerSettingSet(const char *pLayerName, const VkLayerSettingsCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VL_LAYER_SETTING_LOG_CALLBACK pCallback,
VlLayerSettingSet *pLayerSettingSet) {
(void)pAllocator;
VkResult vlRegisterLayerSettings(VkInstance instance, const char *pLayerName, uint32_t settingCount,
VkLayerSettingPropertiesEXT *pSettings, const VkAllocationCallbacks *pAllocator) {
vl::LayerSettings layer_settings(pLayerName, settingCount, pSettings, pAllocator);

vl::LayerSettings* layer_setting_set = new vl::LayerSettings(pLayerName, pCreateInfo, pAllocator, pCallback);
*pLayerSettingSet = (VlLayerSettingSet)layer_setting_set;
::layer_setting_sets

return VK_SUCCESS;
}

void vlDestroyLayerSettingSet(VlLayerSettingSet layerSettingSet, const VkAllocationCallbacks *pAllocator) {
(void)pAllocator;
::layer_setting_sets.insert(std::pair(instance, layer_settings));

vl::LayerSettings *layer_setting_set = (vl::LayerSettings*)layerSettingSet;
delete layer_setting_set;
return VK_SUCCESS;
}

VkBool32 vlHasLayerSetting(VlLayerSettingSet layerSettingSet, const char *pSettingName) {
assert(layerSettingSet != VK_NULL_HANDLE);
VkBool32 vlHasLayerSetting(VkInstance instance, const char *pLayerName, const char *pSettingName) {
assert(pSettingName);
assert(!std::string(pSettingName).empty());

Expand All @@ -61,15 +58,15 @@ VkBool32 vlHasLayerSetting(VlLayerSettingSet layerSettingSet, const char *pSetti
return (has_env_setting || has_file_setting || has_api_setting) ? VK_TRUE : VK_FALSE;
}

VkResult vlGetLayerSettingValues(VlLayerSettingSet layerSettingSet, const char *pSettingName, VkLayerSettingTypeEXT type,
VkResult vlGetLayerSettingValues(VkInstance instance, const char *pLayerName, const char *pSettingName, VkLayerSettingTypeEXT type,
uint32_t *pValueCount, void *pValues) {
assert(pValueCount != nullptr);

if (layerSettingSet == VK_NULL_HANDLE) {
if (instance == VK_NULL_HANDLE) {
return VK_ERROR_INITIALIZATION_FAILED;
}

if (!vlHasLayerSetting(layerSettingSet, pSettingName)) {
if (!vlHasLayerSetting(instance, pLayerName, pSettingName)) {
*pValueCount = 0;
return VK_SUCCESS;
}
Expand Down
1 change: 1 addition & 0 deletions src/layer/vk_layer_settings_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// Author(s):
// - Christophe Riccio <christophe@lunarg.com>

#include "vulkan/layer/vk_layer_settings.hpp"

static std::string Merge(const std::vector<std::string> &strings) {
Expand Down
43 changes: 43 additions & 0 deletions src/layer/vk_layer_settings_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Author(s):
// - Christophe Riccio <christophe@lunarg.com>

#include "vulkan/layer/vk_layer_settings_ext.h"
#include <cassert>
#include <cstring>

#if defined(__GNUC__) && __GNUC__ >= 4
#define LAYER_EXPORT __attribute__((visibility("default")))
#else
#define LAYER_EXPORT
#endif

// Keep synchronized with VkLayer_khronos_profiles.def / VkLayer_khronos_profiles.map
extern "C" {

LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerSettingsEXT(
VkInstance instance, const char* pLayerName,
uint32_t* pSettingCount,
VkLayerSettingPropertiesEXT* pSettings) {

assert(pSettingCount != nullptr);

if (strcmp(pLayerName, VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) != 0) {
return VK_SUCCESS;
}

if (*pSettingCount > 0 && pSettings != nullptr) {

} else {

}

return VK_SUCCESS;
}

} // extern "C"

0 comments on commit e36ed76

Please sign in to comment.