| 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 |  | 
|  | 24 | #include <stdlib.h> | 
|  | 25 | #include <string.h> | 
|  | 26 | #include <algorithm> | 
|  | 27 | #include <mutex> | 
|  | 28 | #include <new> | 
|  | 29 | #include <utility> | 
|  | 30 | #include <cutils/properties.h> | 
|  | 31 | #include <log/log.h> | 
|  | 32 |  | 
|  | 33 | #include <vulkan/vk_layer_interface.h> | 
|  | 34 | #include "api.h" | 
|  | 35 | #include "driver.h" | 
| Chia-I Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 36 | #include "layers_extensions.h" | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 37 |  | 
|  | 38 | namespace vulkan { | 
|  | 39 | namespace api { | 
|  | 40 |  | 
|  | 41 | namespace { | 
|  | 42 |  | 
|  | 43 | // Provide overridden layer names when there are implicit layers.  No effect | 
|  | 44 | // otherwise. | 
|  | 45 | class OverrideLayerNames { | 
|  | 46 | public: | 
|  | 47 | OverrideLayerNames(bool is_instance, const VkAllocationCallbacks& allocator) | 
|  | 48 | : is_instance_(is_instance), | 
|  | 49 | allocator_(allocator), | 
|  | 50 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), | 
|  | 51 | names_(nullptr), | 
|  | 52 | name_count_(0), | 
|  | 53 | implicit_layers_() { | 
|  | 54 | implicit_layers_.result = VK_SUCCESS; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | ~OverrideLayerNames() { | 
|  | 58 | allocator_.pfnFree(allocator_.pUserData, names_); | 
|  | 59 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.elements); | 
|  | 60 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.name_pool); | 
|  | 61 | } | 
|  | 62 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 63 | VkResult Parse(const char* const* names, uint32_t count) { | 
|  | 64 | AddImplicitLayers(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 65 |  | 
|  | 66 | const auto& arr = implicit_layers_; | 
|  | 67 | if (arr.result != VK_SUCCESS) | 
|  | 68 | return arr.result; | 
|  | 69 |  | 
|  | 70 | // no need to override when there is no implicit layer | 
|  | 71 | if (!arr.count) | 
|  | 72 | return VK_SUCCESS; | 
|  | 73 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 74 | names_ = AllocateNameArray(arr.count + count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 75 | if (!names_) | 
|  | 76 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 77 |  | 
|  | 78 | // add implicit layer names | 
|  | 79 | for (uint32_t i = 0; i < arr.count; i++) | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 80 | names_[i] = GetImplicitLayerName(i); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 81 |  | 
|  | 82 | name_count_ = arr.count; | 
|  | 83 |  | 
|  | 84 | // add explicit layer names | 
|  | 85 | for (uint32_t i = 0; i < count; i++) { | 
|  | 86 | // ignore explicit layers that are also implicit | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 87 | if (IsImplicitLayer(names[i])) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 88 | continue; | 
|  | 89 |  | 
|  | 90 | names_[name_count_++] = names[i]; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | return VK_SUCCESS; | 
|  | 94 | } | 
|  | 95 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 96 | const char* const* Names() const { return names_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 97 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 98 | uint32_t Count() const { return name_count_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 99 |  | 
|  | 100 | private: | 
|  | 101 | struct ImplicitLayer { | 
|  | 102 | int priority; | 
|  | 103 | size_t name_offset; | 
|  | 104 | }; | 
|  | 105 |  | 
|  | 106 | struct ImplicitLayerArray { | 
|  | 107 | ImplicitLayer* elements; | 
|  | 108 | uint32_t max_count; | 
|  | 109 | uint32_t count; | 
|  | 110 |  | 
|  | 111 | char* name_pool; | 
|  | 112 | size_t max_pool_size; | 
|  | 113 | size_t pool_size; | 
|  | 114 |  | 
|  | 115 | VkResult result; | 
|  | 116 | }; | 
|  | 117 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 118 | void AddImplicitLayers() { | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 119 | if (!is_instance_ || !driver::Debuggable()) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 120 | return; | 
|  | 121 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 122 | ParseDebugVulkanLayers(); | 
|  | 123 | property_list(ParseDebugVulkanLayer, this); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 124 |  | 
|  | 125 | // sort by priorities | 
|  | 126 | auto& arr = implicit_layers_; | 
|  | 127 | std::sort(arr.elements, arr.elements + arr.count, | 
|  | 128 | [](const ImplicitLayer& a, const ImplicitLayer& b) { | 
|  | 129 | return (a.priority < b.priority); | 
|  | 130 | }); | 
|  | 131 | } | 
|  | 132 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 133 | void ParseDebugVulkanLayers() { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 134 | // debug.vulkan.layers specifies colon-separated layer names | 
|  | 135 | char prop[PROPERTY_VALUE_MAX]; | 
|  | 136 | if (!property_get("debug.vulkan.layers", prop, "")) | 
|  | 137 | return; | 
|  | 138 |  | 
|  | 139 | // assign negative/high priorities to them | 
|  | 140 | int prio = -PROPERTY_VALUE_MAX; | 
|  | 141 |  | 
|  | 142 | const char* p = prop; | 
|  | 143 | const char* delim; | 
|  | 144 | while ((delim = strchr(p, ':'))) { | 
|  | 145 | if (delim > p) | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 146 | AddImplicitLayer(prio, p, static_cast<size_t>(delim - p)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 147 |  | 
|  | 148 | prio++; | 
|  | 149 | p = delim + 1; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | if (p[0] != '\0') | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 153 | AddImplicitLayer(prio, p, strlen(p)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 156 | static void ParseDebugVulkanLayer(const char* key, | 
|  | 157 | const char* val, | 
|  | 158 | void* user_data) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 159 | static const char prefix[] = "debug.vulkan.layer."; | 
|  | 160 | const size_t prefix_len = sizeof(prefix) - 1; | 
|  | 161 |  | 
|  | 162 | if (strncmp(key, prefix, prefix_len) || val[0] == '\0') | 
|  | 163 | return; | 
|  | 164 | key += prefix_len; | 
|  | 165 |  | 
|  | 166 | // debug.vulkan.layer.<priority> | 
|  | 167 | int priority = -1; | 
|  | 168 | if (key[0] >= '0' && key[0] <= '9') | 
|  | 169 | priority = atoi(key); | 
|  | 170 |  | 
|  | 171 | if (priority < 0) { | 
|  | 172 | ALOGW("Ignored implicit layer %s with invalid priority %s", val, | 
|  | 173 | key); | 
|  | 174 | return; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | OverrideLayerNames& override_layers = | 
|  | 178 | *reinterpret_cast<OverrideLayerNames*>(user_data); | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 179 | override_layers.AddImplicitLayer(priority, val, strlen(val)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 182 | void AddImplicitLayer(int priority, const char* name, size_t len) { | 
|  | 183 | if (!GrowImplicitLayerArray(1, 0)) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 184 | return; | 
|  | 185 |  | 
|  | 186 | auto& arr = implicit_layers_; | 
|  | 187 | auto& layer = arr.elements[arr.count++]; | 
|  | 188 |  | 
|  | 189 | layer.priority = priority; | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 190 | layer.name_offset = AddImplicitLayerName(name, len); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 191 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 192 | ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 195 | size_t AddImplicitLayerName(const char* name, size_t len) { | 
|  | 196 | if (!GrowImplicitLayerArray(0, len + 1)) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 197 | return 0; | 
|  | 198 |  | 
|  | 199 | // add the name to the pool | 
|  | 200 | auto& arr = implicit_layers_; | 
|  | 201 | size_t offset = arr.pool_size; | 
|  | 202 | char* dst = arr.name_pool + offset; | 
|  | 203 |  | 
|  | 204 | std::copy(name, name + len, dst); | 
|  | 205 | dst[len] = '\0'; | 
|  | 206 |  | 
|  | 207 | arr.pool_size += len + 1; | 
|  | 208 |  | 
|  | 209 | return offset; | 
|  | 210 | } | 
|  | 211 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 212 | bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 213 | const uint32_t initial_max_count = 16; | 
|  | 214 | const size_t initial_max_pool_size = 512; | 
|  | 215 |  | 
|  | 216 | auto& arr = implicit_layers_; | 
|  | 217 |  | 
|  | 218 | // grow the element array if needed | 
|  | 219 | while (arr.count + layer_count > arr.max_count) { | 
|  | 220 | uint32_t new_max_count = | 
|  | 221 | (arr.max_count) ? (arr.max_count << 1) : initial_max_count; | 
|  | 222 | void* new_mem = nullptr; | 
|  | 223 |  | 
|  | 224 | if (new_max_count > arr.max_count) { | 
|  | 225 | new_mem = allocator_.pfnReallocation( | 
|  | 226 | allocator_.pUserData, arr.elements, | 
|  | 227 | sizeof(ImplicitLayer) * new_max_count, | 
|  | 228 | alignof(ImplicitLayer), scope_); | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | if (!new_mem) { | 
|  | 232 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 233 | arr.count = 0; | 
|  | 234 | return false; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem); | 
|  | 238 | arr.max_count = new_max_count; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | // grow the name pool if needed | 
|  | 242 | while (arr.pool_size + name_size > arr.max_pool_size) { | 
|  | 243 | size_t new_max_pool_size = (arr.max_pool_size) | 
|  | 244 | ? (arr.max_pool_size << 1) | 
|  | 245 | : initial_max_pool_size; | 
|  | 246 | void* new_mem = nullptr; | 
|  | 247 |  | 
|  | 248 | if (new_max_pool_size > arr.max_pool_size) { | 
|  | 249 | new_mem = allocator_.pfnReallocation( | 
|  | 250 | allocator_.pUserData, arr.name_pool, new_max_pool_size, | 
|  | 251 | alignof(char), scope_); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | if (!new_mem) { | 
|  | 255 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 256 | arr.pool_size = 0; | 
|  | 257 | return false; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | arr.name_pool = reinterpret_cast<char*>(new_mem); | 
|  | 261 | arr.max_pool_size = new_max_pool_size; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | return true; | 
|  | 265 | } | 
|  | 266 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 267 | const char* GetImplicitLayerName(uint32_t index) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 268 | const auto& arr = implicit_layers_; | 
|  | 269 |  | 
|  | 270 | // this may return nullptr when arr.result is not VK_SUCCESS | 
|  | 271 | return implicit_layers_.name_pool + arr.elements[index].name_offset; | 
|  | 272 | } | 
|  | 273 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 274 | bool IsImplicitLayer(const char* name) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 275 | const auto& arr = implicit_layers_; | 
|  | 276 |  | 
|  | 277 | for (uint32_t i = 0; i < arr.count; i++) { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 278 | if (strcmp(name, GetImplicitLayerName(i)) == 0) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 279 | return true; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | return false; | 
|  | 283 | } | 
|  | 284 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 285 | const char** AllocateNameArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 286 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( | 
|  | 287 | allocator_.pUserData, sizeof(const char*) * count, | 
|  | 288 | alignof(const char*), scope_)); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | const bool is_instance_; | 
|  | 292 | const VkAllocationCallbacks& allocator_; | 
|  | 293 | const VkSystemAllocationScope scope_; | 
|  | 294 |  | 
|  | 295 | const char** names_; | 
|  | 296 | uint32_t name_count_; | 
|  | 297 |  | 
|  | 298 | ImplicitLayerArray implicit_layers_; | 
|  | 299 | }; | 
|  | 300 |  | 
|  | 301 | // Provide overridden extension names when there are implicit extensions. | 
|  | 302 | // No effect otherwise. | 
|  | 303 | // | 
|  | 304 | // This is used only to enable VK_EXT_debug_report. | 
|  | 305 | class OverrideExtensionNames { | 
|  | 306 | public: | 
|  | 307 | OverrideExtensionNames(bool is_instance, | 
|  | 308 | const VkAllocationCallbacks& allocator) | 
|  | 309 | : is_instance_(is_instance), | 
|  | 310 | allocator_(allocator), | 
|  | 311 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), | 
|  | 312 | names_(nullptr), | 
|  | 313 | name_count_(0), | 
|  | 314 | install_debug_callback_(false) {} | 
|  | 315 |  | 
|  | 316 | ~OverrideExtensionNames() { | 
|  | 317 | allocator_.pfnFree(allocator_.pUserData, names_); | 
|  | 318 | } | 
|  | 319 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 320 | VkResult Parse(const char* const* names, uint32_t count) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 321 | // this is only for debug.vulkan.enable_callback | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 322 | if (!EnableDebugCallback()) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 323 | return VK_SUCCESS; | 
|  | 324 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 325 | names_ = AllocateNameArray(count + 1); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 326 | if (!names_) | 
|  | 327 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 328 |  | 
|  | 329 | std::copy(names, names + count, names_); | 
|  | 330 |  | 
|  | 331 | name_count_ = count; | 
|  | 332 | names_[name_count_++] = "VK_EXT_debug_report"; | 
|  | 333 |  | 
|  | 334 | install_debug_callback_ = true; | 
|  | 335 |  | 
|  | 336 | return VK_SUCCESS; | 
|  | 337 | } | 
|  | 338 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 339 | const char* const* Names() const { return names_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 340 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 341 | uint32_t Count() const { return name_count_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 342 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 343 | bool InstallDebugCallback() const { return install_debug_callback_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 344 |  | 
|  | 345 | private: | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 346 | bool EnableDebugCallback() const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 347 | return (is_instance_ && driver::Debuggable() && | 
|  | 348 | property_get_bool("debug.vulkan.enable_callback", false)); | 
|  | 349 | } | 
|  | 350 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 351 | const char** AllocateNameArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 352 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( | 
|  | 353 | allocator_.pUserData, sizeof(const char*) * count, | 
|  | 354 | alignof(const char*), scope_)); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | const bool is_instance_; | 
|  | 358 | const VkAllocationCallbacks& allocator_; | 
|  | 359 | const VkSystemAllocationScope scope_; | 
|  | 360 |  | 
|  | 361 | const char** names_; | 
|  | 362 | uint32_t name_count_; | 
|  | 363 | bool install_debug_callback_; | 
|  | 364 | }; | 
|  | 365 |  | 
|  | 366 | // vkCreateInstance and vkCreateDevice helpers with support for layer | 
|  | 367 | // chaining. | 
|  | 368 | class LayerChain { | 
|  | 369 | public: | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 370 | struct ActiveLayer { | 
|  | 371 | LayerRef ref; | 
|  | 372 | union { | 
|  | 373 | VkLayerInstanceLink instance_link; | 
|  | 374 | VkLayerDeviceLink device_link; | 
|  | 375 | }; | 
|  | 376 | }; | 
|  | 377 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 378 | static VkResult CreateInstance(const VkInstanceCreateInfo* create_info, | 
|  | 379 | const VkAllocationCallbacks* allocator, | 
|  | 380 | VkInstance* instance_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 381 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 382 | static VkResult CreateDevice(VkPhysicalDevice physical_dev, | 
|  | 383 | const VkDeviceCreateInfo* create_info, | 
|  | 384 | const VkAllocationCallbacks* allocator, | 
|  | 385 | VkDevice* dev_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 386 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 387 | static void DestroyInstance(VkInstance instance, | 
|  | 388 | const VkAllocationCallbacks* allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 389 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 390 | static void DestroyDevice(VkDevice dev, | 
|  | 391 | const VkAllocationCallbacks* allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 392 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 393 | static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev, | 
|  | 394 | uint32_t& count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 395 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 396 | private: | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 397 | LayerChain(bool is_instance, | 
|  | 398 | const driver::DebugReportLogger& logger, | 
|  | 399 | const VkAllocationCallbacks& allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 400 | ~LayerChain(); | 
|  | 401 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 402 | VkResult ActivateLayers(const char* const* layer_names, | 
|  | 403 | uint32_t layer_count, | 
|  | 404 | const char* const* extension_names, | 
|  | 405 | uint32_t extension_count); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 406 | VkResult ActivateLayers(VkPhysicalDevice physical_dev, | 
|  | 407 | const char* const* layer_names, | 
|  | 408 | uint32_t layer_count, | 
|  | 409 | const char* const* extension_names, | 
|  | 410 | uint32_t extension_count); | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 411 | ActiveLayer* AllocateLayerArray(uint32_t count) const; | 
|  | 412 | VkResult LoadLayer(ActiveLayer& layer, const char* name); | 
|  | 413 | void SetupLayerLinks(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 414 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 415 | bool Empty() const; | 
|  | 416 | void ModifyCreateInfo(VkInstanceCreateInfo& info); | 
|  | 417 | void ModifyCreateInfo(VkDeviceCreateInfo& info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 418 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 419 | VkResult Create(const VkInstanceCreateInfo* create_info, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 420 | const VkAllocationCallbacks* allocator, | 
|  | 421 | VkInstance* instance_out); | 
|  | 422 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 423 | VkResult Create(VkPhysicalDevice physical_dev, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 424 | const VkDeviceCreateInfo* create_info, | 
|  | 425 | const VkAllocationCallbacks* allocator, | 
|  | 426 | VkDevice* dev_out); | 
|  | 427 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 428 | VkResult ValidateExtensions(const char* const* extension_names, | 
|  | 429 | uint32_t extension_count); | 
|  | 430 | VkResult ValidateExtensions(VkPhysicalDevice physical_dev, | 
|  | 431 | const char* const* extension_names, | 
|  | 432 | uint32_t extension_count); | 
|  | 433 | VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const; | 
|  | 434 | bool IsLayerExtension(const char* name) const; | 
|  | 435 | bool IsDriverExtension(const char* name) const; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 436 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 437 | template <typename DataType> | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 438 | void StealLayers(DataType& data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 439 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 440 | static void DestroyLayers(ActiveLayer* layers, | 
|  | 441 | uint32_t count, | 
|  | 442 | const VkAllocationCallbacks& allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 443 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 444 | static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance, | 
|  | 445 | void* object); | 
|  | 446 | static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device, | 
|  | 447 | void* object); | 
|  | 448 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 449 | static VKAPI_ATTR VkBool32 | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 450 | DebugReportCallback(VkDebugReportFlagsEXT flags, | 
|  | 451 | VkDebugReportObjectTypeEXT obj_type, | 
|  | 452 | uint64_t obj, | 
|  | 453 | size_t location, | 
|  | 454 | int32_t msg_code, | 
|  | 455 | const char* layer_prefix, | 
|  | 456 | const char* msg, | 
|  | 457 | void* user_data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 458 |  | 
|  | 459 | const bool is_instance_; | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 460 | const driver::DebugReportLogger& logger_; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 461 | const VkAllocationCallbacks& allocator_; | 
|  | 462 |  | 
|  | 463 | OverrideLayerNames override_layers_; | 
|  | 464 | OverrideExtensionNames override_extensions_; | 
|  | 465 |  | 
|  | 466 | ActiveLayer* layers_; | 
|  | 467 | uint32_t layer_count_; | 
|  | 468 |  | 
|  | 469 | PFN_vkGetInstanceProcAddr get_instance_proc_addr_; | 
|  | 470 | PFN_vkGetDeviceProcAddr get_device_proc_addr_; | 
|  | 471 |  | 
|  | 472 | union { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 473 | VkLayerInstanceCreateInfo instance_chain_info_[2]; | 
|  | 474 | VkLayerDeviceCreateInfo device_chain_info_[2]; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 475 | }; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 476 |  | 
|  | 477 | VkExtensionProperties* driver_extensions_; | 
|  | 478 | uint32_t driver_extension_count_; | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 479 | std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 480 | }; | 
|  | 481 |  | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 482 | LayerChain::LayerChain(bool is_instance, | 
|  | 483 | const driver::DebugReportLogger& logger, | 
|  | 484 | const VkAllocationCallbacks& allocator) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 485 | : is_instance_(is_instance), | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 486 | logger_(logger), | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 487 | allocator_(allocator), | 
|  | 488 | override_layers_(is_instance, allocator), | 
|  | 489 | override_extensions_(is_instance, allocator), | 
|  | 490 | layers_(nullptr), | 
|  | 491 | layer_count_(0), | 
|  | 492 | get_instance_proc_addr_(nullptr), | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 493 | get_device_proc_addr_(nullptr), | 
|  | 494 | driver_extensions_(nullptr), | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 495 | driver_extension_count_(0) { | 
|  | 496 | enabled_extensions_.set(driver::ProcHook::EXTENSION_CORE); | 
|  | 497 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 498 |  | 
|  | 499 | LayerChain::~LayerChain() { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 500 | allocator_.pfnFree(allocator_.pUserData, driver_extensions_); | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 501 | DestroyLayers(layers_, layer_count_, allocator_); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 502 | } | 
|  | 503 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 504 | VkResult LayerChain::ActivateLayers(const char* const* layer_names, | 
|  | 505 | uint32_t layer_count, | 
|  | 506 | const char* const* extension_names, | 
|  | 507 | uint32_t extension_count) { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 508 | VkResult result = override_layers_.Parse(layer_names, layer_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 509 | if (result != VK_SUCCESS) | 
|  | 510 | return result; | 
|  | 511 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 512 | result = override_extensions_.Parse(extension_names, extension_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 513 | if (result != VK_SUCCESS) | 
|  | 514 | return result; | 
|  | 515 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 516 | if (override_layers_.Count()) { | 
|  | 517 | layer_names = override_layers_.Names(); | 
|  | 518 | layer_count = override_layers_.Count(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 519 | } | 
|  | 520 |  | 
|  | 521 | if (!layer_count) { | 
|  | 522 | // point head of chain to the driver | 
|  | 523 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 524 |  | 
|  | 525 | return VK_SUCCESS; | 
|  | 526 | } | 
|  | 527 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 528 | layers_ = AllocateLayerArray(layer_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 529 | if (!layers_) | 
|  | 530 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 531 |  | 
|  | 532 | // load layers | 
|  | 533 | for (uint32_t i = 0; i < layer_count; i++) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 534 | result = LoadLayer(layers_[i], layer_names[i]); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 535 | if (result != VK_SUCCESS) | 
|  | 536 | return result; | 
|  | 537 |  | 
|  | 538 | // count loaded layers for proper destructions on errors | 
|  | 539 | layer_count_++; | 
|  | 540 | } | 
|  | 541 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 542 | SetupLayerLinks(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 543 |  | 
|  | 544 | return VK_SUCCESS; | 
|  | 545 | } | 
|  | 546 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 547 | VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev, | 
|  | 548 | const char* const* layer_names, | 
|  | 549 | uint32_t layer_count, | 
|  | 550 | const char* const* extension_names, | 
|  | 551 | uint32_t extension_count) { | 
|  | 552 | uint32_t instance_layer_count; | 
|  | 553 | const ActiveLayer* instance_layers = | 
|  | 554 | GetActiveLayers(physical_dev, instance_layer_count); | 
|  | 555 |  | 
|  | 556 | // log a message if the application device layer array is not empty nor an | 
|  | 557 | // exact match of the instance layer array. | 
|  | 558 | if (layer_count) { | 
|  | 559 | bool exact_match = (instance_layer_count == layer_count); | 
|  | 560 | if (exact_match) { | 
|  | 561 | for (uint32_t i = 0; i < instance_layer_count; i++) { | 
|  | 562 | const Layer& l = *instance_layers[i].ref; | 
|  | 563 | if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) { | 
|  | 564 | exact_match = false; | 
|  | 565 | break; | 
|  | 566 | } | 
|  | 567 | } | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | if (!exact_match) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 571 | logger_.Warn(physical_dev, | 
|  | 572 | "Device layers disagree with instance layers and are " | 
|  | 573 | "overridden by instance layers"); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 574 | } | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | VkResult result = | 
|  | 578 | override_extensions_.Parse(extension_names, extension_count); | 
|  | 579 | if (result != VK_SUCCESS) | 
|  | 580 | return result; | 
|  | 581 |  | 
|  | 582 | if (!instance_layer_count) { | 
|  | 583 | // point head of chain to the driver | 
|  | 584 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
|  | 585 | get_device_proc_addr_ = driver::GetDeviceProcAddr; | 
|  | 586 |  | 
|  | 587 | return VK_SUCCESS; | 
|  | 588 | } | 
|  | 589 |  | 
|  | 590 | layers_ = AllocateLayerArray(instance_layer_count); | 
|  | 591 | if (!layers_) | 
|  | 592 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 593 |  | 
|  | 594 | for (uint32_t i = 0; i < instance_layer_count; i++) { | 
|  | 595 | const Layer& l = *instance_layers[i].ref; | 
|  | 596 |  | 
|  | 597 | // no need to and cannot chain non-global layers | 
|  | 598 | if (!IsLayerGlobal(l)) | 
|  | 599 | continue; | 
|  | 600 |  | 
|  | 601 | // this never fails | 
|  | 602 | new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}}; | 
|  | 603 | } | 
|  | 604 |  | 
| Chia-I Wu | 61b25fd | 2016-05-27 10:18:25 +0800 | [diff] [blame] | 605 | // this may happen when all layers are non-global ones | 
|  | 606 | if (!layer_count_) { | 
|  | 607 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
|  | 608 | get_device_proc_addr_ = driver::GetDeviceProcAddr; | 
|  | 609 | return VK_SUCCESS; | 
|  | 610 | } | 
|  | 611 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 612 | SetupLayerLinks(); | 
|  | 613 |  | 
|  | 614 | return VK_SUCCESS; | 
|  | 615 | } | 
|  | 616 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 617 | LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 618 | VkSystemAllocationScope scope = (is_instance_) | 
|  | 619 | ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 620 | : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 621 |  | 
|  | 622 | return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation( | 
|  | 623 | allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer), | 
|  | 624 | scope)); | 
|  | 625 | } | 
|  | 626 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 627 | VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) { | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 628 | const Layer* l = FindLayer(name); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 629 | if (!l) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 630 | logger_.Err(VK_NULL_HANDLE, "Failed to find layer %s", name); | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 631 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 632 | } | 
|  | 633 |  | 
| Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 634 | new (&layer) ActiveLayer{GetLayerRef(*l), {}}; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 635 | if (!layer.ref) { | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 636 | ALOGW("Failed to open layer %s", name); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 637 | layer.ref.~LayerRef(); | 
|  | 638 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 639 | } | 
|  | 640 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 641 | ALOGI("Loaded layer %s", name); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 642 |  | 
|  | 643 | return VK_SUCCESS; | 
|  | 644 | } | 
|  | 645 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 646 | void LayerChain::SetupLayerLinks() { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 647 | if (is_instance_) { | 
|  | 648 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 649 | ActiveLayer& layer = layers_[i]; | 
|  | 650 |  | 
|  | 651 | // point head of chain to the first layer | 
|  | 652 | if (i == 0) | 
|  | 653 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); | 
|  | 654 |  | 
|  | 655 | // point tail of chain to the driver | 
|  | 656 | if (i == layer_count_ - 1) { | 
|  | 657 | layer.instance_link.pNext = nullptr; | 
|  | 658 | layer.instance_link.pfnNextGetInstanceProcAddr = | 
|  | 659 | driver::GetInstanceProcAddr; | 
|  | 660 | break; | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | const ActiveLayer& next = layers_[i + 1]; | 
|  | 664 |  | 
|  | 665 | // const_cast as some naughty layers want to modify our links! | 
|  | 666 | layer.instance_link.pNext = | 
|  | 667 | const_cast<VkLayerInstanceLink*>(&next.instance_link); | 
|  | 668 | layer.instance_link.pfnNextGetInstanceProcAddr = | 
|  | 669 | next.ref.GetGetInstanceProcAddr(); | 
|  | 670 | } | 
|  | 671 | } else { | 
|  | 672 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 673 | ActiveLayer& layer = layers_[i]; | 
|  | 674 |  | 
|  | 675 | // point head of chain to the first layer | 
|  | 676 | if (i == 0) { | 
|  | 677 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); | 
|  | 678 | get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr(); | 
|  | 679 | } | 
|  | 680 |  | 
|  | 681 | // point tail of chain to the driver | 
|  | 682 | if (i == layer_count_ - 1) { | 
|  | 683 | layer.device_link.pNext = nullptr; | 
|  | 684 | layer.device_link.pfnNextGetInstanceProcAddr = | 
|  | 685 | driver::GetInstanceProcAddr; | 
|  | 686 | layer.device_link.pfnNextGetDeviceProcAddr = | 
|  | 687 | driver::GetDeviceProcAddr; | 
|  | 688 | break; | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 | const ActiveLayer& next = layers_[i + 1]; | 
|  | 692 |  | 
|  | 693 | // const_cast as some naughty layers want to modify our links! | 
|  | 694 | layer.device_link.pNext = | 
|  | 695 | const_cast<VkLayerDeviceLink*>(&next.device_link); | 
|  | 696 | layer.device_link.pfnNextGetInstanceProcAddr = | 
|  | 697 | next.ref.GetGetInstanceProcAddr(); | 
|  | 698 | layer.device_link.pfnNextGetDeviceProcAddr = | 
|  | 699 | next.ref.GetGetDeviceProcAddr(); | 
|  | 700 | } | 
|  | 701 | } | 
|  | 702 | } | 
|  | 703 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 704 | bool LayerChain::Empty() const { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 705 | return (!layer_count_ && !override_layers_.Count() && | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 706 | !override_extensions_.Count()); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 707 | } | 
|  | 708 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 709 | void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 710 | if (layer_count_) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 711 | auto& link_info = instance_chain_info_[1]; | 
|  | 712 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; | 
|  | 713 | link_info.pNext = info.pNext; | 
|  | 714 | link_info.function = VK_LAYER_FUNCTION_LINK; | 
|  | 715 | link_info.u.pLayerInfo = &layers_[0].instance_link; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 716 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 717 | auto& cb_info = instance_chain_info_[0]; | 
|  | 718 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; | 
|  | 719 | cb_info.pNext = &link_info; | 
|  | 720 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; | 
|  | 721 | cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 722 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 723 | info.pNext = &cb_info; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 726 | if (override_layers_.Count()) { | 
|  | 727 | info.enabledLayerCount = override_layers_.Count(); | 
|  | 728 | info.ppEnabledLayerNames = override_layers_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 729 | } | 
|  | 730 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 731 | if (override_extensions_.Count()) { | 
|  | 732 | info.enabledExtensionCount = override_extensions_.Count(); | 
|  | 733 | info.ppEnabledExtensionNames = override_extensions_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 734 | } | 
|  | 735 | } | 
|  | 736 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 737 | void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 738 | if (layer_count_) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 739 | auto& link_info = device_chain_info_[1]; | 
|  | 740 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; | 
|  | 741 | link_info.pNext = info.pNext; | 
|  | 742 | link_info.function = VK_LAYER_FUNCTION_LINK; | 
|  | 743 | link_info.u.pLayerInfo = &layers_[0].device_link; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 744 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 745 | auto& cb_info = device_chain_info_[0]; | 
|  | 746 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; | 
|  | 747 | cb_info.pNext = &link_info; | 
|  | 748 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; | 
|  | 749 | cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 750 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 751 | info.pNext = &cb_info; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 752 | } | 
|  | 753 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 754 | if (override_layers_.Count()) { | 
|  | 755 | info.enabledLayerCount = override_layers_.Count(); | 
|  | 756 | info.ppEnabledLayerNames = override_layers_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 757 | } | 
|  | 758 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 759 | if (override_extensions_.Count()) { | 
|  | 760 | info.enabledExtensionCount = override_extensions_.Count(); | 
|  | 761 | info.ppEnabledExtensionNames = override_extensions_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 762 | } | 
|  | 763 | } | 
|  | 764 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 765 | VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 766 | const VkAllocationCallbacks* allocator, | 
|  | 767 | VkInstance* instance_out) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 768 | VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames, | 
|  | 769 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 770 | if (result != VK_SUCCESS) | 
|  | 771 | return result; | 
|  | 772 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 773 | // call down the chain | 
|  | 774 | PFN_vkCreateInstance create_instance = | 
|  | 775 | reinterpret_cast<PFN_vkCreateInstance>( | 
|  | 776 | get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance")); | 
|  | 777 | VkInstance instance; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 778 | result = create_instance(create_info, allocator, &instance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 779 | if (result != VK_SUCCESS) | 
|  | 780 | return result; | 
|  | 781 |  | 
|  | 782 | // initialize InstanceData | 
|  | 783 | InstanceData& data = GetData(instance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 784 |  | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 785 | if (!InitDispatchTable(instance, get_instance_proc_addr_, | 
|  | 786 | enabled_extensions_)) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 787 | if (data.dispatch.DestroyInstance) | 
|  | 788 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 789 |  | 
|  | 790 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 791 | } | 
|  | 792 |  | 
|  | 793 | // install debug report callback | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 794 | if (override_extensions_.InstallDebugCallback()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 795 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = | 
|  | 796 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( | 
|  | 797 | get_instance_proc_addr_(instance, | 
|  | 798 | "vkCreateDebugReportCallbackEXT")); | 
|  | 799 | data.destroy_debug_callback = | 
|  | 800 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( | 
|  | 801 | get_instance_proc_addr_(instance, | 
|  | 802 | "vkDestroyDebugReportCallbackEXT")); | 
|  | 803 | if (!create_debug_report_callback || !data.destroy_debug_callback) { | 
|  | 804 | ALOGE("Broken VK_EXT_debug_report support"); | 
|  | 805 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 806 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | VkDebugReportCallbackCreateInfoEXT debug_callback_info = {}; | 
|  | 810 | debug_callback_info.sType = | 
|  | 811 | VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; | 
|  | 812 | debug_callback_info.flags = | 
|  | 813 | 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] | 814 | debug_callback_info.pfnCallback = DebugReportCallback; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 815 |  | 
|  | 816 | VkDebugReportCallbackEXT debug_callback; | 
|  | 817 | result = create_debug_report_callback(instance, &debug_callback_info, | 
|  | 818 | nullptr, &debug_callback); | 
|  | 819 | if (result != VK_SUCCESS) { | 
|  | 820 | ALOGE("Failed to install debug report callback"); | 
|  | 821 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 822 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 823 | } | 
|  | 824 |  | 
|  | 825 | data.debug_callback = debug_callback; | 
|  | 826 |  | 
|  | 827 | ALOGI("Installed debug report callback"); | 
|  | 828 | } | 
|  | 829 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 830 | StealLayers(data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 831 |  | 
|  | 832 | *instance_out = instance; | 
|  | 833 |  | 
|  | 834 | return VK_SUCCESS; | 
|  | 835 | } | 
|  | 836 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 837 | VkResult LayerChain::Create(VkPhysicalDevice physical_dev, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 838 | const VkDeviceCreateInfo* create_info, | 
|  | 839 | const VkAllocationCallbacks* allocator, | 
|  | 840 | VkDevice* dev_out) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 841 | VkResult result = | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 842 | ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames, | 
|  | 843 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 844 | if (result != VK_SUCCESS) | 
|  | 845 | return result; | 
|  | 846 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 847 | // call down the chain | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 848 | PFN_vkCreateDevice create_device = | 
|  | 849 | GetData(physical_dev).dispatch.CreateDevice; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 850 | VkDevice dev; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 851 | result = create_device(physical_dev, create_info, allocator, &dev); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 852 | if (result != VK_SUCCESS) | 
|  | 853 | return result; | 
|  | 854 |  | 
|  | 855 | // initialize DeviceData | 
|  | 856 | DeviceData& data = GetData(dev); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 857 |  | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 858 | if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 859 | if (data.dispatch.DestroyDevice) | 
|  | 860 | data.dispatch.DestroyDevice(dev, allocator); | 
|  | 861 |  | 
|  | 862 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 863 | } | 
|  | 864 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 865 | // no StealLayers so that active layers are destroyed with this | 
|  | 866 | // LayerChain | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 867 | *dev_out = dev; | 
|  | 868 |  | 
|  | 869 | return VK_SUCCESS; | 
|  | 870 | } | 
|  | 871 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 872 | VkResult LayerChain::ValidateExtensions(const char* const* extension_names, | 
|  | 873 | uint32_t extension_count) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 874 | if (!extension_count) | 
|  | 875 | return VK_SUCCESS; | 
|  | 876 |  | 
|  | 877 | // query driver instance extensions | 
|  | 878 | uint32_t count; | 
|  | 879 | VkResult result = | 
|  | 880 | EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); | 
|  | 881 | if (result == VK_SUCCESS && count) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 882 | driver_extensions_ = AllocateDriverExtensionArray(count); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 883 | result = (driver_extensions_) ? EnumerateInstanceExtensionProperties( | 
|  | 884 | nullptr, &count, driver_extensions_) | 
|  | 885 | : VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 886 | } | 
|  | 887 | if (result != VK_SUCCESS) | 
|  | 888 | return result; | 
|  | 889 |  | 
|  | 890 | driver_extension_count_ = count; | 
|  | 891 |  | 
|  | 892 | for (uint32_t i = 0; i < extension_count; i++) { | 
|  | 893 | const char* name = extension_names[i]; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 894 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 895 | logger_.Err(VK_NULL_HANDLE, | 
|  | 896 | "Failed to enable missing instance extension %s", name); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 897 | return VK_ERROR_EXTENSION_NOT_PRESENT; | 
|  | 898 | } | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 899 |  | 
|  | 900 | auto ext_bit = driver::GetProcHookExtension(name); | 
|  | 901 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) | 
|  | 902 | enabled_extensions_.set(ext_bit); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 903 | } | 
|  | 904 |  | 
|  | 905 | return VK_SUCCESS; | 
|  | 906 | } | 
|  | 907 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 908 | VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev, | 
|  | 909 | const char* const* extension_names, | 
|  | 910 | uint32_t extension_count) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 911 | if (!extension_count) | 
|  | 912 | return VK_SUCCESS; | 
|  | 913 |  | 
|  | 914 | // query driver device extensions | 
|  | 915 | uint32_t count; | 
|  | 916 | VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr, | 
|  | 917 | &count, nullptr); | 
|  | 918 | if (result == VK_SUCCESS && count) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 919 | driver_extensions_ = AllocateDriverExtensionArray(count); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 920 | result = (driver_extensions_) | 
|  | 921 | ? EnumerateDeviceExtensionProperties( | 
|  | 922 | physical_dev, nullptr, &count, driver_extensions_) | 
|  | 923 | : VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 924 | } | 
|  | 925 | if (result != VK_SUCCESS) | 
|  | 926 | return result; | 
|  | 927 |  | 
|  | 928 | driver_extension_count_ = count; | 
|  | 929 |  | 
|  | 930 | for (uint32_t i = 0; i < extension_count; i++) { | 
|  | 931 | const char* name = extension_names[i]; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 932 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 933 | logger_.Err(physical_dev, | 
|  | 934 | "Failed to enable missing device extension %s", name); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 935 | return VK_ERROR_EXTENSION_NOT_PRESENT; | 
|  | 936 | } | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 937 |  | 
|  | 938 | auto ext_bit = driver::GetProcHookExtension(name); | 
|  | 939 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) | 
|  | 940 | enabled_extensions_.set(ext_bit); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 941 | } | 
|  | 942 |  | 
|  | 943 | return VK_SUCCESS; | 
|  | 944 | } | 
|  | 945 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 946 | VkExtensionProperties* LayerChain::AllocateDriverExtensionArray( | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 947 | uint32_t count) const { | 
|  | 948 | return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( | 
|  | 949 | allocator_.pUserData, sizeof(VkExtensionProperties) * count, | 
|  | 950 | alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); | 
|  | 951 | } | 
|  | 952 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 953 | bool LayerChain::IsLayerExtension(const char* name) const { | 
| Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 954 | if (is_instance_) { | 
|  | 955 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 956 | const ActiveLayer& layer = layers_[i]; | 
|  | 957 | if (FindLayerInstanceExtension(*layer.ref, name)) | 
|  | 958 | return true; | 
|  | 959 | } | 
|  | 960 | } else { | 
|  | 961 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 962 | const ActiveLayer& layer = layers_[i]; | 
|  | 963 | if (FindLayerDeviceExtension(*layer.ref, name)) | 
|  | 964 | return true; | 
|  | 965 | } | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 966 | } | 
|  | 967 |  | 
|  | 968 | return false; | 
|  | 969 | } | 
|  | 970 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 971 | bool LayerChain::IsDriverExtension(const char* name) const { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 972 | for (uint32_t i = 0; i < driver_extension_count_; i++) { | 
|  | 973 | if (strcmp(driver_extensions_[i].extensionName, name) == 0) | 
|  | 974 | return true; | 
|  | 975 | } | 
|  | 976 |  | 
|  | 977 | return false; | 
|  | 978 | } | 
|  | 979 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 980 | template <typename DataType> | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 981 | void LayerChain::StealLayers(DataType& data) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 982 | data.layers = layers_; | 
|  | 983 | data.layer_count = layer_count_; | 
|  | 984 |  | 
|  | 985 | layers_ = nullptr; | 
|  | 986 | layer_count_ = 0; | 
|  | 987 | } | 
|  | 988 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 989 | void LayerChain::DestroyLayers(ActiveLayer* layers, | 
|  | 990 | uint32_t count, | 
|  | 991 | const VkAllocationCallbacks& allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 992 | for (uint32_t i = 0; i < count; i++) | 
|  | 993 | layers[i].ref.~LayerRef(); | 
|  | 994 |  | 
|  | 995 | allocator.pfnFree(allocator.pUserData, layers); | 
|  | 996 | } | 
|  | 997 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 998 | VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) { | 
|  | 999 | driver::InstanceDispatchable dispatchable = | 
|  | 1000 | reinterpret_cast<driver::InstanceDispatchable>(object); | 
|  | 1001 |  | 
|  | 1002 | return (driver::SetDataInternal(dispatchable, &driver::GetData(instance))) | 
|  | 1003 | ? VK_SUCCESS | 
|  | 1004 | : VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1005 | } | 
|  | 1006 |  | 
|  | 1007 | VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) { | 
|  | 1008 | driver::DeviceDispatchable dispatchable = | 
|  | 1009 | reinterpret_cast<driver::DeviceDispatchable>(object); | 
|  | 1010 |  | 
|  | 1011 | return (driver::SetDataInternal(dispatchable, &driver::GetData(device))) | 
|  | 1012 | ? VK_SUCCESS | 
|  | 1013 | : VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1014 | } | 
|  | 1015 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1016 | VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags, | 
|  | 1017 | VkDebugReportObjectTypeEXT obj_type, | 
|  | 1018 | uint64_t obj, | 
|  | 1019 | size_t location, | 
|  | 1020 | int32_t msg_code, | 
|  | 1021 | const char* layer_prefix, | 
|  | 1022 | const char* msg, | 
|  | 1023 | void* user_data) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1024 | int prio; | 
|  | 1025 |  | 
|  | 1026 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) | 
|  | 1027 | prio = ANDROID_LOG_ERROR; | 
|  | 1028 | else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | | 
|  | 1029 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) | 
|  | 1030 | prio = ANDROID_LOG_WARN; | 
|  | 1031 | else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) | 
|  | 1032 | prio = ANDROID_LOG_INFO; | 
|  | 1033 | else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) | 
|  | 1034 | prio = ANDROID_LOG_DEBUG; | 
|  | 1035 | else | 
|  | 1036 | prio = ANDROID_LOG_UNKNOWN; | 
|  | 1037 |  | 
|  | 1038 | LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg); | 
|  | 1039 |  | 
|  | 1040 | (void)obj_type; | 
|  | 1041 | (void)obj; | 
|  | 1042 | (void)location; | 
|  | 1043 | (void)user_data; | 
|  | 1044 |  | 
|  | 1045 | return false; | 
|  | 1046 | } | 
|  | 1047 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1048 | VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info, | 
|  | 1049 | const VkAllocationCallbacks* allocator, | 
|  | 1050 | VkInstance* instance_out) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1051 | LayerChain chain(true, driver::DebugReportLogger(*create_info), | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1052 | (allocator) ? *allocator : driver::GetDefaultAllocator()); | 
|  | 1053 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1054 | VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, | 
|  | 1055 | create_info->enabledLayerCount, | 
|  | 1056 | create_info->ppEnabledExtensionNames, | 
|  | 1057 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1058 | if (result != VK_SUCCESS) | 
|  | 1059 | return result; | 
|  | 1060 |  | 
|  | 1061 | // use a local create info when the chain is not empty | 
|  | 1062 | VkInstanceCreateInfo local_create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1063 | if (!chain.Empty()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1064 | local_create_info = *create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1065 | chain.ModifyCreateInfo(local_create_info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1066 | create_info = &local_create_info; | 
|  | 1067 | } | 
|  | 1068 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1069 | return chain.Create(create_info, allocator, instance_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1070 | } | 
|  | 1071 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1072 | VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev, | 
|  | 1073 | const VkDeviceCreateInfo* create_info, | 
|  | 1074 | const VkAllocationCallbacks* allocator, | 
|  | 1075 | VkDevice* dev_out) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1076 | LayerChain chain( | 
|  | 1077 | false, driver::Logger(physical_dev), | 
|  | 1078 | (allocator) ? *allocator : driver::GetData(physical_dev).allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1079 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1080 | VkResult result = chain.ActivateLayers( | 
|  | 1081 | physical_dev, create_info->ppEnabledLayerNames, | 
|  | 1082 | create_info->enabledLayerCount, create_info->ppEnabledExtensionNames, | 
|  | 1083 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1084 | if (result != VK_SUCCESS) | 
|  | 1085 | return result; | 
|  | 1086 |  | 
|  | 1087 | // use a local create info when the chain is not empty | 
|  | 1088 | VkDeviceCreateInfo local_create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1089 | if (!chain.Empty()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1090 | local_create_info = *create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1091 | chain.ModifyCreateInfo(local_create_info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1092 | create_info = &local_create_info; | 
|  | 1093 | } | 
|  | 1094 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1095 | return chain.Create(physical_dev, create_info, allocator, dev_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1096 | } | 
|  | 1097 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1098 | void LayerChain::DestroyInstance(VkInstance instance, | 
|  | 1099 | const VkAllocationCallbacks* allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1100 | InstanceData& data = GetData(instance); | 
|  | 1101 |  | 
|  | 1102 | if (data.debug_callback != VK_NULL_HANDLE) | 
|  | 1103 | data.destroy_debug_callback(instance, data.debug_callback, allocator); | 
|  | 1104 |  | 
|  | 1105 | ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); | 
|  | 1106 | uint32_t layer_count = data.layer_count; | 
|  | 1107 |  | 
|  | 1108 | VkAllocationCallbacks local_allocator; | 
|  | 1109 | if (!allocator) | 
|  | 1110 | local_allocator = driver::GetData(instance).allocator; | 
|  | 1111 |  | 
|  | 1112 | // this also destroys InstanceData | 
|  | 1113 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 1114 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1115 | DestroyLayers(layers, layer_count, | 
|  | 1116 | (allocator) ? *allocator : local_allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1117 | } | 
|  | 1118 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1119 | void LayerChain::DestroyDevice(VkDevice device, | 
|  | 1120 | const VkAllocationCallbacks* allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1121 | DeviceData& data = GetData(device); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1122 | // this also destroys DeviceData | 
|  | 1123 | data.dispatch.DestroyDevice(device, allocator); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1124 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1125 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1126 | const LayerChain::ActiveLayer* LayerChain::GetActiveLayers( | 
|  | 1127 | VkPhysicalDevice physical_dev, | 
|  | 1128 | uint32_t& count) { | 
|  | 1129 | count = GetData(physical_dev).layer_count; | 
|  | 1130 | return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | // ---------------------------------------------------------------------------- | 
|  | 1134 |  | 
|  | 1135 | bool EnsureInitialized() { | 
|  | 1136 | static std::once_flag once_flag; | 
|  | 1137 | static bool initialized; | 
|  | 1138 |  | 
|  | 1139 | std::call_once(once_flag, []() { | 
|  | 1140 | if (driver::OpenHAL()) { | 
|  | 1141 | DiscoverLayers(); | 
|  | 1142 | initialized = true; | 
|  | 1143 | } | 
|  | 1144 | }); | 
|  | 1145 |  | 
|  | 1146 | return initialized; | 
|  | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | }  // anonymous namespace | 
|  | 1150 |  | 
|  | 1151 | VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, | 
|  | 1152 | const VkAllocationCallbacks* pAllocator, | 
|  | 1153 | VkInstance* pInstance) { | 
|  | 1154 | if (!EnsureInitialized()) | 
|  | 1155 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1156 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1157 | return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1158 | } | 
|  | 1159 |  | 
|  | 1160 | void DestroyInstance(VkInstance instance, | 
|  | 1161 | const VkAllocationCallbacks* pAllocator) { | 
|  | 1162 | if (instance != VK_NULL_HANDLE) | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1163 | LayerChain::DestroyInstance(instance, pAllocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1164 | } | 
|  | 1165 |  | 
|  | 1166 | VkResult CreateDevice(VkPhysicalDevice physicalDevice, | 
|  | 1167 | const VkDeviceCreateInfo* pCreateInfo, | 
|  | 1168 | const VkAllocationCallbacks* pAllocator, | 
|  | 1169 | VkDevice* pDevice) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1170 | return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator, | 
|  | 1171 | pDevice); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1172 | } | 
|  | 1173 |  | 
|  | 1174 | void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { | 
|  | 1175 | if (device != VK_NULL_HANDLE) | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1176 | LayerChain::DestroyDevice(device, pAllocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount, | 
|  | 1180 | VkLayerProperties* pProperties) { | 
|  | 1181 | if (!EnsureInitialized()) | 
|  | 1182 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1183 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1184 | uint32_t count = GetLayerCount(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1185 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1186 | if (!pProperties) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1187 | *pPropertyCount = count; | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1188 | return VK_SUCCESS; | 
|  | 1189 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1190 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1191 | uint32_t copied = std::min(*pPropertyCount, count); | 
|  | 1192 | for (uint32_t i = 0; i < copied; i++) | 
|  | 1193 | pProperties[i] = GetLayerProperties(GetLayer(i)); | 
|  | 1194 | *pPropertyCount = copied; | 
|  | 1195 |  | 
|  | 1196 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1197 | } | 
|  | 1198 |  | 
|  | 1199 | VkResult EnumerateInstanceExtensionProperties( | 
|  | 1200 | const char* pLayerName, | 
|  | 1201 | uint32_t* pPropertyCount, | 
|  | 1202 | VkExtensionProperties* pProperties) { | 
|  | 1203 | if (!EnsureInitialized()) | 
|  | 1204 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1205 |  | 
|  | 1206 | if (pLayerName) { | 
| Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1207 | const Layer* layer = FindLayer(pLayerName); | 
| Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1208 | if (!layer) | 
|  | 1209 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 1210 |  | 
|  | 1211 | uint32_t count; | 
|  | 1212 | const VkExtensionProperties* props = | 
|  | 1213 | GetLayerInstanceExtensions(*layer, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1214 |  | 
|  | 1215 | if (!pProperties || *pPropertyCount > count) | 
|  | 1216 | *pPropertyCount = count; | 
|  | 1217 | if (pProperties) | 
|  | 1218 | std::copy(props, props + *pPropertyCount, pProperties); | 
|  | 1219 |  | 
|  | 1220 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; | 
|  | 1221 | } | 
|  | 1222 |  | 
|  | 1223 | // TODO how about extensions from implicitly enabled layers? | 
|  | 1224 | return vulkan::driver::EnumerateInstanceExtensionProperties( | 
|  | 1225 | nullptr, pPropertyCount, pProperties); | 
|  | 1226 | } | 
|  | 1227 |  | 
|  | 1228 | VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, | 
|  | 1229 | uint32_t* pPropertyCount, | 
|  | 1230 | VkLayerProperties* pProperties) { | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1231 | uint32_t count; | 
|  | 1232 | const LayerChain::ActiveLayer* layers = | 
|  | 1233 | LayerChain::GetActiveLayers(physicalDevice, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1234 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1235 | if (!pProperties) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1236 | *pPropertyCount = count; | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1237 | return VK_SUCCESS; | 
|  | 1238 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1239 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1240 | uint32_t copied = std::min(*pPropertyCount, count); | 
|  | 1241 | for (uint32_t i = 0; i < copied; i++) | 
|  | 1242 | pProperties[i] = GetLayerProperties(*layers[i].ref); | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1243 | *pPropertyCount = copied; | 
|  | 1244 |  | 
|  | 1245 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1246 | } | 
|  | 1247 |  | 
|  | 1248 | VkResult EnumerateDeviceExtensionProperties( | 
|  | 1249 | VkPhysicalDevice physicalDevice, | 
|  | 1250 | const char* pLayerName, | 
|  | 1251 | uint32_t* pPropertyCount, | 
|  | 1252 | VkExtensionProperties* pProperties) { | 
|  | 1253 | if (pLayerName) { | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1254 | // EnumerateDeviceLayerProperties enumerates active layers for | 
|  | 1255 | // backward compatibility.  The extension query here should work for | 
|  | 1256 | // all layers. | 
| Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1257 | const Layer* layer = FindLayer(pLayerName); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1258 | if (!layer) | 
| Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1259 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 1260 |  | 
|  | 1261 | uint32_t count; | 
|  | 1262 | const VkExtensionProperties* props = | 
|  | 1263 | GetLayerDeviceExtensions(*layer, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1264 |  | 
|  | 1265 | if (!pProperties || *pPropertyCount > count) | 
|  | 1266 | *pPropertyCount = count; | 
|  | 1267 | if (pProperties) | 
|  | 1268 | std::copy(props, props + *pPropertyCount, pProperties); | 
|  | 1269 |  | 
|  | 1270 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; | 
|  | 1271 | } | 
|  | 1272 |  | 
|  | 1273 | // TODO how about extensions from implicitly enabled layers? | 
|  | 1274 | const InstanceData& data = GetData(physicalDevice); | 
|  | 1275 | return data.dispatch.EnumerateDeviceExtensionProperties( | 
|  | 1276 | physicalDevice, nullptr, pPropertyCount, pProperties); | 
|  | 1277 | } | 
|  | 1278 |  | 
|  | 1279 | }  // namespace api | 
|  | 1280 | }  // namespace vulkan |