@@ -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
119123std::vector<std::pair<uint32_t , uint32_t >> custom_stype_info{};
120124
@@ -130,65 +134,9 @@ static const VkLayerProperties kGlobalLayer = {
130134static 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-
149137static vl_concurrent_unordered_map<uintptr_t , std::shared_ptr<InstanceData>> instance_data_map;
150138static 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-
192140uintptr_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))
276224InstanceData::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
314269VKAPI_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 {
0 commit comments