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]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 271 | DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 272 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 273 | Vector<LayerRef> active_layers; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 274 | VkDebugReportCallbackEXT message; |
| 275 | DebugReportCallbackList debug_report_callbacks; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 276 | |
| 277 | struct { |
| 278 | VkInstance instance; |
| 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 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 585 | |
| 586 | Vector<VkExtensionProperties> extensions( |
| 587 | Vector<VkExtensionProperties>::allocator_type(instance.alloc)); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 588 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 589 | hwvulkan_dispatch_t* pdev_dispatch = |
| 590 | reinterpret_cast<hwvulkan_dispatch_t*>( |
| 591 | instance.physical_devices[i]); |
| 592 | if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 593 | ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 594 | pdev_dispatch->magic); |
| 595 | DestroyInstance_Bottom(instance.handle, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 596 | return VK_ERROR_INITIALIZATION_FAILED; |
| 597 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 598 | pdev_dispatch->vtbl = instance.dispatch_ptr; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 599 | |
| 600 | uint32_t count; |
| 601 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 602 | instance.physical_devices[i], nullptr, &count, nullptr)) != |
| 603 | VK_SUCCESS) { |
| 604 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 605 | result); |
| 606 | continue; |
| 607 | } |
| 608 | extensions.resize(count); |
| 609 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 610 | instance.physical_devices[i], nullptr, &count, |
| 611 | extensions.data())) != VK_SUCCESS) { |
| 612 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 613 | result); |
| 614 | continue; |
| 615 | } |
| 616 | ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i); |
| 617 | for (const auto& extension : extensions) { |
| 618 | ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion); |
| 619 | DeviceExtension id = |
| 620 | DeviceExtensionFromName(extension.extensionName); |
| 621 | if (id == kDeviceExtensionCount) { |
| 622 | ALOGW("driver gpu[%u] extension '%s' unknown to loader", i, |
| 623 | extension.extensionName); |
| 624 | } else { |
| 625 | instance.physical_device_driver_extensions[i].set(id); |
| 626 | } |
| 627 | } |
| 628 | // Ignore driver attempts to support loader extensions |
| 629 | instance.physical_device_driver_extensions[i].reset(kKHR_swapchain); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 630 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 631 | instance.drv.num_physical_devices = num_physical_devices; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 632 | instance.num_physical_devices = instance.drv.num_physical_devices; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 633 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 634 | return VK_SUCCESS; |
| 635 | } |
| 636 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 637 | PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) { |
| 638 | PFN_vkVoidFunction pfn; |
| 639 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 640 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 641 | return nullptr; |
| 642 | } |
| 643 | |
| 644 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 645 | uint32_t* pdev_count, |
| 646 | VkPhysicalDevice* pdevs) { |
| 647 | Instance& instance = GetDispatchParent(vkinstance); |
| 648 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 649 | if (pdevs) { |
| 650 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 651 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 652 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 653 | } |
| 654 | *pdev_count = count; |
| 655 | return VK_SUCCESS; |
| 656 | } |
| 657 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 658 | void GetPhysicalDeviceProperties_Bottom( |
| 659 | VkPhysicalDevice pdev, |
| 660 | VkPhysicalDeviceProperties* properties) { |
| 661 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties( |
| 662 | pdev, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 665 | void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev, |
| 666 | VkPhysicalDeviceFeatures* features) { |
| 667 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev, |
| 668 | features); |
| 669 | } |
| 670 | |
| 671 | void GetPhysicalDeviceMemoryProperties_Bottom( |
| 672 | VkPhysicalDevice pdev, |
| 673 | VkPhysicalDeviceMemoryProperties* properties) { |
| 674 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties( |
| 675 | pdev, properties); |
| 676 | } |
| 677 | |
| 678 | void GetPhysicalDeviceQueueFamilyProperties_Bottom( |
| 679 | VkPhysicalDevice pdev, |
| 680 | uint32_t* pCount, |
| 681 | VkQueueFamilyProperties* properties) { |
| 682 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties( |
| 683 | pdev, pCount, properties); |
| 684 | } |
| 685 | |
| 686 | void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev, |
| 687 | VkFormat format, |
| 688 | VkFormatProperties* properties) { |
| 689 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 690 | pdev, format, properties); |
| 691 | } |
| 692 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 693 | VkResult GetPhysicalDeviceImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 694 | VkPhysicalDevice pdev, |
| 695 | VkFormat format, |
| 696 | VkImageType type, |
| 697 | VkImageTiling tiling, |
| 698 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 699 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 700 | VkImageFormatProperties* properties) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 701 | return GetDispatchParent(pdev) |
| 702 | .drv.dispatch.GetPhysicalDeviceImageFormatProperties( |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 703 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 706 | void GetPhysicalDeviceSparseImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 707 | VkPhysicalDevice pdev, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 708 | VkFormat format, |
| 709 | VkImageType type, |
| 710 | VkSampleCountFlagBits samples, |
| 711 | VkImageUsageFlags usage, |
| 712 | VkImageTiling tiling, |
| 713 | uint32_t* properties_count, |
| 714 | VkSparseImageFormatProperties* properties) { |
| 715 | GetDispatchParent(pdev) |
| 716 | .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties( |
| 717 | pdev, format, type, samples, usage, tiling, properties_count, |
| 718 | properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 721 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 722 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 723 | VkPhysicalDevice gpu, |
Jesse Hall | 57f7f8c | 2016-01-17 17:21:36 -0800 | [diff] [blame] | 724 | const char* layer_name, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 725 | uint32_t* properties_count, |
Jesse Hall | 57f7f8c | 2016-01-17 17:21:36 -0800 | [diff] [blame] | 726 | VkExtensionProperties* properties) { |
| 727 | const VkExtensionProperties* extensions = nullptr; |
| 728 | uint32_t num_extensions = 0; |
| 729 | if (layer_name) { |
| 730 | GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions); |
| 731 | } else { |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 732 | Instance& instance = GetDispatchParent(gpu); |
| 733 | size_t gpu_idx = 0; |
| 734 | while (instance.physical_devices[gpu_idx] != gpu) |
| 735 | gpu_idx++; |
| 736 | const DeviceExtensionSet driver_extensions = |
| 737 | instance.physical_device_driver_extensions[gpu_idx]; |
| 738 | |
| 739 | // We only support VK_KHR_swapchain if the GPU supports |
| 740 | // VK_ANDROID_native_buffer |
| 741 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 742 | alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties))); |
| 743 | if (driver_extensions[kANDROID_native_buffer]) { |
| 744 | available[num_extensions++] = VkExtensionProperties{ |
| 745 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION}; |
| 746 | } |
| 747 | |
| 748 | // TODO(jessehall): We need to also enumerate extensions supported by |
| 749 | // implicitly-enabled layers. Currently we don't have that list of |
| 750 | // layers until instance creation. |
| 751 | extensions = available; |
Jesse Hall | 57f7f8c | 2016-01-17 17:21:36 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (!properties || *properties_count > num_extensions) |
| 755 | *properties_count = num_extensions; |
| 756 | if (properties) |
| 757 | std::copy(extensions, extensions + *properties_count, properties); |
| 758 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 761 | VKAPI_ATTR |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 762 | VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 763 | uint32_t* properties_count, |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 764 | VkLayerProperties* properties) { |
| 765 | uint32_t layer_count = |
| 766 | EnumerateDeviceLayers(properties ? *properties_count : 0, properties); |
| 767 | if (!properties || *properties_count > layer_count) |
| 768 | *properties_count = layer_count; |
| 769 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | VKAPI_ATTR |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 773 | VkResult CreateDevice_Bottom(VkPhysicalDevice gpu, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 774 | const VkDeviceCreateInfo* create_info, |
| 775 | const VkAllocationCallbacks* allocator, |
| 776 | VkDevice* device_out) { |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 777 | Instance& instance = GetDispatchParent(gpu); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 778 | VkResult result; |
| 779 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 780 | if (!allocator) { |
| 781 | if (instance.alloc) |
| 782 | allocator = instance.alloc; |
| 783 | else |
| 784 | allocator = &kDefaultAllocCallbacks; |
| 785 | } |
| 786 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 787 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device), |
| 788 | alignof(Device), |
| 789 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 790 | if (!mem) |
| 791 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 792 | Device* device = new (mem) Device(&instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 793 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 794 | result = ActivateAllLayers(create_info, &instance, device); |
| 795 | if (result != VK_SUCCESS) { |
| 796 | DestroyDevice(device); |
| 797 | return result; |
| 798 | } |
| 799 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 800 | size_t gpu_idx = 0; |
| 801 | while (instance.physical_devices[gpu_idx] != gpu) |
| 802 | gpu_idx++; |
| 803 | |
| 804 | uint32_t num_driver_extensions = 0; |
| 805 | const char** driver_extensions = static_cast<const char**>( |
| 806 | alloca(create_info->enabledExtensionCount * sizeof(const char*))); |
| 807 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 808 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 809 | |
| 810 | DeviceExtension id = DeviceExtensionFromName(name); |
| 811 | if (id < kDeviceExtensionCount && |
| 812 | (instance.physical_device_driver_extensions[gpu_idx][id] || |
| 813 | id == kKHR_swapchain)) { |
| 814 | if (id == kKHR_swapchain) |
| 815 | name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME; |
| 816 | driver_extensions[num_driver_extensions++] = name; |
| 817 | continue; |
| 818 | } |
| 819 | |
| 820 | bool supported = false; |
| 821 | for (const auto& layer : device->active_layers) { |
| 822 | if (layer.SupportsExtension(name)) |
| 823 | supported = true; |
| 824 | } |
| 825 | if (!supported) { |
| 826 | ALOGE( |
| 827 | "requested device extension '%s' not supported by driver or " |
| 828 | "any active layers", |
| 829 | name); |
| 830 | DestroyDevice(device); |
| 831 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 832 | } |
| 833 | } |
| 834 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 835 | VkDeviceCreateInfo driver_create_info = *create_info; |
| 836 | driver_create_info.enabledLayerCount = 0; |
| 837 | driver_create_info.ppEnabledLayerNames = nullptr; |
| 838 | // TODO(jessehall): As soon as we enumerate device extensions supported by |
| 839 | // the driver, we need to filter the requested extension list to those |
| 840 | // supported by the driver here. Also, add the VK_ANDROID_native_buffer |
| 841 | // extension to the list iff the VK_KHR_swapchain extension was requested, |
| 842 | // instead of adding it unconditionally like we do now. |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 843 | driver_create_info.enabledExtensionCount = num_driver_extensions; |
| 844 | driver_create_info.ppEnabledExtensionNames = driver_extensions; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 845 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 846 | VkDevice drv_device; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 847 | result = instance.drv.dispatch.CreateDevice(gpu, &driver_create_info, |
| 848 | allocator, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 849 | if (result != VK_SUCCESS) { |
| 850 | DestroyDevice(device); |
| 851 | return result; |
| 852 | } |
| 853 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 854 | hwvulkan_dispatch_t* drv_dispatch = |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 855 | reinterpret_cast<hwvulkan_dispatch_t*>(drv_device); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 856 | if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 857 | ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, |
| 858 | drv_dispatch->magic); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 859 | PFN_vkDestroyDevice destroy_device = |
| 860 | reinterpret_cast<PFN_vkDestroyDevice>( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 861 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 862 | "vkDestroyDevice")); |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 863 | destroy_device(drv_device, allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 864 | DestroyDevice(device); |
| 865 | return VK_ERROR_INITIALIZATION_FAILED; |
| 866 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 867 | drv_dispatch->vtbl = &device->dispatch; |
| 868 | device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 869 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 870 | "vkGetDeviceProcAddr")); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 871 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 872 | void* base_object = static_cast<void*>(drv_device); |
| 873 | void* next_object = base_object; |
| 874 | VkLayerLinkedListElem* next_element; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 875 | PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 876 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 877 | device->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 878 | CallbackAllocator<VkLayerLinkedListElem>(instance.alloc)); |
| 879 | |
| 880 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 881 | size_t idx = i - 1; |
| 882 | next_element = &elem_list[idx]; |
| 883 | next_element->get_proc_addr = |
| 884 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 885 | next_element->base_object = base_object; |
| 886 | next_element->next_element = next_object; |
| 887 | next_object = static_cast<void*>(next_element); |
| 888 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 889 | next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 890 | if (!next_get_proc_addr) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 891 | next_object = next_element->next_element; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 892 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 893 | next_element->get_proc_addr); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 897 | // This is the magic call that initializes all the layer devices and |
| 898 | // allows them to create their device_handle -> device_data mapping. |
| 899 | next_get_proc_addr(static_cast<VkDevice>(next_object), |
| 900 | "vkGetDeviceProcAddr"); |
| 901 | |
| 902 | // We must create all the layer devices *before* retrieving the device |
| 903 | // procaddrs, so that the layers know which extensions are enabled and |
| 904 | // therefore which functions to return procaddrs for. |
| 905 | PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>( |
| 906 | next_get_proc_addr(drv_device, "vkCreateDevice")); |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame^] | 907 | create_device(gpu, create_info, allocator, &drv_device); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 908 | |
| 909 | if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object), |
| 910 | next_get_proc_addr, device->dispatch)) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 911 | DestroyDevice(device); |
| 912 | return VK_ERROR_INITIALIZATION_FAILED; |
| 913 | } |
| 914 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 915 | *device_out = drv_device; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 916 | return VK_SUCCESS; |
| 917 | } |
| 918 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 919 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 920 | const VkAllocationCallbacks* allocator) { |
| 921 | Instance& instance = GetDispatchParent(vkinstance); |
| 922 | |
| 923 | // These checks allow us to call DestroyInstance_Bottom from any error |
| 924 | // path in CreateInstance_Bottom, before the driver instance is fully |
| 925 | // initialized. |
| 926 | if (instance.drv.instance != VK_NULL_HANDLE && |
| 927 | instance.drv.dispatch.DestroyInstance) { |
| 928 | instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator); |
| 929 | } |
| 930 | if (instance.message) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 931 | PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback; |
| 932 | destroy_debug_report_callback = |
| 933 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 934 | vkGetInstanceProcAddr(vkinstance, |
| 935 | "vkDestroyDebugReportCallbackEXT")); |
| 936 | destroy_debug_report_callback(vkinstance, instance.message, allocator); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 937 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 938 | instance.active_layers.clear(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 939 | const VkAllocationCallbacks* alloc = instance.alloc; |
| 940 | instance.~Instance(); |
| 941 | alloc->pfnFree(alloc->pUserData, &instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 944 | PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice, |
| 945 | const char* name) { |
| 946 | if (strcmp(name, "vkCreateDevice") == 0) { |
| 947 | // TODO(jessehall): Blegh, having this here is disgusting. The current |
| 948 | // layer init process can't call through the instance dispatch table's |
| 949 | // vkCreateDevice, because that goes through the instance layers rather |
| 950 | // than through the device layers. So we need to be able to get the |
| 951 | // vkCreateDevice pointer through the *device* layer chain. |
| 952 | // |
| 953 | // Because we've already created the driver device before calling |
| 954 | // through the layer vkCreateDevice functions, the loader bottom proc |
| 955 | // is a no-op. |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 956 | return reinterpret_cast<PFN_vkVoidFunction>(Noop); |
| 957 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 958 | |
| 959 | // VK_ANDROID_native_buffer should be hidden from applications and layers. |
| 960 | // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr. |
| 961 | PFN_vkVoidFunction pfn; |
| 962 | if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 || |
| 963 | strcmp(name, "vkAcquireImageANDROID") == 0 || |
| 964 | strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) { |
| 965 | return nullptr; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 966 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 967 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 968 | return pfn; |
| 969 | return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 972 | // ----------------------------------------------------------------------------- |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 973 | // Loader top functions. These are called directly from the loader entry |
| 974 | // points or from the application (via vkGetInstanceProcAddr) without going |
| 975 | // through a dispatch table. |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 976 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 977 | VkResult EnumerateInstanceExtensionProperties_Top( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 978 | const char* layer_name, |
| 979 | uint32_t* properties_count, |
| 980 | VkExtensionProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 981 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 982 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 983 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 984 | const VkExtensionProperties* extensions = nullptr; |
| 985 | uint32_t num_extensions = 0; |
| 986 | if (layer_name) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 987 | GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 988 | } else { |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 989 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 990 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 991 | available[num_extensions++] = VkExtensionProperties{ |
| 992 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 993 | available[num_extensions++] = |
| 994 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 995 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 996 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 997 | available[num_extensions++] = |
| 998 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 999 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 1000 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1001 | // TODO(jessehall): We need to also enumerate extensions supported by |
| 1002 | // implicitly-enabled layers. Currently we don't have that list of |
| 1003 | // layers until instance creation. |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 1004 | extensions = available; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1005 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1006 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1007 | if (!properties || *properties_count > num_extensions) |
| 1008 | *properties_count = num_extensions; |
| 1009 | if (properties) |
| 1010 | std::copy(extensions, extensions + *properties_count, properties); |
| 1011 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1014 | VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count, |
| 1015 | VkLayerProperties* properties) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1016 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1017 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1018 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1019 | uint32_t layer_count = |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 1020 | EnumerateInstanceLayers(properties ? *properties_count : 0, properties); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1021 | if (!properties || *properties_count > layer_count) |
| 1022 | *properties_count = layer_count; |
| 1023 | return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1026 | VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info, |
| 1027 | const VkAllocationCallbacks* allocator, |
| 1028 | VkInstance* instance_out) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1029 | VkResult result; |
| 1030 | |
| 1031 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1032 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1033 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1034 | if (!allocator) |
| 1035 | allocator = &kDefaultAllocCallbacks; |
| 1036 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1037 | VkInstanceCreateInfo local_create_info = *create_info; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1038 | create_info = &local_create_info; |
| 1039 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1040 | void* instance_mem = allocator->pfnAllocation( |
| 1041 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 1042 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1043 | if (!instance_mem) |
| 1044 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1045 | Instance* instance = new (instance_mem) Instance(allocator); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1046 | |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 1047 | result = ActivateAllLayers(create_info, instance, instance); |
| 1048 | if (result != VK_SUCCESS) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1049 | DestroyInstance_Bottom(instance->handle, allocator); |
Jesse Hall | 9a16f97 | 2015-10-28 15:59:53 -0700 | [diff] [blame] | 1050 | return result; |
| 1051 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1052 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1053 | void* base_object = static_cast<void*>(instance->handle); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1054 | void* next_object = base_object; |
| 1055 | VkLayerLinkedListElem* next_element; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1056 | PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1057 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 1058 | instance->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1059 | CallbackAllocator<VkLayerLinkedListElem>(instance->alloc)); |
| 1060 | |
| 1061 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 1062 | size_t idx = i - 1; |
| 1063 | next_element = &elem_list[idx]; |
| 1064 | next_element->get_proc_addr = |
| 1065 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 1066 | next_element->base_object = base_object; |
| 1067 | next_element->next_element = next_object; |
| 1068 | next_object = static_cast<void*>(next_element); |
| 1069 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1070 | next_get_proc_addr = |
| 1071 | instance->active_layers[idx].GetGetInstanceProcAddr(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1072 | if (!next_get_proc_addr) { |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1073 | next_object = next_element->next_element; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1074 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1075 | next_element->get_proc_addr); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1079 | // This is the magic call that initializes all the layer instances and |
| 1080 | // allows them to create their instance_handle -> instance_data mapping. |
| 1081 | next_get_proc_addr(static_cast<VkInstance>(next_object), |
| 1082 | "vkGetInstanceProcAddr"); |
| 1083 | |
| 1084 | if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object), |
| 1085 | next_get_proc_addr, instance->dispatch)) { |
| 1086 | DestroyInstance_Bottom(instance->handle, allocator); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1087 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1088 | } |
| 1089 | |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1090 | // Force enable callback extension if required |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 1091 | bool enable_callback = false; |
| 1092 | bool enable_logging = false; |
| 1093 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 1094 | enable_callback = |
| 1095 | property_get_bool("debug.vulkan.enable_callback", false); |
| 1096 | enable_logging = enable_callback; |
| 1097 | if (enable_callback) { |
| 1098 | enable_callback = AddExtensionToCreateInfo( |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1099 | local_create_info, "VK_EXT_debug_report", instance->alloc); |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 1100 | } |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1103 | *instance_out = instance->handle; |
| 1104 | PFN_vkCreateInstance create_instance = |
| 1105 | reinterpret_cast<PFN_vkCreateInstance>( |
| 1106 | next_get_proc_addr(instance->handle, "vkCreateInstance")); |
| 1107 | result = create_instance(create_info, allocator, instance_out); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1108 | if (enable_callback) |
| 1109 | FreeAllocatedCreateInfo(local_create_info, instance->alloc); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1110 | if (result <= 0) { |
| 1111 | // For every layer, including the loader top and bottom layers: |
| 1112 | // - If a call to the next CreateInstance fails, the layer must clean |
| 1113 | // up anything it has successfully done so far, and propagate the |
| 1114 | // error upwards. |
| 1115 | // - If a layer successfully calls the next layer's CreateInstance, and |
| 1116 | // afterwards must fail for some reason, it must call the next layer's |
| 1117 | // DestroyInstance before returning. |
| 1118 | // - The layer must not call the next layer's DestroyInstance if that |
| 1119 | // layer's CreateInstance wasn't called, or returned failure. |
| 1120 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1121 | // On failure, CreateInstance_Bottom frees the instance struct, so it's |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1122 | // already gone at this point. Nothing to do. |
| 1123 | } |
| 1124 | |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1125 | if (enable_logging) { |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1126 | const VkDebugReportCallbackCreateInfoEXT callback_create_info = { |
| 1127 | .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT, |
| 1128 | .flags = |
| 1129 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT, |
| 1130 | .pfnCallback = LogDebugMessageCallback, |
| 1131 | }; |
| 1132 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = |
| 1133 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 1134 | GetInstanceProcAddr_Top(instance->handle, |
| 1135 | "vkCreateDebugReportCallbackEXT")); |
| 1136 | create_debug_report_callback(instance->handle, &callback_create_info, |
| 1137 | allocator, &instance->message); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 1138 | } |
| 1139 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1140 | return result; |
| 1141 | } |
| 1142 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1143 | PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance, |
| 1144 | const char* name) { |
| 1145 | // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands |
| 1146 | if (!vkinstance) |
| 1147 | return GetLoaderGlobalProcAddr(name); |
| 1148 | |
| 1149 | const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance); |
| 1150 | PFN_vkVoidFunction pfn; |
| 1151 | // Always go through the loader-top function if there is one. |
| 1152 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1153 | return pfn; |
| 1154 | // Otherwise, look up the handler in the instance dispatch table |
| 1155 | if ((pfn = GetDispatchProcAddr(dispatch, name))) |
| 1156 | return pfn; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1157 | // Anything not handled already must be a device-dispatched function |
| 1158 | // without a loader-top. We must return a function that will dispatch based |
| 1159 | // on the dispatchable object parameter -- which is exactly what the |
| 1160 | // exported functions do. So just return them here. |
| 1161 | return GetLoaderExportProcAddr(name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1164 | void DestroyInstance_Top(VkInstance instance, |
| 1165 | const VkAllocationCallbacks* allocator) { |
| 1166 | if (!instance) |
| 1167 | return; |
| 1168 | GetDispatchTable(instance).DestroyInstance(instance, allocator); |
| 1169 | } |
| 1170 | |
| 1171 | PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) { |
| 1172 | PFN_vkVoidFunction pfn; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1173 | if (!device) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1174 | return nullptr; |
| 1175 | if ((pfn = GetLoaderTopProcAddr(name))) |
| 1176 | return pfn; |
| 1177 | return GetDispatchProcAddr(GetDispatchTable(device), name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1180 | void GetDeviceQueue_Top(VkDevice vkdevice, |
| 1181 | uint32_t family, |
| 1182 | uint32_t index, |
| 1183 | VkQueue* queue_out) { |
| 1184 | const auto& table = GetDispatchTable(vkdevice); |
| 1185 | table.GetDeviceQueue(vkdevice, family, index, queue_out); |
| 1186 | hwvulkan_dispatch_t* queue_dispatch = |
| 1187 | reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out); |
| 1188 | if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC && |
| 1189 | queue_dispatch->vtbl != &table) |
| 1190 | ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, |
| 1191 | queue_dispatch->magic); |
| 1192 | queue_dispatch->vtbl = &table; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1195 | VkResult AllocateCommandBuffers_Top( |
| 1196 | VkDevice vkdevice, |
| 1197 | const VkCommandBufferAllocateInfo* alloc_info, |
| 1198 | VkCommandBuffer* cmdbufs) { |
| 1199 | const auto& table = GetDispatchTable(vkdevice); |
| 1200 | VkResult result = |
| 1201 | table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs); |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1202 | if (result != VK_SUCCESS) |
| 1203 | return result; |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1204 | for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1205 | hwvulkan_dispatch_t* cmdbuf_dispatch = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1206 | reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1207 | ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 1208 | "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1209 | cmdbuf_dispatch->magic); |
| 1210 | cmdbuf_dispatch->vtbl = &table; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1211 | } |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1212 | return VK_SUCCESS; |
| 1213 | } |
| 1214 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1215 | void DestroyDevice_Top(VkDevice vkdevice, |
| 1216 | const VkAllocationCallbacks* /*allocator*/) { |
| 1217 | if (!vkdevice) |
| 1218 | return; |
| 1219 | Device& device = GetDispatchParent(vkdevice); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1220 | device.dispatch.DestroyDevice(vkdevice, device.instance->alloc); |
| 1221 | DestroyDevice(&device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1224 | // ----------------------------------------------------------------------------- |
| 1225 | |
| 1226 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 1227 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1230 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
| 1231 | return GetDispatchParent(vkdevice).instance->alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1234 | VkInstance GetDriverInstance(VkInstance instance) { |
| 1235 | return GetDispatchParent(instance).drv.instance; |
| 1236 | } |
| 1237 | |
| 1238 | const DriverDispatchTable& GetDriverDispatch(VkInstance instance) { |
| 1239 | return GetDispatchParent(instance).drv.dispatch; |
| 1240 | } |
| 1241 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1242 | const DriverDispatchTable& GetDriverDispatch(VkDevice device) { |
| 1243 | return GetDispatchParent(device).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1246 | const DriverDispatchTable& GetDriverDispatch(VkQueue queue) { |
| 1247 | return GetDispatchParent(queue).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1250 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 1251 | return GetDispatchParent(instance).debug_report_callbacks; |
| 1252 | } |
| 1253 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1254 | } // namespace vulkan |