Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // The API layer of the loader defines Vulkan API and manages layers. The |
| 18 | // entrypoints are generated and defined in api_dispatch.cpp. Most of them |
| 19 | // simply find the dispatch table and jump. |
| 20 | // |
| 21 | // There are a few of them requiring manual code for things such as layer |
| 22 | // discovery or chaining. They call into functions defined in this file. |
| 23 | |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 25 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <mutex> |
| 31 | #include <new> |
Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <unordered_set> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 34 | #include <utility> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 35 | |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 39 | #include <log/log.h> |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 41 | |
| 42 | #include <vulkan/vk_layer_interface.h> |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 43 | #include <graphicsenv/GraphicsEnv.h> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 44 | #include "api.h" |
| 45 | #include "driver.h" |
Chia-I Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 46 | #include "layers_extensions.h" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 47 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 48 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 49 | namespace vulkan { |
| 50 | namespace api { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | // Provide overridden layer names when there are implicit layers. No effect |
| 55 | // otherwise. |
| 56 | class OverrideLayerNames { |
| 57 | public: |
| 58 | OverrideLayerNames(bool is_instance, const VkAllocationCallbacks& allocator) |
| 59 | : is_instance_(is_instance), |
| 60 | allocator_(allocator), |
| 61 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), |
| 62 | names_(nullptr), |
| 63 | name_count_(0), |
| 64 | implicit_layers_() { |
| 65 | implicit_layers_.result = VK_SUCCESS; |
| 66 | } |
| 67 | |
| 68 | ~OverrideLayerNames() { |
| 69 | allocator_.pfnFree(allocator_.pUserData, names_); |
| 70 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.elements); |
| 71 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.name_pool); |
| 72 | } |
| 73 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 74 | VkResult Parse(const char* const* names, uint32_t count) { |
| 75 | AddImplicitLayers(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 76 | |
| 77 | const auto& arr = implicit_layers_; |
| 78 | if (arr.result != VK_SUCCESS) |
| 79 | return arr.result; |
| 80 | |
| 81 | // no need to override when there is no implicit layer |
| 82 | if (!arr.count) |
| 83 | return VK_SUCCESS; |
| 84 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 85 | names_ = AllocateNameArray(arr.count + count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 86 | if (!names_) |
| 87 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 88 | |
| 89 | // add implicit layer names |
| 90 | for (uint32_t i = 0; i < arr.count; i++) |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 91 | names_[i] = GetImplicitLayerName(i); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 92 | |
| 93 | name_count_ = arr.count; |
| 94 | |
| 95 | // add explicit layer names |
| 96 | for (uint32_t i = 0; i < count; i++) { |
| 97 | // ignore explicit layers that are also implicit |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 98 | if (IsImplicitLayer(names[i])) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 99 | continue; |
| 100 | |
| 101 | names_[name_count_++] = names[i]; |
| 102 | } |
| 103 | |
| 104 | return VK_SUCCESS; |
| 105 | } |
| 106 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 107 | const char* const* Names() const { return names_; } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 108 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 109 | uint32_t Count() const { return name_count_; } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | struct ImplicitLayer { |
| 113 | int priority; |
| 114 | size_t name_offset; |
| 115 | }; |
| 116 | |
| 117 | struct ImplicitLayerArray { |
| 118 | ImplicitLayer* elements; |
| 119 | uint32_t max_count; |
| 120 | uint32_t count; |
| 121 | |
| 122 | char* name_pool; |
| 123 | size_t max_pool_size; |
| 124 | size_t pool_size; |
| 125 | |
| 126 | VkResult result; |
| 127 | }; |
| 128 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 129 | void AddImplicitLayers() { |
Yiwei Zhang | 279df62 | 2020-02-18 10:45:15 -0800 | [diff] [blame] | 130 | if (!is_instance_) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 131 | return; |
| 132 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 133 | GetLayersFromSettings(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 134 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 135 | // If no layers specified via Settings, check legacy properties |
| 136 | if (implicit_layers_.count <= 0) { |
| 137 | ParseDebugVulkanLayers(); |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 138 | ParseDebugVulkanLayer(); |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 139 | |
| 140 | // sort by priorities |
| 141 | auto& arr = implicit_layers_; |
| 142 | std::sort(arr.elements, arr.elements + arr.count, |
| 143 | [](const ImplicitLayer& a, const ImplicitLayer& b) { |
| 144 | return (a.priority < b.priority); |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void GetLayersFromSettings() { |
Yiwei Zhang | 94b18d5 | 2019-11-07 17:12:11 -0800 | [diff] [blame] | 150 | // These will only be available if conditions are met in GraphicsEnvironment |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 151 | // gpu_debug_layers = layer1:layer2:layerN |
| 152 | const std::string layers = android::GraphicsEnv::getInstance().getDebugLayers(); |
| 153 | if (!layers.empty()) { |
| 154 | ALOGV("Debug layer list: %s", layers.c_str()); |
| 155 | std::vector<std::string> paths = android::base::Split(layers, ":"); |
| 156 | for (uint32_t i = 0; i < paths.size(); i++) { |
| 157 | AddImplicitLayer(int(i), paths[i].c_str(), paths[i].length()); |
| 158 | } |
| 159 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 162 | void ParseDebugVulkanLayers() { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 163 | // debug.vulkan.layers specifies colon-separated layer names |
| 164 | char prop[PROPERTY_VALUE_MAX]; |
| 165 | if (!property_get("debug.vulkan.layers", prop, "")) |
| 166 | return; |
| 167 | |
| 168 | // assign negative/high priorities to them |
| 169 | int prio = -PROPERTY_VALUE_MAX; |
| 170 | |
| 171 | const char* p = prop; |
| 172 | const char* delim; |
| 173 | while ((delim = strchr(p, ':'))) { |
| 174 | if (delim > p) |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 175 | AddImplicitLayer(prio, p, static_cast<size_t>(delim - p)); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 176 | |
| 177 | prio++; |
| 178 | p = delim + 1; |
| 179 | } |
| 180 | |
| 181 | if (p[0] != '\0') |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 182 | AddImplicitLayer(prio, p, strlen(p)); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 185 | void ParseDebugVulkanLayer() { |
| 186 | // Checks for consecutive debug.vulkan.layer.<priority> system |
| 187 | // properties after always checking an initial fixed range. |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 188 | static const char prefix[] = "debug.vulkan.layer."; |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 189 | static constexpr int kFixedRangeBeginInclusive = 0; |
| 190 | static constexpr int kFixedRangeEndInclusive = 9; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 191 | |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 192 | bool logged = false; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 193 | |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 194 | int priority = kFixedRangeBeginInclusive; |
| 195 | while (true) { |
| 196 | const std::string prop_key = |
| 197 | std::string(prefix) + std::to_string(priority); |
| 198 | const std::string prop_val = |
| 199 | android::base::GetProperty(prop_key, ""); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 200 | |
Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 201 | if (!prop_val.empty()) { |
| 202 | if (!logged) { |
| 203 | ALOGI( |
| 204 | "Detected Vulkan layers configured with " |
| 205 | "debug.vulkan.layer.<priority>. Checking for " |
| 206 | "debug.vulkan.layer.<priority> in the range [%d, %d] " |
| 207 | "followed by a consecutive scan.", |
| 208 | kFixedRangeBeginInclusive, kFixedRangeEndInclusive); |
| 209 | logged = true; |
| 210 | } |
| 211 | AddImplicitLayer(priority, prop_val.c_str(), prop_val.length()); |
| 212 | } else if (priority >= kFixedRangeEndInclusive) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | ++priority; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 217 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 220 | void AddImplicitLayer(int priority, const char* name, size_t len) { |
| 221 | if (!GrowImplicitLayerArray(1, 0)) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | auto& arr = implicit_layers_; |
| 225 | auto& layer = arr.elements[arr.count++]; |
| 226 | |
| 227 | layer.priority = priority; |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 228 | layer.name_offset = AddImplicitLayerName(name, len); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 229 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 230 | ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1)); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 233 | size_t AddImplicitLayerName(const char* name, size_t len) { |
| 234 | if (!GrowImplicitLayerArray(0, len + 1)) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 235 | return 0; |
| 236 | |
| 237 | // add the name to the pool |
| 238 | auto& arr = implicit_layers_; |
| 239 | size_t offset = arr.pool_size; |
| 240 | char* dst = arr.name_pool + offset; |
| 241 | |
| 242 | std::copy(name, name + len, dst); |
| 243 | dst[len] = '\0'; |
| 244 | |
| 245 | arr.pool_size += len + 1; |
| 246 | |
| 247 | return offset; |
| 248 | } |
| 249 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 250 | bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 251 | const uint32_t initial_max_count = 16; |
| 252 | const size_t initial_max_pool_size = 512; |
| 253 | |
| 254 | auto& arr = implicit_layers_; |
| 255 | |
| 256 | // grow the element array if needed |
| 257 | while (arr.count + layer_count > arr.max_count) { |
| 258 | uint32_t new_max_count = |
| 259 | (arr.max_count) ? (arr.max_count << 1) : initial_max_count; |
| 260 | void* new_mem = nullptr; |
| 261 | |
| 262 | if (new_max_count > arr.max_count) { |
| 263 | new_mem = allocator_.pfnReallocation( |
| 264 | allocator_.pUserData, arr.elements, |
| 265 | sizeof(ImplicitLayer) * new_max_count, |
| 266 | alignof(ImplicitLayer), scope_); |
| 267 | } |
| 268 | |
| 269 | if (!new_mem) { |
| 270 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; |
| 271 | arr.count = 0; |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem); |
| 276 | arr.max_count = new_max_count; |
| 277 | } |
| 278 | |
| 279 | // grow the name pool if needed |
| 280 | while (arr.pool_size + name_size > arr.max_pool_size) { |
| 281 | size_t new_max_pool_size = (arr.max_pool_size) |
| 282 | ? (arr.max_pool_size << 1) |
| 283 | : initial_max_pool_size; |
| 284 | void* new_mem = nullptr; |
| 285 | |
| 286 | if (new_max_pool_size > arr.max_pool_size) { |
| 287 | new_mem = allocator_.pfnReallocation( |
| 288 | allocator_.pUserData, arr.name_pool, new_max_pool_size, |
| 289 | alignof(char), scope_); |
| 290 | } |
| 291 | |
| 292 | if (!new_mem) { |
| 293 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; |
| 294 | arr.pool_size = 0; |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | arr.name_pool = reinterpret_cast<char*>(new_mem); |
| 299 | arr.max_pool_size = new_max_pool_size; |
| 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 305 | const char* GetImplicitLayerName(uint32_t index) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 306 | const auto& arr = implicit_layers_; |
| 307 | |
| 308 | // this may return nullptr when arr.result is not VK_SUCCESS |
| 309 | return implicit_layers_.name_pool + arr.elements[index].name_offset; |
| 310 | } |
| 311 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 312 | bool IsImplicitLayer(const char* name) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 313 | const auto& arr = implicit_layers_; |
| 314 | |
| 315 | for (uint32_t i = 0; i < arr.count; i++) { |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 316 | if (strcmp(name, GetImplicitLayerName(i)) == 0) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | return false; |
| 321 | } |
| 322 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 323 | const char** AllocateNameArray(uint32_t count) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 324 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( |
| 325 | allocator_.pUserData, sizeof(const char*) * count, |
| 326 | alignof(const char*), scope_)); |
| 327 | } |
| 328 | |
| 329 | const bool is_instance_; |
| 330 | const VkAllocationCallbacks& allocator_; |
| 331 | const VkSystemAllocationScope scope_; |
| 332 | |
| 333 | const char** names_; |
| 334 | uint32_t name_count_; |
| 335 | |
| 336 | ImplicitLayerArray implicit_layers_; |
| 337 | }; |
| 338 | |
| 339 | // Provide overridden extension names when there are implicit extensions. |
| 340 | // No effect otherwise. |
| 341 | // |
| 342 | // This is used only to enable VK_EXT_debug_report. |
| 343 | class OverrideExtensionNames { |
| 344 | public: |
| 345 | OverrideExtensionNames(bool is_instance, |
| 346 | const VkAllocationCallbacks& allocator) |
| 347 | : is_instance_(is_instance), |
| 348 | allocator_(allocator), |
| 349 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), |
| 350 | names_(nullptr), |
| 351 | name_count_(0), |
| 352 | install_debug_callback_(false) {} |
| 353 | |
| 354 | ~OverrideExtensionNames() { |
| 355 | allocator_.pfnFree(allocator_.pUserData, names_); |
| 356 | } |
| 357 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 358 | VkResult Parse(const char* const* names, uint32_t count) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 359 | // this is only for debug.vulkan.enable_callback |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 360 | if (!EnableDebugCallback()) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 361 | return VK_SUCCESS; |
| 362 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 363 | names_ = AllocateNameArray(count + 1); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 364 | if (!names_) |
| 365 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 366 | |
| 367 | std::copy(names, names + count, names_); |
| 368 | |
| 369 | name_count_ = count; |
| 370 | names_[name_count_++] = "VK_EXT_debug_report"; |
| 371 | |
| 372 | install_debug_callback_ = true; |
| 373 | |
| 374 | return VK_SUCCESS; |
| 375 | } |
| 376 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 377 | const char* const* Names() const { return names_; } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 378 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 379 | uint32_t Count() const { return name_count_; } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 380 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 381 | bool InstallDebugCallback() const { return install_debug_callback_; } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 382 | |
| 383 | private: |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 384 | bool EnableDebugCallback() const { |
Yiwei Zhang | 6a674c9 | 2019-11-08 11:55:36 -0800 | [diff] [blame] | 385 | return (is_instance_ && |
| 386 | android::GraphicsEnv::getInstance().isDebuggable() && |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 387 | property_get_bool("debug.vulkan.enable_callback", false)); |
| 388 | } |
| 389 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 390 | const char** AllocateNameArray(uint32_t count) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 391 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( |
| 392 | allocator_.pUserData, sizeof(const char*) * count, |
| 393 | alignof(const char*), scope_)); |
| 394 | } |
| 395 | |
| 396 | const bool is_instance_; |
| 397 | const VkAllocationCallbacks& allocator_; |
| 398 | const VkSystemAllocationScope scope_; |
| 399 | |
| 400 | const char** names_; |
| 401 | uint32_t name_count_; |
| 402 | bool install_debug_callback_; |
| 403 | }; |
| 404 | |
| 405 | // vkCreateInstance and vkCreateDevice helpers with support for layer |
| 406 | // chaining. |
| 407 | class LayerChain { |
| 408 | public: |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 409 | struct ActiveLayer { |
| 410 | LayerRef ref; |
| 411 | union { |
| 412 | VkLayerInstanceLink instance_link; |
| 413 | VkLayerDeviceLink device_link; |
| 414 | }; |
| 415 | }; |
| 416 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 417 | static VkResult CreateInstance(const VkInstanceCreateInfo* create_info, |
| 418 | const VkAllocationCallbacks* allocator, |
| 419 | VkInstance* instance_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 420 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 421 | static VkResult CreateDevice(VkPhysicalDevice physical_dev, |
| 422 | const VkDeviceCreateInfo* create_info, |
| 423 | const VkAllocationCallbacks* allocator, |
| 424 | VkDevice* dev_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 425 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 426 | static void DestroyInstance(VkInstance instance, |
| 427 | const VkAllocationCallbacks* allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 428 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 429 | static void DestroyDevice(VkDevice dev, |
| 430 | const VkAllocationCallbacks* allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 431 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 432 | static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev, |
| 433 | uint32_t& count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 434 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 435 | private: |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 436 | LayerChain(bool is_instance, |
| 437 | const driver::DebugReportLogger& logger, |
| 438 | const VkAllocationCallbacks& allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 439 | ~LayerChain(); |
| 440 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 441 | VkResult ActivateLayers(const char* const* layer_names, |
| 442 | uint32_t layer_count, |
| 443 | const char* const* extension_names, |
| 444 | uint32_t extension_count); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 445 | VkResult ActivateLayers(VkPhysicalDevice physical_dev, |
| 446 | const char* const* layer_names, |
| 447 | uint32_t layer_count, |
| 448 | const char* const* extension_names, |
| 449 | uint32_t extension_count); |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 450 | ActiveLayer* AllocateLayerArray(uint32_t count) const; |
| 451 | VkResult LoadLayer(ActiveLayer& layer, const char* name); |
| 452 | void SetupLayerLinks(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 453 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 454 | bool Empty() const; |
| 455 | void ModifyCreateInfo(VkInstanceCreateInfo& info); |
| 456 | void ModifyCreateInfo(VkDeviceCreateInfo& info); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 457 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 458 | VkResult Create(const VkInstanceCreateInfo* create_info, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 459 | const VkAllocationCallbacks* allocator, |
| 460 | VkInstance* instance_out); |
| 461 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 462 | VkResult Create(VkPhysicalDevice physical_dev, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 463 | const VkDeviceCreateInfo* create_info, |
| 464 | const VkAllocationCallbacks* allocator, |
| 465 | VkDevice* dev_out); |
| 466 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 467 | VkResult ValidateExtensions(const char* const* extension_names, |
| 468 | uint32_t extension_count); |
| 469 | VkResult ValidateExtensions(VkPhysicalDevice physical_dev, |
| 470 | const char* const* extension_names, |
| 471 | uint32_t extension_count); |
| 472 | VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const; |
| 473 | bool IsLayerExtension(const char* name) const; |
| 474 | bool IsDriverExtension(const char* name) const; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 475 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 476 | template <typename DataType> |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 477 | void StealLayers(DataType& data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 478 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 479 | static void DestroyLayers(ActiveLayer* layers, |
| 480 | uint32_t count, |
| 481 | const VkAllocationCallbacks& allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 482 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 483 | static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance, |
| 484 | void* object); |
| 485 | static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device, |
| 486 | void* object); |
| 487 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 488 | static VKAPI_ATTR VkBool32 |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 489 | DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 490 | VkDebugReportObjectTypeEXT obj_type, |
| 491 | uint64_t obj, |
| 492 | size_t location, |
| 493 | int32_t msg_code, |
| 494 | const char* layer_prefix, |
| 495 | const char* msg, |
| 496 | void* user_data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 497 | |
| 498 | const bool is_instance_; |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 499 | const driver::DebugReportLogger& logger_; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 500 | const VkAllocationCallbacks& allocator_; |
| 501 | |
| 502 | OverrideLayerNames override_layers_; |
| 503 | OverrideExtensionNames override_extensions_; |
| 504 | |
| 505 | ActiveLayer* layers_; |
| 506 | uint32_t layer_count_; |
| 507 | |
| 508 | PFN_vkGetInstanceProcAddr get_instance_proc_addr_; |
| 509 | PFN_vkGetDeviceProcAddr get_device_proc_addr_; |
| 510 | |
| 511 | union { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 512 | VkLayerInstanceCreateInfo instance_chain_info_[2]; |
| 513 | VkLayerDeviceCreateInfo device_chain_info_[2]; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 514 | }; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 515 | |
| 516 | VkExtensionProperties* driver_extensions_; |
| 517 | uint32_t driver_extension_count_; |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 518 | std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 519 | }; |
| 520 | |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 521 | LayerChain::LayerChain(bool is_instance, |
| 522 | const driver::DebugReportLogger& logger, |
| 523 | const VkAllocationCallbacks& allocator) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 524 | : is_instance_(is_instance), |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 525 | logger_(logger), |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 526 | allocator_(allocator), |
| 527 | override_layers_(is_instance, allocator), |
| 528 | override_extensions_(is_instance, allocator), |
| 529 | layers_(nullptr), |
| 530 | layer_count_(0), |
| 531 | get_instance_proc_addr_(nullptr), |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 532 | get_device_proc_addr_(nullptr), |
| 533 | driver_extensions_(nullptr), |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 534 | driver_extension_count_(0) { |
Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 535 | // advertise the loader supported core Vulkan API version at vulkan::api |
| 536 | for (uint32_t i = driver::ProcHook::EXTENSION_CORE_1_0; |
| 537 | i != driver::ProcHook::EXTENSION_COUNT; ++i) { |
| 538 | enabled_extensions_.set(i); |
| 539 | } |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 540 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 541 | |
| 542 | LayerChain::~LayerChain() { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 543 | allocator_.pfnFree(allocator_.pUserData, driver_extensions_); |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 544 | DestroyLayers(layers_, layer_count_, allocator_); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 545 | } |
| 546 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 547 | VkResult LayerChain::ActivateLayers(const char* const* layer_names, |
| 548 | uint32_t layer_count, |
| 549 | const char* const* extension_names, |
| 550 | uint32_t extension_count) { |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 551 | VkResult result = override_layers_.Parse(layer_names, layer_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 552 | if (result != VK_SUCCESS) |
| 553 | return result; |
| 554 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 555 | result = override_extensions_.Parse(extension_names, extension_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 556 | if (result != VK_SUCCESS) |
| 557 | return result; |
| 558 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 559 | if (override_layers_.Count()) { |
| 560 | layer_names = override_layers_.Names(); |
| 561 | layer_count = override_layers_.Count(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (!layer_count) { |
| 565 | // point head of chain to the driver |
| 566 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 567 | |
| 568 | return VK_SUCCESS; |
| 569 | } |
| 570 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 571 | layers_ = AllocateLayerArray(layer_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 572 | if (!layers_) |
| 573 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 574 | |
| 575 | // load layers |
| 576 | for (uint32_t i = 0; i < layer_count; i++) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 577 | result = LoadLayer(layers_[i], layer_names[i]); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 578 | if (result != VK_SUCCESS) |
| 579 | return result; |
| 580 | |
| 581 | // count loaded layers for proper destructions on errors |
| 582 | layer_count_++; |
| 583 | } |
| 584 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 585 | SetupLayerLinks(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 586 | |
| 587 | return VK_SUCCESS; |
| 588 | } |
| 589 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 590 | VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev, |
| 591 | const char* const* layer_names, |
| 592 | uint32_t layer_count, |
| 593 | const char* const* extension_names, |
| 594 | uint32_t extension_count) { |
| 595 | uint32_t instance_layer_count; |
| 596 | const ActiveLayer* instance_layers = |
| 597 | GetActiveLayers(physical_dev, instance_layer_count); |
| 598 | |
| 599 | // log a message if the application device layer array is not empty nor an |
| 600 | // exact match of the instance layer array. |
| 601 | if (layer_count) { |
| 602 | bool exact_match = (instance_layer_count == layer_count); |
| 603 | if (exact_match) { |
| 604 | for (uint32_t i = 0; i < instance_layer_count; i++) { |
| 605 | const Layer& l = *instance_layers[i].ref; |
| 606 | if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) { |
| 607 | exact_match = false; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (!exact_match) { |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 614 | logger_.Warn(physical_dev, |
| 615 | "Device layers disagree with instance layers and are " |
| 616 | "overridden by instance layers"); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | VkResult result = |
| 621 | override_extensions_.Parse(extension_names, extension_count); |
| 622 | if (result != VK_SUCCESS) |
| 623 | return result; |
| 624 | |
| 625 | if (!instance_layer_count) { |
| 626 | // point head of chain to the driver |
| 627 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; |
| 628 | get_device_proc_addr_ = driver::GetDeviceProcAddr; |
| 629 | |
| 630 | return VK_SUCCESS; |
| 631 | } |
| 632 | |
| 633 | layers_ = AllocateLayerArray(instance_layer_count); |
| 634 | if (!layers_) |
| 635 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 636 | |
| 637 | for (uint32_t i = 0; i < instance_layer_count; i++) { |
| 638 | const Layer& l = *instance_layers[i].ref; |
| 639 | |
| 640 | // no need to and cannot chain non-global layers |
| 641 | if (!IsLayerGlobal(l)) |
| 642 | continue; |
| 643 | |
| 644 | // this never fails |
| 645 | new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}}; |
| 646 | } |
| 647 | |
Chia-I Wu | 61b25fd | 2016-05-27 10:18:25 +0800 | [diff] [blame] | 648 | // this may happen when all layers are non-global ones |
| 649 | if (!layer_count_) { |
| 650 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; |
| 651 | get_device_proc_addr_ = driver::GetDeviceProcAddr; |
| 652 | return VK_SUCCESS; |
| 653 | } |
| 654 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 655 | SetupLayerLinks(); |
| 656 | |
| 657 | return VK_SUCCESS; |
| 658 | } |
| 659 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 660 | LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 661 | VkSystemAllocationScope scope = (is_instance_) |
| 662 | ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 663 | : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 664 | |
| 665 | return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation( |
| 666 | allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer), |
| 667 | scope)); |
| 668 | } |
| 669 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 670 | VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) { |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 671 | const Layer* l = FindLayer(name); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 672 | if (!l) { |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 673 | logger_.Err(VK_NULL_HANDLE, "Failed to find layer %s", name); |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 674 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 675 | } |
| 676 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 677 | new (&layer) ActiveLayer{GetLayerRef(*l), {}}; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 678 | if (!layer.ref) { |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 679 | ALOGW("Failed to open layer %s", name); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 680 | layer.ref.~LayerRef(); |
| 681 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 682 | } |
| 683 | |
Adam Bodnar | 87edb95 | 2019-07-17 12:35:53 -0700 | [diff] [blame] | 684 | if (!layer.ref.GetGetInstanceProcAddr()) { |
| 685 | ALOGW("Failed to locate vkGetInstanceProcAddr in layer %s", name); |
| 686 | layer.ref.~LayerRef(); |
| 687 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 688 | } |
| 689 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 690 | ALOGI("Loaded layer %s", name); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 691 | |
| 692 | return VK_SUCCESS; |
| 693 | } |
| 694 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 695 | void LayerChain::SetupLayerLinks() { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 696 | if (is_instance_) { |
| 697 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 698 | ActiveLayer& layer = layers_[i]; |
| 699 | |
| 700 | // point head of chain to the first layer |
| 701 | if (i == 0) |
| 702 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); |
| 703 | |
| 704 | // point tail of chain to the driver |
| 705 | if (i == layer_count_ - 1) { |
| 706 | layer.instance_link.pNext = nullptr; |
| 707 | layer.instance_link.pfnNextGetInstanceProcAddr = |
| 708 | driver::GetInstanceProcAddr; |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | const ActiveLayer& next = layers_[i + 1]; |
| 713 | |
| 714 | // const_cast as some naughty layers want to modify our links! |
| 715 | layer.instance_link.pNext = |
| 716 | const_cast<VkLayerInstanceLink*>(&next.instance_link); |
| 717 | layer.instance_link.pfnNextGetInstanceProcAddr = |
| 718 | next.ref.GetGetInstanceProcAddr(); |
| 719 | } |
| 720 | } else { |
| 721 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 722 | ActiveLayer& layer = layers_[i]; |
| 723 | |
| 724 | // point head of chain to the first layer |
| 725 | if (i == 0) { |
| 726 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); |
| 727 | get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr(); |
| 728 | } |
| 729 | |
| 730 | // point tail of chain to the driver |
| 731 | if (i == layer_count_ - 1) { |
| 732 | layer.device_link.pNext = nullptr; |
| 733 | layer.device_link.pfnNextGetInstanceProcAddr = |
| 734 | driver::GetInstanceProcAddr; |
| 735 | layer.device_link.pfnNextGetDeviceProcAddr = |
| 736 | driver::GetDeviceProcAddr; |
| 737 | break; |
| 738 | } |
| 739 | |
| 740 | const ActiveLayer& next = layers_[i + 1]; |
| 741 | |
| 742 | // const_cast as some naughty layers want to modify our links! |
| 743 | layer.device_link.pNext = |
| 744 | const_cast<VkLayerDeviceLink*>(&next.device_link); |
| 745 | layer.device_link.pfnNextGetInstanceProcAddr = |
| 746 | next.ref.GetGetInstanceProcAddr(); |
| 747 | layer.device_link.pfnNextGetDeviceProcAddr = |
| 748 | next.ref.GetGetDeviceProcAddr(); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 753 | bool LayerChain::Empty() const { |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 754 | return (!layer_count_ && !override_layers_.Count() && |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 755 | !override_extensions_.Count()); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 756 | } |
| 757 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 758 | void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 759 | if (layer_count_) { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 760 | auto& link_info = instance_chain_info_[1]; |
| 761 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 762 | link_info.pNext = info.pNext; |
| 763 | link_info.function = VK_LAYER_FUNCTION_LINK; |
| 764 | link_info.u.pLayerInfo = &layers_[0].instance_link; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 765 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 766 | auto& cb_info = instance_chain_info_[0]; |
| 767 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 768 | cb_info.pNext = &link_info; |
| 769 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; |
| 770 | cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 771 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 772 | info.pNext = &cb_info; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 773 | } |
| 774 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 775 | if (override_layers_.Count()) { |
| 776 | info.enabledLayerCount = override_layers_.Count(); |
| 777 | info.ppEnabledLayerNames = override_layers_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 778 | } |
| 779 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 780 | if (override_extensions_.Count()) { |
| 781 | info.enabledExtensionCount = override_extensions_.Count(); |
| 782 | info.ppEnabledExtensionNames = override_extensions_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 786 | void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 787 | if (layer_count_) { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 788 | auto& link_info = device_chain_info_[1]; |
| 789 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 790 | link_info.pNext = info.pNext; |
| 791 | link_info.function = VK_LAYER_FUNCTION_LINK; |
| 792 | link_info.u.pLayerInfo = &layers_[0].device_link; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 793 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 794 | auto& cb_info = device_chain_info_[0]; |
| 795 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 796 | cb_info.pNext = &link_info; |
| 797 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; |
| 798 | cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 799 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 800 | info.pNext = &cb_info; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 801 | } |
| 802 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 803 | if (override_layers_.Count()) { |
| 804 | info.enabledLayerCount = override_layers_.Count(); |
| 805 | info.ppEnabledLayerNames = override_layers_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 806 | } |
| 807 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 808 | if (override_extensions_.Count()) { |
| 809 | info.enabledExtensionCount = override_extensions_.Count(); |
| 810 | info.ppEnabledExtensionNames = override_extensions_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 814 | VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 815 | const VkAllocationCallbacks* allocator, |
| 816 | VkInstance* instance_out) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 817 | VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames, |
| 818 | create_info->enabledExtensionCount); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 819 | if (result != VK_SUCCESS) |
| 820 | return result; |
| 821 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 822 | // call down the chain |
| 823 | PFN_vkCreateInstance create_instance = |
| 824 | reinterpret_cast<PFN_vkCreateInstance>( |
| 825 | get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance")); |
| 826 | VkInstance instance; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 827 | result = create_instance(create_info, allocator, &instance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 828 | if (result != VK_SUCCESS) |
| 829 | return result; |
| 830 | |
| 831 | // initialize InstanceData |
| 832 | InstanceData& data = GetData(instance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 833 | |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 834 | if (!InitDispatchTable(instance, get_instance_proc_addr_, |
| 835 | enabled_extensions_)) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 836 | if (data.dispatch.DestroyInstance) |
| 837 | data.dispatch.DestroyInstance(instance, allocator); |
| 838 | |
| 839 | return VK_ERROR_INITIALIZATION_FAILED; |
| 840 | } |
| 841 | |
| 842 | // install debug report callback |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 843 | if (override_extensions_.InstallDebugCallback()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 844 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = |
| 845 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 846 | get_instance_proc_addr_(instance, |
| 847 | "vkCreateDebugReportCallbackEXT")); |
| 848 | data.destroy_debug_callback = |
| 849 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 850 | get_instance_proc_addr_(instance, |
| 851 | "vkDestroyDebugReportCallbackEXT")); |
| 852 | if (!create_debug_report_callback || !data.destroy_debug_callback) { |
| 853 | ALOGE("Broken VK_EXT_debug_report support"); |
| 854 | data.dispatch.DestroyInstance(instance, allocator); |
| 855 | return VK_ERROR_INITIALIZATION_FAILED; |
| 856 | } |
| 857 | |
| 858 | VkDebugReportCallbackCreateInfoEXT debug_callback_info = {}; |
| 859 | debug_callback_info.sType = |
| 860 | VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 861 | debug_callback_info.flags = |
| 862 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 863 | debug_callback_info.pfnCallback = DebugReportCallback; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 864 | |
| 865 | VkDebugReportCallbackEXT debug_callback; |
| 866 | result = create_debug_report_callback(instance, &debug_callback_info, |
| 867 | nullptr, &debug_callback); |
| 868 | if (result != VK_SUCCESS) { |
| 869 | ALOGE("Failed to install debug report callback"); |
| 870 | data.dispatch.DestroyInstance(instance, allocator); |
| 871 | return VK_ERROR_INITIALIZATION_FAILED; |
| 872 | } |
| 873 | |
| 874 | data.debug_callback = debug_callback; |
| 875 | |
| 876 | ALOGI("Installed debug report callback"); |
| 877 | } |
| 878 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 879 | StealLayers(data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 880 | |
| 881 | *instance_out = instance; |
| 882 | |
| 883 | return VK_SUCCESS; |
| 884 | } |
| 885 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 886 | VkResult LayerChain::Create(VkPhysicalDevice physical_dev, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 887 | const VkDeviceCreateInfo* create_info, |
| 888 | const VkAllocationCallbacks* allocator, |
| 889 | VkDevice* dev_out) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 890 | VkResult result = |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 891 | ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames, |
| 892 | create_info->enabledExtensionCount); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 893 | if (result != VK_SUCCESS) |
| 894 | return result; |
| 895 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 896 | // call down the chain |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 897 | PFN_vkCreateDevice create_device = |
| 898 | GetData(physical_dev).dispatch.CreateDevice; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 899 | VkDevice dev; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 900 | result = create_device(physical_dev, create_info, allocator, &dev); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 901 | if (result != VK_SUCCESS) |
| 902 | return result; |
| 903 | |
| 904 | // initialize DeviceData |
| 905 | DeviceData& data = GetData(dev); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 906 | |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 907 | if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 908 | if (data.dispatch.DestroyDevice) |
| 909 | data.dispatch.DestroyDevice(dev, allocator); |
| 910 | |
| 911 | return VK_ERROR_INITIALIZATION_FAILED; |
| 912 | } |
| 913 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 914 | // no StealLayers so that active layers are destroyed with this |
| 915 | // LayerChain |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 916 | *dev_out = dev; |
| 917 | |
| 918 | return VK_SUCCESS; |
| 919 | } |
| 920 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 921 | VkResult LayerChain::ValidateExtensions(const char* const* extension_names, |
| 922 | uint32_t extension_count) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 923 | if (!extension_count) |
| 924 | return VK_SUCCESS; |
| 925 | |
| 926 | // query driver instance extensions |
| 927 | uint32_t count; |
| 928 | VkResult result = |
| 929 | EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); |
| 930 | if (result == VK_SUCCESS && count) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 931 | driver_extensions_ = AllocateDriverExtensionArray(count); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 932 | result = (driver_extensions_) ? EnumerateInstanceExtensionProperties( |
| 933 | nullptr, &count, driver_extensions_) |
| 934 | : VK_ERROR_OUT_OF_HOST_MEMORY; |
| 935 | } |
| 936 | if (result != VK_SUCCESS) |
| 937 | return result; |
| 938 | |
| 939 | driver_extension_count_ = count; |
| 940 | |
| 941 | for (uint32_t i = 0; i < extension_count; i++) { |
| 942 | const char* name = extension_names[i]; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 943 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 944 | logger_.Err(VK_NULL_HANDLE, |
| 945 | "Failed to enable missing instance extension %s", name); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 946 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 947 | } |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 948 | |
| 949 | auto ext_bit = driver::GetProcHookExtension(name); |
| 950 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 951 | enabled_extensions_.set(ext_bit); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | return VK_SUCCESS; |
| 955 | } |
| 956 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 957 | VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev, |
| 958 | const char* const* extension_names, |
| 959 | uint32_t extension_count) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 960 | if (!extension_count) |
| 961 | return VK_SUCCESS; |
| 962 | |
| 963 | // query driver device extensions |
| 964 | uint32_t count; |
| 965 | VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr, |
| 966 | &count, nullptr); |
| 967 | if (result == VK_SUCCESS && count) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 968 | driver_extensions_ = AllocateDriverExtensionArray(count); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 969 | result = (driver_extensions_) |
| 970 | ? EnumerateDeviceExtensionProperties( |
| 971 | physical_dev, nullptr, &count, driver_extensions_) |
| 972 | : VK_ERROR_OUT_OF_HOST_MEMORY; |
| 973 | } |
| 974 | if (result != VK_SUCCESS) |
| 975 | return result; |
| 976 | |
| 977 | driver_extension_count_ = count; |
| 978 | |
| 979 | for (uint32_t i = 0; i < extension_count; i++) { |
| 980 | const char* name = extension_names[i]; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 981 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 982 | logger_.Err(physical_dev, |
| 983 | "Failed to enable missing device extension %s", name); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 984 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 985 | } |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 986 | |
| 987 | auto ext_bit = driver::GetProcHookExtension(name); |
| 988 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 989 | enabled_extensions_.set(ext_bit); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | return VK_SUCCESS; |
| 993 | } |
| 994 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 995 | VkExtensionProperties* LayerChain::AllocateDriverExtensionArray( |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 996 | uint32_t count) const { |
| 997 | return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( |
| 998 | allocator_.pUserData, sizeof(VkExtensionProperties) * count, |
| 999 | alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 1000 | } |
| 1001 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1002 | bool LayerChain::IsLayerExtension(const char* name) const { |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 1003 | if (is_instance_) { |
| 1004 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 1005 | const ActiveLayer& layer = layers_[i]; |
| 1006 | if (FindLayerInstanceExtension(*layer.ref, name)) |
| 1007 | return true; |
| 1008 | } |
| 1009 | } else { |
| 1010 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 1011 | const ActiveLayer& layer = layers_[i]; |
| 1012 | if (FindLayerDeviceExtension(*layer.ref, name)) |
| 1013 | return true; |
| 1014 | } |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1020 | bool LayerChain::IsDriverExtension(const char* name) const { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 1021 | for (uint32_t i = 0; i < driver_extension_count_; i++) { |
| 1022 | if (strcmp(driver_extensions_[i].extensionName, name) == 0) |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1029 | template <typename DataType> |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1030 | void LayerChain::StealLayers(DataType& data) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1031 | data.layers = layers_; |
| 1032 | data.layer_count = layer_count_; |
| 1033 | |
| 1034 | layers_ = nullptr; |
| 1035 | layer_count_ = 0; |
| 1036 | } |
| 1037 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1038 | void LayerChain::DestroyLayers(ActiveLayer* layers, |
| 1039 | uint32_t count, |
| 1040 | const VkAllocationCallbacks& allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1041 | for (uint32_t i = 0; i < count; i++) |
| 1042 | layers[i].ref.~LayerRef(); |
| 1043 | |
| 1044 | allocator.pfnFree(allocator.pUserData, layers); |
| 1045 | } |
| 1046 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 1047 | VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) { |
| 1048 | driver::InstanceDispatchable dispatchable = |
| 1049 | reinterpret_cast<driver::InstanceDispatchable>(object); |
| 1050 | |
| 1051 | return (driver::SetDataInternal(dispatchable, &driver::GetData(instance))) |
| 1052 | ? VK_SUCCESS |
| 1053 | : VK_ERROR_INITIALIZATION_FAILED; |
| 1054 | } |
| 1055 | |
| 1056 | VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) { |
| 1057 | driver::DeviceDispatchable dispatchable = |
| 1058 | reinterpret_cast<driver::DeviceDispatchable>(object); |
| 1059 | |
| 1060 | return (driver::SetDataInternal(dispatchable, &driver::GetData(device))) |
| 1061 | ? VK_SUCCESS |
| 1062 | : VK_ERROR_INITIALIZATION_FAILED; |
| 1063 | } |
| 1064 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1065 | VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 1066 | VkDebugReportObjectTypeEXT obj_type, |
| 1067 | uint64_t obj, |
| 1068 | size_t location, |
| 1069 | int32_t msg_code, |
| 1070 | const char* layer_prefix, |
| 1071 | const char* msg, |
| 1072 | void* user_data) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1073 | int prio; |
| 1074 | |
| 1075 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) |
| 1076 | prio = ANDROID_LOG_ERROR; |
| 1077 | else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 1078 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) |
| 1079 | prio = ANDROID_LOG_WARN; |
| 1080 | else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) |
| 1081 | prio = ANDROID_LOG_INFO; |
| 1082 | else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) |
| 1083 | prio = ANDROID_LOG_DEBUG; |
| 1084 | else |
| 1085 | prio = ANDROID_LOG_UNKNOWN; |
| 1086 | |
| 1087 | LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg); |
| 1088 | |
| 1089 | (void)obj_type; |
| 1090 | (void)obj; |
| 1091 | (void)location; |
| 1092 | (void)user_data; |
| 1093 | |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1097 | VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info, |
| 1098 | const VkAllocationCallbacks* allocator, |
| 1099 | VkInstance* instance_out) { |
Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1100 | const driver::DebugReportLogger logger(*create_info); |
| 1101 | LayerChain chain(true, logger, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1102 | (allocator) ? *allocator : driver::GetDefaultAllocator()); |
| 1103 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1104 | VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, |
| 1105 | create_info->enabledLayerCount, |
| 1106 | create_info->ppEnabledExtensionNames, |
| 1107 | create_info->enabledExtensionCount); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1108 | if (result != VK_SUCCESS) |
| 1109 | return result; |
| 1110 | |
| 1111 | // use a local create info when the chain is not empty |
| 1112 | VkInstanceCreateInfo local_create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1113 | if (!chain.Empty()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1114 | local_create_info = *create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1115 | chain.ModifyCreateInfo(local_create_info); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1116 | create_info = &local_create_info; |
| 1117 | } |
| 1118 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1119 | return chain.Create(create_info, allocator, instance_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1122 | VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev, |
| 1123 | const VkDeviceCreateInfo* create_info, |
| 1124 | const VkAllocationCallbacks* allocator, |
| 1125 | VkDevice* dev_out) { |
Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1126 | const driver::DebugReportLogger logger = driver::Logger(physical_dev); |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1127 | LayerChain chain( |
Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1128 | false, logger, |
Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1129 | (allocator) ? *allocator : driver::GetData(physical_dev).allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1130 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1131 | VkResult result = chain.ActivateLayers( |
| 1132 | physical_dev, create_info->ppEnabledLayerNames, |
| 1133 | create_info->enabledLayerCount, create_info->ppEnabledExtensionNames, |
| 1134 | create_info->enabledExtensionCount); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1135 | if (result != VK_SUCCESS) |
| 1136 | return result; |
| 1137 | |
| 1138 | // use a local create info when the chain is not empty |
| 1139 | VkDeviceCreateInfo local_create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1140 | if (!chain.Empty()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1141 | local_create_info = *create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1142 | chain.ModifyCreateInfo(local_create_info); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1143 | create_info = &local_create_info; |
| 1144 | } |
| 1145 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1146 | return chain.Create(physical_dev, create_info, allocator, dev_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1149 | void LayerChain::DestroyInstance(VkInstance instance, |
| 1150 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1151 | InstanceData& data = GetData(instance); |
| 1152 | |
| 1153 | if (data.debug_callback != VK_NULL_HANDLE) |
| 1154 | data.destroy_debug_callback(instance, data.debug_callback, allocator); |
| 1155 | |
| 1156 | ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); |
| 1157 | uint32_t layer_count = data.layer_count; |
| 1158 | |
| 1159 | VkAllocationCallbacks local_allocator; |
| 1160 | if (!allocator) |
| 1161 | local_allocator = driver::GetData(instance).allocator; |
| 1162 | |
| 1163 | // this also destroys InstanceData |
| 1164 | data.dispatch.DestroyInstance(instance, allocator); |
| 1165 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1166 | DestroyLayers(layers, layer_count, |
| 1167 | (allocator) ? *allocator : local_allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1170 | void LayerChain::DestroyDevice(VkDevice device, |
| 1171 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1172 | DeviceData& data = GetData(device); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1173 | // this also destroys DeviceData |
| 1174 | data.dispatch.DestroyDevice(device, allocator); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1175 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1176 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1177 | const LayerChain::ActiveLayer* LayerChain::GetActiveLayers( |
| 1178 | VkPhysicalDevice physical_dev, |
| 1179 | uint32_t& count) { |
| 1180 | count = GetData(physical_dev).layer_count; |
| 1181 | return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | // ---------------------------------------------------------------------------- |
| 1185 | |
| 1186 | bool EnsureInitialized() { |
Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1187 | static bool initialized = false; |
| 1188 | static pid_t init_attempted_for_pid = 0; |
| 1189 | static std::mutex init_lock; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1190 | |
Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1191 | std::lock_guard<std::mutex> lock(init_lock); |
| 1192 | if (init_attempted_for_pid == getpid()) |
| 1193 | return initialized; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1194 | |
Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1195 | init_attempted_for_pid = getpid(); |
| 1196 | if (driver::OpenHAL()) { |
| 1197 | DiscoverLayers(); |
| 1198 | initialized = true; |
Yiwei Zhang | 9dfc93a | 2019-08-09 17:25:24 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1201 | return initialized; |
| 1202 | } |
| 1203 | |
Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1204 | template <typename Functor> |
| 1205 | void ForEachLayerFromSettings(Functor functor) { |
| 1206 | const std::string layersSetting = |
| 1207 | android::GraphicsEnv::getInstance().getDebugLayers(); |
| 1208 | if (!layersSetting.empty()) { |
| 1209 | std::vector<std::string> layers = |
| 1210 | android::base::Split(layersSetting, ":"); |
| 1211 | for (uint32_t i = 0; i < layers.size(); i++) { |
| 1212 | const Layer* layer = FindLayer(layers[i].c_str()); |
| 1213 | if (!layer) { |
| 1214 | continue; |
| 1215 | } |
| 1216 | functor(layer); |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1221 | } // anonymous namespace |
| 1222 | |
| 1223 | VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, |
| 1224 | const VkAllocationCallbacks* pAllocator, |
| 1225 | VkInstance* pInstance) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1226 | ATRACE_CALL(); |
| 1227 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1228 | if (!EnsureInitialized()) |
| 1229 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1230 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1231 | return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | void DestroyInstance(VkInstance instance, |
| 1235 | const VkAllocationCallbacks* pAllocator) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1236 | ATRACE_CALL(); |
| 1237 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1238 | if (instance != VK_NULL_HANDLE) |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1239 | LayerChain::DestroyInstance(instance, pAllocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | VkResult CreateDevice(VkPhysicalDevice physicalDevice, |
| 1243 | const VkDeviceCreateInfo* pCreateInfo, |
| 1244 | const VkAllocationCallbacks* pAllocator, |
| 1245 | VkDevice* pDevice) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1246 | ATRACE_CALL(); |
| 1247 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1248 | return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator, |
| 1249 | pDevice); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1253 | ATRACE_CALL(); |
| 1254 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1255 | if (device != VK_NULL_HANDLE) |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1256 | LayerChain::DestroyDevice(device, pAllocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount, |
| 1260 | VkLayerProperties* pProperties) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1261 | ATRACE_CALL(); |
| 1262 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1263 | if (!EnsureInitialized()) |
Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1264 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1265 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1266 | uint32_t count = GetLayerCount(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1267 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1268 | if (!pProperties) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1269 | *pPropertyCount = count; |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1270 | return VK_SUCCESS; |
| 1271 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1272 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1273 | uint32_t copied = std::min(*pPropertyCount, count); |
| 1274 | for (uint32_t i = 0; i < copied; i++) |
| 1275 | pProperties[i] = GetLayerProperties(GetLayer(i)); |
| 1276 | *pPropertyCount = copied; |
| 1277 | |
| 1278 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | VkResult EnumerateInstanceExtensionProperties( |
| 1282 | const char* pLayerName, |
| 1283 | uint32_t* pPropertyCount, |
| 1284 | VkExtensionProperties* pProperties) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1285 | ATRACE_CALL(); |
| 1286 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1287 | if (!EnsureInitialized()) |
Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1288 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1289 | |
| 1290 | if (pLayerName) { |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1291 | const Layer* layer = FindLayer(pLayerName); |
Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1292 | if (!layer) |
| 1293 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 1294 | |
| 1295 | uint32_t count; |
| 1296 | const VkExtensionProperties* props = |
| 1297 | GetLayerInstanceExtensions(*layer, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1298 | |
| 1299 | if (!pProperties || *pPropertyCount > count) |
| 1300 | *pPropertyCount = count; |
| 1301 | if (pProperties) |
| 1302 | std::copy(props, props + *pPropertyCount, pProperties); |
| 1303 | |
| 1304 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; |
| 1305 | } |
| 1306 | |
Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1307 | // If the pLayerName is nullptr, we must advertise all instance extensions |
| 1308 | // from all implicitly enabled layers and the driver implementation. If |
| 1309 | // there are duplicates among layers and the driver implementation, always |
| 1310 | // only preserve the top layer closest to the application regardless of the |
| 1311 | // spec version. |
| 1312 | std::vector<VkExtensionProperties> properties; |
| 1313 | std::unordered_set<std::string> extensionNames; |
| 1314 | |
| 1315 | // Expose extensions from implicitly enabled layers. |
Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1316 | ForEachLayerFromSettings([&](const Layer* layer) { |
| 1317 | uint32_t count = 0; |
| 1318 | const VkExtensionProperties* props = |
| 1319 | GetLayerInstanceExtensions(*layer, count); |
| 1320 | if (count > 0) { |
| 1321 | for (uint32_t i = 0; i < count; ++i) { |
| 1322 | if (extensionNames.emplace(props[i].extensionName).second) { |
| 1323 | properties.push_back(props[i]); |
Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | } |
Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1327 | }); |
Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1328 | |
| 1329 | // TODO(b/143293104): Parse debug.vulkan.layers properties |
| 1330 | |
| 1331 | // Expose extensions from driver implementation. |
| 1332 | { |
| 1333 | uint32_t count = 0; |
| 1334 | VkResult result = vulkan::driver::EnumerateInstanceExtensionProperties( |
| 1335 | nullptr, &count, nullptr); |
| 1336 | if (result == VK_SUCCESS && count > 0) { |
| 1337 | std::vector<VkExtensionProperties> props(count); |
| 1338 | result = vulkan::driver::EnumerateInstanceExtensionProperties( |
| 1339 | nullptr, &count, props.data()); |
| 1340 | for (auto prop : props) { |
| 1341 | if (extensionNames.emplace(prop.extensionName).second) { |
| 1342 | properties.push_back(prop); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | uint32_t totalCount = properties.size(); |
| 1349 | if (!pProperties || *pPropertyCount > totalCount) { |
| 1350 | *pPropertyCount = totalCount; |
| 1351 | } |
| 1352 | if (pProperties) { |
| 1353 | std::copy(properties.data(), properties.data() + *pPropertyCount, |
| 1354 | pProperties); |
| 1355 | } |
| 1356 | return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, |
| 1360 | uint32_t* pPropertyCount, |
| 1361 | VkLayerProperties* pProperties) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1362 | ATRACE_CALL(); |
| 1363 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1364 | uint32_t count; |
| 1365 | const LayerChain::ActiveLayer* layers = |
| 1366 | LayerChain::GetActiveLayers(physicalDevice, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1367 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1368 | if (!pProperties) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1369 | *pPropertyCount = count; |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1370 | return VK_SUCCESS; |
| 1371 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1372 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1373 | uint32_t copied = std::min(*pPropertyCount, count); |
| 1374 | for (uint32_t i = 0; i < copied; i++) |
| 1375 | pProperties[i] = GetLayerProperties(*layers[i].ref); |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1376 | *pPropertyCount = copied; |
| 1377 | |
| 1378 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | VkResult EnumerateDeviceExtensionProperties( |
| 1382 | VkPhysicalDevice physicalDevice, |
| 1383 | const char* pLayerName, |
| 1384 | uint32_t* pPropertyCount, |
| 1385 | VkExtensionProperties* pProperties) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1386 | ATRACE_CALL(); |
| 1387 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1388 | if (pLayerName) { |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1389 | // EnumerateDeviceLayerProperties enumerates active layers for |
| 1390 | // backward compatibility. The extension query here should work for |
| 1391 | // all layers. |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1392 | const Layer* layer = FindLayer(pLayerName); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1393 | if (!layer) |
Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1394 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 1395 | |
| 1396 | uint32_t count; |
| 1397 | const VkExtensionProperties* props = |
| 1398 | GetLayerDeviceExtensions(*layer, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1399 | |
| 1400 | if (!pProperties || *pPropertyCount > count) |
| 1401 | *pPropertyCount = count; |
| 1402 | if (pProperties) |
| 1403 | std::copy(props, props + *pPropertyCount, pProperties); |
| 1404 | |
| 1405 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; |
| 1406 | } |
| 1407 | |
Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1408 | // If the pLayerName is nullptr, we must advertise all device extensions |
| 1409 | // from all implicitly enabled layers and the driver implementation. If |
| 1410 | // there are duplicates among layers and the driver implementation, always |
| 1411 | // only preserve the top layer closest to the application regardless of the |
| 1412 | // spec version. |
| 1413 | std::vector<VkExtensionProperties> properties; |
| 1414 | std::unordered_set<std::string> extensionNames; |
| 1415 | |
| 1416 | // Expose extensions from implicitly enabled layers. |
| 1417 | ForEachLayerFromSettings([&](const Layer* layer) { |
| 1418 | uint32_t count = 0; |
| 1419 | const VkExtensionProperties* props = |
| 1420 | GetLayerDeviceExtensions(*layer, count); |
| 1421 | if (count > 0) { |
| 1422 | for (uint32_t i = 0; i < count; ++i) { |
| 1423 | if (extensionNames.emplace(props[i].extensionName).second) { |
| 1424 | properties.push_back(props[i]); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | }); |
| 1429 | |
| 1430 | // TODO(b/143293104): Parse debug.vulkan.layers properties |
| 1431 | |
| 1432 | // Expose extensions from driver implementation. |
| 1433 | { |
| 1434 | const InstanceData& data = GetData(physicalDevice); |
| 1435 | uint32_t count = 0; |
| 1436 | VkResult result = data.dispatch.EnumerateDeviceExtensionProperties( |
| 1437 | physicalDevice, nullptr, &count, nullptr); |
| 1438 | if (result == VK_SUCCESS && count > 0) { |
| 1439 | std::vector<VkExtensionProperties> props(count); |
| 1440 | result = data.dispatch.EnumerateDeviceExtensionProperties( |
| 1441 | physicalDevice, nullptr, &count, props.data()); |
| 1442 | for (auto prop : props) { |
| 1443 | if (extensionNames.emplace(prop.extensionName).second) { |
| 1444 | properties.push_back(prop); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | uint32_t totalCount = properties.size(); |
| 1451 | if (!pProperties || *pPropertyCount > totalCount) { |
| 1452 | *pPropertyCount = totalCount; |
| 1453 | } |
| 1454 | if (pProperties) { |
| 1455 | std::copy(properties.data(), properties.data() + *pPropertyCount, |
| 1456 | pProperties); |
| 1457 | } |
| 1458 | return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1459 | } |
| 1460 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1461 | VkResult EnumerateInstanceVersion(uint32_t* pApiVersion) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1462 | ATRACE_CALL(); |
| 1463 | |
Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1464 | // Load the driver here if not done yet. This api will be used in Zygote |
| 1465 | // for Vulkan driver pre-loading because of the minimum overhead. |
| 1466 | if (!EnsureInitialized()) |
| 1467 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1468 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1469 | *pApiVersion = VK_API_VERSION_1_1; |
| 1470 | return VK_SUCCESS; |
| 1471 | } |
| 1472 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1473 | } // namespace api |
| 1474 | } // namespace vulkan |