Skip to content

Commit 7ccfcb7

Browse files
christophe-lunargjuan-lunarg
authored andcommitted
layers: Add layer settings library
1 parent 6ac5fcb commit 7ccfcb7

23 files changed

+168
-810
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ else()
6666
endif()
6767
endif()
6868

69+
find_package(VulkanUtilityLibraries CONFIG REQUIRED)
70+
6971
option(BUILD_WERROR "Treat compiler warnings as errors")
7072
if (BUILD_WERROR)
7173
add_compile_options("$<IF:$<CXX_COMPILER_ID:MSVC>,/WX,-Werror>")

layers/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ endif()
4747
foreach(extension_layer ${EXTENSION_LAYERS})
4848
target_include_directories(${extension_layer} PRIVATE .)
4949

50-
target_link_libraries(${extension_layer} PRIVATE VkExtLayer_utils)
50+
target_link_libraries(${extension_layer} PRIVATE VkExtLayer_utils Vulkan::LayerSettings)
5151

5252
if(MSVC)
5353
target_link_options(${extension_layer} PRIVATE /DEF:${CMAKE_CURRENT_SOURCE_DIR}/${extension_layer}.def)

layers/VkLayer_khronos_synchronization2.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33
;
4-
; Copyright (c) 2015-2021 The Khronos Group Inc.
5-
; Copyright (c) 2015-2021 Valve Corporation
6-
; Copyright (c) 2015-2021 LunarG, Inc.
4+
; Copyright (c) 2015-2023 The Khronos Group Inc.
5+
; Copyright (c) 2015-2023 Valve Corporation
6+
; Copyright (c) 2015-2023 LunarG, Inc.
77
; Copyright (c) 2020 Intel Corporation
88
;
99
; Licensed under the Apache License, Version 2.0 (the "License");

layers/VkLayer_khronos_timeline_semaphore.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33
;
4-
; Copyright (c) 2015-2019, 2021 The Khronos Group Inc.
5-
; Copyright (c) 2015-2019, 2021 Valve Corporation
6-
; Copyright (c) 2015-2019, 2021 LunarG, Inc.
4+
; Copyright (c) 2015-2023 The Khronos Group Inc.
5+
; Copyright (c) 2015-2023 Valve Corporation
6+
; Copyright (c) 2015-2023 LunarG, Inc.
77
; Copyright (c) 2020 Intel Corporation
88
;
99
; Licensed under the Apache License, Version 2.0 (the "License");

layers/decompression/decompression.cpp

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ static bool logging_enabled = false;
2828
}
2929

3030
#include <vulkan/vk_layer.h>
31+
#include <vulkan/layer/vk_layer_settings.hpp>
3132
#include "allocator.h"
3233
#include "log.h"
33-
#include "vk_layer_config.h"
3434
#include "vk_safe_struct.h"
3535
#include "vk_util.h"
3636
#include "decompression.h"
@@ -115,6 +115,10 @@ static const ByteCode kIndirectGInflateBytecode[] = {
115115
{(const uint8_t*)kIndirectGInflate64_HAVE_INT16_HAVE_INT64, sizeof(kIndirectGInflate64_HAVE_INT16_HAVE_INT64)},
116116
};
117117

118+
#define kLayerSettingsForceEnable "force_device"
119+
#define kLayerSettingsCustomSTypeInfo "custom_stype_list"
120+
#define kLayerSettingsLogging "logging"
121+
118122
// required by vk_safe_struct
119123
std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info{};
120124

@@ -130,65 +134,9 @@ static const VkLayerProperties kGlobalLayer = {
130134
static const VkExtensionProperties kDeviceExtension = {VK_NV_MEMORY_DECOMPRESSION_EXTENSION_NAME,
131135
VK_NV_MEMORY_DECOMPRESSION_SPEC_VERSION};
132136

133-
static const char* const kEnvarForceEnable =
134-
#if defined(__ANDROID__)
135-
"debug.vulkan.memory_decompression.force_enable";
136-
#else
137-
"VK_MEMORY_DECOMPRESSION_FORCE_ENABLE";
138-
#endif
139-
static const char* const kLayerSettingsForceEnable = "khronos_memory_decompression.force_enable";
140-
141-
static const char* const kEnvarLogging =
142-
#if defined(__ANDROID__)
143-
"debug.vulkan.memory_decompression.logging";
144-
#else
145-
"VK_MEMORY_DECOMPRESSION_LOGGING";
146-
#endif
147-
static const char* const kLayerSettingsLogging = "khronos_memory_decompression.logging";
148-
149137
static vl_concurrent_unordered_map<uintptr_t, std::shared_ptr<InstanceData>> instance_data_map;
150138
static vl_concurrent_unordered_map<uintptr_t, std::shared_ptr<DeviceData>> device_data_map;
151139

152-
static void string_tolower(std::string& s) {
153-
for (auto& c : s) {
154-
c = tolower(c);
155-
}
156-
}
157-
158-
static bool GetForceEnable() {
159-
bool result = false;
160-
std::string setting = GetEnvironment(kEnvarForceEnable);
161-
if (setting.empty()) {
162-
setting = GetLayerOption(kLayerSettingsForceEnable);
163-
}
164-
if (!setting.empty()) {
165-
string_tolower(setting);
166-
if (setting == "true") {
167-
result = true;
168-
} else {
169-
result = std::atoi(setting.c_str()) != 0;
170-
}
171-
}
172-
return result;
173-
}
174-
175-
static bool GetLoggingEnabled() {
176-
bool result = false;
177-
std::string setting = GetEnvironment(kEnvarLogging);
178-
if (setting.empty()) {
179-
setting = GetLayerOption(kLayerSettingsLogging);
180-
}
181-
if (!setting.empty()) {
182-
string_tolower(setting);
183-
if (setting == "true") {
184-
result = true;
185-
} else {
186-
result = std::atoi(setting.c_str()) != 0;
187-
}
188-
}
189-
return result;
190-
}
191-
192140
uintptr_t DispatchKey(const void* object) {
193141
auto tmp = reinterpret_cast<const struct VkLayerDispatchTable_* const*>(object);
194142
return reinterpret_cast<uintptr_t>(*tmp);
@@ -274,7 +222,7 @@ VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalD
274222

275223
#define INIT_HOOK(_vt, _inst, fn) _vt.fn = reinterpret_cast<PFN_vk##fn>(vtable.GetInstanceProcAddr(_inst, "vk" #fn))
276224
InstanceData::InstanceData(VkInstance inst, PFN_vkGetInstanceProcAddr gpa, const VkAllocationCallbacks* alloc)
277-
: instance(inst), api_version(0), force_enable(false), allocator(alloc) {
225+
: instance(inst), api_version(0), allocator(alloc) {
278226
vtable.GetInstanceProcAddr = gpa;
279227
INIT_HOOK(vtable, instance, CreateInstance);
280228
INIT_HOOK(vtable, instance, DestroyInstance);
@@ -297,18 +245,25 @@ static VkLayerInstanceCreateInfo* GetChainInfo(const VkInstanceCreateInfo* pCrea
297245
return chain_info;
298246
}
299247

300-
// Get all elements from a vkEnumerate*() lambda into a std::vector.
301-
template <typename T>
302-
VkResult EnumerateAll(std::vector<T>* vect, std::function<VkResult(uint32_t*, T*)> func) {
303-
VkResult result = VK_INCOMPLETE;
304-
do {
305-
uint32_t count = 0;
306-
result = func(&count, nullptr);
307-
ASSERT(result == VK_SUCCESS);
308-
vect->resize(count);
309-
result = func(&count, vect->data());
310-
} while (result == VK_INCOMPLETE);
311-
return result;
248+
void InitLayerSettings(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, LayerSettings* layer_settings) {
249+
assert(layer_settings != nullptr);
250+
251+
VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE;
252+
vlCreateLayerSettingSet(kGlobalLayer.layerName, vlFindLayerSettingsCreateInfo(pCreateInfo), pAllocator, nullptr, &layerSettingSet);
253+
254+
if (vlHasLayerSetting(layerSettingSet, kLayerSettingsForceEnable)) {
255+
vlGetLayerSettingValue(layerSettingSet, kLayerSettingsForceEnable, layer_settings->force_enable);
256+
}
257+
258+
if (vlHasLayerSetting(layerSettingSet, kLayerSettingsLogging)) {
259+
vlGetLayerSettingValue(layerSettingSet, kLayerSettingsLogging, layer_settings->logging);
260+
}
261+
262+
if (vlHasLayerSetting(layerSettingSet, kLayerSettingsCustomSTypeInfo)) {
263+
vlGetLayerSettingValues(layerSettingSet, kLayerSettingsCustomSTypeInfo, custom_stype_info);
264+
}
265+
266+
vlDestroyLayerSettingSet(layerSettingSet, pAllocator);
312267
}
313268

314269
VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
@@ -322,8 +277,6 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo* pCreat
322277
return VK_ERROR_INITIALIZATION_FAILED;
323278
}
324279

325-
logging_enabled = GetLoggingEnabled();
326-
327280
// Advance the link info for the next element on the chain
328281
chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
329282

@@ -337,8 +290,10 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo* pCreat
337290

338291
instance_data_map.insert(DispatchKey(*pInstance), instance_data);
339292

340-
instance_data->force_enable = GetForceEnable();
341293
instance_data->api_version = pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : 0;
294+
295+
InitLayerSettings(pCreateInfo, pAllocator, &instance_data->layer_settings);
296+
logging_enabled = instance_data->layer_settings.logging;
342297
} catch (const std::bad_alloc&) {
343298
auto destroy_instance = reinterpret_cast<PFN_vkDestroyInstance>(gpa(NULL, "vkDestroyInstance"));
344299
destroy_instance(*pInstance, pAllocator);
@@ -749,7 +704,7 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevice, con
749704
PRINT("Memory decompression feature not available in the driver, enabling decompression layer.\n");
750705
enable_layer = true;
751706
} else {
752-
if (instance_data->force_enable) {
707+
if (instance_data->layer_settings.force_enable) {
753708
PRINT("Memory decompression feature available in the driver, but force enabling decompression layer.\n");
754709
enable_layer = true;
755710
} else {

layers/decompression/decompression.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ enum class SynchronizationScope { kFirst, kSecond };
3636
struct DeviceData;
3737
struct DeviceFeatures;
3838

39+
struct LayerSettings {
40+
bool force_enable{false};
41+
bool logging{false};
42+
};
43+
3944
template <typename T>
4045
using CmdVector = std::vector<T, extension_layer::CmdAlloc<T>>;
4146

@@ -74,7 +79,7 @@ struct InstanceData {
7479

7580
VkInstance instance;
7681
uint32_t api_version;
77-
bool force_enable;
82+
LayerSettings layer_settings;
7883
const VkAllocationCallbacks* allocator;
7984
struct InstanceDispatchTable {
8085
DECLARE_HOOK(GetInstanceProcAddr);

layers/shader_object.cpp

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232

3333
#include <vulkan/vulkan.h>
3434
#include <vulkan/vk_layer.h>
35+
#include <vulkan/layer/vk_layer_settings.hpp>
3536

3637
#include "log.h"
3738
#include "vk_safe_struct.h"
3839
#include "vk_api_hash.h"
39-
#include "vk_layer_config.h"
40+
41+
#define kLayerSettingsForceEnable "force_enable"
42+
#define kLayerSettingsCustomSTypeInfo "custom_stype_list"
4043

4144
// Required by vk_safe_struct
4245
std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info{};
@@ -80,37 +83,6 @@ namespace shader_object {
8083
static const char* kLayerName = "VK_LAYER_KHRONOS_shader_object";
8184
static const VkExtensionProperties kExtensionProperties = {VK_EXT_SHADER_OBJECT_EXTENSION_NAME, VK_EXT_SHADER_OBJECT_SPEC_VERSION};
8285

83-
static const char* const kEnvarForceEnable =
84-
#if defined(__ANDROID__)
85-
"debug.vulkan.shader_object.force_enable";
86-
#else
87-
"VK_SHADER_OBJECT_FORCE_ENABLE";
88-
#endif
89-
static const char* const kLayerSettingsForceEnable = "khronos_shader_object.force_enable";
90-
91-
static void string_tolower(std::string &s) {
92-
for (auto& c: s) {
93-
c = tolower(c);
94-
}
95-
}
96-
97-
static bool GetForceEnable() {
98-
bool result = false;
99-
std::string setting = GetEnvironment(kEnvarForceEnable);
100-
if (setting.empty()) {
101-
setting = GetLayerOption(kLayerSettingsForceEnable);
102-
}
103-
if (!setting.empty()) {
104-
string_tolower(setting);
105-
if (setting == "true") {
106-
result = true;
107-
} else {
108-
result = std::atoi(setting.c_str()) != 0;
109-
}
110-
}
111-
return result;
112-
}
113-
11486
class AlignedMemory {
11587
public:
11688
AlignedMemory() : size_(0), max_alignment_(1), memory_write_ptr_(nullptr) {}
@@ -1219,11 +1191,16 @@ class CommandBufferData {
12191191
FullDrawStateData* draw_state_data_;
12201192
};
12211193

1194+
struct LayerSettings {
1195+
bool force_enable{false};
1196+
};
1197+
12221198
struct InstanceData {
12231199
LayerDispatchInstance vtable;
12241200
VkInstance instance;
12251201
VkPhysicalDevice* physical_devices;
12261202
uint32_t physical_device_count;
1203+
LayerSettings layer_settings;
12271204
};
12281205

12291206
struct PhysicalDeviceData {
@@ -2610,6 +2587,24 @@ static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance i
26102587

26112588
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char* pName);
26122589

2590+
void InitLayerSettings(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, LayerSettings* layer_settings) {
2591+
assert(layer_settings != nullptr);
2592+
2593+
VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE;
2594+
const VkLayerSettingsCreateInfoEXT *pLayerSettingsCreateInfo = vlFindLayerSettingsCreateInfo(pCreateInfo);
2595+
vlCreateLayerSettingSet(kLayerName, pLayerSettingsCreateInfo, pAllocator, nullptr, &layerSettingSet);
2596+
2597+
if (vlHasLayerSetting(layerSettingSet, kLayerSettingsForceEnable)) {
2598+
vlGetLayerSettingValue(layerSettingSet, kLayerSettingsForceEnable, layer_settings->force_enable);
2599+
}
2600+
2601+
if (vlHasLayerSetting(layerSettingSet, kLayerSettingsCustomSTypeInfo)) {
2602+
vlGetLayerSettingValues(layerSettingSet, kLayerSettingsCustomSTypeInfo, custom_stype_info);
2603+
}
2604+
2605+
vlDestroyLayerSettingSet(layerSettingSet, pAllocator);
2606+
}
2607+
26132608
static VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
26142609
const VkAllocationCallbacks* pAllocator, VkInstance* pInstance) {
26152610
auto allocator = pAllocator ? *pAllocator : kDefaultAllocator;
@@ -2653,6 +2648,8 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo*
26532648
instance_data->physical_device_count = physical_device_count;
26542649
instance_data->vtable.Initialize(*pInstance, fpGetInstanceProcAddr);
26552650

2651+
InitLayerSettings(pCreateInfo, pAllocator, &instance_data->layer_settings);
2652+
26562653
result = fpEnumeratePhysicalDevices(*pInstance, &instance_data->physical_device_count, instance_data->physical_devices);
26572654
ASSERT(result == VK_SUCCESS);
26582655

@@ -2917,7 +2914,7 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physicalDevi
29172914
// only enable the layer if the application asked for the extension and feature AND the driver does not have a native
29182915
// implementation (unless if the user specified to ignore the native implementation via environment variable)
29192916
bool enable_layer = shader_object_feature_requested && shader_object_extension_requested;
2920-
bool ignore_native_implementation = GetForceEnable();
2917+
bool ignore_native_implementation = instance_data->layer_settings.force_enable;
29212918
if (ignore_native_implementation) {
29222919
DEBUG_LOG("ignoring native driver implementation of shader object\n");
29232920
}

0 commit comments

Comments
 (0)