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