Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Jeremy Gebben | 50edce6 | 2022-01-03 14:54:40 -0700 | [diff] [blame] | 3 | * Copyright 2014-2022 Valve Software |
| 4 | * Copyright 2015-2022 Google Inc. |
| 5 | * Copyright 2019-2022 LunarG, Inc. |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 6 | * All Rights Reserved. |
| 7 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 11 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 13 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 19 | * |
Jon Ashburn | e922f71 | 2015-11-03 13:41:23 -0700 | [diff] [blame] | 20 | * Author: Jon Ashburn <jon@lunarg.com> |
| 21 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 22 | * Author: Tobin Ehlis <tobin@lunarg.com> |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 23 | * Author: Mark Lobodzinski <mark@lunarg.com> |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 24 | **************************************************************************/ |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 25 | #include "vk_layer_config.h" |
Petr Kraus | 6c4bdce | 2019-08-27 17:35:01 +0200 | [diff] [blame] | 26 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 27 | #include <string.h> |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 28 | #include <fstream> |
| 29 | #include <iostream> |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 30 | #include <sstream> |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 31 | #include <string> |
| 32 | #include <sys/stat.h> |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 33 | |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 34 | #include <vulkan/vk_layer.h> |
Mark Lobodzinski | 103c58a | 2020-01-29 13:20:00 -0700 | [diff] [blame] | 35 | // sdk_platform header redefines NOMINMAX |
| 36 | #undef NOMINMAX |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 37 | #include <vulkan/vk_sdk_platform.h> |
| 38 | #include "vk_layer_utils.h" |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 39 | |
Mark Lobodzinski | d5aa8de | 2018-02-02 13:58:41 -0700 | [diff] [blame] | 40 | #if defined(_WIN32) |
Martin Herkt | 094130b | 2018-03-06 22:44:13 +0100 | [diff] [blame] | 41 | #include <windows.h> |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 42 | #include <direct.h> |
| 43 | #define GetCurrentDir _getcwd |
| 44 | #else |
| 45 | #include <unistd.h> |
| 46 | #define GetCurrentDir getcwd |
Mark Lobodzinski | d5aa8de | 2018-02-02 13:58:41 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 49 | using std::string; |
| 50 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 51 | class ConfigFile { |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 52 | public: |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 53 | ConfigFile(); |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 54 | ~ConfigFile(){}; |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 55 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 56 | const char *GetOption(const string &option); |
| 57 | void SetOption(const string &option, const string &value); |
| 58 | string vk_layer_disables_env_var; |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 59 | SettingsFileInfo settings_info{}; |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 60 | |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 61 | private: |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 62 | bool file_is_parsed_; |
| 63 | std::map<string, string> value_map_; |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 64 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 65 | string FindSettings(); |
| 66 | void ParseFile(const char *filename); |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 69 | static ConfigFile layer_config; |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 70 | |
Jeremy Gebben | 427d397 | 2022-07-14 13:48:08 -0600 | [diff] [blame] | 71 | VK_LAYER_EXPORT std::string GetEnvironment(const char *variable) { |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 72 | #if !defined(__ANDROID__) && !defined(_WIN32) |
| 73 | const char *output = getenv(variable); |
| 74 | return output == NULL ? "" : output; |
| 75 | #elif defined(_WIN32) |
| 76 | int size = GetEnvironmentVariable(variable, NULL, 0); |
| 77 | if (size == 0) { |
| 78 | return ""; |
| 79 | } |
| 80 | char *buffer = new char[size]; |
| 81 | GetEnvironmentVariable(variable, buffer, size); |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 82 | string output = buffer; |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 83 | delete[] buffer; |
| 84 | return output; |
sfricke-samsung | 98acb88 | 2020-11-03 19:16:10 -0800 | [diff] [blame] | 85 | #elif defined(__ANDROID__) |
| 86 | string command = "getprop " + string(variable); |
| 87 | FILE *pPipe = popen(command.c_str(), "r"); |
| 88 | if (pPipe != nullptr) { |
| 89 | char value[256]; |
| 90 | fgets(value, 256, pPipe); |
| 91 | pclose(pPipe); |
| 92 | |
Nathaniel Cesario | f6b4a3d | 2021-04-22 09:36:14 -0600 | [diff] [blame] | 93 | // Make sure its not an empty line and does not end in a newline |
| 94 | const auto str_len = strcspn(value, "\r\n"); |
| 95 | if (str_len == 0) { |
sfricke-samsung | 98acb88 | 2020-11-03 19:16:10 -0800 | [diff] [blame] | 96 | return ""; |
| 97 | } else { |
Nathaniel Cesario | f6b4a3d | 2021-04-22 09:36:14 -0600 | [diff] [blame] | 98 | value[str_len] = '\0'; |
sfricke-samsung | 98acb88 | 2020-11-03 19:16:10 -0800 | [diff] [blame] | 99 | return string(value); |
| 100 | } |
| 101 | } else { |
| 102 | return ""; |
| 103 | } |
Lenny Komow | 807ebf0 | 2016-09-30 14:15:25 -0600 | [diff] [blame] | 104 | #else |
| 105 | return ""; |
| 106 | #endif |
| 107 | } |
| 108 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 109 | VK_LAYER_EXPORT const char *getLayerOption(const char *option) { return layer_config.GetOption(option); } |
| 110 | VK_LAYER_EXPORT const char *GetLayerEnvVar(const char *option) { |
Jeremy Gebben | 427d397 | 2022-07-14 13:48:08 -0600 | [diff] [blame] | 111 | // NOTE: new code should use GetEnvironment directly. This is a workaround for the problem |
| 112 | // described in https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/3048 |
| 113 | static std::string result; |
| 114 | result = GetEnvironment(option); |
| 115 | return result.c_str(); |
Mark Lobodzinski | 2af57ec | 2019-03-20 09:17:56 -0600 | [diff] [blame] | 116 | } |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 117 | |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 118 | VK_LAYER_EXPORT const SettingsFileInfo *GetLayerSettingsFileInfo() { return &layer_config.settings_info; } |
| 119 | |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 120 | // If option is NULL or stdout, return stdout, otherwise try to open option |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 121 | // as a filename. If successful, return file handle, otherwise stdout |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 122 | VK_LAYER_EXPORT FILE *getLayerLogOutput(const char *option, const char *layer_name) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 123 | FILE *log_output = NULL; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 124 | if (!option || !strcmp("stdout", option)) { |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 125 | log_output = stdout; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 126 | } else { |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 127 | log_output = fopen(option, "w"); |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 128 | if (log_output == NULL) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 129 | if (option) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 130 | std::cout << std::endl |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 131 | << layer_name << " ERROR: Bad output filename specified: " << option << ". Writing to STDOUT instead" |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 132 | << std::endl |
| 133 | << std::endl; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 134 | } |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 135 | log_output = stdout; |
| 136 | } |
| 137 | } |
Cody Northrop | e3e39ef | 2015-09-16 08:35:29 -0600 | [diff] [blame] | 138 | return log_output; |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 139 | } |
| 140 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 141 | // Map option strings to flag enum values |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 142 | VK_LAYER_EXPORT VkFlags GetLayerOptionFlags(string option, layer_data::unordered_map<string, VkFlags> const &enum_data, |
Mark Lobodzinski | 07954a5 | 2018-02-01 12:14:30 -0700 | [diff] [blame] | 143 | uint32_t option_default) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 144 | VkDebugReportFlagsEXT flags = option_default; |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 145 | string option_list = layer_config.GetOption(option.c_str()); |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 146 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 147 | while (option_list.length() != 0) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 148 | // Find length of option string |
| 149 | std::size_t option_length = option_list.find(","); |
| 150 | if (option_length == option_list.npos) { |
| 151 | option_length = option_list.size(); |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 152 | } |
| 153 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 154 | // Get first option item in list |
| 155 | const string option_item = option_list.substr(0, option_length); |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 156 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 157 | auto enum_value = enum_data.find(option_item); |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 158 | if (enum_value != enum_data.end()) { |
| 159 | flags |= enum_value->second; |
| 160 | } |
| 161 | |
| 162 | // Remove first option from option_list |
| 163 | option_list.erase(0, option_length); |
| 164 | // Remove possible comma separator |
| 165 | std::size_t char_position = option_list.find(","); |
| 166 | if (char_position == 0) { |
| 167 | option_list.erase(char_position, 1); |
| 168 | } |
| 169 | // Remove possible space |
| 170 | char_position = option_list.find(" "); |
| 171 | if (char_position == 0) { |
| 172 | option_list.erase(char_position, 1); |
| 173 | } |
Courtney Goeltzenleuchter | b874b8c | 2015-06-14 11:34:49 -0600 | [diff] [blame] | 174 | } |
| 175 | return flags; |
| 176 | } |
| 177 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 178 | VK_LAYER_EXPORT void setLayerOption(const char *option, const char *value) { layer_config.SetOption(option, value); } |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 179 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 180 | // Constructor for ConfigFile. Initialize layers to log error messages to stdout by default. If a vk_layer_settings file is present, |
| 181 | // its settings will override the defaults. |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 182 | ConfigFile::ConfigFile() : file_is_parsed_(false) { |
| 183 | value_map_["khronos_validation.report_flags"] = "error"; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 184 | |
| 185 | #ifdef WIN32 |
| 186 | // For Windows, enable message logging AND OutputDebugString |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 187 | value_map_["khronos_validation.debug_action"] = |
Mark Lobodzinski | 13500d3 | 2019-03-20 10:06:09 -0600 | [diff] [blame] | 188 | "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG,VK_DBG_LAYER_ACTION_DEBUG_OUTPUT"; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 189 | #else // WIN32 |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 190 | value_map_["khronos_validation.debug_action"] = "VK_DBG_LAYER_ACTION_DEFAULT,VK_DBG_LAYER_ACTION_LOG_MSG"; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 191 | #endif // WIN32 |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 192 | value_map_["khronos_validation.log_filename"] = "stdout"; |
Jeremy Gebben | 81d9b13 | 2022-05-04 08:54:37 -0600 | [diff] [blame] | 193 | value_map_["khronos_validation.fine_grained_locking"] = "true"; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 194 | } |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 195 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 196 | const char *ConfigFile::GetOption(const string &option) { |
| 197 | std::map<string, string>::const_iterator it; |
| 198 | if (!file_is_parsed_) { |
| 199 | string settings_file = FindSettings(); |
| 200 | ParseFile(settings_file.c_str()); |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 203 | if ((it = value_map_.find(option)) == value_map_.end()) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 204 | return ""; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 205 | } else { |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 206 | return it->second.c_str(); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 207 | } |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 210 | void ConfigFile::SetOption(const string &option, const string &val) { |
| 211 | if (!file_is_parsed_) { |
| 212 | string settings_file = FindSettings(); |
| 213 | ParseFile(settings_file.c_str()); |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 216 | value_map_[option] = val; |
Jon Ashburn | dec6051 | 2015-01-13 17:24:01 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Dorian Apanel | fcfe5a3 | 2020-08-09 11:19:50 +0200 | [diff] [blame] | 219 | #if defined(WIN32) |
| 220 | // Check for admin rights |
| 221 | static inline bool IsHighIntegrity() { |
| 222 | HANDLE process_token; |
| 223 | if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_QUERY_SOURCE, &process_token)) { |
| 224 | // Maximum possible size of SID_AND_ATTRIBUTES is maximum size of a SID + size of attributes DWORD. |
| 225 | uint8_t mandatory_label_buffer[SECURITY_MAX_SID_SIZE + sizeof(DWORD)]; |
| 226 | DWORD buffer_size; |
| 227 | if (GetTokenInformation(process_token, TokenIntegrityLevel, mandatory_label_buffer, sizeof(mandatory_label_buffer), |
| 228 | &buffer_size) != 0) { |
| 229 | const TOKEN_MANDATORY_LABEL *mandatory_label = (const TOKEN_MANDATORY_LABEL *)mandatory_label_buffer; |
| 230 | const DWORD sub_authority_count = *GetSidSubAuthorityCount(mandatory_label->Label.Sid); |
| 231 | const DWORD integrity_level = *GetSidSubAuthority(mandatory_label->Label.Sid, sub_authority_count - 1); |
| 232 | |
| 233 | CloseHandle(process_token); |
| 234 | return integrity_level > SECURITY_MANDATORY_MEDIUM_RID; |
| 235 | } |
| 236 | |
| 237 | CloseHandle(process_token); |
| 238 | } |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | #endif |
| 243 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 244 | string ConfigFile::FindSettings() { |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 245 | struct stat info; |
| 246 | |
| 247 | #if defined(WIN32) |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 248 | // Look for VkConfig-specific settings location specified in the windows registry |
Dorian Apanel | fcfe5a3 | 2020-08-09 11:19:50 +0200 | [diff] [blame] | 249 | HKEY key; |
| 250 | |
| 251 | const std::array<HKEY, 2> hives = {DEFAULT_VK_REGISTRY_HIVE, SECONDARY_VK_REGISTRY_HIVE}; |
| 252 | const size_t hives_to_check_count = IsHighIntegrity() ? 1 : hives.size(); // Admin checks only the default hive |
| 253 | |
| 254 | for (size_t hive_index = 0; hive_index < hives_to_check_count; ++hive_index) { |
| 255 | LSTATUS err = RegOpenKeyEx(hives[hive_index], "Software\\Khronos\\Vulkan\\Settings", 0, KEY_READ, &key); |
| 256 | if (err == ERROR_SUCCESS) { |
| 257 | char name[2048]; |
| 258 | DWORD i = 0, name_size, type, value, value_size; |
| 259 | while (ERROR_SUCCESS == RegEnumValue(key, i++, name, &(name_size = sizeof(name)), nullptr, &type, |
| 260 | reinterpret_cast<LPBYTE>(&value), &(value_size = sizeof(value)))) { |
| 261 | // Check if the registry entry is a dword with a value of zero |
| 262 | if (type != REG_DWORD || value != 0) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | // Check if this actually points to a file |
| 267 | if ((stat(name, &info) != 0) || !(info.st_mode & S_IFREG)) { |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | // Use this file |
| 272 | RegCloseKey(key); |
| 273 | settings_info.source = kVkConfig; |
| 274 | settings_info.location = name; |
| 275 | return name; |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 276 | } |
| 277 | |
Dorian Apanel | fcfe5a3 | 2020-08-09 11:19:50 +0200 | [diff] [blame] | 278 | RegCloseKey(key); |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 279 | } |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 280 | } |
Dorian Apanel | fcfe5a3 | 2020-08-09 11:19:50 +0200 | [diff] [blame] | 281 | |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 282 | #else |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 283 | // Look for VkConfig-specific settings location specified in a specific spot in the linux settings store |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 284 | string search_path = GetEnvironment("XDG_DATA_HOME"); |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 285 | if (search_path == "") { |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 286 | search_path = GetEnvironment("HOME"); |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 287 | if (search_path != "") { |
| 288 | search_path += "/.local/share"; |
| 289 | } |
| 290 | } |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 291 | // Use the vk_layer_settings.txt file from here, if it is present |
| 292 | if (search_path != "") { |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 293 | string home_file = search_path + "/vulkan/settings.d/vk_layer_settings.txt"; |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 294 | if (stat(home_file.c_str(), &info) == 0) { |
| 295 | if (info.st_mode & S_IFREG) { |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 296 | settings_info.source = kVkConfig; |
| 297 | settings_info.location = home_file; |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 298 | return home_file; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | #endif |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 304 | // Look for an enviornment variable override for the settings file location |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 305 | string env_path = GetEnvironment("VK_LAYER_SETTINGS_PATH"); |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 306 | |
| 307 | // If the path exists use it, else use vk_layer_settings |
| 308 | if (stat(env_path.c_str(), &info) == 0) { |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 309 | // If this is a directory, append settings file name |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 310 | if (info.st_mode & S_IFDIR) { |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 311 | env_path.append("/vk_layer_settings.txt"); |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 312 | } |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 313 | settings_info.source = kEnvVar; |
| 314 | settings_info.location = env_path; |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 315 | return env_path; |
| 316 | } |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 317 | |
| 318 | // Default -- use the current working directory for the settings file location |
| 319 | settings_info.source = kLocal; |
| 320 | char buff[512]; |
| 321 | auto buf_ptr = GetCurrentDir(buff, 512); |
| 322 | if (buf_ptr) { |
| 323 | settings_info.location = buf_ptr; |
| 324 | settings_info.location.append("\\vk_layer_settings.txt"); |
| 325 | } |
Lenny Komow | 5c57fcb | 2018-10-02 09:18:51 -0600 | [diff] [blame] | 326 | return "vk_layer_settings.txt"; |
| 327 | } |
| 328 | |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 329 | void ConfigFile::ParseFile(const char *filename) { |
Petr Kraus | c53604b | 2019-09-06 00:31:48 +0200 | [diff] [blame] | 330 | file_is_parsed_ = true; |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 331 | |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 332 | // Extract option = value pairs from a file |
Petr Kraus | 168417e | 2019-09-07 16:45:40 +0200 | [diff] [blame] | 333 | std::ifstream file(filename); |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 334 | if (file.good()) { |
| 335 | settings_info.file_found = true; |
| 336 | for (string line; std::getline(file, line);) { |
| 337 | // discard comments, which start with '#' |
| 338 | const auto comments_pos = line.find_first_of('#'); |
| 339 | if (comments_pos != string::npos) line.erase(comments_pos); |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 340 | |
Mark Lobodzinski | f5b1715 | 2020-06-09 10:16:31 -0600 | [diff] [blame] | 341 | const auto value_pos = line.find_first_of('='); |
| 342 | if (value_pos != string::npos) { |
| 343 | const string option = string_trim(line.substr(0, value_pos)); |
| 344 | const string value = string_trim(line.substr(value_pos + 1)); |
| 345 | value_map_[option] = value; |
| 346 | } |
Ricardo Garcia | 5efc392 | 2019-11-21 16:43:27 +0100 | [diff] [blame] | 347 | } |
Jon Ashburn | be58264 | 2014-12-22 12:04:40 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 351 | VK_LAYER_EXPORT void PrintMessageFlags(VkFlags vk_flags, char *msg_flags) { |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 352 | bool separator = false; |
| 353 | |
| 354 | msg_flags[0] = 0; |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 355 | if (vk_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) { |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 356 | strcat(msg_flags, "DEBUG"); |
| 357 | separator = true; |
| 358 | } |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 359 | if (vk_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 360 | if (separator) strcat(msg_flags, ","); |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 361 | strcat(msg_flags, "INFO"); |
| 362 | separator = true; |
| 363 | } |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 364 | if (vk_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 365 | if (separator) strcat(msg_flags, ","); |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 366 | strcat(msg_flags, "WARN"); |
| 367 | separator = true; |
| 368 | } |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 369 | if (vk_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 370 | if (separator) strcat(msg_flags, ","); |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 371 | strcat(msg_flags, "PERF"); |
| 372 | separator = true; |
| 373 | } |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 374 | if (vk_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 375 | if (separator) strcat(msg_flags, ","); |
Courtney Goeltzenleuchter | f85e361 | 2015-06-14 11:33:06 -0600 | [diff] [blame] | 376 | strcat(msg_flags, "ERROR"); |
| 377 | } |
| 378 | } |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 379 | |
| 380 | VK_LAYER_EXPORT void PrintMessageSeverity(VkFlags vk_flags, char *msg_flags) { |
| 381 | bool separator = false; |
| 382 | |
| 383 | msg_flags[0] = 0; |
| 384 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { |
| 385 | strcat(msg_flags, "VERBOSE"); |
| 386 | separator = true; |
| 387 | } |
| 388 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { |
| 389 | if (separator) strcat(msg_flags, ","); |
| 390 | strcat(msg_flags, "INFO"); |
| 391 | separator = true; |
| 392 | } |
| 393 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { |
| 394 | if (separator) strcat(msg_flags, ","); |
| 395 | strcat(msg_flags, "WARN"); |
| 396 | separator = true; |
| 397 | } |
| 398 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { |
| 399 | if (separator) strcat(msg_flags, ","); |
| 400 | strcat(msg_flags, "ERROR"); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | VK_LAYER_EXPORT void PrintMessageType(VkFlags vk_flags, char *msg_flags) { |
| 405 | bool separator = false; |
| 406 | |
| 407 | msg_flags[0] = 0; |
| 408 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) { |
| 409 | strcat(msg_flags, "GEN"); |
| 410 | separator = true; |
| 411 | } |
| 412 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) { |
Petr Kraus | 6c4bdce | 2019-08-27 17:35:01 +0200 | [diff] [blame] | 413 | if (separator) strcat(msg_flags, ","); |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 414 | strcat(msg_flags, "SPEC"); |
| 415 | separator = true; |
| 416 | } |
| 417 | if (vk_flags & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) { |
| 418 | if (separator) strcat(msg_flags, ","); |
| 419 | strcat(msg_flags, "PERF"); |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
sfricke-samsung | 98acb88 | 2020-11-03 19:16:10 -0800 | [diff] [blame] | 422 | |
| 423 | // This catches before dlopen fails if the default Android-26 layers are being used and attempted to be ran on Android 25 or below |
| 424 | #if defined(__ANDROID__) |
| 425 | #include "android_ndk_types.h" // get AHB_VALIDATION_SUPPORT macro |
| 426 | void __attribute__((constructor)) CheckAndroidVersion(); |
| 427 | void CheckAndroidVersion() { |
| 428 | #ifdef AHB_VALIDATION_SUPPORT |
| 429 | string version_env = GetEnvironment("ro.build.version.sdk"); |
| 430 | int target_version = atoi(version_env.c_str()); |
| 431 | |
| 432 | // atoi returns 0 if GetEnvironment fails and don't want false positive errors |
| 433 | if ((target_version != 0) && (target_version < 26)) { |
| 434 | LOGCONSOLE( |
| 435 | "ERROR - Targeted Android version is %d and needs to be 26 or above. Please read " |
| 436 | "https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/master/BUILD.md for how to build the Validation Layers " |
| 437 | "for Android 25 and below", |
| 438 | target_version); |
| 439 | } |
| 440 | #endif // AHB_VALIDATION_SUPPORT |
| 441 | } |
| 442 | #endif // defined(__ANDROID__) |