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