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