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 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 17 | // module header |
| 18 | #include "loader.h" |
| 19 | // standard C headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 20 | #include <dirent.h> |
| 21 | #include <dlfcn.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 25 | #include <string.h> |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 26 | #include <sys/prctl.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 27 | // standard C++ headers |
| 28 | #include <algorithm> |
| 29 | #include <mutex> |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 30 | #include <sstream> |
| 31 | #include <string> |
| 32 | #include <unordered_map> |
| 33 | #include <vector> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 34 | // platform/library headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 36 | #include <hardware/hwvulkan.h> |
| 37 | #include <log/log.h> |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 38 | #include <vulkan/vulkan_loader_data.h> |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 39 | #include <vulkan/vk_layer_interface.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 40 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 41 | // #define ENABLE_ALLOC_CALLSTACKS 1 |
| 42 | #if ENABLE_ALLOC_CALLSTACKS |
| 43 | #include <utils/CallStack.h> |
| 44 | #define ALOGD_CALLSTACK(...) \ |
| 45 | do { \ |
| 46 | ALOGD(__VA_ARGS__); \ |
| 47 | android::CallStack callstack; \ |
| 48 | callstack.update(); \ |
| 49 | callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \ |
| 50 | } while (false) |
| 51 | #else |
| 52 | #define ALOGD_CALLSTACK(...) \ |
| 53 | do { \ |
| 54 | } while (false) |
| 55 | #endif |
| 56 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 57 | using namespace vulkan; |
| 58 | |
| 59 | static const uint32_t kMaxPhysicalDevices = 4; |
| 60 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 61 | namespace { |
| 62 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 63 | // ---------------------------------------------------------------------------- |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 64 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 65 | // Standard-library allocator that delegates to VkAllocationCallbacks. |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 66 | // |
| 67 | // TODO(jessehall): This class currently always uses |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 68 | // VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 69 | // parameter or a constructor parameter. The former would help catch bugs |
| 70 | // where we use the wrong scope, e.g. adding a command-scope string to an |
| 71 | // 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] | 72 | template <class T> |
| 73 | class CallbackAllocator { |
| 74 | public: |
| 75 | typedef T value_type; |
| 76 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 77 | CallbackAllocator(const VkAllocationCallbacks* alloc_input) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 78 | : alloc(alloc_input) {} |
| 79 | |
| 80 | template <class T2> |
| 81 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 82 | : alloc(other.alloc) {} |
| 83 | |
| 84 | T* allocate(std::size_t n) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 85 | void* mem = |
| 86 | alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T), |
| 87 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 88 | if (!mem) |
| 89 | throw std::bad_alloc(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 90 | return static_cast<T*>(mem); |
| 91 | } |
| 92 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 93 | void deallocate(T* array, std::size_t /*n*/) noexcept { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 94 | alloc->pfnFree(alloc->pUserData, array); |
| 95 | } |
| 96 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 97 | const VkAllocationCallbacks* alloc; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 98 | }; |
| 99 | // These are needed in order to move Strings |
| 100 | template <class T> |
| 101 | bool operator==(const CallbackAllocator<T>& alloc1, |
| 102 | const CallbackAllocator<T>& alloc2) { |
| 103 | return alloc1.alloc == alloc2.alloc; |
| 104 | } |
| 105 | template <class T> |
| 106 | bool operator!=(const CallbackAllocator<T>& alloc1, |
| 107 | const CallbackAllocator<T>& alloc2) { |
| 108 | return !(alloc1 == alloc2); |
| 109 | } |
| 110 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 111 | template <class T> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 112 | using Vector = std::vector<T, CallbackAllocator<T>>; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 113 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 114 | typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>> |
| 115 | String; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 116 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 117 | // ---------------------------------------------------------------------------- |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 118 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 119 | VKAPI_ATTR void* DefaultAllocate(void*, |
| 120 | size_t size, |
| 121 | size_t alignment, |
| 122 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 123 | void* ptr = nullptr; |
| 124 | // Vulkan requires 'alignment' to be a power of two, but posix_memalign |
| 125 | // additionally requires that it be at least sizeof(void*). |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 126 | int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size); |
| 127 | ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment, |
| 128 | ret, ptr); |
| 129 | return ret == 0 ? ptr : nullptr; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 132 | VKAPI_ATTR void* DefaultReallocate(void*, |
| 133 | void* ptr, |
| 134 | size_t size, |
| 135 | size_t alignment, |
| 136 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 137 | if (size == 0) { |
| 138 | free(ptr); |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | // TODO(jessehall): Right now we never shrink allocations; if the new |
| 143 | // request is smaller than the existing chunk, we just continue using it. |
| 144 | // Right now the loader never reallocs, so this doesn't matter. If that |
| 145 | // changes, or if this code is copied into some other project, this should |
| 146 | // probably have a heuristic to allocate-copy-free when doing so will save |
| 147 | // "enough" space. |
| 148 | size_t old_size = ptr ? malloc_usable_size(ptr) : 0; |
| 149 | if (size <= old_size) |
| 150 | return ptr; |
| 151 | |
| 152 | void* new_ptr = nullptr; |
| 153 | if (posix_memalign(&new_ptr, alignment, size) != 0) |
| 154 | return nullptr; |
| 155 | if (ptr) { |
| 156 | memcpy(new_ptr, ptr, std::min(old_size, size)); |
| 157 | free(ptr); |
| 158 | } |
| 159 | return new_ptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 162 | VKAPI_ATTR void DefaultFree(void*, void* ptr) { |
| 163 | ALOGD_CALLSTACK("Free: %p", ptr); |
| 164 | free(ptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 167 | const VkAllocationCallbacks kDefaultAllocCallbacks = { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 168 | .pUserData = nullptr, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 169 | .pfnAllocation = DefaultAllocate, |
| 170 | .pfnReallocation = DefaultReallocate, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 171 | .pfnFree = DefaultFree, |
| 172 | }; |
| 173 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 174 | // ---------------------------------------------------------------------------- |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 175 | // Global Data and Initialization |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 176 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 177 | hwvulkan_device_t* g_hwdevice = nullptr; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 178 | InstanceExtensionSet g_driver_instance_extensions; |
| 179 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 180 | void LoadVulkanHAL() { |
| 181 | static const hwvulkan_module_t* module; |
| 182 | int result = |
| 183 | hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module)); |
| 184 | if (result != 0) { |
| 185 | ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result); |
| 186 | return; |
| 187 | } |
| 188 | result = module->common.methods->open( |
| 189 | &module->common, HWVULKAN_DEVICE_0, |
| 190 | reinterpret_cast<hw_device_t**>(&g_hwdevice)); |
| 191 | if (result != 0) { |
| 192 | ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result), |
| 193 | result); |
| 194 | module = nullptr; |
| 195 | return; |
| 196 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 197 | |
| 198 | VkResult vkresult; |
| 199 | uint32_t count; |
| 200 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 201 | nullptr, &count, nullptr)) != VK_SUCCESS) { |
| 202 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 203 | vkresult); |
| 204 | g_hwdevice->common.close(&g_hwdevice->common); |
| 205 | g_hwdevice = nullptr; |
| 206 | module = nullptr; |
| 207 | return; |
| 208 | } |
| 209 | VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>( |
| 210 | alloca(count * sizeof(VkExtensionProperties))); |
| 211 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 212 | nullptr, &count, extensions)) != VK_SUCCESS) { |
| 213 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 214 | vkresult); |
| 215 | g_hwdevice->common.close(&g_hwdevice->common); |
| 216 | g_hwdevice = nullptr; |
| 217 | module = nullptr; |
| 218 | return; |
| 219 | } |
| 220 | ALOGV_IF(count > 0, "Driver-supported instance extensions:"); |
| 221 | for (uint32_t i = 0; i < count; i++) { |
| 222 | ALOGV(" %s (v%u)", extensions[i].extensionName, |
| 223 | extensions[i].specVersion); |
| 224 | InstanceExtension id = |
| 225 | InstanceExtensionFromName(extensions[i].extensionName); |
| 226 | if (id != kInstanceExtensionCount) |
| 227 | g_driver_instance_extensions.set(id); |
| 228 | } |
| 229 | // Ignore driver attempts to support loader extensions |
| 230 | g_driver_instance_extensions.reset(kKHR_surface); |
| 231 | g_driver_instance_extensions.reset(kKHR_android_surface); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 234 | bool EnsureInitialized() { |
| 235 | static std::once_flag once_flag; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 236 | std::call_once(once_flag, []() { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 237 | LoadVulkanHAL(); |
| 238 | DiscoverLayers(); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 239 | }); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 240 | return g_hwdevice != nullptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 243 | // ----------------------------------------------------------------------------- |
| 244 | |
| 245 | struct Instance { |
| 246 | Instance(const VkAllocationCallbacks* alloc_callbacks) |
| 247 | : dispatch_ptr(&dispatch), |
| 248 | handle(reinterpret_cast<VkInstance>(&dispatch_ptr)), |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 249 | alloc(alloc_callbacks), |
| 250 | num_physical_devices(0), |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 251 | active_layers(CallbackAllocator<LayerRef>(alloc)), |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 252 | message(VK_NULL_HANDLE) { |
| 253 | memset(&dispatch, 0, sizeof(dispatch)); |
| 254 | memset(physical_devices, 0, sizeof(physical_devices)); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 255 | drv.instance = VK_NULL_HANDLE; |
| 256 | memset(&drv.dispatch, 0, sizeof(drv.dispatch)); |
| 257 | drv.num_physical_devices = 0; |
| 258 | } |
| 259 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 260 | ~Instance() {} |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 261 | |
| 262 | const InstanceDispatchTable* dispatch_ptr; |
| 263 | const VkInstance handle; |
| 264 | InstanceDispatchTable dispatch; |
| 265 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 266 | const VkAllocationCallbacks* alloc; |
| 267 | uint32_t num_physical_devices; |
| 268 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 269 | DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 270 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 271 | Vector<LayerRef> active_layers; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 272 | VkDebugReportCallbackEXT message; |
| 273 | DebugReportCallbackList debug_report_callbacks; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 274 | |
| 275 | struct { |
| 276 | VkInstance instance; |
| 277 | DriverDispatchTable dispatch; |
| 278 | uint32_t num_physical_devices; |
| 279 | } drv; // may eventually be an array |
| 280 | }; |
| 281 | |
| 282 | struct Device { |
| 283 | Device(Instance* instance_) |
| 284 | : instance(instance_), |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 285 | active_layers(CallbackAllocator<LayerRef>(instance->alloc)) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 286 | memset(&dispatch, 0, sizeof(dispatch)); |
| 287 | } |
| 288 | DeviceDispatchTable dispatch; |
| 289 | Instance* instance; |
| 290 | PFN_vkGetDeviceProcAddr get_device_proc_addr; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 291 | Vector<LayerRef> active_layers; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | template <typename THandle> |
| 295 | struct HandleTraits {}; |
| 296 | template <> |
| 297 | struct HandleTraits<VkInstance> { |
| 298 | typedef Instance LoaderObjectType; |
| 299 | }; |
| 300 | template <> |
| 301 | struct HandleTraits<VkPhysicalDevice> { |
| 302 | typedef Instance LoaderObjectType; |
| 303 | }; |
| 304 | template <> |
| 305 | struct HandleTraits<VkDevice> { |
| 306 | typedef Device LoaderObjectType; |
| 307 | }; |
| 308 | template <> |
| 309 | struct HandleTraits<VkQueue> { |
| 310 | typedef Device LoaderObjectType; |
| 311 | }; |
| 312 | template <> |
| 313 | struct HandleTraits<VkCommandBuffer> { |
| 314 | typedef Device LoaderObjectType; |
| 315 | }; |
| 316 | |
| 317 | template <typename THandle> |
| 318 | typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent( |
| 319 | THandle handle) { |
| 320 | // TODO(jessehall): Make Instance and Device POD types (by removing the |
| 321 | // non-default constructors), so that offsetof is actually legal to use. |
| 322 | // The specific case we're using here is safe in gcc/clang (and probably |
| 323 | // most other C++ compilers), but isn't guaranteed by C++. |
| 324 | typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType; |
| 325 | #pragma clang diagnostic push |
| 326 | #pragma clang diagnostic ignored "-Winvalid-offsetof" |
| 327 | const size_t kDispatchOffset = offsetof(ObjectType, dispatch); |
| 328 | #pragma clang diagnostic pop |
| 329 | |
| 330 | const auto& dispatch = GetDispatchTable(handle); |
| 331 | uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch); |
| 332 | uintptr_t object_addr = dispatch_addr - kDispatchOffset; |
| 333 | return *reinterpret_cast<ObjectType*>(object_addr); |
| 334 | } |
| 335 | |
| 336 | // ----------------------------------------------------------------------------- |
| 337 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 338 | void DestroyDevice(Device* device) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 339 | const VkAllocationCallbacks* alloc = device->instance->alloc; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 340 | device->~Device(); |
| 341 | alloc->pfnFree(alloc->pUserData, device); |
| 342 | } |
| 343 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 344 | template <class TObject> |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 345 | LayerRef GetLayerRef(const char* name); |
| 346 | template <> |
| 347 | LayerRef GetLayerRef<Instance>(const char* name) { |
| 348 | return GetInstanceLayerRef(name); |
| 349 | } |
| 350 | template <> |
| 351 | LayerRef GetLayerRef<Device>(const char* name) { |
| 352 | return GetDeviceLayerRef(name); |
| 353 | } |
| 354 | |
| 355 | template <class TObject> |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 356 | bool ActivateLayer(TObject* object, const char* name) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 357 | LayerRef layer(GetLayerRef<TObject>(name)); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 358 | if (!layer) |
| 359 | return false; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 360 | if (std::find(object->active_layers.begin(), object->active_layers.end(), |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 361 | layer) == object->active_layers.end()) { |
| 362 | try { |
| 363 | object->active_layers.push_back(std::move(layer)); |
| 364 | } catch (std::bad_alloc&) { |
| 365 | // TODO(jessehall): We should fail with VK_ERROR_OUT_OF_MEMORY |
| 366 | // if we can't enable a requested layer. Callers currently ignore |
| 367 | // ActivateLayer's return value. |
| 368 | ALOGW("failed to activate layer '%s': out of memory", name); |
| 369 | return false; |
| 370 | } |
| 371 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 372 | ALOGV("activated layer '%s'", name); |
| 373 | return true; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 376 | struct InstanceNamesPair { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 377 | Instance* instance; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 378 | Vector<String>* layer_names; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 379 | }; |
| 380 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 381 | void SetLayerNamesFromProperty(const char* name, |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 382 | const char* value, |
| 383 | void* data) { |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 384 | try { |
| 385 | const char prefix[] = "debug.vulkan.layer."; |
| 386 | const size_t prefixlen = sizeof(prefix) - 1; |
| 387 | if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0) |
| 388 | return; |
| 389 | const char* number_str = name + prefixlen; |
| 390 | long layer_number = strtol(number_str, nullptr, 10); |
| 391 | if (layer_number <= 0 || layer_number == LONG_MAX) { |
| 392 | ALOGW("Cannot use a layer at number %ld from string %s", |
| 393 | layer_number, number_str); |
| 394 | return; |
| 395 | } |
| 396 | auto instance_names_pair = static_cast<InstanceNamesPair*>(data); |
| 397 | Vector<String>* layer_names = instance_names_pair->layer_names; |
| 398 | Instance* instance = instance_names_pair->instance; |
| 399 | size_t layer_size = static_cast<size_t>(layer_number); |
| 400 | if (layer_size > layer_names->size()) { |
| 401 | layer_names->resize( |
| 402 | layer_size, String(CallbackAllocator<char>(instance->alloc))); |
| 403 | } |
| 404 | (*layer_names)[layer_size - 1] = value; |
| 405 | } catch (std::bad_alloc&) { |
| 406 | ALOGW("failed to handle property '%s'='%s': out of memory", name, |
| 407 | value); |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 408 | return; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | template <class TInfo, class TObject> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 413 | VkResult ActivateAllLayers(TInfo create_info, |
| 414 | Instance* instance, |
| 415 | TObject* object) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 416 | ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO || |
| 417 | create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 418 | "Cannot activate layers for unknown object %p", object); |
| 419 | CallbackAllocator<char> string_allocator(instance->alloc); |
| 420 | // Load system layers |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 421 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 422 | char layer_prop[PROPERTY_VALUE_MAX]; |
| 423 | property_get("debug.vulkan.layers", layer_prop, ""); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 424 | char* strtok_state; |
| 425 | char* layer_name = nullptr; |
| 426 | while ((layer_name = strtok_r(layer_name ? nullptr : layer_prop, ":", |
| 427 | &strtok_state))) { |
| 428 | ActivateLayer(object, layer_name); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 429 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 430 | Vector<String> layer_names(CallbackAllocator<String>(instance->alloc)); |
| 431 | InstanceNamesPair instance_names_pair = {.instance = instance, |
| 432 | .layer_names = &layer_names}; |
| 433 | property_list(SetLayerNamesFromProperty, |
| 434 | static_cast<void*>(&instance_names_pair)); |
| 435 | for (auto layer_name_element : layer_names) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 436 | ActivateLayer(object, layer_name_element.c_str()); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 437 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 438 | } |
| 439 | // Load app layers |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 440 | for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 441 | if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) { |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 442 | ALOGE("requested %s layer '%s' not present", |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 443 | create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO |
| 444 | ? "instance" |
| 445 | : "device", |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 446 | create_info->ppEnabledLayerNames[i]); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 447 | return VK_ERROR_LAYER_NOT_PRESENT; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 448 | } |
| 449 | } |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 450 | return VK_SUCCESS; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | template <class TCreateInfo> |
| 454 | bool AddExtensionToCreateInfo(TCreateInfo& local_create_info, |
| 455 | const char* extension_name, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 456 | const VkAllocationCallbacks* alloc) { |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 457 | for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 458 | if (!strcmp(extension_name, |
| 459 | local_create_info.ppEnabledExtensionNames[i])) { |
| 460 | return false; |
| 461 | } |
| 462 | } |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 463 | uint32_t extension_count = local_create_info.enabledExtensionCount; |
| 464 | local_create_info.enabledExtensionCount++; |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 465 | void* mem = alloc->pfnAllocation( |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 466 | alloc->pUserData, |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 467 | local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*), |
| 468 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 469 | if (mem) { |
| 470 | const char** enabled_extensions = static_cast<const char**>(mem); |
| 471 | for (uint32_t i = 0; i < extension_count; ++i) { |
| 472 | enabled_extensions[i] = |
| 473 | local_create_info.ppEnabledExtensionNames[i]; |
| 474 | } |
| 475 | enabled_extensions[extension_count] = extension_name; |
| 476 | local_create_info.ppEnabledExtensionNames = enabled_extensions; |
| 477 | } else { |
| 478 | ALOGW("%s extension cannot be enabled: memory allocation failed", |
| 479 | extension_name); |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 480 | local_create_info.enabledExtensionCount--; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | template <class T> |
| 487 | void FreeAllocatedCreateInfo(T& local_create_info, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 488 | const VkAllocationCallbacks* alloc) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 489 | alloc->pfnFree( |
| 490 | alloc->pUserData, |
| 491 | const_cast<char**>(local_create_info.ppEnabledExtensionNames)); |
| 492 | } |
| 493 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 494 | VKAPI_ATTR |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 495 | VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags, |
| 496 | VkDebugReportObjectTypeEXT /*objectType*/, |
| 497 | uint64_t /*object*/, |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 498 | size_t /*location*/, |
| 499 | int32_t message_code, |
| 500 | const char* layer_prefix, |
| 501 | const char* message, |
| 502 | void* /*user_data*/) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 503 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 504 | ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 505 | } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 506 | ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 507 | } |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 508 | return false; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 511 | VkResult Noop() { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 512 | return VK_SUCCESS; |
| 513 | } |
| 514 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 515 | /* |
| 516 | * This function will return the pNext pointer of any |
| 517 | * CreateInfo extensions that are not loader extensions. |
| 518 | * This is used to skip past the loader extensions prepended |
| 519 | * to the list during CreateInstance and CreateDevice. |
| 520 | */ |
| 521 | void* StripCreateExtensions(const void* pNext) { |
| 522 | VkLayerInstanceCreateInfo* create_info = |
| 523 | const_cast<VkLayerInstanceCreateInfo*>( |
| 524 | static_cast<const VkLayerInstanceCreateInfo*>(pNext)); |
| 525 | |
| 526 | while ( |
| 527 | create_info && |
| 528 | (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO || |
| 529 | create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) { |
| 530 | create_info = const_cast<VkLayerInstanceCreateInfo*>( |
| 531 | static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext)); |
| 532 | } |
| 533 | |
| 534 | return create_info; |
| 535 | } |
| 536 | |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 537 | // Separate out cleaning up the layers and instance storage |
| 538 | // to avoid code duplication in the many failure cases in |
| 539 | // in CreateInstance_Top |
| 540 | void TeardownInstance( |
| 541 | VkInstance vkinstance, |
| 542 | const VkAllocationCallbacks* /* allocator */) { |
| 543 | Instance& instance = GetDispatchParent(vkinstance); |
| 544 | instance.active_layers.clear(); |
| 545 | const VkAllocationCallbacks* alloc = instance.alloc; |
| 546 | instance.~Instance(); |
| 547 | alloc->pfnFree(alloc->pUserData, &instance); |
| 548 | } |
| 549 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 550 | } // anonymous namespace |
| 551 | |
| 552 | namespace vulkan { |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 553 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 554 | // ----------------------------------------------------------------------------- |
| 555 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 556 | // chain. |
| 557 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 558 | VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info, |
| 559 | const VkAllocationCallbacks* allocator, |
| 560 | VkInstance* vkinstance) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 561 | VkResult result; |
| 562 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 563 | VkLayerInstanceCreateInfo* chain_info = |
| 564 | const_cast<VkLayerInstanceCreateInfo*>( |
| 565 | static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext)); |
| 566 | while ( |
| 567 | chain_info && |
| 568 | !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && |
| 569 | chain_info->function == VK_LAYER_FUNCTION_INSTANCE)) { |
| 570 | chain_info = const_cast<VkLayerInstanceCreateInfo*>( |
| 571 | static_cast<const VkLayerInstanceCreateInfo*>(chain_info->pNext)); |
| 572 | } |
| 573 | ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!"); |
| 574 | |
| 575 | Instance& instance = GetDispatchParent( |
| 576 | static_cast<VkInstance>(chain_info->u.instanceInfo.instance_info)); |
| 577 | |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 578 | // Check that all enabled extensions are supported |
| 579 | InstanceExtensionSet enabled_extensions; |
| 580 | uint32_t num_driver_extensions = 0; |
| 581 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 582 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 583 | InstanceExtension id = InstanceExtensionFromName(name); |
| 584 | if (id != kInstanceExtensionCount) { |
| 585 | if (g_driver_instance_extensions[id]) { |
| 586 | num_driver_extensions++; |
| 587 | enabled_extensions.set(id); |
| 588 | continue; |
| 589 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 590 | if (id == kKHR_surface || id == kKHR_android_surface) { |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 591 | enabled_extensions.set(id); |
| 592 | continue; |
| 593 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 594 | // The loader natively supports debug report. |
| 595 | if (id == kEXT_debug_report) { |
| 596 | continue; |
| 597 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 598 | } |
| 599 | bool supported = false; |
| 600 | for (const auto& layer : instance.active_layers) { |
| 601 | if (layer.SupportsExtension(name)) |
| 602 | supported = true; |
| 603 | } |
| 604 | if (!supported) { |
| 605 | ALOGE( |
| 606 | "requested instance extension '%s' not supported by " |
| 607 | "loader, driver, or any active layers", |
| 608 | name); |
| 609 | DestroyInstance_Bottom(instance.handle, allocator); |
| 610 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 611 | } |
| 612 | } |
| 613 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 614 | VkInstanceCreateInfo driver_create_info = *create_info; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 615 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 616 | driver_create_info.enabledLayerCount = 0; |
| 617 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 618 | driver_create_info.enabledExtensionCount = 0; |
| 619 | driver_create_info.ppEnabledExtensionNames = nullptr; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 620 | if (num_driver_extensions > 0) { |
| 621 | const char** names = static_cast<const char**>( |
| 622 | alloca(num_driver_extensions * sizeof(char*))); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 623 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 624 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 625 | InstanceExtension id = InstanceExtensionFromName(name); |
| 626 | if (id != kInstanceExtensionCount) { |
| 627 | if (g_driver_instance_extensions[id]) { |
| 628 | names[driver_create_info.enabledExtensionCount++] = name; |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 629 | continue; |
| 630 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | driver_create_info.ppEnabledExtensionNames = names; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 634 | ALOG_ASSERT( |
| 635 | driver_create_info.enabledExtensionCount == num_driver_extensions, |
| 636 | "counted enabled driver instance extensions twice and got " |
| 637 | "different answers!"); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 638 | } |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 639 | |
| 640 | result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 641 | &instance.drv.instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 642 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 643 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 644 | return result; |
| 645 | } |
| 646 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 647 | hwvulkan_dispatch_t* drv_dispatch = |
| 648 | reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 649 | if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 650 | ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 651 | drv_dispatch->magic); |
| 652 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 653 | return VK_ERROR_INITIALIZATION_FAILED; |
| 654 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 655 | // Skip setting drv_dispatch->vtbl, since we never call through it; |
| 656 | // we go through instance.drv.dispatch instead. |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 657 | |
Courtney Goeltzenleuchter | aa6c872 | 2016-01-29 08:57:16 -0700 | [diff] [blame] | 658 | if (!LoadDriverDispatchTable(instance.drv.instance, |
| 659 | g_hwdevice->GetInstanceProcAddr, |
| 660 | enabled_extensions, instance.drv.dispatch)) { |
| 661 | DestroyInstance_Bottom(instance.handle, allocator); |
| 662 | return VK_ERROR_INITIALIZATION_FAILED; |
| 663 | } |
| 664 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 665 | uint32_t num_physical_devices = 0; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 666 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
| 667 | instance.drv.instance, &num_physical_devices, nullptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 668 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 669 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 670 | return VK_ERROR_INITIALIZATION_FAILED; |
| 671 | } |
| 672 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 673 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
| 674 | instance.drv.instance, &num_physical_devices, |
| 675 | instance.physical_devices); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 676 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 677 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 678 | return VK_ERROR_INITIALIZATION_FAILED; |
| 679 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 680 | |
| 681 | Vector<VkExtensionProperties> extensions( |
| 682 | Vector<VkExtensionProperties>::allocator_type(instance.alloc)); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 683 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 684 | hwvulkan_dispatch_t* pdev_dispatch = |
| 685 | reinterpret_cast<hwvulkan_dispatch_t*>( |
| 686 | instance.physical_devices[i]); |
| 687 | if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 688 | ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 689 | pdev_dispatch->magic); |
| 690 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 691 | return VK_ERROR_INITIALIZATION_FAILED; |
| 692 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 693 | pdev_dispatch->vtbl = instance.dispatch_ptr; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 694 | |
| 695 | uint32_t count; |
| 696 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 697 | instance.physical_devices[i], nullptr, &count, nullptr)) != |
| 698 | VK_SUCCESS) { |
| 699 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 700 | result); |
| 701 | continue; |
| 702 | } |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 703 | try { |
| 704 | extensions.resize(count); |
| 705 | } catch (std::bad_alloc&) { |
| 706 | ALOGE("instance creation failed: out of memory"); |
| 707 | DestroyInstance_Bottom(instance.handle, allocator); |
| 708 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 709 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 710 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 711 | instance.physical_devices[i], nullptr, &count, |
| 712 | extensions.data())) != VK_SUCCESS) { |
| 713 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 714 | result); |
| 715 | continue; |
| 716 | } |
| 717 | ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i); |
| 718 | for (const auto& extension : extensions) { |
| 719 | ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion); |
| 720 | DeviceExtension id = |
| 721 | DeviceExtensionFromName(extension.extensionName); |
| 722 | if (id == kDeviceExtensionCount) { |
| 723 | ALOGW("driver gpu[%u] extension '%s' unknown to loader", i, |
| 724 | extension.extensionName); |
| 725 | } else { |
| 726 | instance.physical_device_driver_extensions[i].set(id); |
| 727 | } |
| 728 | } |
| 729 | // Ignore driver attempts to support loader extensions |
| 730 | instance.physical_device_driver_extensions[i].reset(kKHR_swapchain); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 731 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 732 | instance.drv.num_physical_devices = num_physical_devices; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 733 | instance.num_physical_devices = instance.drv.num_physical_devices; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 734 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 735 | *vkinstance = instance.handle; |
| 736 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 737 | return VK_SUCCESS; |
| 738 | } |
| 739 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 740 | PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) { |
| 741 | PFN_vkVoidFunction pfn; |
| 742 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 743 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 744 | return nullptr; |
| 745 | } |
| 746 | |
| 747 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 748 | uint32_t* pdev_count, |
| 749 | VkPhysicalDevice* pdevs) { |
| 750 | Instance& instance = GetDispatchParent(vkinstance); |
| 751 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 752 | if (pdevs) { |
| 753 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 754 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 755 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 756 | } |
| 757 | *pdev_count = count; |
| 758 | return VK_SUCCESS; |
| 759 | } |
| 760 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 761 | void GetPhysicalDeviceProperties_Bottom( |
| 762 | VkPhysicalDevice pdev, |
| 763 | VkPhysicalDeviceProperties* properties) { |
| 764 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties( |
| 765 | pdev, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 768 | void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev, |
| 769 | VkPhysicalDeviceFeatures* features) { |
| 770 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev, |
| 771 | features); |
| 772 | } |
| 773 | |
| 774 | void GetPhysicalDeviceMemoryProperties_Bottom( |
| 775 | VkPhysicalDevice pdev, |
| 776 | VkPhysicalDeviceMemoryProperties* properties) { |
| 777 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties( |
| 778 | pdev, properties); |
| 779 | } |
| 780 | |
| 781 | void GetPhysicalDeviceQueueFamilyProperties_Bottom( |
| 782 | VkPhysicalDevice pdev, |
| 783 | uint32_t* pCount, |
| 784 | VkQueueFamilyProperties* properties) { |
| 785 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties( |
| 786 | pdev, pCount, properties); |
| 787 | } |
| 788 | |
| 789 | void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev, |
| 790 | VkFormat format, |
| 791 | VkFormatProperties* properties) { |
| 792 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 793 | pdev, format, properties); |
| 794 | } |
| 795 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 796 | VkResult GetPhysicalDeviceImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 797 | VkPhysicalDevice pdev, |
| 798 | VkFormat format, |
| 799 | VkImageType type, |
| 800 | VkImageTiling tiling, |
| 801 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 802 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 803 | VkImageFormatProperties* properties) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 804 | return GetDispatchParent(pdev) |
| 805 | .drv.dispatch.GetPhysicalDeviceImageFormatProperties( |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 806 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 809 | void GetPhysicalDeviceSparseImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 810 | VkPhysicalDevice pdev, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 811 | VkFormat format, |
| 812 | VkImageType type, |
| 813 | VkSampleCountFlagBits samples, |
| 814 | VkImageUsageFlags usage, |
| 815 | VkImageTiling tiling, |
| 816 | uint32_t* properties_count, |
| 817 | VkSparseImageFormatProperties* properties) { |
| 818 | GetDispatchParent(pdev) |
| 819 | .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties( |
| 820 | pdev, format, type, samples, usage, tiling, properties_count, |
| 821 | properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Courtney Goeltzenleuchter | 26d394b | 2016-02-07 10:32:27 -0700 | [diff] [blame^] | 824 | // This is a no-op, the Top function returns the aggregate layer property |
| 825 | // data. This is to keep the dispatch generator happy. |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 826 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 827 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Courtney Goeltzenleuchter | 26d394b | 2016-02-07 10:32:27 -0700 | [diff] [blame^] | 828 | VkPhysicalDevice /*pdev*/, |
| 829 | const char* /*layer_name*/, |
| 830 | uint32_t* /*properties_count*/, |
| 831 | VkExtensionProperties* /*properties*/) { |
| 832 | return VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Courtney Goeltzenleuchter | 1cc0d37 | 2016-02-05 17:10:59 -0700 | [diff] [blame] | 835 | // This is a no-op, the Top function returns the aggregate layer property |
| 836 | // data. This is to keep the dispatch generator happy. |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 837 | VKAPI_ATTR |
Courtney Goeltzenleuchter | 1cc0d37 | 2016-02-05 17:10:59 -0700 | [diff] [blame] | 838 | VkResult EnumerateDeviceLayerProperties_Bottom( |
| 839 | VkPhysicalDevice /*pdev*/, |
| 840 | uint32_t* /*properties_count*/, |
| 841 | VkLayerProperties* /*properties*/) { |
| 842 | return VK_SUCCESS; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | VKAPI_ATTR |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 846 | VkResult CreateDevice_Bottom(VkPhysicalDevice gpu, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 847 | const VkDeviceCreateInfo* create_info, |
| 848 | const VkAllocationCallbacks* allocator, |
| 849 | VkDevice* device_out) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 850 | VkLayerDeviceCreateInfo* chain_info = const_cast<VkLayerDeviceCreateInfo*>( |
| 851 | static_cast<const VkLayerDeviceCreateInfo*>(create_info->pNext)); |
| 852 | while (chain_info && |
| 853 | !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && |
| 854 | chain_info->function == VK_LAYER_FUNCTION_DEVICE)) { |
| 855 | chain_info = const_cast<VkLayerDeviceCreateInfo*>( |
| 856 | static_cast<const VkLayerDeviceCreateInfo*>(chain_info->pNext)); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 857 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 858 | ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!"); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 859 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 860 | Instance& instance = GetDispatchParent(gpu); |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 861 | size_t gpu_idx = 0; |
| 862 | while (instance.physical_devices[gpu_idx] != gpu) |
| 863 | gpu_idx++; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 864 | Device* device = static_cast<Device*>(chain_info->u.deviceInfo.device_info); |
| 865 | PFN_vkGetInstanceProcAddr get_instance_proc_addr = |
| 866 | chain_info->u.deviceInfo.pfnNextGetInstanceProcAddr; |
| 867 | |
| 868 | VkDeviceCreateInfo driver_create_info = *create_info; |
| 869 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
| 870 | driver_create_info.enabledLayerCount = 0; |
| 871 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 872 | |
| 873 | uint32_t num_driver_extensions = 0; |
| 874 | const char** driver_extensions = static_cast<const char**>( |
| 875 | alloca(create_info->enabledExtensionCount * sizeof(const char*))); |
| 876 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 877 | const char* name = create_info->ppEnabledExtensionNames[i]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 878 | DeviceExtension id = DeviceExtensionFromName(name); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 879 | if (id != kDeviceExtensionCount) { |
| 880 | if (instance.physical_device_driver_extensions[gpu_idx][id]) { |
| 881 | driver_extensions[num_driver_extensions++] = name; |
| 882 | continue; |
| 883 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 884 | // Add the VK_ANDROID_native_buffer extension to the list iff |
| 885 | // the VK_KHR_swapchain extension was requested |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 886 | if (id == kKHR_swapchain && |
| 887 | instance.physical_device_driver_extensions |
| 888 | [gpu_idx][kANDROID_native_buffer]) { |
| 889 | driver_extensions[num_driver_extensions++] = |
| 890 | VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME; |
| 891 | continue; |
| 892 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 893 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 894 | bool supported = false; |
| 895 | for (const auto& layer : device->active_layers) { |
| 896 | if (layer.SupportsExtension(name)) |
| 897 | supported = true; |
| 898 | } |
| 899 | if (!supported) { |
| 900 | ALOGE( |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 901 | "requested device extension '%s' not supported by loader, " |
| 902 | "driver, or any active layers", |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 903 | name); |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 904 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 905 | } |
| 906 | } |
| 907 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 908 | driver_create_info.enabledExtensionCount = num_driver_extensions; |
| 909 | driver_create_info.ppEnabledExtensionNames = driver_extensions; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 910 | VkDevice drv_device; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 911 | VkResult result = instance.drv.dispatch.CreateDevice( |
| 912 | gpu, &driver_create_info, allocator, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 913 | if (result != VK_SUCCESS) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 914 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 917 | hwvulkan_dispatch_t* drv_dispatch = |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 918 | reinterpret_cast<hwvulkan_dispatch_t*>(drv_device); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 919 | if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 920 | ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, |
| 921 | drv_dispatch->magic); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 922 | PFN_vkDestroyDevice destroy_device = |
| 923 | reinterpret_cast<PFN_vkDestroyDevice>( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 924 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 925 | "vkDestroyDevice")); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 926 | destroy_device(drv_device, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 927 | return VK_ERROR_INITIALIZATION_FAILED; |
| 928 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 929 | |
| 930 | // Set dispatch table for newly created Device |
| 931 | // CreateDevice_Top will fill in the details |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 932 | drv_dispatch->vtbl = &device->dispatch; |
| 933 | device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 934 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 935 | "vkGetDeviceProcAddr")); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 936 | *device_out = drv_device; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 937 | return VK_SUCCESS; |
| 938 | } |
| 939 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 940 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 941 | const VkAllocationCallbacks* allocator) { |
| 942 | Instance& instance = GetDispatchParent(vkinstance); |
| 943 | |
| 944 | // These checks allow us to call DestroyInstance_Bottom from any error |
| 945 | // path in CreateInstance_Bottom, before the driver instance is fully |
| 946 | // initialized. |
| 947 | if (instance.drv.instance != VK_NULL_HANDLE && |
| 948 | instance.drv.dispatch.DestroyInstance) { |
| 949 | instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator); |
| 950 | } |
| 951 | if (instance.message) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 952 | PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback; |
| 953 | destroy_debug_report_callback = |
| 954 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 955 | vkGetInstanceProcAddr(vkinstance, |
| 956 | "vkDestroyDebugReportCallbackEXT")); |
| 957 | destroy_debug_report_callback(vkinstance, instance.message, allocator); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 958 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 961 | PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice, |
| 962 | const char* name) { |
| 963 | if (strcmp(name, "vkCreateDevice") == 0) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 964 | return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 965 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 966 | |
| 967 | // VK_ANDROID_native_buffer should be hidden from applications and layers. |
| 968 | // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr. |
| 969 | PFN_vkVoidFunction pfn; |
| 970 | if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 || |
| 971 | strcmp(name, "vkAcquireImageANDROID") == 0 || |
| 972 | strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) { |
| 973 | return nullptr; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 974 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 975 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 976 | return pfn; |
| 977 | return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 980 | // ----------------------------------------------------------------------------- |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 981 | // Loader top functions. These are called directly from the loader entry |
| 982 | // points or from the application (via vkGetInstanceProcAddr) without going |
| 983 | // through a dispatch table. |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 984 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 985 | VkResult EnumerateInstanceExtensionProperties_Top( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 986 | const char* layer_name, |
| 987 | uint32_t* properties_count, |
| 988 | VkExtensionProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 989 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 990 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 991 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 992 | const VkExtensionProperties* extensions = nullptr; |
| 993 | uint32_t num_extensions = 0; |
| 994 | if (layer_name) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 995 | GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 996 | } else { |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 997 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 998 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 999 | available[num_extensions++] = VkExtensionProperties{ |
| 1000 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 1001 | available[num_extensions++] = |
| 1002 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 1003 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 1004 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 1005 | available[num_extensions++] = |
| 1006 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 1007 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 1008 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1009 | // TODO(jessehall): We need to also enumerate extensions supported by |
| 1010 | // implicitly-enabled layers. Currently we don't have that list of |
| 1011 | // layers until instance creation. |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 1012 | extensions = available; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1013 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1014 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1015 | if (!properties || *properties_count > num_extensions) |
| 1016 | *properties_count = num_extensions; |
| 1017 | if (properties) |
| 1018 | std::copy(extensions, extensions + *properties_count, properties); |
| 1019 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1022 | VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count, |
| 1023 | VkLayerProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1024 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1025 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1026 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1027 | uint32_t layer_count = |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 1028 | EnumerateInstanceLayers(properties ? *properties_count : 0, properties); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1029 | if (!properties || *properties_count > layer_count) |
| 1030 | *properties_count = layer_count; |
| 1031 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Courtney Goeltzenleuchter | 26d394b | 2016-02-07 10:32:27 -0700 | [diff] [blame^] | 1034 | VKAPI_ATTR |
| 1035 | VkResult EnumerateDeviceExtensionProperties_Top( |
| 1036 | VkPhysicalDevice gpu, |
| 1037 | const char* layer_name, |
| 1038 | uint32_t* properties_count, |
| 1039 | VkExtensionProperties* properties) { |
| 1040 | const VkExtensionProperties* extensions = nullptr; |
| 1041 | uint32_t num_extensions = 0; |
| 1042 | |
| 1043 | ALOGV("EnumerateDeviceExtensionProperties_Top:"); |
| 1044 | if (layer_name) { |
| 1045 | ALOGV(" layer %s", layer_name); |
| 1046 | GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions); |
| 1047 | } else { |
| 1048 | ALOGV(" no layer"); |
| 1049 | Instance& instance = GetDispatchParent(gpu); |
| 1050 | size_t gpu_idx = 0; |
| 1051 | while (instance.physical_devices[gpu_idx] != gpu) |
| 1052 | gpu_idx++; |
| 1053 | const DeviceExtensionSet driver_extensions = |
| 1054 | instance.physical_device_driver_extensions[gpu_idx]; |
| 1055 | |
| 1056 | // We only support VK_KHR_swapchain if the GPU supports |
| 1057 | // VK_ANDROID_native_buffer |
| 1058 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 1059 | alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties))); |
| 1060 | if (driver_extensions[kANDROID_native_buffer]) { |
| 1061 | available[num_extensions++] = VkExtensionProperties{ |
| 1062 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION}; |
| 1063 | } |
| 1064 | |
| 1065 | // TODO(jessehall): We need to also enumerate extensions supported by |
| 1066 | // implicitly-enabled layers. Currently we don't have that list of |
| 1067 | // layers until instance creation. |
| 1068 | extensions = available; |
| 1069 | } |
| 1070 | |
| 1071 | ALOGV(" num: %d, extensions: %p", num_extensions, extensions); |
| 1072 | if (!properties || *properties_count > num_extensions) |
| 1073 | *properties_count = num_extensions; |
| 1074 | if (properties) |
| 1075 | std::copy(extensions, extensions + *properties_count, properties); |
| 1076 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
| 1077 | } |
| 1078 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1079 | VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info, |
| 1080 | const VkAllocationCallbacks* allocator, |
| 1081 | VkInstance* instance_out) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1082 | VkResult result; |
| 1083 | |
| 1084 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1085 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1086 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1087 | if (!allocator) |
| 1088 | allocator = &kDefaultAllocCallbacks; |
| 1089 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1090 | VkInstanceCreateInfo local_create_info = *create_info; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1091 | create_info = &local_create_info; |
| 1092 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1093 | void* instance_mem = allocator->pfnAllocation( |
| 1094 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 1095 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1096 | if (!instance_mem) |
| 1097 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1098 | Instance* instance = new (instance_mem) Instance(allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1099 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 1100 | result = ActivateAllLayers(create_info, instance, instance); |
| 1101 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1102 | DestroyInstance_Bottom(instance->handle, allocator); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1103 | TeardownInstance(instance->handle, allocator); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 1104 | return result; |
| 1105 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1106 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1107 | uint32_t activated_layers = 0; |
| 1108 | VkLayerInstanceCreateInfo chain_info; |
| 1109 | VkLayerInstanceLink* layer_instance_link_info = nullptr; |
| 1110 | PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom; |
| 1111 | VkInstance local_instance = nullptr; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1112 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1113 | if (instance->active_layers.size() > 0) { |
| 1114 | chain_info.u.pLayerInfo = nullptr; |
| 1115 | chain_info.pNext = create_info->pNext; |
| 1116 | chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 1117 | chain_info.function = VK_LAYER_FUNCTION_LINK; |
| 1118 | local_create_info.pNext = &chain_info; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1119 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1120 | layer_instance_link_info = static_cast<VkLayerInstanceLink*>(alloca( |
| 1121 | sizeof(VkLayerInstanceLink) * instance->active_layers.size())); |
| 1122 | if (!layer_instance_link_info) { |
| 1123 | ALOGE("Failed to alloc Instance objects for layers"); |
| 1124 | DestroyInstance_Bottom(instance->handle, allocator); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1125 | TeardownInstance(instance->handle, allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1126 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1127 | } |
| 1128 | |
| 1129 | /* Create instance chain of enabled layers */ |
| 1130 | for (auto rit = instance->active_layers.rbegin(); |
| 1131 | rit != instance->active_layers.rend(); ++rit) { |
| 1132 | LayerRef& layer = *rit; |
| 1133 | layer_instance_link_info[activated_layers].pNext = |
| 1134 | chain_info.u.pLayerInfo; |
| 1135 | layer_instance_link_info[activated_layers] |
| 1136 | .pfnNextGetInstanceProcAddr = next_gipa; |
| 1137 | chain_info.u.pLayerInfo = |
| 1138 | &layer_instance_link_info[activated_layers]; |
| 1139 | next_gipa = layer.GetGetInstanceProcAddr(); |
| 1140 | |
| 1141 | ALOGV("Insert instance layer %s (v%u)", layer.GetName(), |
| 1142 | layer.GetSpecVersion()); |
| 1143 | |
| 1144 | activated_layers++; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1148 | PFN_vkCreateInstance create_instance = |
| 1149 | reinterpret_cast<PFN_vkCreateInstance>( |
| 1150 | next_gipa(VK_NULL_HANDLE, "vkCreateInstance")); |
| 1151 | if (!create_instance) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1152 | DestroyInstance_Bottom(instance->handle, allocator); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1153 | TeardownInstance(instance->handle, allocator); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1154 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1155 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1156 | VkLayerInstanceCreateInfo instance_create_info; |
| 1157 | |
| 1158 | instance_create_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; |
| 1159 | instance_create_info.function = VK_LAYER_FUNCTION_INSTANCE; |
| 1160 | |
| 1161 | instance_create_info.u.instanceInfo.instance_info = instance; |
| 1162 | instance_create_info.u.instanceInfo.pfnNextGetInstanceProcAddr = next_gipa; |
| 1163 | |
| 1164 | instance_create_info.pNext = local_create_info.pNext; |
| 1165 | local_create_info.pNext = &instance_create_info; |
| 1166 | |
| 1167 | result = create_instance(&local_create_info, allocator, &local_instance); |
| 1168 | |
| 1169 | if (result != VK_SUCCESS) { |
| 1170 | DestroyInstance_Bottom(instance->handle, allocator); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1171 | TeardownInstance(instance->handle, allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1172 | return result; |
| 1173 | } |
| 1174 | |
| 1175 | const InstanceDispatchTable& instance_dispatch = |
| 1176 | GetDispatchTable(local_instance); |
| 1177 | if (!LoadInstanceDispatchTable( |
| 1178 | local_instance, next_gipa, |
| 1179 | const_cast<InstanceDispatchTable&>(instance_dispatch))) { |
| 1180 | ALOGV("Failed to initialize instance dispatch table"); |
| 1181 | PFN_vkDestroyInstance destroy_instance = |
| 1182 | reinterpret_cast<PFN_vkDestroyInstance>( |
| 1183 | next_gipa(VK_NULL_HANDLE, "vkDestroyInstance")); |
| 1184 | if (!destroy_instance) { |
| 1185 | ALOGD("Loader unable to find DestroyInstance"); |
| 1186 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1187 | } |
| 1188 | destroy_instance(local_instance, allocator); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1189 | DestroyInstance_Bottom(instance->handle, allocator); |
| 1190 | TeardownInstance(instance->handle, allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1191 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1192 | } |
| 1193 | *instance_out = local_instance; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1194 | |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1195 | // Force enable callback extension if required |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 1196 | bool enable_callback = false; |
| 1197 | bool enable_logging = false; |
| 1198 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 1199 | enable_callback = |
| 1200 | property_get_bool("debug.vulkan.enable_callback", false); |
| 1201 | enable_logging = enable_callback; |
| 1202 | if (enable_callback) { |
| 1203 | enable_callback = AddExtensionToCreateInfo( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1204 | local_create_info, "VK_EXT_debug_report", instance->alloc); |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 1205 | } |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1208 | if (enable_logging) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1209 | const VkDebugReportCallbackCreateInfoEXT callback_create_info = { |
| 1210 | .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT, |
| 1211 | .flags = |
| 1212 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT, |
| 1213 | .pfnCallback = LogDebugMessageCallback, |
| 1214 | }; |
| 1215 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = |
| 1216 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 1217 | GetInstanceProcAddr_Top(instance->handle, |
| 1218 | "vkCreateDebugReportCallbackEXT")); |
| 1219 | create_debug_report_callback(instance->handle, &callback_create_info, |
| 1220 | allocator, &instance->message); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1221 | } |
| 1222 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1223 | return result; |
| 1224 | } |
| 1225 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1226 | PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance, |
| 1227 | const char* name) { |
| 1228 | // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands |
| 1229 | if (!vkinstance) |
| 1230 | return GetLoaderGlobalProcAddr(name); |
| 1231 | |
| 1232 | const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance); |
| 1233 | PFN_vkVoidFunction pfn; |
| 1234 | // Always go through the loader-top function if there is one. |
| 1235 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1236 | return pfn; |
| 1237 | // Otherwise, look up the handler in the instance dispatch table |
| 1238 | if ((pfn = GetDispatchProcAddr(dispatch, name))) |
| 1239 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1240 | // Anything not handled already must be a device-dispatched function |
| 1241 | // without a loader-top. We must return a function that will dispatch based |
| 1242 | // on the dispatchable object parameter -- which is exactly what the |
| 1243 | // exported functions do. So just return them here. |
| 1244 | return GetLoaderExportProcAddr(name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1247 | void DestroyInstance_Top(VkInstance vkinstance, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1248 | const VkAllocationCallbacks* allocator) { |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1249 | if (!vkinstance) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1250 | return; |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 1251 | GetDispatchTable(vkinstance).DestroyInstance(vkinstance, allocator); |
| 1252 | |
| 1253 | TeardownInstance(vkinstance, allocator); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1256 | VKAPI_ATTR |
Courtney Goeltzenleuchter | 1cc0d37 | 2016-02-05 17:10:59 -0700 | [diff] [blame] | 1257 | VkResult EnumerateDeviceLayerProperties_Top(VkPhysicalDevice /*pdev*/, |
| 1258 | uint32_t* properties_count, |
| 1259 | VkLayerProperties* properties) { |
| 1260 | uint32_t layer_count = |
| 1261 | EnumerateDeviceLayers(properties ? *properties_count : 0, properties); |
| 1262 | if (!properties || *properties_count > layer_count) |
| 1263 | *properties_count = layer_count; |
| 1264 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
| 1265 | } |
| 1266 | |
| 1267 | VKAPI_ATTR |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 1268 | VkResult CreateDevice_Top(VkPhysicalDevice gpu, |
| 1269 | const VkDeviceCreateInfo* create_info, |
| 1270 | const VkAllocationCallbacks* allocator, |
| 1271 | VkDevice* device_out) { |
| 1272 | Instance& instance = GetDispatchParent(gpu); |
| 1273 | VkResult result; |
| 1274 | |
| 1275 | // FIXME(jessehall): We don't have good conventions or infrastructure yet to |
| 1276 | // do better than just using the instance allocator and scope for |
| 1277 | // everything. See b/26732122. |
| 1278 | if (true /*!allocator*/) |
| 1279 | allocator = instance.alloc; |
| 1280 | |
| 1281 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device), |
| 1282 | alignof(Device), |
| 1283 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 1284 | if (!mem) |
| 1285 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1286 | Device* device = new (mem) Device(&instance); |
| 1287 | |
| 1288 | result = ActivateAllLayers(create_info, &instance, device); |
| 1289 | if (result != VK_SUCCESS) { |
| 1290 | DestroyDevice(device); |
| 1291 | return result; |
| 1292 | } |
| 1293 | |
| 1294 | size_t gpu_idx = 0; |
| 1295 | while (instance.physical_devices[gpu_idx] != gpu) |
| 1296 | gpu_idx++; |
| 1297 | |
| 1298 | uint32_t activated_layers = 0; |
| 1299 | VkLayerDeviceCreateInfo chain_info; |
| 1300 | VkLayerDeviceLink* layer_device_link_info = nullptr; |
| 1301 | PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom; |
| 1302 | PFN_vkGetDeviceProcAddr next_gdpa = GetDeviceProcAddr_Bottom; |
| 1303 | VkDeviceCreateInfo local_create_info = *create_info; |
| 1304 | VkDevice local_device = nullptr; |
| 1305 | |
| 1306 | if (device->active_layers.size() > 0) { |
| 1307 | chain_info.u.pLayerInfo = nullptr; |
| 1308 | chain_info.pNext = local_create_info.pNext; |
| 1309 | chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 1310 | chain_info.function = VK_LAYER_FUNCTION_LINK; |
| 1311 | local_create_info.pNext = &chain_info; |
| 1312 | |
| 1313 | layer_device_link_info = static_cast<VkLayerDeviceLink*>( |
| 1314 | alloca(sizeof(VkLayerDeviceLink) * device->active_layers.size())); |
| 1315 | if (!layer_device_link_info) { |
| 1316 | ALOGE("Failed to alloc Device objects for layers"); |
| 1317 | DestroyDevice(device); |
| 1318 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1319 | } |
| 1320 | |
| 1321 | /* Create device chain of enabled layers */ |
| 1322 | for (auto rit = device->active_layers.rbegin(); |
| 1323 | rit != device->active_layers.rend(); ++rit) { |
| 1324 | LayerRef& layer = *rit; |
| 1325 | layer_device_link_info[activated_layers].pNext = |
| 1326 | chain_info.u.pLayerInfo; |
| 1327 | layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr = |
| 1328 | next_gdpa; |
| 1329 | layer_device_link_info[activated_layers] |
| 1330 | .pfnNextGetInstanceProcAddr = next_gipa; |
| 1331 | chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers]; |
| 1332 | |
| 1333 | next_gipa = layer.GetGetInstanceProcAddr(); |
| 1334 | next_gdpa = layer.GetGetDeviceProcAddr(); |
| 1335 | |
| 1336 | ALOGV("Insert device layer %s (v%u)", layer.GetName(), |
| 1337 | layer.GetSpecVersion()); |
| 1338 | |
| 1339 | activated_layers++; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>( |
| 1344 | next_gipa(VK_NULL_HANDLE, "vkCreateDevice")); |
| 1345 | if (!create_device) { |
| 1346 | ALOGE("Unable to find vkCreateDevice for driver"); |
| 1347 | DestroyDevice(device); |
| 1348 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1349 | } |
| 1350 | |
| 1351 | VkLayerDeviceCreateInfo device_create_info; |
| 1352 | |
| 1353 | device_create_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; |
| 1354 | device_create_info.function = VK_LAYER_FUNCTION_DEVICE; |
| 1355 | |
| 1356 | device_create_info.u.deviceInfo.device_info = device; |
| 1357 | device_create_info.u.deviceInfo.pfnNextGetInstanceProcAddr = next_gipa; |
| 1358 | |
| 1359 | device_create_info.pNext = local_create_info.pNext; |
| 1360 | local_create_info.pNext = &device_create_info; |
| 1361 | |
| 1362 | result = create_device(gpu, &local_create_info, allocator, &local_device); |
| 1363 | |
| 1364 | if (result != VK_SUCCESS) { |
| 1365 | DestroyDevice(device); |
| 1366 | return result; |
| 1367 | } |
| 1368 | |
| 1369 | // Set dispatch table for newly created Device |
| 1370 | hwvulkan_dispatch_t* vulkan_dispatch = |
| 1371 | reinterpret_cast<hwvulkan_dispatch_t*>(local_device); |
| 1372 | vulkan_dispatch->vtbl = &device->dispatch; |
| 1373 | |
| 1374 | const DeviceDispatchTable& device_dispatch = GetDispatchTable(local_device); |
| 1375 | if (!LoadDeviceDispatchTable( |
| 1376 | local_device, next_gdpa, |
| 1377 | const_cast<DeviceDispatchTable&>(device_dispatch))) { |
| 1378 | ALOGV("Failed to initialize device dispatch table"); |
| 1379 | PFN_vkDestroyDevice destroy_device = |
| 1380 | reinterpret_cast<PFN_vkDestroyDevice>( |
| 1381 | next_gipa(VK_NULL_HANDLE, "vkDestroyDevice")); |
| 1382 | ALOG_ASSERT(destroy_device != nullptr, |
| 1383 | "Loader unable to find DestroyDevice"); |
| 1384 | destroy_device(local_device, allocator); |
| 1385 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1386 | } |
| 1387 | *device_out = local_device; |
| 1388 | |
| 1389 | return VK_SUCCESS; |
| 1390 | } |
| 1391 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1392 | PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) { |
| 1393 | PFN_vkVoidFunction pfn; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1394 | if (!device) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1395 | return nullptr; |
| 1396 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1397 | return pfn; |
| 1398 | return GetDispatchProcAddr(GetDispatchTable(device), name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1401 | void GetDeviceQueue_Top(VkDevice vkdevice, |
| 1402 | uint32_t family, |
| 1403 | uint32_t index, |
| 1404 | VkQueue* queue_out) { |
| 1405 | const auto& table = GetDispatchTable(vkdevice); |
| 1406 | table.GetDeviceQueue(vkdevice, family, index, queue_out); |
| 1407 | hwvulkan_dispatch_t* queue_dispatch = |
| 1408 | reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out); |
| 1409 | if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC && |
| 1410 | queue_dispatch->vtbl != &table) |
| 1411 | ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, |
| 1412 | queue_dispatch->magic); |
| 1413 | queue_dispatch->vtbl = &table; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1416 | VkResult AllocateCommandBuffers_Top( |
| 1417 | VkDevice vkdevice, |
| 1418 | const VkCommandBufferAllocateInfo* alloc_info, |
| 1419 | VkCommandBuffer* cmdbufs) { |
| 1420 | const auto& table = GetDispatchTable(vkdevice); |
| 1421 | VkResult result = |
| 1422 | table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs); |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1423 | if (result != VK_SUCCESS) |
| 1424 | return result; |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1425 | for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1426 | hwvulkan_dispatch_t* cmdbuf_dispatch = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1427 | reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1428 | ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1429 | "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1430 | cmdbuf_dispatch->magic); |
| 1431 | cmdbuf_dispatch->vtbl = &table; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1432 | } |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1433 | return VK_SUCCESS; |
| 1434 | } |
| 1435 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1436 | void DestroyDevice_Top(VkDevice vkdevice, |
| 1437 | const VkAllocationCallbacks* /*allocator*/) { |
| 1438 | if (!vkdevice) |
| 1439 | return; |
| 1440 | Device& device = GetDispatchParent(vkdevice); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1441 | device.dispatch.DestroyDevice(vkdevice, device.instance->alloc); |
| 1442 | DestroyDevice(&device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1445 | // ----------------------------------------------------------------------------- |
| 1446 | |
| 1447 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 1448 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1451 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
| 1452 | return GetDispatchParent(vkdevice).instance->alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1455 | VkInstance GetDriverInstance(VkInstance instance) { |
| 1456 | return GetDispatchParent(instance).drv.instance; |
| 1457 | } |
| 1458 | |
| 1459 | const DriverDispatchTable& GetDriverDispatch(VkInstance instance) { |
| 1460 | return GetDispatchParent(instance).drv.dispatch; |
| 1461 | } |
| 1462 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1463 | const DriverDispatchTable& GetDriverDispatch(VkDevice device) { |
| 1464 | return GetDispatchParent(device).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1467 | const DriverDispatchTable& GetDriverDispatch(VkQueue queue) { |
| 1468 | return GetDispatchParent(queue).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1471 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 1472 | return GetDispatchParent(instance).debug_report_callbacks; |
| 1473 | } |
| 1474 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1475 | } // namespace vulkan |