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 | 504db7f | 2016-01-14 15:53:57 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 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> |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 28 | #include <sys/prctl.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 29 | // standard C++ headers |
| 30 | #include <algorithm> |
| 31 | #include <mutex> |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 32 | #include <sstream> |
| 33 | #include <string> |
| 34 | #include <unordered_map> |
| 35 | #include <vector> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 36 | // platform/library headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 38 | #include <hardware/hwvulkan.h> |
| 39 | #include <log/log.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 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 59 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 60 | // Standard-library allocator that delegates to VkAllocationCallbacks. |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 61 | // |
| 62 | // TODO(jessehall): This class currently always uses |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 63 | // 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] | 64 | // parameter or a constructor parameter. The former would help catch bugs |
| 65 | // where we use the wrong scope, e.g. adding a command-scope string to an |
| 66 | // 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] | 67 | template <class T> |
| 68 | class CallbackAllocator { |
| 69 | public: |
| 70 | typedef T value_type; |
| 71 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 72 | CallbackAllocator(const VkAllocationCallbacks* alloc_input) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 73 | : alloc(alloc_input) {} |
| 74 | |
| 75 | template <class T2> |
| 76 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 77 | : alloc(other.alloc) {} |
| 78 | |
| 79 | T* allocate(std::size_t n) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 80 | void* mem = |
| 81 | alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T), |
| 82 | VK_SYSTEM_ALLOCATION_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 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 90 | const VkAllocationCallbacks* alloc; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 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>, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 107 | class Pred = std::equal_to<Key>> |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 108 | using UnorderedMap = |
| 109 | std::unordered_map<Key, |
| 110 | T, |
| 111 | Hash, |
| 112 | Pred, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 113 | CallbackAllocator<std::pair<const Key, T>>>; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 114 | |
| 115 | template <class T> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 116 | using Vector = std::vector<T, CallbackAllocator<T>>; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 117 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 118 | typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>> |
| 119 | String; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 120 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 121 | // ---------------------------------------------------------------------------- |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 122 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 123 | VKAPI_ATTR void* DefaultAllocate(void*, |
| 124 | size_t size, |
| 125 | size_t alignment, |
| 126 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 127 | void* ptr = nullptr; |
| 128 | // Vulkan requires 'alignment' to be a power of two, but posix_memalign |
| 129 | // additionally requires that it be at least sizeof(void*). |
| 130 | return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0 |
| 131 | ? ptr |
| 132 | : nullptr; |
| 133 | } |
| 134 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 135 | VKAPI_ATTR void* DefaultReallocate(void*, |
| 136 | void* ptr, |
| 137 | size_t size, |
| 138 | size_t alignment, |
| 139 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 140 | if (size == 0) { |
| 141 | free(ptr); |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | // TODO(jessehall): Right now we never shrink allocations; if the new |
| 146 | // request is smaller than the existing chunk, we just continue using it. |
| 147 | // Right now the loader never reallocs, so this doesn't matter. If that |
| 148 | // changes, or if this code is copied into some other project, this should |
| 149 | // probably have a heuristic to allocate-copy-free when doing so will save |
| 150 | // "enough" space. |
| 151 | size_t old_size = ptr ? malloc_usable_size(ptr) : 0; |
| 152 | if (size <= old_size) |
| 153 | return ptr; |
| 154 | |
| 155 | void* new_ptr = nullptr; |
| 156 | if (posix_memalign(&new_ptr, alignment, size) != 0) |
| 157 | return nullptr; |
| 158 | if (ptr) { |
| 159 | memcpy(new_ptr, ptr, std::min(old_size, size)); |
| 160 | free(ptr); |
| 161 | } |
| 162 | return new_ptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 165 | VKAPI_ATTR void DefaultFree(void*, void* pMem) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 166 | free(pMem); |
| 167 | } |
| 168 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 169 | const VkAllocationCallbacks kDefaultAllocCallbacks = { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 170 | .pUserData = nullptr, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 171 | .pfnAllocation = DefaultAllocate, |
| 172 | .pfnReallocation = DefaultReallocate, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 173 | .pfnFree = DefaultFree, |
| 174 | }; |
| 175 | |
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 | // Global Data and Initialization |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 178 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 179 | hwvulkan_device_t* g_hwdevice = nullptr; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 180 | InstanceExtensionSet g_driver_instance_extensions; |
| 181 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 182 | void LoadVulkanHAL() { |
| 183 | static const hwvulkan_module_t* module; |
| 184 | int result = |
| 185 | hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module)); |
| 186 | if (result != 0) { |
| 187 | ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result); |
| 188 | return; |
| 189 | } |
| 190 | result = module->common.methods->open( |
| 191 | &module->common, HWVULKAN_DEVICE_0, |
| 192 | reinterpret_cast<hw_device_t**>(&g_hwdevice)); |
| 193 | if (result != 0) { |
| 194 | ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result), |
| 195 | result); |
| 196 | module = nullptr; |
| 197 | return; |
| 198 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 199 | |
| 200 | VkResult vkresult; |
| 201 | uint32_t count; |
| 202 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 203 | nullptr, &count, nullptr)) != VK_SUCCESS) { |
| 204 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 205 | vkresult); |
| 206 | g_hwdevice->common.close(&g_hwdevice->common); |
| 207 | g_hwdevice = nullptr; |
| 208 | module = nullptr; |
| 209 | return; |
| 210 | } |
| 211 | VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>( |
| 212 | alloca(count * sizeof(VkExtensionProperties))); |
| 213 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 214 | nullptr, &count, extensions)) != VK_SUCCESS) { |
| 215 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 216 | vkresult); |
| 217 | g_hwdevice->common.close(&g_hwdevice->common); |
| 218 | g_hwdevice = nullptr; |
| 219 | module = nullptr; |
| 220 | return; |
| 221 | } |
| 222 | ALOGV_IF(count > 0, "Driver-supported instance extensions:"); |
| 223 | for (uint32_t i = 0; i < count; i++) { |
| 224 | ALOGV(" %s (v%u)", extensions[i].extensionName, |
| 225 | extensions[i].specVersion); |
| 226 | InstanceExtension id = |
| 227 | InstanceExtensionFromName(extensions[i].extensionName); |
| 228 | if (id != kInstanceExtensionCount) |
| 229 | g_driver_instance_extensions.set(id); |
| 230 | } |
| 231 | // Ignore driver attempts to support loader extensions |
| 232 | g_driver_instance_extensions.reset(kKHR_surface); |
| 233 | g_driver_instance_extensions.reset(kKHR_android_surface); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 236 | bool EnsureInitialized() { |
| 237 | static std::once_flag once_flag; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 238 | std::call_once(once_flag, []() { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 239 | LoadVulkanHAL(); |
| 240 | DiscoverLayers(); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 241 | }); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 242 | return g_hwdevice != nullptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 245 | // ----------------------------------------------------------------------------- |
| 246 | |
| 247 | struct Instance { |
| 248 | Instance(const VkAllocationCallbacks* alloc_callbacks) |
| 249 | : dispatch_ptr(&dispatch), |
| 250 | handle(reinterpret_cast<VkInstance>(&dispatch_ptr)), |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 251 | alloc(alloc_callbacks), |
| 252 | num_physical_devices(0), |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 253 | active_layers(CallbackAllocator<LayerRef>(alloc)), |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 254 | message(VK_NULL_HANDLE) { |
| 255 | memset(&dispatch, 0, sizeof(dispatch)); |
| 256 | memset(physical_devices, 0, sizeof(physical_devices)); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 257 | drv.instance = VK_NULL_HANDLE; |
| 258 | memset(&drv.dispatch, 0, sizeof(drv.dispatch)); |
| 259 | drv.num_physical_devices = 0; |
| 260 | } |
| 261 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 262 | ~Instance() {} |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 263 | |
| 264 | const InstanceDispatchTable* dispatch_ptr; |
| 265 | const VkInstance handle; |
| 266 | InstanceDispatchTable dispatch; |
| 267 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 268 | const VkAllocationCallbacks* alloc; |
| 269 | uint32_t num_physical_devices; |
| 270 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
| 271 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 272 | Vector<LayerRef> active_layers; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 273 | VkDebugReportCallbackEXT message; |
| 274 | DebugReportCallbackList debug_report_callbacks; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 275 | |
| 276 | struct { |
| 277 | VkInstance instance; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 278 | InstanceExtensionSet supported_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 279 | DriverDispatchTable dispatch; |
| 280 | uint32_t num_physical_devices; |
| 281 | } drv; // may eventually be an array |
| 282 | }; |
| 283 | |
| 284 | struct Device { |
| 285 | Device(Instance* instance_) |
| 286 | : instance(instance_), |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 287 | active_layers(CallbackAllocator<LayerRef>(instance->alloc)) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 288 | memset(&dispatch, 0, sizeof(dispatch)); |
| 289 | } |
| 290 | DeviceDispatchTable dispatch; |
| 291 | Instance* instance; |
| 292 | PFN_vkGetDeviceProcAddr get_device_proc_addr; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 293 | Vector<LayerRef> active_layers; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | template <typename THandle> |
| 297 | struct HandleTraits {}; |
| 298 | template <> |
| 299 | struct HandleTraits<VkInstance> { |
| 300 | typedef Instance LoaderObjectType; |
| 301 | }; |
| 302 | template <> |
| 303 | struct HandleTraits<VkPhysicalDevice> { |
| 304 | typedef Instance LoaderObjectType; |
| 305 | }; |
| 306 | template <> |
| 307 | struct HandleTraits<VkDevice> { |
| 308 | typedef Device LoaderObjectType; |
| 309 | }; |
| 310 | template <> |
| 311 | struct HandleTraits<VkQueue> { |
| 312 | typedef Device LoaderObjectType; |
| 313 | }; |
| 314 | template <> |
| 315 | struct HandleTraits<VkCommandBuffer> { |
| 316 | typedef Device LoaderObjectType; |
| 317 | }; |
| 318 | |
| 319 | template <typename THandle> |
| 320 | typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent( |
| 321 | THandle handle) { |
| 322 | // TODO(jessehall): Make Instance and Device POD types (by removing the |
| 323 | // non-default constructors), so that offsetof is actually legal to use. |
| 324 | // The specific case we're using here is safe in gcc/clang (and probably |
| 325 | // most other C++ compilers), but isn't guaranteed by C++. |
| 326 | typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType; |
| 327 | #pragma clang diagnostic push |
| 328 | #pragma clang diagnostic ignored "-Winvalid-offsetof" |
| 329 | const size_t kDispatchOffset = offsetof(ObjectType, dispatch); |
| 330 | #pragma clang diagnostic pop |
| 331 | |
| 332 | const auto& dispatch = GetDispatchTable(handle); |
| 333 | uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch); |
| 334 | uintptr_t object_addr = dispatch_addr - kDispatchOffset; |
| 335 | return *reinterpret_cast<ObjectType*>(object_addr); |
| 336 | } |
| 337 | |
| 338 | // ----------------------------------------------------------------------------- |
| 339 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 340 | void DestroyDevice(Device* device) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 341 | const VkAllocationCallbacks* alloc = device->instance->alloc; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 342 | device->~Device(); |
| 343 | alloc->pfnFree(alloc->pUserData, device); |
| 344 | } |
| 345 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 346 | template <class TObject> |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame^] | 347 | LayerRef GetLayerRef(const char* name); |
| 348 | template <> |
| 349 | LayerRef GetLayerRef<Instance>(const char* name) { |
| 350 | return GetInstanceLayerRef(name); |
| 351 | } |
| 352 | template <> |
| 353 | LayerRef GetLayerRef<Device>(const char* name) { |
| 354 | return GetDeviceLayerRef(name); |
| 355 | } |
| 356 | |
| 357 | template <class TObject> |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 358 | bool ActivateLayer(TObject* object, const char* name) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame^] | 359 | LayerRef layer(GetLayerRef<TObject>(name)); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 360 | if (!layer) |
| 361 | return false; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 362 | if (std::find(object->active_layers.begin(), object->active_layers.end(), |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 363 | layer) == object->active_layers.end()) |
| 364 | object->active_layers.push_back(std::move(layer)); |
| 365 | ALOGV("activated layer '%s'", name); |
| 366 | return true; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 367 | } |
| 368 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 369 | struct InstanceNamesPair { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 370 | Instance* instance; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 371 | Vector<String>* layer_names; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 372 | }; |
| 373 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 374 | void SetLayerNamesFromProperty(const char* name, |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 375 | const char* value, |
| 376 | void* data) { |
| 377 | const char prefix[] = "debug.vulkan.layer."; |
| 378 | const size_t prefixlen = sizeof(prefix) - 1; |
| 379 | if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0) |
| 380 | return; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 381 | const char* number_str = name + prefixlen; |
| 382 | long layer_number = strtol(number_str, nullptr, 10); |
| 383 | if (layer_number <= 0 || layer_number == LONG_MAX) { |
| 384 | ALOGW("Cannot use a layer at number %ld from string %s", layer_number, |
| 385 | number_str); |
| 386 | return; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 387 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 388 | auto instance_names_pair = static_cast<InstanceNamesPair*>(data); |
| 389 | Vector<String>* layer_names = instance_names_pair->layer_names; |
| 390 | Instance* instance = instance_names_pair->instance; |
| 391 | size_t layer_size = static_cast<size_t>(layer_number); |
| 392 | if (layer_size > layer_names->size()) { |
| 393 | layer_names->resize(layer_size, |
| 394 | String(CallbackAllocator<char>(instance->alloc))); |
| 395 | } |
| 396 | (*layer_names)[layer_size - 1] = value; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | template <class TInfo, class TObject> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 400 | VkResult ActivateAllLayers(TInfo create_info, |
| 401 | Instance* instance, |
| 402 | TObject* object) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 403 | ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO || |
| 404 | create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 405 | "Cannot activate layers for unknown object %p", object); |
| 406 | CallbackAllocator<char> string_allocator(instance->alloc); |
| 407 | // Load system layers |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 408 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 409 | char layer_prop[PROPERTY_VALUE_MAX]; |
| 410 | property_get("debug.vulkan.layers", layer_prop, ""); |
| 411 | String layer_name(string_allocator); |
| 412 | String layer_prop_str(layer_prop, string_allocator); |
| 413 | size_t end, start = 0; |
| 414 | while ((end = layer_prop_str.find(':', start)) != std::string::npos) { |
| 415 | layer_name = layer_prop_str.substr(start, end - start); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 416 | ActivateLayer(object, layer_name.c_str()); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 417 | start = end + 1; |
| 418 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 419 | Vector<String> layer_names(CallbackAllocator<String>(instance->alloc)); |
| 420 | InstanceNamesPair instance_names_pair = {.instance = instance, |
| 421 | .layer_names = &layer_names}; |
| 422 | property_list(SetLayerNamesFromProperty, |
| 423 | static_cast<void*>(&instance_names_pair)); |
| 424 | for (auto layer_name_element : layer_names) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 425 | ActivateLayer(object, layer_name_element.c_str()); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 426 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 427 | } |
| 428 | // Load app layers |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 429 | for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 430 | if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) { |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 431 | ALOGE("requested %s layer '%s' not present", |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 432 | create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO |
| 433 | ? "instance" |
| 434 | : "device", |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 435 | create_info->ppEnabledLayerNames[i]); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 436 | return VK_ERROR_LAYER_NOT_PRESENT; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 437 | } |
| 438 | } |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 439 | return VK_SUCCESS; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | template <class TCreateInfo> |
| 443 | bool AddExtensionToCreateInfo(TCreateInfo& local_create_info, |
| 444 | const char* extension_name, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 445 | const VkAllocationCallbacks* alloc) { |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 446 | for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 447 | if (!strcmp(extension_name, |
| 448 | local_create_info.ppEnabledExtensionNames[i])) { |
| 449 | return false; |
| 450 | } |
| 451 | } |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 452 | uint32_t extension_count = local_create_info.enabledExtensionCount; |
| 453 | local_create_info.enabledExtensionCount++; |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 454 | void* mem = alloc->pfnAllocation( |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 455 | alloc->pUserData, |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 456 | local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*), |
| 457 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 458 | if (mem) { |
| 459 | const char** enabled_extensions = static_cast<const char**>(mem); |
| 460 | for (uint32_t i = 0; i < extension_count; ++i) { |
| 461 | enabled_extensions[i] = |
| 462 | local_create_info.ppEnabledExtensionNames[i]; |
| 463 | } |
| 464 | enabled_extensions[extension_count] = extension_name; |
| 465 | local_create_info.ppEnabledExtensionNames = enabled_extensions; |
| 466 | } else { |
| 467 | ALOGW("%s extension cannot be enabled: memory allocation failed", |
| 468 | extension_name); |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 469 | local_create_info.enabledExtensionCount--; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | template <class T> |
| 476 | void FreeAllocatedCreateInfo(T& local_create_info, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 477 | const VkAllocationCallbacks* alloc) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 478 | alloc->pfnFree( |
| 479 | alloc->pUserData, |
| 480 | const_cast<char**>(local_create_info.ppEnabledExtensionNames)); |
| 481 | } |
| 482 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 483 | VKAPI_ATTR |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 484 | VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags, |
| 485 | VkDebugReportObjectTypeEXT /*objectType*/, |
| 486 | uint64_t /*object*/, |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 487 | size_t /*location*/, |
| 488 | int32_t message_code, |
| 489 | const char* layer_prefix, |
| 490 | const char* message, |
| 491 | void* /*user_data*/) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 492 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 493 | ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message); |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 494 | } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 495 | ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 496 | } |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame] | 497 | return false; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 498 | } |
| 499 | |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 500 | VkResult Noop() { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 501 | return VK_SUCCESS; |
| 502 | } |
| 503 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 504 | } // anonymous namespace |
| 505 | |
| 506 | namespace vulkan { |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 507 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 508 | // ----------------------------------------------------------------------------- |
| 509 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 510 | // chain. |
| 511 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 512 | VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info, |
| 513 | const VkAllocationCallbacks* allocator, |
| 514 | VkInstance* vkinstance) { |
| 515 | Instance& instance = GetDispatchParent(*vkinstance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 516 | VkResult result; |
| 517 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 518 | VkInstanceCreateInfo driver_create_info = *create_info; |
| 519 | driver_create_info.enabledLayerCount = 0; |
| 520 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 521 | |
| 522 | InstanceExtensionSet enabled_extensions; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 523 | driver_create_info.enabledExtensionCount = 0; |
| 524 | driver_create_info.ppEnabledExtensionNames = nullptr; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 525 | size_t max_names = |
| 526 | std::min(static_cast<size_t>(create_info->enabledExtensionCount), |
| 527 | g_driver_instance_extensions.count()); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 528 | if (max_names > 0) { |
| 529 | const char** names = |
| 530 | static_cast<const char**>(alloca(max_names * sizeof(char*))); |
| 531 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 532 | InstanceExtension id = InstanceExtensionFromName( |
| 533 | create_info->ppEnabledExtensionNames[i]); |
| 534 | if (id != kInstanceExtensionCount && |
| 535 | g_driver_instance_extensions[id]) { |
| 536 | names[driver_create_info.enabledExtensionCount++] = |
| 537 | create_info->ppEnabledExtensionNames[i]; |
| 538 | enabled_extensions.set(id); |
| 539 | } |
| 540 | } |
| 541 | driver_create_info.ppEnabledExtensionNames = names; |
| 542 | } |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 543 | |
| 544 | result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 545 | &instance.drv.instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 546 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 547 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 548 | return result; |
| 549 | } |
| 550 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 551 | if (!LoadDriverDispatchTable(instance.drv.instance, |
| 552 | g_hwdevice->GetInstanceProcAddr, |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 553 | enabled_extensions, instance.drv.dispatch)) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 554 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 555 | return VK_ERROR_INITIALIZATION_FAILED; |
| 556 | } |
| 557 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 558 | hwvulkan_dispatch_t* drv_dispatch = |
| 559 | reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance); |
| 560 | if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) { |
| 561 | // Skip setting drv_dispatch->vtbl, since we never call through it; |
| 562 | // we go through instance.drv.dispatch instead. |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 563 | } else { |
| 564 | ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 565 | drv_dispatch->magic); |
| 566 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 567 | return VK_ERROR_INITIALIZATION_FAILED; |
| 568 | } |
| 569 | |
| 570 | uint32_t num_physical_devices = 0; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 571 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
| 572 | instance.drv.instance, &num_physical_devices, nullptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 573 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 574 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 575 | return VK_ERROR_INITIALIZATION_FAILED; |
| 576 | } |
| 577 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 578 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
| 579 | instance.drv.instance, &num_physical_devices, |
| 580 | instance.physical_devices); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 581 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 582 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 583 | return VK_ERROR_INITIALIZATION_FAILED; |
| 584 | } |
| 585 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 586 | hwvulkan_dispatch_t* pdev_dispatch = |
| 587 | reinterpret_cast<hwvulkan_dispatch_t*>( |
| 588 | instance.physical_devices[i]); |
| 589 | if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 590 | ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 591 | pdev_dispatch->magic); |
| 592 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 593 | return VK_ERROR_INITIALIZATION_FAILED; |
| 594 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 595 | pdev_dispatch->vtbl = instance.dispatch_ptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 596 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 597 | instance.drv.num_physical_devices = num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 598 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 599 | instance.num_physical_devices = instance.drv.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 600 | return VK_SUCCESS; |
| 601 | } |
| 602 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 603 | PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) { |
| 604 | PFN_vkVoidFunction pfn; |
| 605 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 606 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 607 | return nullptr; |
| 608 | } |
| 609 | |
| 610 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 611 | uint32_t* pdev_count, |
| 612 | VkPhysicalDevice* pdevs) { |
| 613 | Instance& instance = GetDispatchParent(vkinstance); |
| 614 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 615 | if (pdevs) { |
| 616 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 617 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 618 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 619 | } |
| 620 | *pdev_count = count; |
| 621 | return VK_SUCCESS; |
| 622 | } |
| 623 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 624 | void GetPhysicalDeviceProperties_Bottom( |
| 625 | VkPhysicalDevice pdev, |
| 626 | VkPhysicalDeviceProperties* properties) { |
| 627 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties( |
| 628 | pdev, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 631 | void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev, |
| 632 | VkPhysicalDeviceFeatures* features) { |
| 633 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev, |
| 634 | features); |
| 635 | } |
| 636 | |
| 637 | void GetPhysicalDeviceMemoryProperties_Bottom( |
| 638 | VkPhysicalDevice pdev, |
| 639 | VkPhysicalDeviceMemoryProperties* properties) { |
| 640 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties( |
| 641 | pdev, properties); |
| 642 | } |
| 643 | |
| 644 | void GetPhysicalDeviceQueueFamilyProperties_Bottom( |
| 645 | VkPhysicalDevice pdev, |
| 646 | uint32_t* pCount, |
| 647 | VkQueueFamilyProperties* properties) { |
| 648 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties( |
| 649 | pdev, pCount, properties); |
| 650 | } |
| 651 | |
| 652 | void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev, |
| 653 | VkFormat format, |
| 654 | VkFormatProperties* properties) { |
| 655 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 656 | pdev, format, properties); |
| 657 | } |
| 658 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 659 | VkResult GetPhysicalDeviceImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 660 | VkPhysicalDevice pdev, |
| 661 | VkFormat format, |
| 662 | VkImageType type, |
| 663 | VkImageTiling tiling, |
| 664 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 665 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 666 | VkImageFormatProperties* properties) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 667 | return GetDispatchParent(pdev) |
| 668 | .drv.dispatch.GetPhysicalDeviceImageFormatProperties( |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 669 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 672 | void GetPhysicalDeviceSparseImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 673 | VkPhysicalDevice pdev, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 674 | VkFormat format, |
| 675 | VkImageType type, |
| 676 | VkSampleCountFlagBits samples, |
| 677 | VkImageUsageFlags usage, |
| 678 | VkImageTiling tiling, |
| 679 | uint32_t* properties_count, |
| 680 | VkSparseImageFormatProperties* properties) { |
| 681 | GetDispatchParent(pdev) |
| 682 | .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties( |
| 683 | pdev, format, type, samples, usage, tiling, properties_count, |
| 684 | properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 687 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 688 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 689 | VkPhysicalDevice /*pdev*/, |
| 690 | const char* /*layer_name*/, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 691 | uint32_t* properties_count, |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 692 | VkExtensionProperties* /*properties*/) { |
| 693 | // TODO(jessehall): Implement me... |
| 694 | *properties_count = 0; |
| 695 | return VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 698 | VKAPI_ATTR |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 699 | VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 700 | uint32_t* properties_count, |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame^] | 701 | VkLayerProperties* properties) { |
| 702 | uint32_t layer_count = |
| 703 | EnumerateDeviceLayers(properties ? *properties_count : 0, properties); |
| 704 | if (!properties || *properties_count > layer_count) |
| 705 | *properties_count = layer_count; |
| 706 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | VKAPI_ATTR |
| 710 | VkResult CreateDevice_Bottom(VkPhysicalDevice pdev, |
| 711 | const VkDeviceCreateInfo* create_info, |
| 712 | const VkAllocationCallbacks* allocator, |
| 713 | VkDevice* device_out) { |
| 714 | Instance& instance = GetDispatchParent(pdev); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 715 | VkResult result; |
| 716 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 717 | if (!allocator) { |
| 718 | if (instance.alloc) |
| 719 | allocator = instance.alloc; |
| 720 | else |
| 721 | allocator = &kDefaultAllocCallbacks; |
| 722 | } |
| 723 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 724 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device), |
| 725 | alignof(Device), |
| 726 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 727 | if (!mem) |
| 728 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 729 | Device* device = new (mem) Device(&instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 730 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 731 | result = ActivateAllLayers(create_info, &instance, device); |
| 732 | if (result != VK_SUCCESS) { |
| 733 | DestroyDevice(device); |
| 734 | return result; |
| 735 | } |
| 736 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 737 | const char* kAndroidNativeBufferExtensionName = "VK_ANDROID_native_buffer"; |
| 738 | VkDeviceCreateInfo driver_create_info = *create_info; |
| 739 | driver_create_info.enabledLayerCount = 0; |
| 740 | driver_create_info.ppEnabledLayerNames = nullptr; |
| 741 | // TODO(jessehall): As soon as we enumerate device extensions supported by |
| 742 | // the driver, we need to filter the requested extension list to those |
| 743 | // supported by the driver here. Also, add the VK_ANDROID_native_buffer |
| 744 | // extension to the list iff the VK_KHR_swapchain extension was requested, |
| 745 | // instead of adding it unconditionally like we do now. |
| 746 | driver_create_info.enabledExtensionCount = 1; |
| 747 | driver_create_info.ppEnabledExtensionNames = &kAndroidNativeBufferExtensionName; |
| 748 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 749 | VkDevice drv_device; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 750 | result = instance.drv.dispatch.CreateDevice(pdev, &driver_create_info, allocator, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 751 | &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 752 | if (result != VK_SUCCESS) { |
| 753 | DestroyDevice(device); |
| 754 | return result; |
| 755 | } |
| 756 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 757 | hwvulkan_dispatch_t* drv_dispatch = |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 758 | reinterpret_cast<hwvulkan_dispatch_t*>(drv_device); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 759 | if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 760 | ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, |
| 761 | drv_dispatch->magic); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 762 | PFN_vkDestroyDevice destroy_device = |
| 763 | reinterpret_cast<PFN_vkDestroyDevice>( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 764 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 765 | "vkDestroyDevice")); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 766 | destroy_device(drv_device, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 767 | DestroyDevice(device); |
| 768 | return VK_ERROR_INITIALIZATION_FAILED; |
| 769 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 770 | drv_dispatch->vtbl = &device->dispatch; |
| 771 | device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 772 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 773 | "vkGetDeviceProcAddr")); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 774 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 775 | void* base_object = static_cast<void*>(drv_device); |
| 776 | void* next_object = base_object; |
| 777 | VkLayerLinkedListElem* next_element; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 778 | PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 779 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 780 | device->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 781 | CallbackAllocator<VkLayerLinkedListElem>(instance.alloc)); |
| 782 | |
| 783 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 784 | size_t idx = i - 1; |
| 785 | next_element = &elem_list[idx]; |
| 786 | next_element->get_proc_addr = |
| 787 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 788 | next_element->base_object = base_object; |
| 789 | next_element->next_element = next_object; |
| 790 | next_object = static_cast<void*>(next_element); |
| 791 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 792 | next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 793 | if (!next_get_proc_addr) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 794 | next_object = next_element->next_element; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 795 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 796 | next_element->get_proc_addr); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 800 | // This is the magic call that initializes all the layer devices and |
| 801 | // allows them to create their device_handle -> device_data mapping. |
| 802 | next_get_proc_addr(static_cast<VkDevice>(next_object), |
| 803 | "vkGetDeviceProcAddr"); |
| 804 | |
| 805 | // We must create all the layer devices *before* retrieving the device |
| 806 | // procaddrs, so that the layers know which extensions are enabled and |
| 807 | // therefore which functions to return procaddrs for. |
| 808 | PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>( |
| 809 | next_get_proc_addr(drv_device, "vkCreateDevice")); |
| 810 | create_device(pdev, create_info, allocator, &drv_device); |
| 811 | |
| 812 | if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object), |
| 813 | next_get_proc_addr, device->dispatch)) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 814 | DestroyDevice(device); |
| 815 | return VK_ERROR_INITIALIZATION_FAILED; |
| 816 | } |
| 817 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 818 | *device_out = drv_device; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 819 | return VK_SUCCESS; |
| 820 | } |
| 821 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 822 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 823 | const VkAllocationCallbacks* allocator) { |
| 824 | Instance& instance = GetDispatchParent(vkinstance); |
| 825 | |
| 826 | // These checks allow us to call DestroyInstance_Bottom from any error |
| 827 | // path in CreateInstance_Bottom, before the driver instance is fully |
| 828 | // initialized. |
| 829 | if (instance.drv.instance != VK_NULL_HANDLE && |
| 830 | instance.drv.dispatch.DestroyInstance) { |
| 831 | instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator); |
| 832 | } |
| 833 | if (instance.message) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 834 | PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback; |
| 835 | destroy_debug_report_callback = |
| 836 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 837 | vkGetInstanceProcAddr(vkinstance, |
| 838 | "vkDestroyDebugReportCallbackEXT")); |
| 839 | destroy_debug_report_callback(vkinstance, instance.message, allocator); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 840 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 841 | instance.active_layers.clear(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 842 | const VkAllocationCallbacks* alloc = instance.alloc; |
| 843 | instance.~Instance(); |
| 844 | alloc->pfnFree(alloc->pUserData, &instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 847 | PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice, |
| 848 | const char* name) { |
| 849 | if (strcmp(name, "vkCreateDevice") == 0) { |
| 850 | // TODO(jessehall): Blegh, having this here is disgusting. The current |
| 851 | // layer init process can't call through the instance dispatch table's |
| 852 | // vkCreateDevice, because that goes through the instance layers rather |
| 853 | // than through the device layers. So we need to be able to get the |
| 854 | // vkCreateDevice pointer through the *device* layer chain. |
| 855 | // |
| 856 | // Because we've already created the driver device before calling |
| 857 | // through the layer vkCreateDevice functions, the loader bottom proc |
| 858 | // is a no-op. |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 859 | return reinterpret_cast<PFN_vkVoidFunction>(Noop); |
| 860 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 861 | |
| 862 | // VK_ANDROID_native_buffer should be hidden from applications and layers. |
| 863 | // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr. |
| 864 | PFN_vkVoidFunction pfn; |
| 865 | if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 || |
| 866 | strcmp(name, "vkAcquireImageANDROID") == 0 || |
| 867 | strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) { |
| 868 | return nullptr; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 869 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 870 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 871 | return pfn; |
| 872 | return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 875 | // ----------------------------------------------------------------------------- |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 876 | // Loader top functions. These are called directly from the loader entry |
| 877 | // points or from the application (via vkGetInstanceProcAddr) without going |
| 878 | // through a dispatch table. |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 879 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 880 | VkResult EnumerateInstanceExtensionProperties_Top( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 881 | const char* layer_name, |
| 882 | uint32_t* properties_count, |
| 883 | VkExtensionProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 884 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 885 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 886 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 887 | const VkExtensionProperties* extensions = nullptr; |
| 888 | uint32_t num_extensions = 0; |
| 889 | if (layer_name) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame^] | 890 | GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 891 | } else { |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 892 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 893 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 894 | available[num_extensions++] = VkExtensionProperties{ |
| 895 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 896 | available[num_extensions++] = |
| 897 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 898 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 899 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 900 | available[num_extensions++] = |
| 901 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 902 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 903 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 904 | // TODO(jessehall): We need to also enumerate extensions supported by |
| 905 | // implicitly-enabled layers. Currently we don't have that list of |
| 906 | // layers until instance creation. |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 907 | extensions = available; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 908 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 909 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 910 | if (!properties || *properties_count > num_extensions) |
| 911 | *properties_count = num_extensions; |
| 912 | if (properties) |
| 913 | std::copy(extensions, extensions + *properties_count, properties); |
| 914 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 917 | VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count, |
| 918 | VkLayerProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 919 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 920 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 921 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 922 | uint32_t layer_count = |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame^] | 923 | EnumerateInstanceLayers(properties ? *properties_count : 0, properties); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 924 | if (!properties || *properties_count > layer_count) |
| 925 | *properties_count = layer_count; |
| 926 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 929 | VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info, |
| 930 | const VkAllocationCallbacks* allocator, |
| 931 | VkInstance* instance_out) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 932 | VkResult result; |
| 933 | |
| 934 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 935 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 936 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 937 | if (!allocator) |
| 938 | allocator = &kDefaultAllocCallbacks; |
| 939 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 940 | VkInstanceCreateInfo local_create_info = *create_info; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 941 | create_info = &local_create_info; |
| 942 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 943 | void* instance_mem = allocator->pfnAllocation( |
| 944 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 945 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 946 | if (!instance_mem) |
| 947 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 948 | Instance* instance = new (instance_mem) Instance(allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 949 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 950 | result = ActivateAllLayers(create_info, instance, instance); |
| 951 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 952 | DestroyInstance_Bottom(instance->handle, allocator); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 953 | return result; |
| 954 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 955 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 956 | void* base_object = static_cast<void*>(instance->handle); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 957 | void* next_object = base_object; |
| 958 | VkLayerLinkedListElem* next_element; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 959 | PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 960 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 961 | instance->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 962 | CallbackAllocator<VkLayerLinkedListElem>(instance->alloc)); |
| 963 | |
| 964 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 965 | size_t idx = i - 1; |
| 966 | next_element = &elem_list[idx]; |
| 967 | next_element->get_proc_addr = |
| 968 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 969 | next_element->base_object = base_object; |
| 970 | next_element->next_element = next_object; |
| 971 | next_object = static_cast<void*>(next_element); |
| 972 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 973 | next_get_proc_addr = |
| 974 | instance->active_layers[idx].GetGetInstanceProcAddr(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 975 | if (!next_get_proc_addr) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 976 | next_object = next_element->next_element; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 977 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 978 | next_element->get_proc_addr); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 982 | // This is the magic call that initializes all the layer instances and |
| 983 | // allows them to create their instance_handle -> instance_data mapping. |
| 984 | next_get_proc_addr(static_cast<VkInstance>(next_object), |
| 985 | "vkGetInstanceProcAddr"); |
| 986 | |
| 987 | if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object), |
| 988 | next_get_proc_addr, instance->dispatch)) { |
| 989 | DestroyInstance_Bottom(instance->handle, allocator); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 990 | return VK_ERROR_INITIALIZATION_FAILED; |
| 991 | } |
| 992 | |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 993 | // Force enable callback extension if required |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 994 | bool enable_callback = false; |
| 995 | bool enable_logging = false; |
| 996 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 997 | enable_callback = |
| 998 | property_get_bool("debug.vulkan.enable_callback", false); |
| 999 | enable_logging = enable_callback; |
| 1000 | if (enable_callback) { |
| 1001 | enable_callback = AddExtensionToCreateInfo( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1002 | local_create_info, "VK_EXT_debug_report", instance->alloc); |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 1003 | } |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1006 | *instance_out = instance->handle; |
| 1007 | PFN_vkCreateInstance create_instance = |
| 1008 | reinterpret_cast<PFN_vkCreateInstance>( |
| 1009 | next_get_proc_addr(instance->handle, "vkCreateInstance")); |
| 1010 | result = create_instance(create_info, allocator, instance_out); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1011 | if (enable_callback) |
| 1012 | FreeAllocatedCreateInfo(local_create_info, instance->alloc); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1013 | if (result <= 0) { |
| 1014 | // For every layer, including the loader top and bottom layers: |
| 1015 | // - If a call to the next CreateInstance fails, the layer must clean |
| 1016 | // up anything it has successfully done so far, and propagate the |
| 1017 | // error upwards. |
| 1018 | // - If a layer successfully calls the next layer's CreateInstance, and |
| 1019 | // afterwards must fail for some reason, it must call the next layer's |
| 1020 | // DestroyInstance before returning. |
| 1021 | // - The layer must not call the next layer's DestroyInstance if that |
| 1022 | // layer's CreateInstance wasn't called, or returned failure. |
| 1023 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1024 | // On failure, CreateInstance_Bottom frees the instance struct, so it's |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1025 | // already gone at this point. Nothing to do. |
| 1026 | } |
| 1027 | |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1028 | if (enable_logging) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1029 | const VkDebugReportCallbackCreateInfoEXT callback_create_info = { |
| 1030 | .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT, |
| 1031 | .flags = |
| 1032 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT, |
| 1033 | .pfnCallback = LogDebugMessageCallback, |
| 1034 | }; |
| 1035 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = |
| 1036 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 1037 | GetInstanceProcAddr_Top(instance->handle, |
| 1038 | "vkCreateDebugReportCallbackEXT")); |
| 1039 | create_debug_report_callback(instance->handle, &callback_create_info, |
| 1040 | allocator, &instance->message); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1043 | return result; |
| 1044 | } |
| 1045 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1046 | PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance, |
| 1047 | const char* name) { |
| 1048 | // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands |
| 1049 | if (!vkinstance) |
| 1050 | return GetLoaderGlobalProcAddr(name); |
| 1051 | |
| 1052 | const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance); |
| 1053 | PFN_vkVoidFunction pfn; |
| 1054 | // Always go through the loader-top function if there is one. |
| 1055 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1056 | return pfn; |
| 1057 | // Otherwise, look up the handler in the instance dispatch table |
| 1058 | if ((pfn = GetDispatchProcAddr(dispatch, name))) |
| 1059 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1060 | // Anything not handled already must be a device-dispatched function |
| 1061 | // without a loader-top. We must return a function that will dispatch based |
| 1062 | // on the dispatchable object parameter -- which is exactly what the |
| 1063 | // exported functions do. So just return them here. |
| 1064 | return GetLoaderExportProcAddr(name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1067 | void DestroyInstance_Top(VkInstance instance, |
| 1068 | const VkAllocationCallbacks* allocator) { |
| 1069 | if (!instance) |
| 1070 | return; |
| 1071 | GetDispatchTable(instance).DestroyInstance(instance, allocator); |
| 1072 | } |
| 1073 | |
| 1074 | PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) { |
| 1075 | PFN_vkVoidFunction pfn; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1076 | if (!device) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1077 | return nullptr; |
| 1078 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1079 | return pfn; |
| 1080 | return GetDispatchProcAddr(GetDispatchTable(device), name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1083 | void GetDeviceQueue_Top(VkDevice vkdevice, |
| 1084 | uint32_t family, |
| 1085 | uint32_t index, |
| 1086 | VkQueue* queue_out) { |
| 1087 | const auto& table = GetDispatchTable(vkdevice); |
| 1088 | table.GetDeviceQueue(vkdevice, family, index, queue_out); |
| 1089 | hwvulkan_dispatch_t* queue_dispatch = |
| 1090 | reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out); |
| 1091 | if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC && |
| 1092 | queue_dispatch->vtbl != &table) |
| 1093 | ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, |
| 1094 | queue_dispatch->magic); |
| 1095 | queue_dispatch->vtbl = &table; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1098 | VkResult AllocateCommandBuffers_Top( |
| 1099 | VkDevice vkdevice, |
| 1100 | const VkCommandBufferAllocateInfo* alloc_info, |
| 1101 | VkCommandBuffer* cmdbufs) { |
| 1102 | const auto& table = GetDispatchTable(vkdevice); |
| 1103 | VkResult result = |
| 1104 | table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs); |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1105 | if (result != VK_SUCCESS) |
| 1106 | return result; |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1107 | for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1108 | hwvulkan_dispatch_t* cmdbuf_dispatch = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1109 | reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1110 | ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1111 | "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1112 | cmdbuf_dispatch->magic); |
| 1113 | cmdbuf_dispatch->vtbl = &table; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1114 | } |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1115 | return VK_SUCCESS; |
| 1116 | } |
| 1117 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1118 | void DestroyDevice_Top(VkDevice vkdevice, |
| 1119 | const VkAllocationCallbacks* /*allocator*/) { |
| 1120 | if (!vkdevice) |
| 1121 | return; |
| 1122 | Device& device = GetDispatchParent(vkdevice); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1123 | device.dispatch.DestroyDevice(vkdevice, device.instance->alloc); |
| 1124 | DestroyDevice(&device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1127 | // ----------------------------------------------------------------------------- |
| 1128 | |
| 1129 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 1130 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1133 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
| 1134 | return GetDispatchParent(vkdevice).instance->alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1137 | VkInstance GetDriverInstance(VkInstance instance) { |
| 1138 | return GetDispatchParent(instance).drv.instance; |
| 1139 | } |
| 1140 | |
| 1141 | const DriverDispatchTable& GetDriverDispatch(VkInstance instance) { |
| 1142 | return GetDispatchParent(instance).drv.dispatch; |
| 1143 | } |
| 1144 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1145 | const DriverDispatchTable& GetDriverDispatch(VkDevice device) { |
| 1146 | return GetDispatchParent(device).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1149 | const DriverDispatchTable& GetDriverDispatch(VkQueue queue) { |
| 1150 | return GetDispatchParent(queue).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1153 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 1154 | return GetDispatchParent(instance).debug_report_callbacks; |
| 1155 | } |
| 1156 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1157 | } // namespace vulkan |