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 | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 397 | LayerChain(bool is_instance, const VkAllocationCallbacks& allocator); |
| 398 | ~LayerChain(); |
| 399 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 400 | VkResult ActivateLayers(const char* const* layer_names, |
| 401 | uint32_t layer_count, |
| 402 | const char* const* extension_names, |
| 403 | uint32_t extension_count); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 404 | VkResult ActivateLayers(VkPhysicalDevice physical_dev, |
| 405 | const char* const* layer_names, |
| 406 | uint32_t layer_count, |
| 407 | const char* const* extension_names, |
| 408 | uint32_t extension_count); |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 409 | ActiveLayer* AllocateLayerArray(uint32_t count) const; |
| 410 | VkResult LoadLayer(ActiveLayer& layer, const char* name); |
| 411 | void SetupLayerLinks(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 412 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 413 | bool Empty() const; |
| 414 | void ModifyCreateInfo(VkInstanceCreateInfo& info); |
| 415 | void ModifyCreateInfo(VkDeviceCreateInfo& info); |
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 | VkResult Create(const VkInstanceCreateInfo* create_info, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 418 | const VkAllocationCallbacks* allocator, |
| 419 | VkInstance* instance_out); |
| 420 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 421 | VkResult Create(VkPhysicalDevice physical_dev, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 422 | const VkDeviceCreateInfo* create_info, |
| 423 | const VkAllocationCallbacks* allocator, |
| 424 | VkDevice* dev_out); |
| 425 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 426 | VkResult ValidateExtensions(const char* const* extension_names, |
| 427 | uint32_t extension_count); |
| 428 | VkResult ValidateExtensions(VkPhysicalDevice physical_dev, |
| 429 | const char* const* extension_names, |
| 430 | uint32_t extension_count); |
| 431 | VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const; |
| 432 | bool IsLayerExtension(const char* name) const; |
| 433 | bool IsDriverExtension(const char* name) const; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 434 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 435 | template <typename DataType> |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 436 | void StealLayers(DataType& data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 437 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 438 | static void DestroyLayers(ActiveLayer* layers, |
| 439 | uint32_t count, |
| 440 | const VkAllocationCallbacks& allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 441 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 442 | static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance, |
| 443 | void* object); |
| 444 | static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device, |
| 445 | void* object); |
| 446 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 447 | static VKAPI_ATTR VkBool32 |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 448 | DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 449 | VkDebugReportObjectTypeEXT obj_type, |
| 450 | uint64_t obj, |
| 451 | size_t location, |
| 452 | int32_t msg_code, |
| 453 | const char* layer_prefix, |
| 454 | const char* msg, |
| 455 | void* user_data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 456 | |
| 457 | const bool is_instance_; |
| 458 | const VkAllocationCallbacks& allocator_; |
| 459 | |
| 460 | OverrideLayerNames override_layers_; |
| 461 | OverrideExtensionNames override_extensions_; |
| 462 | |
| 463 | ActiveLayer* layers_; |
| 464 | uint32_t layer_count_; |
| 465 | |
| 466 | PFN_vkGetInstanceProcAddr get_instance_proc_addr_; |
| 467 | PFN_vkGetDeviceProcAddr get_device_proc_addr_; |
| 468 | |
| 469 | union { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 470 | VkLayerInstanceCreateInfo instance_chain_info_[2]; |
| 471 | VkLayerDeviceCreateInfo device_chain_info_[2]; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 472 | }; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 473 | |
| 474 | VkExtensionProperties* driver_extensions_; |
| 475 | uint32_t driver_extension_count_; |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 476 | std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator) |
| 480 | : is_instance_(is_instance), |
| 481 | allocator_(allocator), |
| 482 | override_layers_(is_instance, allocator), |
| 483 | override_extensions_(is_instance, allocator), |
| 484 | layers_(nullptr), |
| 485 | layer_count_(0), |
| 486 | get_instance_proc_addr_(nullptr), |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 487 | get_device_proc_addr_(nullptr), |
| 488 | driver_extensions_(nullptr), |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 489 | driver_extension_count_(0) { |
| 490 | enabled_extensions_.set(driver::ProcHook::EXTENSION_CORE); |
| 491 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 492 | |
| 493 | LayerChain::~LayerChain() { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 494 | allocator_.pfnFree(allocator_.pUserData, driver_extensions_); |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 495 | DestroyLayers(layers_, layer_count_, allocator_); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 498 | VkResult LayerChain::ActivateLayers(const char* const* layer_names, |
| 499 | uint32_t layer_count, |
| 500 | const char* const* extension_names, |
| 501 | uint32_t extension_count) { |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 502 | VkResult result = override_layers_.Parse(layer_names, layer_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 503 | if (result != VK_SUCCESS) |
| 504 | return result; |
| 505 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 506 | result = override_extensions_.Parse(extension_names, extension_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 507 | if (result != VK_SUCCESS) |
| 508 | return result; |
| 509 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 510 | if (override_layers_.Count()) { |
| 511 | layer_names = override_layers_.Names(); |
| 512 | layer_count = override_layers_.Count(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if (!layer_count) { |
| 516 | // point head of chain to the driver |
| 517 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 518 | |
| 519 | return VK_SUCCESS; |
| 520 | } |
| 521 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 522 | layers_ = AllocateLayerArray(layer_count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 523 | if (!layers_) |
| 524 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 525 | |
| 526 | // load layers |
| 527 | for (uint32_t i = 0; i < layer_count; i++) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 528 | result = LoadLayer(layers_[i], layer_names[i]); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 529 | if (result != VK_SUCCESS) |
| 530 | return result; |
| 531 | |
| 532 | // count loaded layers for proper destructions on errors |
| 533 | layer_count_++; |
| 534 | } |
| 535 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 536 | SetupLayerLinks(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 537 | |
| 538 | return VK_SUCCESS; |
| 539 | } |
| 540 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 541 | VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev, |
| 542 | const char* const* layer_names, |
| 543 | uint32_t layer_count, |
| 544 | const char* const* extension_names, |
| 545 | uint32_t extension_count) { |
| 546 | uint32_t instance_layer_count; |
| 547 | const ActiveLayer* instance_layers = |
| 548 | GetActiveLayers(physical_dev, instance_layer_count); |
| 549 | |
| 550 | // log a message if the application device layer array is not empty nor an |
| 551 | // exact match of the instance layer array. |
| 552 | if (layer_count) { |
| 553 | bool exact_match = (instance_layer_count == layer_count); |
| 554 | if (exact_match) { |
| 555 | for (uint32_t i = 0; i < instance_layer_count; i++) { |
| 556 | const Layer& l = *instance_layers[i].ref; |
| 557 | if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) { |
| 558 | exact_match = false; |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (!exact_match) { |
| 565 | ALOGW("Device layers"); |
| 566 | for (uint32_t i = 0; i < layer_count; i++) |
| 567 | ALOGW(" %s", layer_names[i]); |
| 568 | ALOGW( |
| 569 | "disagree with instance layers and are overridden by " |
| 570 | "instance layers"); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | VkResult result = |
| 575 | override_extensions_.Parse(extension_names, extension_count); |
| 576 | if (result != VK_SUCCESS) |
| 577 | return result; |
| 578 | |
| 579 | if (!instance_layer_count) { |
| 580 | // point head of chain to the driver |
| 581 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; |
| 582 | get_device_proc_addr_ = driver::GetDeviceProcAddr; |
| 583 | |
| 584 | return VK_SUCCESS; |
| 585 | } |
| 586 | |
| 587 | layers_ = AllocateLayerArray(instance_layer_count); |
| 588 | if (!layers_) |
| 589 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 590 | |
| 591 | for (uint32_t i = 0; i < instance_layer_count; i++) { |
| 592 | const Layer& l = *instance_layers[i].ref; |
| 593 | |
| 594 | // no need to and cannot chain non-global layers |
| 595 | if (!IsLayerGlobal(l)) |
| 596 | continue; |
| 597 | |
| 598 | // this never fails |
| 599 | new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}}; |
| 600 | } |
| 601 | |
| 602 | SetupLayerLinks(); |
| 603 | |
| 604 | return VK_SUCCESS; |
| 605 | } |
| 606 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 607 | LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 608 | VkSystemAllocationScope scope = (is_instance_) |
| 609 | ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 610 | : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 611 | |
| 612 | return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation( |
| 613 | allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer), |
| 614 | scope)); |
| 615 | } |
| 616 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 617 | VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) { |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 618 | const Layer* l = FindLayer(name); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 619 | if (!l) { |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 620 | ALOGW("Failed to find layer %s", name); |
| 621 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 622 | } |
| 623 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 624 | new (&layer) ActiveLayer{GetLayerRef(*l), {}}; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 625 | if (!layer.ref) { |
Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 626 | ALOGW("Failed to open layer %s", name); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 627 | layer.ref.~LayerRef(); |
| 628 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 629 | } |
| 630 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 631 | ALOGI("Loaded layer %s", name); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 632 | |
| 633 | return VK_SUCCESS; |
| 634 | } |
| 635 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 636 | void LayerChain::SetupLayerLinks() { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 637 | if (is_instance_) { |
| 638 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 639 | ActiveLayer& layer = layers_[i]; |
| 640 | |
| 641 | // point head of chain to the first layer |
| 642 | if (i == 0) |
| 643 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); |
| 644 | |
| 645 | // point tail of chain to the driver |
| 646 | if (i == layer_count_ - 1) { |
| 647 | layer.instance_link.pNext = nullptr; |
| 648 | layer.instance_link.pfnNextGetInstanceProcAddr = |
| 649 | driver::GetInstanceProcAddr; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | const ActiveLayer& next = layers_[i + 1]; |
| 654 | |
| 655 | // const_cast as some naughty layers want to modify our links! |
| 656 | layer.instance_link.pNext = |
| 657 | const_cast<VkLayerInstanceLink*>(&next.instance_link); |
| 658 | layer.instance_link.pfnNextGetInstanceProcAddr = |
| 659 | next.ref.GetGetInstanceProcAddr(); |
| 660 | } |
| 661 | } else { |
| 662 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 663 | ActiveLayer& layer = layers_[i]; |
| 664 | |
| 665 | // point head of chain to the first layer |
| 666 | if (i == 0) { |
| 667 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); |
| 668 | get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr(); |
| 669 | } |
| 670 | |
| 671 | // point tail of chain to the driver |
| 672 | if (i == layer_count_ - 1) { |
| 673 | layer.device_link.pNext = nullptr; |
| 674 | layer.device_link.pfnNextGetInstanceProcAddr = |
| 675 | driver::GetInstanceProcAddr; |
| 676 | layer.device_link.pfnNextGetDeviceProcAddr = |
| 677 | driver::GetDeviceProcAddr; |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | const ActiveLayer& next = layers_[i + 1]; |
| 682 | |
| 683 | // const_cast as some naughty layers want to modify our links! |
| 684 | layer.device_link.pNext = |
| 685 | const_cast<VkLayerDeviceLink*>(&next.device_link); |
| 686 | layer.device_link.pfnNextGetInstanceProcAddr = |
| 687 | next.ref.GetGetInstanceProcAddr(); |
| 688 | layer.device_link.pfnNextGetDeviceProcAddr = |
| 689 | next.ref.GetGetDeviceProcAddr(); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 694 | bool LayerChain::Empty() const { |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 695 | return (!layer_count_ && !override_layers_.Count() && |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 696 | !override_extensions_.Count()); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 697 | } |
| 698 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 699 | void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 700 | if (layer_count_) { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 701 | auto& link_info = instance_chain_info_[1]; |
| 702 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 703 | link_info.pNext = info.pNext; |
| 704 | link_info.function = VK_LAYER_FUNCTION_LINK; |
| 705 | link_info.u.pLayerInfo = &layers_[0].instance_link; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 706 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 707 | auto& cb_info = instance_chain_info_[0]; |
| 708 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 709 | cb_info.pNext = &link_info; |
| 710 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; |
| 711 | cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 712 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 713 | info.pNext = &cb_info; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 714 | } |
| 715 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 716 | if (override_layers_.Count()) { |
| 717 | info.enabledLayerCount = override_layers_.Count(); |
| 718 | info.ppEnabledLayerNames = override_layers_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 719 | } |
| 720 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 721 | if (override_extensions_.Count()) { |
| 722 | info.enabledExtensionCount = override_extensions_.Count(); |
| 723 | info.ppEnabledExtensionNames = override_extensions_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 727 | void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 728 | if (layer_count_) { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 729 | auto& link_info = device_chain_info_[1]; |
| 730 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 731 | link_info.pNext = info.pNext; |
| 732 | link_info.function = VK_LAYER_FUNCTION_LINK; |
| 733 | link_info.u.pLayerInfo = &layers_[0].device_link; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 734 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 735 | auto& cb_info = device_chain_info_[0]; |
| 736 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 737 | cb_info.pNext = &link_info; |
| 738 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; |
| 739 | cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 740 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 741 | info.pNext = &cb_info; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 742 | } |
| 743 | |
Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 744 | if (override_layers_.Count()) { |
| 745 | info.enabledLayerCount = override_layers_.Count(); |
| 746 | info.ppEnabledLayerNames = override_layers_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 747 | } |
| 748 | |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 749 | if (override_extensions_.Count()) { |
| 750 | info.enabledExtensionCount = override_extensions_.Count(); |
| 751 | info.ppEnabledExtensionNames = override_extensions_.Names(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 755 | VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 756 | const VkAllocationCallbacks* allocator, |
| 757 | VkInstance* instance_out) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 758 | VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames, |
| 759 | create_info->enabledExtensionCount); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 760 | if (result != VK_SUCCESS) |
| 761 | return result; |
| 762 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 763 | // call down the chain |
| 764 | PFN_vkCreateInstance create_instance = |
| 765 | reinterpret_cast<PFN_vkCreateInstance>( |
| 766 | get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance")); |
| 767 | VkInstance instance; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 768 | result = create_instance(create_info, allocator, &instance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 769 | if (result != VK_SUCCESS) |
| 770 | return result; |
| 771 | |
| 772 | // initialize InstanceData |
| 773 | InstanceData& data = GetData(instance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 774 | |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 775 | if (!InitDispatchTable(instance, get_instance_proc_addr_, |
| 776 | enabled_extensions_)) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 777 | if (data.dispatch.DestroyInstance) |
| 778 | data.dispatch.DestroyInstance(instance, allocator); |
| 779 | |
| 780 | return VK_ERROR_INITIALIZATION_FAILED; |
| 781 | } |
| 782 | |
| 783 | // install debug report callback |
Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 784 | if (override_extensions_.InstallDebugCallback()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 785 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = |
| 786 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 787 | get_instance_proc_addr_(instance, |
| 788 | "vkCreateDebugReportCallbackEXT")); |
| 789 | data.destroy_debug_callback = |
| 790 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 791 | get_instance_proc_addr_(instance, |
| 792 | "vkDestroyDebugReportCallbackEXT")); |
| 793 | if (!create_debug_report_callback || !data.destroy_debug_callback) { |
| 794 | ALOGE("Broken VK_EXT_debug_report support"); |
| 795 | data.dispatch.DestroyInstance(instance, allocator); |
| 796 | return VK_ERROR_INITIALIZATION_FAILED; |
| 797 | } |
| 798 | |
| 799 | VkDebugReportCallbackCreateInfoEXT debug_callback_info = {}; |
| 800 | debug_callback_info.sType = |
| 801 | VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 802 | debug_callback_info.flags = |
| 803 | 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] | 804 | debug_callback_info.pfnCallback = DebugReportCallback; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 805 | |
| 806 | VkDebugReportCallbackEXT debug_callback; |
| 807 | result = create_debug_report_callback(instance, &debug_callback_info, |
| 808 | nullptr, &debug_callback); |
| 809 | if (result != VK_SUCCESS) { |
| 810 | ALOGE("Failed to install debug report callback"); |
| 811 | data.dispatch.DestroyInstance(instance, allocator); |
| 812 | return VK_ERROR_INITIALIZATION_FAILED; |
| 813 | } |
| 814 | |
| 815 | data.debug_callback = debug_callback; |
| 816 | |
| 817 | ALOGI("Installed debug report callback"); |
| 818 | } |
| 819 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 820 | StealLayers(data); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 821 | |
| 822 | *instance_out = instance; |
| 823 | |
| 824 | return VK_SUCCESS; |
| 825 | } |
| 826 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 827 | VkResult LayerChain::Create(VkPhysicalDevice physical_dev, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 828 | const VkDeviceCreateInfo* create_info, |
| 829 | const VkAllocationCallbacks* allocator, |
| 830 | VkDevice* dev_out) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 831 | VkResult result = |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 832 | ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames, |
| 833 | create_info->enabledExtensionCount); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 834 | if (result != VK_SUCCESS) |
| 835 | return result; |
| 836 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 837 | // call down the chain |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 838 | PFN_vkCreateDevice create_device = |
| 839 | GetData(physical_dev).dispatch.CreateDevice; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 840 | VkDevice dev; |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 841 | result = create_device(physical_dev, create_info, allocator, &dev); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 842 | if (result != VK_SUCCESS) |
| 843 | return result; |
| 844 | |
| 845 | // initialize DeviceData |
| 846 | DeviceData& data = GetData(dev); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 847 | |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 848 | if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 849 | if (data.dispatch.DestroyDevice) |
| 850 | data.dispatch.DestroyDevice(dev, allocator); |
| 851 | |
| 852 | return VK_ERROR_INITIALIZATION_FAILED; |
| 853 | } |
| 854 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 855 | // no StealLayers so that active layers are destroyed with this |
| 856 | // LayerChain |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 857 | *dev_out = dev; |
| 858 | |
| 859 | return VK_SUCCESS; |
| 860 | } |
| 861 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 862 | VkResult LayerChain::ValidateExtensions(const char* const* extension_names, |
| 863 | uint32_t extension_count) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 864 | if (!extension_count) |
| 865 | return VK_SUCCESS; |
| 866 | |
| 867 | // query driver instance extensions |
| 868 | uint32_t count; |
| 869 | VkResult result = |
| 870 | EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); |
| 871 | if (result == VK_SUCCESS && count) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 872 | driver_extensions_ = AllocateDriverExtensionArray(count); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 873 | result = (driver_extensions_) ? EnumerateInstanceExtensionProperties( |
| 874 | nullptr, &count, driver_extensions_) |
| 875 | : VK_ERROR_OUT_OF_HOST_MEMORY; |
| 876 | } |
| 877 | if (result != VK_SUCCESS) |
| 878 | return result; |
| 879 | |
| 880 | driver_extension_count_ = count; |
| 881 | |
| 882 | for (uint32_t i = 0; i < extension_count; i++) { |
| 883 | const char* name = extension_names[i]; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 884 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 885 | ALOGE("Failed to enable missing instance extension %s", name); |
| 886 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 887 | } |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 888 | |
| 889 | auto ext_bit = driver::GetProcHookExtension(name); |
| 890 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 891 | enabled_extensions_.set(ext_bit); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return VK_SUCCESS; |
| 895 | } |
| 896 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 897 | VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev, |
| 898 | const char* const* extension_names, |
| 899 | uint32_t extension_count) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 900 | if (!extension_count) |
| 901 | return VK_SUCCESS; |
| 902 | |
| 903 | // query driver device extensions |
| 904 | uint32_t count; |
| 905 | VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr, |
| 906 | &count, nullptr); |
| 907 | if (result == VK_SUCCESS && count) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 908 | driver_extensions_ = AllocateDriverExtensionArray(count); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 909 | result = (driver_extensions_) |
| 910 | ? EnumerateDeviceExtensionProperties( |
| 911 | physical_dev, nullptr, &count, driver_extensions_) |
| 912 | : VK_ERROR_OUT_OF_HOST_MEMORY; |
| 913 | } |
| 914 | if (result != VK_SUCCESS) |
| 915 | return result; |
| 916 | |
| 917 | driver_extension_count_ = count; |
| 918 | |
| 919 | for (uint32_t i = 0; i < extension_count; i++) { |
| 920 | const char* name = extension_names[i]; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 921 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 922 | ALOGE("Failed to enable missing device extension %s", name); |
| 923 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 924 | } |
Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 925 | |
| 926 | auto ext_bit = driver::GetProcHookExtension(name); |
| 927 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 928 | enabled_extensions_.set(ext_bit); |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | return VK_SUCCESS; |
| 932 | } |
| 933 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 934 | VkExtensionProperties* LayerChain::AllocateDriverExtensionArray( |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 935 | uint32_t count) const { |
| 936 | return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( |
| 937 | allocator_.pUserData, sizeof(VkExtensionProperties) * count, |
| 938 | alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 939 | } |
| 940 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 941 | bool LayerChain::IsLayerExtension(const char* name) const { |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 942 | if (is_instance_) { |
| 943 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 944 | const ActiveLayer& layer = layers_[i]; |
| 945 | if (FindLayerInstanceExtension(*layer.ref, name)) |
| 946 | return true; |
| 947 | } |
| 948 | } else { |
| 949 | for (uint32_t i = 0; i < layer_count_; i++) { |
| 950 | const ActiveLayer& layer = layers_[i]; |
| 951 | if (FindLayerDeviceExtension(*layer.ref, name)) |
| 952 | return true; |
| 953 | } |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | return false; |
| 957 | } |
| 958 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 959 | bool LayerChain::IsDriverExtension(const char* name) const { |
Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 960 | for (uint32_t i = 0; i < driver_extension_count_; i++) { |
| 961 | if (strcmp(driver_extensions_[i].extensionName, name) == 0) |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | return false; |
| 966 | } |
| 967 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 968 | template <typename DataType> |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 969 | void LayerChain::StealLayers(DataType& data) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 970 | data.layers = layers_; |
| 971 | data.layer_count = layer_count_; |
| 972 | |
| 973 | layers_ = nullptr; |
| 974 | layer_count_ = 0; |
| 975 | } |
| 976 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 977 | void LayerChain::DestroyLayers(ActiveLayer* layers, |
| 978 | uint32_t count, |
| 979 | const VkAllocationCallbacks& allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 980 | for (uint32_t i = 0; i < count; i++) |
| 981 | layers[i].ref.~LayerRef(); |
| 982 | |
| 983 | allocator.pfnFree(allocator.pUserData, layers); |
| 984 | } |
| 985 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 986 | VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) { |
| 987 | driver::InstanceDispatchable dispatchable = |
| 988 | reinterpret_cast<driver::InstanceDispatchable>(object); |
| 989 | |
| 990 | return (driver::SetDataInternal(dispatchable, &driver::GetData(instance))) |
| 991 | ? VK_SUCCESS |
| 992 | : VK_ERROR_INITIALIZATION_FAILED; |
| 993 | } |
| 994 | |
| 995 | VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) { |
| 996 | driver::DeviceDispatchable dispatchable = |
| 997 | reinterpret_cast<driver::DeviceDispatchable>(object); |
| 998 | |
| 999 | return (driver::SetDataInternal(dispatchable, &driver::GetData(device))) |
| 1000 | ? VK_SUCCESS |
| 1001 | : VK_ERROR_INITIALIZATION_FAILED; |
| 1002 | } |
| 1003 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1004 | VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 1005 | VkDebugReportObjectTypeEXT obj_type, |
| 1006 | uint64_t obj, |
| 1007 | size_t location, |
| 1008 | int32_t msg_code, |
| 1009 | const char* layer_prefix, |
| 1010 | const char* msg, |
| 1011 | void* user_data) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1012 | int prio; |
| 1013 | |
| 1014 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) |
| 1015 | prio = ANDROID_LOG_ERROR; |
| 1016 | else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 1017 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) |
| 1018 | prio = ANDROID_LOG_WARN; |
| 1019 | else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) |
| 1020 | prio = ANDROID_LOG_INFO; |
| 1021 | else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) |
| 1022 | prio = ANDROID_LOG_DEBUG; |
| 1023 | else |
| 1024 | prio = ANDROID_LOG_UNKNOWN; |
| 1025 | |
| 1026 | LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg); |
| 1027 | |
| 1028 | (void)obj_type; |
| 1029 | (void)obj; |
| 1030 | (void)location; |
| 1031 | (void)user_data; |
| 1032 | |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1036 | VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info, |
| 1037 | const VkAllocationCallbacks* allocator, |
| 1038 | VkInstance* instance_out) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1039 | LayerChain chain(true, |
| 1040 | (allocator) ? *allocator : driver::GetDefaultAllocator()); |
| 1041 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1042 | VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, |
| 1043 | create_info->enabledLayerCount, |
| 1044 | create_info->ppEnabledExtensionNames, |
| 1045 | create_info->enabledExtensionCount); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1046 | if (result != VK_SUCCESS) |
| 1047 | return result; |
| 1048 | |
| 1049 | // use a local create info when the chain is not empty |
| 1050 | VkInstanceCreateInfo local_create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1051 | if (!chain.Empty()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1052 | local_create_info = *create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1053 | chain.ModifyCreateInfo(local_create_info); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1054 | create_info = &local_create_info; |
| 1055 | } |
| 1056 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1057 | return chain.Create(create_info, allocator, instance_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1060 | VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev, |
| 1061 | const VkDeviceCreateInfo* create_info, |
| 1062 | const VkAllocationCallbacks* allocator, |
| 1063 | VkDevice* dev_out) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1064 | LayerChain chain(false, (allocator) |
| 1065 | ? *allocator |
| 1066 | : driver::GetData(physical_dev).allocator); |
| 1067 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1068 | VkResult result = chain.ActivateLayers( |
| 1069 | physical_dev, create_info->ppEnabledLayerNames, |
| 1070 | create_info->enabledLayerCount, create_info->ppEnabledExtensionNames, |
| 1071 | create_info->enabledExtensionCount); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1072 | if (result != VK_SUCCESS) |
| 1073 | return result; |
| 1074 | |
| 1075 | // use a local create info when the chain is not empty |
| 1076 | VkDeviceCreateInfo local_create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1077 | if (!chain.Empty()) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1078 | local_create_info = *create_info; |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1079 | chain.ModifyCreateInfo(local_create_info); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1080 | create_info = &local_create_info; |
| 1081 | } |
| 1082 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1083 | return chain.Create(physical_dev, create_info, allocator, dev_out); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1086 | void LayerChain::DestroyInstance(VkInstance instance, |
| 1087 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1088 | InstanceData& data = GetData(instance); |
| 1089 | |
| 1090 | if (data.debug_callback != VK_NULL_HANDLE) |
| 1091 | data.destroy_debug_callback(instance, data.debug_callback, allocator); |
| 1092 | |
| 1093 | ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); |
| 1094 | uint32_t layer_count = data.layer_count; |
| 1095 | |
| 1096 | VkAllocationCallbacks local_allocator; |
| 1097 | if (!allocator) |
| 1098 | local_allocator = driver::GetData(instance).allocator; |
| 1099 | |
| 1100 | // this also destroys InstanceData |
| 1101 | data.dispatch.DestroyInstance(instance, allocator); |
| 1102 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1103 | DestroyLayers(layers, layer_count, |
| 1104 | (allocator) ? *allocator : local_allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1107 | void LayerChain::DestroyDevice(VkDevice device, |
| 1108 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1109 | DeviceData& data = GetData(device); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1110 | // this also destroys DeviceData |
| 1111 | data.dispatch.DestroyDevice(device, allocator); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1112 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1113 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1114 | const LayerChain::ActiveLayer* LayerChain::GetActiveLayers( |
| 1115 | VkPhysicalDevice physical_dev, |
| 1116 | uint32_t& count) { |
| 1117 | count = GetData(physical_dev).layer_count; |
| 1118 | return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | // ---------------------------------------------------------------------------- |
| 1122 | |
| 1123 | bool EnsureInitialized() { |
| 1124 | static std::once_flag once_flag; |
| 1125 | static bool initialized; |
| 1126 | |
| 1127 | std::call_once(once_flag, []() { |
| 1128 | if (driver::OpenHAL()) { |
| 1129 | DiscoverLayers(); |
| 1130 | initialized = true; |
| 1131 | } |
| 1132 | }); |
| 1133 | |
| 1134 | return initialized; |
| 1135 | } |
| 1136 | |
| 1137 | } // anonymous namespace |
| 1138 | |
| 1139 | VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, |
| 1140 | const VkAllocationCallbacks* pAllocator, |
| 1141 | VkInstance* pInstance) { |
| 1142 | if (!EnsureInitialized()) |
| 1143 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1144 | |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1145 | return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | void DestroyInstance(VkInstance instance, |
| 1149 | const VkAllocationCallbacks* pAllocator) { |
| 1150 | if (instance != VK_NULL_HANDLE) |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1151 | LayerChain::DestroyInstance(instance, pAllocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | VkResult CreateDevice(VkPhysicalDevice physicalDevice, |
| 1155 | const VkDeviceCreateInfo* pCreateInfo, |
| 1156 | const VkAllocationCallbacks* pAllocator, |
| 1157 | VkDevice* pDevice) { |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1158 | return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator, |
| 1159 | pDevice); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { |
| 1163 | if (device != VK_NULL_HANDLE) |
Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1164 | LayerChain::DestroyDevice(device, pAllocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount, |
| 1168 | VkLayerProperties* pProperties) { |
| 1169 | if (!EnsureInitialized()) |
| 1170 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1171 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1172 | uint32_t count = GetLayerCount(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1173 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1174 | if (!pProperties) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1175 | *pPropertyCount = count; |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1176 | return VK_SUCCESS; |
| 1177 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1178 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1179 | uint32_t copied = std::min(*pPropertyCount, count); |
| 1180 | for (uint32_t i = 0; i < copied; i++) |
| 1181 | pProperties[i] = GetLayerProperties(GetLayer(i)); |
| 1182 | *pPropertyCount = copied; |
| 1183 | |
| 1184 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | VkResult EnumerateInstanceExtensionProperties( |
| 1188 | const char* pLayerName, |
| 1189 | uint32_t* pPropertyCount, |
| 1190 | VkExtensionProperties* pProperties) { |
| 1191 | if (!EnsureInitialized()) |
| 1192 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1193 | |
| 1194 | if (pLayerName) { |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1195 | const Layer* layer = FindLayer(pLayerName); |
Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1196 | if (!layer) |
| 1197 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 1198 | |
| 1199 | uint32_t count; |
| 1200 | const VkExtensionProperties* props = |
| 1201 | GetLayerInstanceExtensions(*layer, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1202 | |
| 1203 | if (!pProperties || *pPropertyCount > count) |
| 1204 | *pPropertyCount = count; |
| 1205 | if (pProperties) |
| 1206 | std::copy(props, props + *pPropertyCount, pProperties); |
| 1207 | |
| 1208 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; |
| 1209 | } |
| 1210 | |
| 1211 | // TODO how about extensions from implicitly enabled layers? |
| 1212 | return vulkan::driver::EnumerateInstanceExtensionProperties( |
| 1213 | nullptr, pPropertyCount, pProperties); |
| 1214 | } |
| 1215 | |
| 1216 | VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, |
| 1217 | uint32_t* pPropertyCount, |
| 1218 | VkLayerProperties* pProperties) { |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1219 | uint32_t count; |
| 1220 | const LayerChain::ActiveLayer* layers = |
| 1221 | LayerChain::GetActiveLayers(physicalDevice, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1222 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1223 | if (!pProperties) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1224 | *pPropertyCount = count; |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1225 | return VK_SUCCESS; |
| 1226 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1227 | |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1228 | uint32_t copied = std::min(*pPropertyCount, count); |
| 1229 | for (uint32_t i = 0; i < copied; i++) |
| 1230 | pProperties[i] = GetLayerProperties(*layers[i].ref); |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1231 | *pPropertyCount = copied; |
| 1232 | |
| 1233 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | VkResult EnumerateDeviceExtensionProperties( |
| 1237 | VkPhysicalDevice physicalDevice, |
| 1238 | const char* pLayerName, |
| 1239 | uint32_t* pPropertyCount, |
| 1240 | VkExtensionProperties* pProperties) { |
| 1241 | if (pLayerName) { |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1242 | // EnumerateDeviceLayerProperties enumerates active layers for |
| 1243 | // backward compatibility. The extension query here should work for |
| 1244 | // all layers. |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1245 | const Layer* layer = FindLayer(pLayerName); |
Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame^] | 1246 | if (!layer) |
Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1247 | return VK_ERROR_LAYER_NOT_PRESENT; |
| 1248 | |
| 1249 | uint32_t count; |
| 1250 | const VkExtensionProperties* props = |
| 1251 | GetLayerDeviceExtensions(*layer, count); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1252 | |
| 1253 | if (!pProperties || *pPropertyCount > count) |
| 1254 | *pPropertyCount = count; |
| 1255 | if (pProperties) |
| 1256 | std::copy(props, props + *pPropertyCount, pProperties); |
| 1257 | |
| 1258 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; |
| 1259 | } |
| 1260 | |
| 1261 | // TODO how about extensions from implicitly enabled layers? |
| 1262 | const InstanceData& data = GetData(physicalDevice); |
| 1263 | return data.dispatch.EnumerateDeviceExtensionProperties( |
| 1264 | physicalDevice, nullptr, pPropertyCount, pProperties); |
| 1265 | } |
| 1266 | |
| 1267 | } // namespace api |
| 1268 | } // namespace vulkan |