Jesse Hall | d02edcb | 2015-09-08 07:44:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 19 | // module header |
| 20 | #include "loader.h" |
| 21 | // standard C headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <dlfcn.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 25 | #include <pthread.h> |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 27 | #include <string.h> |
| 28 | // standard C++ headers |
| 29 | #include <algorithm> |
| 30 | #include <mutex> |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | #include <unordered_map> |
| 34 | #include <vector> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 35 | // platform/library headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 37 | #include <hardware/hwvulkan.h> |
| 38 | #include <log/log.h> |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 39 | #include <vulkan/vk_debug_report_lunarg.h> |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 40 | #include <vulkan/vulkan_loader_data.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace vulkan; |
| 43 | |
| 44 | static const uint32_t kMaxPhysicalDevices = 4; |
| 45 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | // These definitions are taken from the LunarG Vulkan Loader. They are used to |
| 49 | // enforce compatability between the Loader and Layers. |
| 50 | typedef void* (*PFN_vkGetProcAddr)(void* obj, const char* pName); |
| 51 | |
| 52 | typedef struct VkLayerLinkedListElem_ { |
| 53 | PFN_vkGetProcAddr get_proc_addr; |
| 54 | void* next_element; |
| 55 | void* base_object; |
| 56 | } VkLayerLinkedListElem; |
| 57 | |
| 58 | // Define Handle typedef to be void* as returned from dlopen. |
| 59 | typedef void* SharedLibraryHandle; |
| 60 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 61 | // Standard-library allocator that delegates to VkAllocCallbacks. |
| 62 | // |
| 63 | // TODO(jessehall): This class currently always uses |
| 64 | // VK_SYSTEM_ALLOC_SCOPE_INSTANCE. The scope to use could be a template |
| 65 | // parameter or a constructor parameter. The former would help catch bugs |
| 66 | // where we use the wrong scope, e.g. adding a command-scope string to an |
| 67 | // instance-scope vector. But that might also be pretty annoying to deal with. |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 68 | template <class T> |
| 69 | class CallbackAllocator { |
| 70 | public: |
| 71 | typedef T value_type; |
| 72 | |
| 73 | CallbackAllocator(const VkAllocCallbacks* alloc_input) |
| 74 | : alloc(alloc_input) {} |
| 75 | |
| 76 | template <class T2> |
| 77 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 78 | : alloc(other.alloc) {} |
| 79 | |
| 80 | T* allocate(std::size_t n) { |
| 81 | void* mem = alloc->pfnAlloc(alloc->pUserData, n * sizeof(T), alignof(T), |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 82 | VK_SYSTEM_ALLOC_SCOPE_INSTANCE); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 83 | return static_cast<T*>(mem); |
| 84 | } |
| 85 | |
| 86 | void deallocate(T* array, std::size_t /*n*/) { |
| 87 | alloc->pfnFree(alloc->pUserData, array); |
| 88 | } |
| 89 | |
| 90 | const VkAllocCallbacks* alloc; |
| 91 | }; |
| 92 | // These are needed in order to move Strings |
| 93 | template <class T> |
| 94 | bool operator==(const CallbackAllocator<T>& alloc1, |
| 95 | const CallbackAllocator<T>& alloc2) { |
| 96 | return alloc1.alloc == alloc2.alloc; |
| 97 | } |
| 98 | template <class T> |
| 99 | bool operator!=(const CallbackAllocator<T>& alloc1, |
| 100 | const CallbackAllocator<T>& alloc2) { |
| 101 | return !(alloc1 == alloc2); |
| 102 | } |
| 103 | |
| 104 | template <class Key, |
| 105 | class T, |
| 106 | class Hash = std::hash<Key>, |
| 107 | class Pred = std::equal_to<Key> > |
| 108 | using UnorderedMap = |
| 109 | std::unordered_map<Key, |
| 110 | T, |
| 111 | Hash, |
| 112 | Pred, |
| 113 | CallbackAllocator<std::pair<const Key, T> > >; |
| 114 | |
| 115 | template <class T> |
| 116 | using Vector = std::vector<T, CallbackAllocator<T> >; |
| 117 | |
| 118 | typedef std::basic_string<char, |
| 119 | std::char_traits<char>, |
| 120 | CallbackAllocator<char> > String; |
| 121 | |
| 122 | } // namespace |
| 123 | |
| 124 | // ----------------------------------------------------------------------------- |
| 125 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 126 | namespace { |
| 127 | |
| 128 | struct LayerData { |
| 129 | String path; |
| 130 | SharedLibraryHandle handle; |
| 131 | uint32_t ref_count; |
| 132 | }; |
| 133 | |
| 134 | typedef UnorderedMap<String, LayerData>::iterator LayerMapIterator; |
| 135 | |
| 136 | } // namespace |
| 137 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 138 | struct VkInstance_T { |
| 139 | VkInstance_T(const VkAllocCallbacks* alloc_callbacks) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 140 | : vtbl(&vtbl_storage), |
| 141 | alloc(alloc_callbacks), |
| 142 | num_physical_devices(0), |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 143 | layers(CallbackAllocator<std::pair<String, LayerData> >(alloc)), |
| 144 | active_layers(CallbackAllocator<String>(alloc)) { |
| 145 | pthread_mutex_init(&layer_lock, 0); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 146 | memset(&vtbl_storage, 0, sizeof(vtbl_storage)); |
| 147 | memset(physical_devices, 0, sizeof(physical_devices)); |
| 148 | memset(&drv.vtbl, 0, sizeof(drv.vtbl)); |
| 149 | drv.GetDeviceProcAddr = nullptr; |
| 150 | drv.num_physical_devices = 0; |
| 151 | } |
| 152 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 153 | ~VkInstance_T() { pthread_mutex_destroy(&layer_lock); } |
| 154 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 155 | InstanceVtbl* vtbl; |
| 156 | InstanceVtbl vtbl_storage; |
| 157 | |
| 158 | const VkAllocCallbacks* alloc; |
| 159 | uint32_t num_physical_devices; |
| 160 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
| 161 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 162 | pthread_mutex_t layer_lock; |
| 163 | // Map of layer names to layer data |
| 164 | UnorderedMap<String, LayerData> layers; |
| 165 | // Vector of layers active for this instance |
| 166 | Vector<LayerMapIterator> active_layers; |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 167 | VkDbgMsgCallback message; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 168 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 169 | struct Driver { |
| 170 | // Pointers to driver entry points. Used explicitly by the loader; not |
| 171 | // set as the dispatch table for any objects. |
| 172 | InstanceVtbl vtbl; |
| 173 | |
| 174 | // Pointer to the driver's get_device_proc_addr, must be valid for any |
| 175 | // of the driver's physical devices. Not part of the InstanceVtbl since |
| 176 | // it's not an Instance/PhysicalDevice function. |
| 177 | PFN_vkGetDeviceProcAddr GetDeviceProcAddr; |
| 178 | |
| 179 | // Number of physical devices owned by this driver. |
| 180 | uint32_t num_physical_devices; |
| 181 | } drv; // may eventually be an array |
| 182 | }; |
| 183 | |
| 184 | // ----------------------------------------------------------------------------- |
| 185 | |
| 186 | namespace { |
| 187 | |
| 188 | typedef VkInstance_T Instance; |
| 189 | |
| 190 | struct Device { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 191 | Device(Instance* instance_input) |
| 192 | : instance(instance_input), |
| 193 | active_layers(CallbackAllocator<LayerMapIterator>(instance->alloc)) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 194 | memset(&vtbl_storage, 0, sizeof(vtbl_storage)); |
| 195 | vtbl_storage.device = this; |
| 196 | } |
| 197 | DeviceVtbl vtbl_storage; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 198 | Instance* instance; |
| 199 | // Vector of layers active for this device |
| 200 | Vector<LayerMapIterator> active_layers; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | // ----------------------------------------------------------------------------- |
| 204 | // Utility Code |
| 205 | |
| 206 | inline const InstanceVtbl* GetVtbl(VkPhysicalDevice physicalDevice) { |
| 207 | return *reinterpret_cast<InstanceVtbl**>(physicalDevice); |
| 208 | } |
| 209 | |
| 210 | inline const DeviceVtbl* GetVtbl(VkDevice device) { |
| 211 | return *reinterpret_cast<DeviceVtbl**>(device); |
| 212 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 213 | inline const DeviceVtbl* GetVtbl(VkQueue queue) { |
| 214 | return *reinterpret_cast<DeviceVtbl**>(queue); |
| 215 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 216 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 217 | void* DefaultAlloc(void*, size_t size, size_t alignment, VkSystemAllocScope) { |
| 218 | void* ptr = nullptr; |
| 219 | // Vulkan requires 'alignment' to be a power of two, but posix_memalign |
| 220 | // additionally requires that it be at least sizeof(void*). |
| 221 | return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0 |
| 222 | ? ptr |
| 223 | : nullptr; |
| 224 | } |
| 225 | |
| 226 | void* DefaultRealloc(void*, |
| 227 | void* ptr, |
| 228 | size_t size, |
| 229 | size_t alignment, |
| 230 | VkSystemAllocScope) { |
| 231 | if (size == 0) { |
| 232 | free(ptr); |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | // TODO(jessehall): Right now we never shrink allocations; if the new |
| 237 | // request is smaller than the existing chunk, we just continue using it. |
| 238 | // Right now the loader never reallocs, so this doesn't matter. If that |
| 239 | // changes, or if this code is copied into some other project, this should |
| 240 | // probably have a heuristic to allocate-copy-free when doing so will save |
| 241 | // "enough" space. |
| 242 | size_t old_size = ptr ? malloc_usable_size(ptr) : 0; |
| 243 | if (size <= old_size) |
| 244 | return ptr; |
| 245 | |
| 246 | void* new_ptr = nullptr; |
| 247 | if (posix_memalign(&new_ptr, alignment, size) != 0) |
| 248 | return nullptr; |
| 249 | if (ptr) { |
| 250 | memcpy(new_ptr, ptr, std::min(old_size, size)); |
| 251 | free(ptr); |
| 252 | } |
| 253 | return new_ptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void DefaultFree(void*, void* pMem) { |
| 257 | free(pMem); |
| 258 | } |
| 259 | |
| 260 | const VkAllocCallbacks kDefaultAllocCallbacks = { |
| 261 | .pUserData = nullptr, |
| 262 | .pfnAlloc = DefaultAlloc, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 263 | .pfnRealloc = DefaultRealloc, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 264 | .pfnFree = DefaultFree, |
| 265 | }; |
| 266 | |
| 267 | hwvulkan_device_t* g_hwdevice; |
| 268 | bool EnsureInitialized() { |
| 269 | static std::once_flag once_flag; |
| 270 | static const hwvulkan_module_t* module; |
| 271 | |
| 272 | std::call_once(once_flag, []() { |
| 273 | int result; |
| 274 | result = hw_get_module("vulkan", |
| 275 | reinterpret_cast<const hw_module_t**>(&module)); |
| 276 | if (result != 0) { |
| 277 | ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), |
| 278 | result); |
| 279 | return; |
| 280 | } |
| 281 | result = module->common.methods->open( |
| 282 | &module->common, HWVULKAN_DEVICE_0, |
| 283 | reinterpret_cast<hw_device_t**>(&g_hwdevice)); |
| 284 | if (result != 0) { |
| 285 | ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result), |
| 286 | result); |
| 287 | module = nullptr; |
| 288 | return; |
| 289 | } |
| 290 | }); |
| 291 | |
| 292 | return module != nullptr && g_hwdevice != nullptr; |
| 293 | } |
| 294 | |
| 295 | void DestroyDevice(Device* device) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 296 | const VkAllocCallbacks* alloc = device->instance->alloc; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 297 | device->~Device(); |
| 298 | alloc->pfnFree(alloc->pUserData, device); |
| 299 | } |
| 300 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 301 | void FindLayersInDirectory(Instance& instance, const String& dir_name) { |
Jesse Hall | 0ecdd3e | 2015-10-29 11:20:07 -0700 | [diff] [blame] | 302 | DIR* directory = opendir(dir_name.c_str()); |
| 303 | if (!directory) { |
| 304 | android_LogPriority log_priority = |
| 305 | (errno == ENOENT) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_ERROR; |
| 306 | LOG_PRI(log_priority, LOG_TAG, |
| 307 | "failed to open layer directory '%s': %s (%d)", |
| 308 | dir_name.c_str(), strerror(errno), errno); |
| 309 | return; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 310 | } |
Jesse Hall | 0ecdd3e | 2015-10-29 11:20:07 -0700 | [diff] [blame] | 311 | |
| 312 | Vector<VkLayerProperties> properties( |
| 313 | CallbackAllocator<VkLayerProperties>(instance.alloc)); |
| 314 | struct dirent* entry; |
| 315 | while ((entry = readdir(directory))) { |
| 316 | size_t length = strlen(entry->d_name); |
| 317 | if (strncmp(entry->d_name, "libVKLayer", 10) != 0 || |
| 318 | strncmp(entry->d_name + length - 3, ".so", 3) != 0) |
| 319 | continue; |
| 320 | // Open so |
| 321 | SharedLibraryHandle layer_handle = |
| 322 | dlopen((dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL); |
| 323 | if (!layer_handle) { |
| 324 | ALOGE("%s failed to load with error %s; Skipping", entry->d_name, |
| 325 | dlerror()); |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | // Get Layers in so |
| 330 | PFN_vkEnumerateInstanceLayerProperties get_layer_properties = |
| 331 | reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>( |
| 332 | dlsym(layer_handle, "vkEnumerateInstanceLayerProperties")); |
| 333 | if (!get_layer_properties) { |
| 334 | ALOGE( |
| 335 | "%s failed to find vkEnumerateInstanceLayerProperties with " |
| 336 | "error %s; Skipping", |
| 337 | entry->d_name, dlerror()); |
| 338 | dlclose(layer_handle); |
| 339 | continue; |
| 340 | } |
| 341 | uint32_t count; |
| 342 | get_layer_properties(&count, nullptr); |
| 343 | |
| 344 | properties.resize(count); |
| 345 | get_layer_properties(&count, &properties[0]); |
| 346 | |
| 347 | // Add Layers to potential list |
| 348 | for (uint32_t i = 0; i < count; ++i) { |
| 349 | String layer_name(properties[i].layerName, |
| 350 | CallbackAllocator<char>(instance.alloc)); |
| 351 | LayerData layer_data = {dir_name + entry->d_name, 0, 0}; |
| 352 | instance.layers.insert(std::make_pair(layer_name, layer_data)); |
| 353 | ALOGV("Found layer %s", properties[i].layerName); |
| 354 | } |
| 355 | dlclose(layer_handle); |
| 356 | } |
| 357 | |
| 358 | closedir(directory); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 361 | template <class TObject> |
| 362 | void ActivateLayer(TObject* object, Instance* instance, const String& name) { |
| 363 | // If object has layer, do nothing |
| 364 | auto element = instance->layers.find(name); |
Michael Lentine | 233ac73 | 2015-11-18 18:28:07 -0800 | [diff] [blame] | 365 | if (element == instance->layers.end()) { |
| 366 | return; |
| 367 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 368 | if (std::find(object->active_layers.begin(), object->active_layers.end(), |
| 369 | element) != object->active_layers.end()) { |
| 370 | ALOGW("Layer %s already activated; skipping", name.c_str()); |
| 371 | return; |
| 372 | } |
| 373 | // If layer is not open, open it |
| 374 | LayerData& layer_data = element->second; |
| 375 | pthread_mutex_lock(&instance->layer_lock); |
| 376 | if (layer_data.ref_count == 0) { |
| 377 | SharedLibraryHandle layer_handle = |
| 378 | dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 379 | if (!layer_handle) { |
| 380 | pthread_mutex_unlock(&instance->layer_lock); |
| 381 | ALOGE("%s failed to load with error %s; Skipping", |
| 382 | layer_data.path.c_str(), dlerror()); |
| 383 | return; |
| 384 | } |
| 385 | layer_data.handle = layer_handle; |
| 386 | } |
| 387 | layer_data.ref_count++; |
| 388 | pthread_mutex_unlock(&instance->layer_lock); |
| 389 | ALOGV("Activating layer %s", name.c_str()); |
| 390 | object->active_layers.push_back(element); |
| 391 | } |
| 392 | |
Michael Lentine | 1d1e65f | 2015-11-19 14:23:06 -0800 | [diff] [blame] | 393 | void DeactivateLayer(Instance* instance, |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 394 | Vector<LayerMapIterator>::iterator& element) { |
| 395 | LayerMapIterator& layer_map_data = *element; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 396 | LayerData& layer_data = layer_map_data->second; |
| 397 | pthread_mutex_lock(&instance->layer_lock); |
| 398 | layer_data.ref_count--; |
| 399 | if (!layer_data.ref_count) { |
| 400 | dlclose(layer_data.handle); |
| 401 | } |
| 402 | pthread_mutex_unlock(&instance->layer_lock); |
| 403 | } |
| 404 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 405 | struct InstanceNamesPair { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 406 | Instance* instance; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 407 | Vector<String>* layer_names; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 408 | }; |
| 409 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 410 | void SetLayerNamesFromProperty(const char* name, |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 411 | const char* value, |
| 412 | void* data) { |
| 413 | const char prefix[] = "debug.vulkan.layer."; |
| 414 | const size_t prefixlen = sizeof(prefix) - 1; |
| 415 | if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0) |
| 416 | return; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 417 | const char* number_str = name + prefixlen; |
| 418 | long layer_number = strtol(number_str, nullptr, 10); |
| 419 | if (layer_number <= 0 || layer_number == LONG_MAX) { |
| 420 | ALOGW("Cannot use a layer at number %ld from string %s", layer_number, |
| 421 | number_str); |
| 422 | return; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 423 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 424 | auto instance_names_pair = static_cast<InstanceNamesPair*>(data); |
| 425 | Vector<String>* layer_names = instance_names_pair->layer_names; |
| 426 | Instance* instance = instance_names_pair->instance; |
| 427 | size_t layer_size = static_cast<size_t>(layer_number); |
| 428 | if (layer_size > layer_names->size()) { |
| 429 | layer_names->resize(layer_size, |
| 430 | String(CallbackAllocator<char>(instance->alloc))); |
| 431 | } |
| 432 | (*layer_names)[layer_size - 1] = value; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | template <class TInfo, class TObject> |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 436 | VkResult ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 437 | ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO || |
| 438 | create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 439 | "Cannot activate layers for unknown object %p", object); |
| 440 | CallbackAllocator<char> string_allocator(instance->alloc); |
| 441 | // Load system layers |
| 442 | { |
| 443 | char layer_prop[PROPERTY_VALUE_MAX]; |
| 444 | property_get("debug.vulkan.layers", layer_prop, ""); |
| 445 | String layer_name(string_allocator); |
| 446 | String layer_prop_str(layer_prop, string_allocator); |
| 447 | size_t end, start = 0; |
| 448 | while ((end = layer_prop_str.find(':', start)) != std::string::npos) { |
| 449 | layer_name = layer_prop_str.substr(start, end - start); |
Michael Lentine | 233ac73 | 2015-11-18 18:28:07 -0800 | [diff] [blame] | 450 | ActivateLayer(object, instance, layer_name); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 451 | start = end + 1; |
| 452 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 453 | Vector<String> layer_names(CallbackAllocator<String>(instance->alloc)); |
| 454 | InstanceNamesPair instance_names_pair = {.instance = instance, |
| 455 | .layer_names = &layer_names}; |
| 456 | property_list(SetLayerNamesFromProperty, |
| 457 | static_cast<void*>(&instance_names_pair)); |
| 458 | for (auto layer_name_element : layer_names) { |
| 459 | ActivateLayer(object, instance, layer_name_element); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 460 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 461 | } |
| 462 | // Load app layers |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 463 | for (uint32_t i = 0; i < create_info->enabledLayerNameCount; ++i) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 464 | String layer_name(create_info->ppEnabledLayerNames[i], |
| 465 | string_allocator); |
| 466 | auto element = instance->layers.find(layer_name); |
| 467 | if (element == instance->layers.end()) { |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 468 | ALOGE("requested %s layer '%s' not present", |
| 469 | create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ? |
| 470 | "instance" : "device", |
| 471 | layer_name.c_str()); |
| 472 | return VK_ERROR_LAYER_NOT_PRESENT; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 473 | } else { |
| 474 | ActivateLayer(object, instance, layer_name); |
| 475 | } |
| 476 | } |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 477 | return VK_SUCCESS; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | template <class TCreateInfo> |
| 481 | bool AddExtensionToCreateInfo(TCreateInfo& local_create_info, |
| 482 | const char* extension_name, |
| 483 | const VkAllocCallbacks* alloc) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 484 | for (uint32_t i = 0; i < local_create_info.enabledExtensionNameCount; ++i) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 485 | if (!strcmp(extension_name, |
| 486 | local_create_info.ppEnabledExtensionNames[i])) { |
| 487 | return false; |
| 488 | } |
| 489 | } |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 490 | uint32_t extension_count = local_create_info.enabledExtensionNameCount; |
| 491 | local_create_info.enabledExtensionNameCount++; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 492 | void* mem = alloc->pfnAlloc( |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 493 | alloc->pUserData, |
| 494 | local_create_info.enabledExtensionNameCount * sizeof(char*), |
| 495 | alignof(char*), VK_SYSTEM_ALLOC_SCOPE_INSTANCE); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 496 | if (mem) { |
| 497 | const char** enabled_extensions = static_cast<const char**>(mem); |
| 498 | for (uint32_t i = 0; i < extension_count; ++i) { |
| 499 | enabled_extensions[i] = |
| 500 | local_create_info.ppEnabledExtensionNames[i]; |
| 501 | } |
| 502 | enabled_extensions[extension_count] = extension_name; |
| 503 | local_create_info.ppEnabledExtensionNames = enabled_extensions; |
| 504 | } else { |
| 505 | ALOGW("%s extension cannot be enabled: memory allocation failed", |
| 506 | extension_name); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 507 | local_create_info.enabledExtensionNameCount--; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | template <class T> |
| 514 | void FreeAllocatedCreateInfo(T& local_create_info, |
| 515 | const VkAllocCallbacks* alloc) { |
| 516 | alloc->pfnFree( |
| 517 | alloc->pUserData, |
| 518 | const_cast<char**>(local_create_info.ppEnabledExtensionNames)); |
| 519 | } |
| 520 | |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 521 | VkBool32 LogDebugMessageCallback(VkFlags message_flags, |
| 522 | VkDbgObjectType /*obj_type*/, |
| 523 | uint64_t /*src_object*/, |
| 524 | size_t /*location*/, |
| 525 | int32_t message_code, |
| 526 | const char* layer_prefix, |
| 527 | const char* message, |
| 528 | void* /*user_data*/) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 529 | if (message_flags & VK_DBG_REPORT_ERROR_BIT) { |
| 530 | ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 531 | } else if (message_flags & VK_DBG_REPORT_WARN_BIT) { |
| 532 | ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 533 | } |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 534 | return false; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Michael Lentine | d1d5e5e | 2015-11-02 18:32:04 -0800 | [diff] [blame] | 537 | VkResult Noop(...) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 538 | return VK_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) { |
| 542 | if (strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 543 | return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr); |
| 544 | } |
| 545 | if (strcmp(name, "vkCreateDevice") == 0) { |
Michael Lentine | d1d5e5e | 2015-11-02 18:32:04 -0800 | [diff] [blame] | 546 | return reinterpret_cast<PFN_vkVoidFunction>(Noop); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 547 | } |
Michael Lentine | 88594d7 | 2015-11-12 12:49:45 -0800 | [diff] [blame] | 548 | // WSI extensions are not in the driver so return the loader functions |
Michael Lentine | 88594d7 | 2015-11-12 12:49:45 -0800 | [diff] [blame] | 549 | if (strcmp(name, "vkCreateSwapchainKHR") == 0) { |
| 550 | return reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR); |
| 551 | } |
| 552 | if (strcmp(name, "vkDestroySwapchainKHR") == 0) { |
| 553 | return reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR); |
| 554 | } |
| 555 | if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) { |
| 556 | return reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR); |
| 557 | } |
| 558 | if (strcmp(name, "vkAcquireNextImageKHR") == 0) { |
| 559 | return reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR); |
| 560 | } |
| 561 | if (strcmp(name, "vkQueuePresentKHR") == 0) { |
| 562 | return reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR); |
| 563 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 564 | if (!device) |
| 565 | return GetGlobalDeviceProcAddr(name); |
| 566 | Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 567 | return loader_device->instance->drv.GetDeviceProcAddr(device, name); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 568 | } |
| 569 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 570 | // ----------------------------------------------------------------------------- |
| 571 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 572 | // chain. |
| 573 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 574 | void DestroyInstanceBottom(VkInstance instance, |
| 575 | const VkAllocCallbacks* allocator) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 576 | // These checks allow us to call DestroyInstanceBottom from any error path |
| 577 | // in CreateInstanceBottom, before the driver instance is fully initialized. |
| 578 | if (instance->drv.vtbl.instance != VK_NULL_HANDLE && |
| 579 | instance->drv.vtbl.DestroyInstance) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 580 | instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance, |
| 581 | allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 582 | } |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 583 | if (instance->message) { |
| 584 | PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 585 | DebugDestroyMessageCallback = |
| 586 | reinterpret_cast<PFN_vkDbgDestroyMsgCallback>( |
| 587 | vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback")); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 588 | DebugDestroyMessageCallback(instance, instance->message); |
| 589 | } |
Michael Lentine | d1d5e5e | 2015-11-02 18:32:04 -0800 | [diff] [blame] | 590 | for (auto it = instance->active_layers.begin(); |
| 591 | it != instance->active_layers.end(); ++it) { |
Michael Lentine | 1d1e65f | 2015-11-19 14:23:06 -0800 | [diff] [blame] | 592 | DeactivateLayer(instance, it); |
Michael Lentine | d1d5e5e | 2015-11-02 18:32:04 -0800 | [diff] [blame] | 593 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 594 | const VkAllocCallbacks* alloc = instance->alloc; |
| 595 | instance->~VkInstance_T(); |
| 596 | alloc->pfnFree(alloc->pUserData, instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 600 | const VkAllocCallbacks* allocator, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 601 | VkInstance* instance_ptr) { |
| 602 | Instance* instance = *instance_ptr; |
| 603 | VkResult result; |
| 604 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 605 | result = g_hwdevice->CreateInstance(create_info, instance->alloc, |
| 606 | &instance->drv.vtbl.instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 607 | if (result != VK_SUCCESS) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 608 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 609 | return result; |
| 610 | } |
| 611 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 612 | if (!LoadInstanceVtbl( |
| 613 | instance->drv.vtbl.instance, instance->drv.vtbl.instance, |
| 614 | g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 615 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 616 | return VK_ERROR_INITIALIZATION_FAILED; |
| 617 | } |
| 618 | |
| 619 | // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be |
| 620 | // queryable from the Instance, and that the resulting function work for any |
| 621 | // VkDevice created from the instance. |
| 622 | instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 623 | g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance, |
| 624 | "vkGetDeviceProcAddr")); |
| 625 | if (!instance->drv.GetDeviceProcAddr) { |
| 626 | ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr"); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 627 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 628 | return VK_ERROR_INITIALIZATION_FAILED; |
| 629 | } |
| 630 | |
| 631 | hwvulkan_dispatch_t* dispatch = |
| 632 | reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance); |
| 633 | if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) { |
| 634 | // Skip setting dispatch->vtbl on the driver instance handle, since we |
| 635 | // never intentionally call through it; we go through Instance::drv.vtbl |
| 636 | // instead. |
| 637 | } else { |
| 638 | ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR, |
| 639 | dispatch->magic); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 640 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 641 | return VK_ERROR_INITIALIZATION_FAILED; |
| 642 | } |
| 643 | |
| 644 | uint32_t num_physical_devices = 0; |
| 645 | result = instance->drv.vtbl.EnumeratePhysicalDevices( |
| 646 | instance->drv.vtbl.instance, &num_physical_devices, nullptr); |
| 647 | if (result != VK_SUCCESS) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 648 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 649 | return VK_ERROR_INITIALIZATION_FAILED; |
| 650 | } |
| 651 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
| 652 | result = instance->drv.vtbl.EnumeratePhysicalDevices( |
| 653 | instance->drv.vtbl.instance, &num_physical_devices, |
| 654 | instance->physical_devices); |
| 655 | if (result != VK_SUCCESS) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 656 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 657 | return VK_ERROR_INITIALIZATION_FAILED; |
| 658 | } |
| 659 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
| 660 | dispatch = reinterpret_cast<hwvulkan_dispatch_t*>( |
| 661 | instance->physical_devices[i]); |
| 662 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 663 | ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR, |
| 664 | dispatch->magic); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 665 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 666 | return VK_ERROR_INITIALIZATION_FAILED; |
| 667 | } |
| 668 | dispatch->vtbl = instance->vtbl; |
| 669 | } |
| 670 | instance->drv.num_physical_devices = num_physical_devices; |
| 671 | |
| 672 | instance->num_physical_devices = instance->drv.num_physical_devices; |
| 673 | return VK_SUCCESS; |
| 674 | } |
| 675 | |
| 676 | VkResult EnumeratePhysicalDevicesBottom(VkInstance instance, |
| 677 | uint32_t* pdev_count, |
| 678 | VkPhysicalDevice* pdevs) { |
| 679 | uint32_t count = instance->num_physical_devices; |
| 680 | if (pdevs) { |
| 681 | count = std::min(count, *pdev_count); |
| 682 | std::copy(instance->physical_devices, |
| 683 | instance->physical_devices + count, pdevs); |
| 684 | } |
| 685 | *pdev_count = count; |
| 686 | return VK_SUCCESS; |
| 687 | } |
| 688 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 689 | void GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev, |
| 690 | VkPhysicalDeviceFeatures* features) { |
| 691 | GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 694 | void GetPhysicalDeviceFormatPropertiesBottom(VkPhysicalDevice pdev, |
| 695 | VkFormat format, |
| 696 | VkFormatProperties* properties) { |
| 697 | GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 698 | pdev, format, properties); |
| 699 | } |
| 700 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 701 | void GetPhysicalDeviceImageFormatPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 702 | VkPhysicalDevice pdev, |
| 703 | VkFormat format, |
| 704 | VkImageType type, |
| 705 | VkImageTiling tiling, |
| 706 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 707 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 708 | VkImageFormatProperties* properties) { |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 709 | GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties( |
| 710 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 713 | void GetPhysicalDevicePropertiesBottom(VkPhysicalDevice pdev, |
| 714 | VkPhysicalDeviceProperties* properties) { |
| 715 | GetVtbl(pdev) |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 716 | ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties); |
| 717 | } |
| 718 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 719 | void GetPhysicalDeviceQueueFamilyPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 720 | VkPhysicalDevice pdev, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 721 | uint32_t* pCount, |
| 722 | VkQueueFamilyProperties* properties) { |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 723 | GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties( |
| 724 | pdev, pCount, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 727 | void GetPhysicalDeviceMemoryPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 728 | VkPhysicalDevice pdev, |
| 729 | VkPhysicalDeviceMemoryProperties* properties) { |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 730 | GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 731 | pdev, properties); |
| 732 | } |
| 733 | |
| 734 | VkResult CreateDeviceBottom(VkPhysicalDevice pdev, |
| 735 | const VkDeviceCreateInfo* create_info, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 736 | const VkAllocCallbacks* allocator, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 737 | VkDevice* out_device) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 738 | Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 739 | VkResult result; |
| 740 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 741 | if (!allocator) { |
| 742 | if (instance.alloc) |
| 743 | allocator = instance.alloc; |
| 744 | else |
| 745 | allocator = &kDefaultAllocCallbacks; |
| 746 | } |
| 747 | |
| 748 | void* mem = |
| 749 | allocator->pfnAlloc(allocator->pUserData, sizeof(Device), |
| 750 | alignof(Device), VK_SYSTEM_ALLOC_SCOPE_DEVICE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 751 | if (!mem) |
| 752 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 753 | Device* device = new (mem) Device(&instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 754 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 755 | result = ActivateAllLayers(create_info, &instance, device); |
| 756 | if (result != VK_SUCCESS) { |
| 757 | DestroyDevice(device); |
| 758 | return result; |
| 759 | } |
| 760 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 761 | VkDevice drv_device; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 762 | result = instance.drv.vtbl.CreateDevice(pdev, create_info, allocator, |
| 763 | &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 764 | if (result != VK_SUCCESS) { |
| 765 | DestroyDevice(device); |
| 766 | return result; |
| 767 | } |
| 768 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 769 | hwvulkan_dispatch_t* dispatch = |
| 770 | reinterpret_cast<hwvulkan_dispatch_t*>(drv_device); |
| 771 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 772 | ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 773 | PFN_vkDestroyDevice destroy_device = |
| 774 | reinterpret_cast<PFN_vkDestroyDevice>( |
| 775 | instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice")); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 776 | destroy_device(drv_device, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 777 | DestroyDevice(device); |
| 778 | return VK_ERROR_INITIALIZATION_FAILED; |
| 779 | } |
| 780 | dispatch->vtbl = &device->vtbl_storage; |
| 781 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 782 | void* base_object = static_cast<void*>(drv_device); |
| 783 | void* next_object = base_object; |
| 784 | VkLayerLinkedListElem* next_element; |
| 785 | PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr; |
| 786 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 787 | device->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 788 | CallbackAllocator<VkLayerLinkedListElem>(instance.alloc)); |
| 789 | |
| 790 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 791 | size_t idx = i - 1; |
| 792 | next_element = &elem_list[idx]; |
| 793 | next_element->get_proc_addr = |
| 794 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 795 | next_element->base_object = base_object; |
| 796 | next_element->next_element = next_object; |
| 797 | next_object = static_cast<void*>(next_element); |
| 798 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 799 | auto& name = device->active_layers[idx]->first; |
| 800 | auto& handle = device->active_layers[idx]->second.handle; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 801 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 802 | dlsym(handle, (name + "GetDeviceProcAddr").c_str())); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 803 | if (!next_get_proc_addr) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 804 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 805 | dlsym(handle, "vkGetDeviceProcAddr")); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 806 | if (!next_get_proc_addr) { |
| 807 | ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s", |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 808 | name.c_str(), dlerror()); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 809 | next_object = next_element->next_element; |
| 810 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 811 | next_element->get_proc_addr); |
| 812 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object), |
| 817 | static_cast<VkDevice>(next_object), next_get_proc_addr, |
| 818 | device->vtbl_storage)) { |
| 819 | DestroyDevice(device); |
| 820 | return VK_ERROR_INITIALIZATION_FAILED; |
| 821 | } |
| 822 | |
| 823 | PFN_vkCreateDevice layer_createDevice = |
| 824 | reinterpret_cast<PFN_vkCreateDevice>( |
| 825 | device->vtbl_storage.GetDeviceProcAddr(drv_device, |
| 826 | "vkCreateDevice")); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 827 | layer_createDevice(pdev, create_info, allocator, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 828 | |
Michael Lentine | 88594d7 | 2015-11-12 12:49:45 -0800 | [diff] [blame] | 829 | // TODO(mlentine) : This is needed to use WSI layer validation. Remove this |
| 830 | // when new version of layer initialization exits. |
| 831 | if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object), |
| 832 | static_cast<VkDevice>(next_object), next_get_proc_addr, |
| 833 | device->vtbl_storage)) { |
| 834 | DestroyDevice(device); |
| 835 | return VK_ERROR_INITIALIZATION_FAILED; |
| 836 | } |
| 837 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 838 | *out_device = drv_device; |
| 839 | return VK_SUCCESS; |
| 840 | } |
| 841 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 842 | VkResult EnumerateDeviceExtensionPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 843 | VkPhysicalDevice pdev, |
| 844 | const char* layer_name, |
| 845 | uint32_t* properties_count, |
| 846 | VkExtensionProperties* properties) { |
| 847 | // TODO: what are we supposed to do with layer_name here? |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 848 | return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties( |
| 849 | pdev, layer_name, properties_count, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 852 | VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev, |
| 853 | uint32_t* properties_count, |
| 854 | VkLayerProperties* properties) { |
| 855 | return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 856 | pdev, properties_count, properties); |
| 857 | } |
| 858 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 859 | void GetPhysicalDeviceSparseImageFormatPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 860 | VkPhysicalDevice pdev, |
| 861 | VkFormat format, |
| 862 | VkImageType type, |
| 863 | uint32_t samples, |
| 864 | VkImageUsageFlags usage, |
| 865 | VkImageTiling tiling, |
| 866 | uint32_t* properties_count, |
| 867 | VkSparseImageFormatProperties* properties) { |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 868 | GetVtbl(pdev) |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 869 | ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties( |
| 870 | pdev, format, type, samples, usage, tiling, properties_count, |
| 871 | properties); |
| 872 | } |
| 873 | |
| 874 | PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*); |
| 875 | |
| 876 | const InstanceVtbl kBottomInstanceFunctions = { |
| 877 | // clang-format off |
| 878 | .instance = nullptr, |
| 879 | .CreateInstance = CreateInstanceBottom, |
| 880 | .DestroyInstance = DestroyInstanceBottom, |
| 881 | .GetInstanceProcAddr = GetInstanceProcAddrBottom, |
| 882 | .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom, |
| 883 | .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom, |
| 884 | .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom, |
| 885 | .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 886 | .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 887 | .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 888 | .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom, |
| 889 | .CreateDevice = CreateDeviceBottom, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 890 | .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom, |
| 891 | .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 892 | .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom, |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 893 | .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR, |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 894 | .GetPhysicalDeviceSurfaceCapabilitiesKHR = GetPhysicalDeviceSurfaceCapabilitiesKHR, |
| 895 | .GetPhysicalDeviceSurfaceFormatsKHR = GetPhysicalDeviceSurfaceFormatsKHR, |
| 896 | .GetPhysicalDeviceSurfacePresentModesKHR = GetPhysicalDeviceSurfacePresentModesKHR, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 897 | // clang-format on |
| 898 | }; |
| 899 | |
| 900 | PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 901 | // TODO: Possibly move this into the instance table |
| 902 | // TODO: Possibly register the callbacks in the loader |
| 903 | if (strcmp(name, "vkDbgCreateMsgCallback") == 0 || |
| 904 | strcmp(name, "vkDbgDestroyMsgCallback") == 0) { |
| 905 | return reinterpret_cast<PFN_vkVoidFunction>(Noop); |
| 906 | } |
| 907 | if (strcmp(name, "vkCreateInstance") == 0) { |
| 908 | return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom); |
| 909 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 910 | return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name); |
| 911 | } |
| 912 | |
| 913 | } // namespace |
| 914 | |
| 915 | // ----------------------------------------------------------------------------- |
| 916 | // Global functions. These are called directly from the loader entry points, |
| 917 | // without going through a dispatch table. |
| 918 | |
| 919 | namespace vulkan { |
| 920 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 921 | VkResult EnumerateInstanceExtensionProperties( |
| 922 | const char* /*layer_name*/, |
| 923 | uint32_t* count, |
| 924 | VkExtensionProperties* /*properties*/) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 925 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 926 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 927 | |
| 928 | // TODO: not yet implemented |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 929 | ALOGW("vkEnumerateInstanceExtensionProperties not implemented"); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 930 | |
| 931 | *count = 0; |
| 932 | return VK_SUCCESS; |
| 933 | } |
| 934 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 935 | VkResult EnumerateInstanceLayerProperties(uint32_t* count, |
| 936 | VkLayerProperties* /*properties*/) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 937 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 938 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 939 | |
| 940 | // TODO: not yet implemented |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 941 | ALOGW("vkEnumerateInstanceLayerProperties not implemented"); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 942 | |
| 943 | *count = 0; |
| 944 | return VK_SUCCESS; |
| 945 | } |
| 946 | |
| 947 | VkResult CreateInstance(const VkInstanceCreateInfo* create_info, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 948 | const VkAllocCallbacks* allocator, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 949 | VkInstance* out_instance) { |
| 950 | VkResult result; |
| 951 | |
| 952 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 953 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 954 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 955 | if (!allocator) |
| 956 | allocator = &kDefaultAllocCallbacks; |
| 957 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 958 | VkInstanceCreateInfo local_create_info = *create_info; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 959 | create_info = &local_create_info; |
| 960 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 961 | void* instance_mem = |
| 962 | allocator->pfnAlloc(allocator->pUserData, sizeof(Instance), |
| 963 | alignof(Instance), VK_SYSTEM_ALLOC_SCOPE_INSTANCE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 964 | if (!instance_mem) |
| 965 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 966 | Instance* instance = new (instance_mem) Instance(allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 967 | |
| 968 | instance->vtbl_storage = kBottomInstanceFunctions; |
| 969 | instance->vtbl_storage.instance = instance; |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 970 | instance->message = VK_NULL_HANDLE; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 971 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 972 | // Scan layers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 973 | CallbackAllocator<char> string_allocator(instance->alloc); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 974 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 975 | String dir_name("/data/local/tmp/vulkan/", string_allocator); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 976 | FindLayersInDirectory(*instance, dir_name); |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 977 | const std::string& path = LoaderData::GetInstance().layer_path; |
| 978 | dir_name.assign(path.c_str(), path.size()); |
| 979 | dir_name.append("/"); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 980 | FindLayersInDirectory(*instance, dir_name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 981 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 982 | result = ActivateAllLayers(create_info, instance, instance); |
| 983 | if (result != VK_SUCCESS) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 984 | DestroyInstanceBottom(instance, allocator); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 985 | return result; |
| 986 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 987 | |
| 988 | void* base_object = static_cast<void*>(instance); |
| 989 | void* next_object = base_object; |
| 990 | VkLayerLinkedListElem* next_element; |
| 991 | PFN_vkGetInstanceProcAddr next_get_proc_addr = |
| 992 | kBottomInstanceFunctions.GetInstanceProcAddr; |
| 993 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 994 | instance->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 995 | CallbackAllocator<VkLayerLinkedListElem>(instance->alloc)); |
| 996 | |
| 997 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 998 | size_t idx = i - 1; |
| 999 | next_element = &elem_list[idx]; |
| 1000 | next_element->get_proc_addr = |
| 1001 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 1002 | next_element->base_object = base_object; |
| 1003 | next_element->next_element = next_object; |
| 1004 | next_object = static_cast<void*>(next_element); |
| 1005 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1006 | auto& name = instance->active_layers[idx]->first; |
| 1007 | auto& handle = instance->active_layers[idx]->second.handle; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1008 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1009 | dlsym(handle, (name + "GetInstanceProcAddr").c_str())); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1010 | if (!next_get_proc_addr) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1011 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1012 | dlsym(handle, "vkGetInstanceProcAddr")); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 1013 | if (!next_get_proc_addr) { |
| 1014 | ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s", |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1015 | name.c_str(), dlerror()); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 1016 | next_object = next_element->next_element; |
| 1017 | next_get_proc_addr = |
| 1018 | reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
| 1019 | next_element->get_proc_addr); |
| 1020 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object), |
| 1025 | static_cast<VkInstance>(next_object), |
| 1026 | next_get_proc_addr, instance->vtbl_storage)) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1027 | DestroyInstanceBottom(instance, allocator); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1028 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1029 | } |
| 1030 | |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1031 | // Force enable callback extension if required |
| 1032 | bool enable_callback = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1033 | property_get_bool("debug.vulkan.enable_callback", false); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1034 | bool enable_logging = enable_callback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1035 | const char* extension_name = "DEBUG_REPORT"; |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1036 | if (enable_callback) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1037 | enable_callback = AddExtensionToCreateInfo( |
| 1038 | local_create_info, extension_name, instance->alloc); |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1041 | *out_instance = instance; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1042 | result = instance->vtbl_storage.CreateInstance(create_info, allocator, |
| 1043 | out_instance); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1044 | if (enable_callback) |
| 1045 | FreeAllocatedCreateInfo(local_create_info, instance->alloc); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1046 | if (result <= 0) { |
| 1047 | // For every layer, including the loader top and bottom layers: |
| 1048 | // - If a call to the next CreateInstance fails, the layer must clean |
| 1049 | // up anything it has successfully done so far, and propagate the |
| 1050 | // error upwards. |
| 1051 | // - If a layer successfully calls the next layer's CreateInstance, and |
| 1052 | // afterwards must fail for some reason, it must call the next layer's |
| 1053 | // DestroyInstance before returning. |
| 1054 | // - The layer must not call the next layer's DestroyInstance if that |
| 1055 | // layer's CreateInstance wasn't called, or returned failure. |
| 1056 | |
| 1057 | // On failure, CreateInstanceBottom frees the instance struct, so it's |
| 1058 | // already gone at this point. Nothing to do. |
| 1059 | } |
| 1060 | |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1061 | if (enable_logging) { |
| 1062 | PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1063 | DebugCreateMessageCallback = |
| 1064 | reinterpret_cast<PFN_vkDbgCreateMsgCallback>( |
| 1065 | vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback")); |
| 1066 | DebugCreateMessageCallback( |
| 1067 | instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
| 1068 | LogDebugMessageCallback, NULL, &instance->message); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1069 | } |
| 1070 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1071 | return result; |
| 1072 | } |
| 1073 | |
| 1074 | PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) { |
| 1075 | if (!instance) |
| 1076 | return GetGlobalInstanceProcAddr(name); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1077 | // TODO: Possibly move this into the instance table |
| 1078 | if (strcmp(name, "vkDbgCreateMsgCallback") == 0 || |
| 1079 | strcmp(name, "vkDbgDestroyMsgCallback") == 0) { |
| 1080 | if (!instance->vtbl) |
| 1081 | return NULL; |
| 1082 | PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr; |
| 1083 | return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name)); |
| 1084 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1085 | // For special-case functions we always return the loader entry |
| 1086 | if (strcmp(name, "vkGetInstanceProcAddr") == 0 || |
| 1087 | strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 1088 | return GetGlobalInstanceProcAddr(name); |
| 1089 | } |
| 1090 | return GetSpecificInstanceProcAddr(instance->vtbl, name); |
| 1091 | } |
| 1092 | |
| 1093 | PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) { |
| 1094 | if (!device) |
| 1095 | return GetGlobalDeviceProcAddr(name); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1096 | if (strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 1097 | return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr); |
| 1098 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1099 | if (strcmp(name, "vkGetDeviceQueue") == 0) { |
| 1100 | return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue); |
| 1101 | } |
Jesse Hall | fbf97b0 | 2015-11-20 14:17:03 -0800 | [diff] [blame] | 1102 | if (strcmp(name, "vkAllocCommandBuffers") == 0) { |
| 1103 | return reinterpret_cast<PFN_vkVoidFunction>(AllocCommandBuffers); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1104 | } |
| 1105 | if (strcmp(name, "vkDestroyDevice") == 0) { |
| 1106 | return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice); |
| 1107 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1108 | return GetSpecificDeviceProcAddr(GetVtbl(device), name); |
| 1109 | } |
| 1110 | |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 1111 | void GetDeviceQueue(VkDevice drv_device, |
| 1112 | uint32_t family, |
| 1113 | uint32_t index, |
| 1114 | VkQueue* out_queue) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1115 | VkResult result; |
| 1116 | VkQueue queue; |
| 1117 | const DeviceVtbl* vtbl = GetVtbl(drv_device); |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 1118 | vtbl->GetDeviceQueue(drv_device, family, index, &queue); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1119 | hwvulkan_dispatch_t* dispatch = |
| 1120 | reinterpret_cast<hwvulkan_dispatch_t*>(queue); |
Jesse Hall | 606a54e | 2015-11-19 22:17:28 -0800 | [diff] [blame] | 1121 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != vtbl) |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1122 | ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1123 | dispatch->vtbl = vtbl; |
| 1124 | *out_queue = queue; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Jesse Hall | fbf97b0 | 2015-11-20 14:17:03 -0800 | [diff] [blame] | 1127 | VkResult AllocCommandBuffers(VkDevice device, |
| 1128 | const VkCmdBufferAllocInfo* alloc_info, |
| 1129 | VkCmdBuffer* cmdbuffers) { |
| 1130 | const DeviceVtbl* vtbl = GetVtbl(device); |
| 1131 | VkResult result = vtbl->AllocCommandBuffers(device, alloc_info, cmdbuffers); |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1132 | if (result != VK_SUCCESS) |
| 1133 | return result; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1134 | for (uint32_t i = 0; i < alloc_info->bufferCount; i++) { |
Jesse Hall | fbf97b0 | 2015-11-20 14:17:03 -0800 | [diff] [blame] | 1135 | hwvulkan_dispatch_t* dispatch = |
| 1136 | reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuffers[i]); |
| 1137 | ALOGE_IF(dispatch->magic != HWVULKAN_DISPATCH_MAGIC, |
| 1138 | "invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR, |
| 1139 | dispatch->magic); |
| 1140 | dispatch->vtbl = vtbl; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1141 | } |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1142 | return VK_SUCCESS; |
| 1143 | } |
| 1144 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1145 | VkResult DestroyDevice(VkDevice drv_device, |
| 1146 | const VkAllocCallbacks* /*allocator*/) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1147 | const DeviceVtbl* vtbl = GetVtbl(drv_device); |
| 1148 | Device* device = static_cast<Device*>(vtbl->device); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1149 | for (auto it = device->active_layers.begin(); |
| 1150 | it != device->active_layers.end(); ++it) { |
Michael Lentine | 1d1e65f | 2015-11-19 14:23:06 -0800 | [diff] [blame] | 1151 | DeactivateLayer(device->instance, it); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1152 | } |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1153 | vtbl->DestroyDevice(drv_device, device->instance->alloc); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1154 | DestroyDevice(device); |
| 1155 | return VK_SUCCESS; |
| 1156 | } |
| 1157 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1158 | void* AllocMem(VkInstance instance, |
| 1159 | size_t size, |
| 1160 | size_t align, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1161 | VkSystemAllocScope scope) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1162 | const VkAllocCallbacks* alloc_cb = instance->alloc; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1163 | return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, scope); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | void FreeMem(VkInstance instance, void* ptr) { |
| 1167 | const VkAllocCallbacks* alloc_cb = instance->alloc; |
| 1168 | alloc_cb->pfnFree(alloc_cb->pUserData, ptr); |
| 1169 | } |
| 1170 | |
| 1171 | void* AllocMem(VkDevice device, |
| 1172 | size_t size, |
| 1173 | size_t align, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1174 | VkSystemAllocScope scope) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1175 | const VkAllocCallbacks* alloc_cb = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1176 | static_cast<Device*>(GetVtbl(device)->device)->instance->alloc; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1177 | return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, scope); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1180 | void FreeMem(VkDevice device, void* ptr) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1181 | const VkAllocCallbacks* alloc_cb = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1182 | static_cast<Device*>(GetVtbl(device)->device)->instance->alloc; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1183 | alloc_cb->pfnFree(alloc_cb->pUserData, ptr); |
| 1184 | } |
| 1185 | |
| 1186 | const DeviceVtbl& GetDriverVtbl(VkDevice device) { |
| 1187 | // TODO(jessehall): This actually returns the API-level vtbl for the |
| 1188 | // device, not the driver entry points. Given the current use -- getting |
| 1189 | // the driver's private swapchain-related functions -- that works, but is |
| 1190 | // misleading and likely to cause bugs. Fix as part of separating the |
| 1191 | // loader->driver interface from the app->loader interface. |
| 1192 | return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage; |
| 1193 | } |
| 1194 | |
| 1195 | const DeviceVtbl& GetDriverVtbl(VkQueue queue) { |
| 1196 | // TODO(jessehall): This actually returns the API-level vtbl for the |
| 1197 | // device, not the driver entry points. Given the current use -- getting |
| 1198 | // the driver's private swapchain-related functions -- that works, but is |
| 1199 | // misleading and likely to cause bugs. Fix as part of separating the |
| 1200 | // loader->driver interface from the app->loader interface. |
| 1201 | return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage; |
| 1202 | } |
| 1203 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1204 | } // namespace vulkan |