Jesse Hall | d02edcb | 2015-09-08 07:44:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 17 | // module header |
| 18 | #include "loader.h" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 19 | #include "driver.h" |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 20 | // standard C headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <dlfcn.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 24 | #include <pthread.h> |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 26 | #include <string.h> |
Jesse Hall | 2159766 | 2015-12-18 13:48:24 -0800 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 28 | // standard C++ headers |
| 29 | #include <algorithm> |
| 30 | #include <mutex> |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | #include <unordered_map> |
| 34 | #include <vector> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 35 | // platform/library headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 37 | #include <hardware/hwvulkan.h> |
| 38 | #include <log/log.h> |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 39 | #include <vulkan/vulkan_loader_data.h> |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 40 | #include <vulkan/vk_layer_interface.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 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 48 | // ---------------------------------------------------------------------------- |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 49 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 50 | // Standard-library allocator that delegates to VkAllocationCallbacks. |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 51 | // |
| 52 | // TODO(jessehall): This class currently always uses |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 53 | // 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] | 54 | // parameter or a constructor parameter. The former would help catch bugs |
| 55 | // where we use the wrong scope, e.g. adding a command-scope string to an |
| 56 | // 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] | 57 | template <class T> |
| 58 | class CallbackAllocator { |
| 59 | public: |
| 60 | typedef T value_type; |
| 61 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 62 | CallbackAllocator(const VkAllocationCallbacks* alloc_input) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 63 | : alloc(alloc_input) {} |
| 64 | |
| 65 | template <class T2> |
| 66 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 67 | : alloc(other.alloc) {} |
| 68 | |
| 69 | T* allocate(std::size_t n) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 70 | void* mem = |
| 71 | alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T), |
| 72 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 73 | if (!mem) |
| 74 | throw std::bad_alloc(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 75 | return static_cast<T*>(mem); |
| 76 | } |
| 77 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 78 | void deallocate(T* array, std::size_t /*n*/) noexcept { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 79 | alloc->pfnFree(alloc->pUserData, array); |
| 80 | } |
| 81 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 82 | const VkAllocationCallbacks* alloc; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 83 | }; |
| 84 | // These are needed in order to move Strings |
| 85 | template <class T> |
| 86 | bool operator==(const CallbackAllocator<T>& alloc1, |
| 87 | const CallbackAllocator<T>& alloc2) { |
| 88 | return alloc1.alloc == alloc2.alloc; |
| 89 | } |
| 90 | template <class T> |
| 91 | bool operator!=(const CallbackAllocator<T>& alloc1, |
| 92 | const CallbackAllocator<T>& alloc2) { |
| 93 | return !(alloc1 == alloc2); |
| 94 | } |
| 95 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 96 | template <class T> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 97 | using Vector = std::vector<T, CallbackAllocator<T>>; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 98 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 99 | typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>> |
| 100 | String; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 101 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 102 | // ---------------------------------------------------------------------------- |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 103 | // Global Data and Initialization |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 104 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 105 | hwvulkan_device_t* g_hwdevice = nullptr; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 106 | InstanceExtensionSet g_driver_instance_extensions; |
| 107 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 108 | bool LoadVulkanHAL() { |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 109 | VkResult vkresult; |
| 110 | uint32_t count; |
| 111 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 112 | nullptr, &count, nullptr)) != VK_SUCCESS) { |
| 113 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 114 | vkresult); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 115 | return false; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 116 | } |
| 117 | VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>( |
| 118 | alloca(count * sizeof(VkExtensionProperties))); |
| 119 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 120 | nullptr, &count, extensions)) != VK_SUCCESS) { |
| 121 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 122 | vkresult); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 123 | return false; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 124 | } |
| 125 | ALOGV_IF(count > 0, "Driver-supported instance extensions:"); |
| 126 | for (uint32_t i = 0; i < count; i++) { |
| 127 | ALOGV(" %s (v%u)", extensions[i].extensionName, |
| 128 | extensions[i].specVersion); |
| 129 | InstanceExtension id = |
| 130 | InstanceExtensionFromName(extensions[i].extensionName); |
| 131 | if (id != kInstanceExtensionCount) |
| 132 | g_driver_instance_extensions.set(id); |
| 133 | } |
| 134 | // Ignore driver attempts to support loader extensions |
| 135 | g_driver_instance_extensions.reset(kKHR_surface); |
| 136 | g_driver_instance_extensions.reset(kKHR_android_surface); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 137 | |
| 138 | return true; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 141 | // ----------------------------------------------------------------------------- |
| 142 | |
| 143 | struct Instance { |
| 144 | Instance(const VkAllocationCallbacks* alloc_callbacks) |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 145 | : base(*alloc_callbacks), |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 146 | alloc(&base.allocator), |
| 147 | num_physical_devices(0) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 148 | memset(physical_devices, 0, sizeof(physical_devices)); |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 149 | enabled_extensions.reset(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 152 | ~Instance() {} |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 153 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 154 | driver::InstanceData base; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 155 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 156 | const VkAllocationCallbacks* alloc; |
| 157 | uint32_t num_physical_devices; |
Courtney Goeltzenleuchter | 84cd4c2 | 2016-03-16 12:20:13 -0600 | [diff] [blame] | 158 | VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 159 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 160 | DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 161 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 162 | DebugReportCallbackList debug_report_callbacks; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 163 | InstanceExtensionSet enabled_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 166 | template <typename THandle> |
| 167 | struct HandleTraits {}; |
| 168 | template <> |
| 169 | struct HandleTraits<VkInstance> { |
| 170 | typedef Instance LoaderObjectType; |
| 171 | }; |
| 172 | template <> |
| 173 | struct HandleTraits<VkPhysicalDevice> { |
| 174 | typedef Instance LoaderObjectType; |
| 175 | }; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 176 | |
| 177 | template <typename THandle> |
| 178 | typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent( |
| 179 | THandle handle) { |
| 180 | // TODO(jessehall): Make Instance and Device POD types (by removing the |
| 181 | // non-default constructors), so that offsetof is actually legal to use. |
| 182 | // The specific case we're using here is safe in gcc/clang (and probably |
| 183 | // most other C++ compilers), but isn't guaranteed by C++. |
| 184 | typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType; |
| 185 | #pragma clang diagnostic push |
| 186 | #pragma clang diagnostic ignored "-Winvalid-offsetof" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 187 | const size_t kBaseOffset = offsetof(ObjectType, base); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 188 | #pragma clang diagnostic pop |
| 189 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 190 | const auto& base = driver::GetData(handle); |
| 191 | uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base); |
| 192 | uintptr_t object_addr = base_addr - kBaseOffset; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 193 | return *reinterpret_cast<ObjectType*>(object_addr); |
| 194 | } |
| 195 | |
| 196 | // ----------------------------------------------------------------------------- |
| 197 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 198 | /* |
| 199 | * This function will return the pNext pointer of any |
| 200 | * CreateInfo extensions that are not loader extensions. |
| 201 | * This is used to skip past the loader extensions prepended |
| 202 | * to the list during CreateInstance and CreateDevice. |
| 203 | */ |
| 204 | void* StripCreateExtensions(const void* pNext) { |
| 205 | VkLayerInstanceCreateInfo* create_info = |
| 206 | const_cast<VkLayerInstanceCreateInfo*>( |
| 207 | static_cast<const VkLayerInstanceCreateInfo*>(pNext)); |
| 208 | |
| 209 | while ( |
| 210 | create_info && |
| 211 | (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO || |
| 212 | create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) { |
| 213 | create_info = const_cast<VkLayerInstanceCreateInfo*>( |
| 214 | static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext)); |
| 215 | } |
| 216 | |
| 217 | return create_info; |
| 218 | } |
| 219 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 220 | // Clean up and deallocate an Instance; called from both the failure paths in |
| 221 | // CreateInstance_Top as well as from DestroyInstance_Top. This function does |
| 222 | // not call down the dispatch chain; that should be done before calling this |
| 223 | // function, iff the lower vkCreateInstance call has been made and returned |
| 224 | // successfully. |
| 225 | void DestroyInstance(Instance* instance, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 226 | const VkAllocationCallbacks* allocator, |
| 227 | VkInstance vkinstance) { |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 228 | if (vkinstance != VK_NULL_HANDLE && instance->base.driver.DestroyInstance) |
| 229 | instance->base.driver.DestroyInstance(vkinstance, allocator); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 230 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 231 | instance->~Instance(); |
| 232 | allocator->pfnFree(allocator->pUserData, instance); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 235 | driver::ProcHook::Extension InstanceExtensionToProcHookExtension( |
| 236 | InstanceExtension id) { |
| 237 | switch (id) { |
| 238 | case kKHR_surface: |
| 239 | return driver::ProcHook::KHR_surface; |
| 240 | case kKHR_android_surface: |
| 241 | return driver::ProcHook::KHR_android_surface; |
| 242 | case kEXT_debug_report: |
| 243 | return driver::ProcHook::EXT_debug_report; |
| 244 | default: |
| 245 | return driver::ProcHook::EXTENSION_UNKNOWN; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | driver::ProcHook::Extension DeviceExtensionToProcHookExtension( |
| 250 | DeviceExtension id) { |
| 251 | switch (id) { |
| 252 | case kKHR_swapchain: |
| 253 | return driver::ProcHook::KHR_swapchain; |
| 254 | case kANDROID_native_buffer: |
| 255 | return driver::ProcHook::ANDROID_native_buffer; |
| 256 | default: |
| 257 | return driver::ProcHook::EXTENSION_UNKNOWN; |
| 258 | } |
| 259 | } |
| 260 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 261 | } // anonymous namespace |
| 262 | |
| 263 | namespace vulkan { |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 264 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 265 | // ----------------------------------------------------------------------------- |
| 266 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 267 | // chain. |
| 268 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 269 | VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info, |
| 270 | const VkAllocationCallbacks* allocator, |
| 271 | VkInstance* vkinstance) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 272 | VkResult result; |
| 273 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 274 | if (!allocator) |
Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 275 | allocator = &driver::GetDefaultAllocator(); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 276 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 277 | void* instance_mem = allocator->pfnAllocation( |
| 278 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 279 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 280 | if (!instance_mem) |
| 281 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 282 | Instance& instance = *new (instance_mem) Instance(allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 283 | |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 284 | // Check that all enabled extensions are supported |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 285 | uint32_t num_driver_extensions = 0; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 286 | bool enable_kEXT_debug_report = false; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 287 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 288 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 289 | InstanceExtension id = InstanceExtensionFromName(name); |
| 290 | if (id != kInstanceExtensionCount) { |
| 291 | if (g_driver_instance_extensions[id]) { |
| 292 | num_driver_extensions++; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 293 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 294 | continue; |
| 295 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 296 | if (id == kKHR_surface || id == kKHR_android_surface) { |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 297 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 298 | continue; |
| 299 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 300 | // The loader natively supports debug report. |
| 301 | if (id == kEXT_debug_report) { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 302 | enable_kEXT_debug_report = true; |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 303 | continue; |
| 304 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 305 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 308 | auto& hal_exts = instance.base.hal_extensions; |
| 309 | for (size_t i = 0; i < instance.enabled_extensions.size(); i++) { |
| 310 | if (instance.enabled_extensions[i]) { |
| 311 | auto bit = InstanceExtensionToProcHookExtension( |
| 312 | static_cast<InstanceExtension>(i)); |
| 313 | if (bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 314 | hal_exts.set(bit); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | auto& hook_exts = instance.base.hook_extensions; |
| 319 | hook_exts = hal_exts; |
| 320 | if (enable_kEXT_debug_report) |
| 321 | hook_exts.set(driver::ProcHook::EXT_debug_report); |
| 322 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 323 | VkInstanceCreateInfo driver_create_info = *create_info; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 324 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 325 | driver_create_info.enabledLayerCount = 0; |
| 326 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 327 | driver_create_info.enabledExtensionCount = 0; |
| 328 | driver_create_info.ppEnabledExtensionNames = nullptr; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 329 | if (num_driver_extensions > 0) { |
| 330 | const char** names = static_cast<const char**>( |
| 331 | alloca(num_driver_extensions * sizeof(char*))); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 332 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 333 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 334 | InstanceExtension id = InstanceExtensionFromName(name); |
| 335 | if (id != kInstanceExtensionCount) { |
| 336 | if (g_driver_instance_extensions[id]) { |
| 337 | names[driver_create_info.enabledExtensionCount++] = name; |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 338 | continue; |
| 339 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | driver_create_info.ppEnabledExtensionNames = names; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 343 | ALOG_ASSERT( |
| 344 | driver_create_info.enabledExtensionCount == num_driver_extensions, |
| 345 | "counted enabled driver instance extensions twice and got " |
| 346 | "different answers!"); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 347 | } |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 348 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 349 | VkInstance drv_instance; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 350 | result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 351 | &drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 352 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 353 | DestroyInstance(&instance, allocator, VK_NULL_HANDLE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 354 | return result; |
| 355 | } |
| 356 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 357 | if (!driver::SetData(drv_instance, instance.base)) { |
| 358 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 359 | return VK_ERROR_INITIALIZATION_FAILED; |
| 360 | } |
| 361 | |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 362 | if (!driver::InitDriverTable(drv_instance, |
| 363 | g_hwdevice->GetInstanceProcAddr)) { |
| 364 | DestroyInstance(&instance, allocator, drv_instance); |
| 365 | return VK_ERROR_INITIALIZATION_FAILED; |
| 366 | } |
| 367 | |
| 368 | instance.base.get_device_proc_addr = |
| 369 | reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 370 | g_hwdevice->GetInstanceProcAddr(drv_instance, |
| 371 | "vkGetDeviceProcAddr")); |
| 372 | if (!instance.base.get_device_proc_addr) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 373 | DestroyInstance(&instance, allocator, drv_instance); |
Courtney Goeltzenleuchter | aa6c872 | 2016-01-29 08:57:16 -0700 | [diff] [blame] | 374 | return VK_ERROR_INITIALIZATION_FAILED; |
| 375 | } |
| 376 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 377 | uint32_t num_physical_devices = 0; |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 378 | result = instance.base.driver.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 379 | drv_instance, &num_physical_devices, nullptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 380 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 381 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 382 | return VK_ERROR_INITIALIZATION_FAILED; |
| 383 | } |
| 384 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 385 | result = instance.base.driver.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 386 | drv_instance, &num_physical_devices, instance.physical_devices); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 387 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 388 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 389 | return VK_ERROR_INITIALIZATION_FAILED; |
| 390 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 391 | |
| 392 | Vector<VkExtensionProperties> extensions( |
| 393 | Vector<VkExtensionProperties>::allocator_type(instance.alloc)); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 394 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 395 | if (!driver::SetData(instance.physical_devices[i], instance.base)) { |
| 396 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 397 | return VK_ERROR_INITIALIZATION_FAILED; |
| 398 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 399 | |
| 400 | uint32_t count; |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 401 | if ((result = instance.base.driver.EnumerateDeviceExtensionProperties( |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 402 | instance.physical_devices[i], nullptr, &count, nullptr)) != |
| 403 | VK_SUCCESS) { |
| 404 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 405 | result); |
| 406 | continue; |
| 407 | } |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 408 | try { |
| 409 | extensions.resize(count); |
| 410 | } catch (std::bad_alloc&) { |
| 411 | ALOGE("instance creation failed: out of memory"); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 412 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 413 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 414 | } |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 415 | if ((result = instance.base.driver.EnumerateDeviceExtensionProperties( |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 416 | instance.physical_devices[i], nullptr, &count, |
| 417 | extensions.data())) != VK_SUCCESS) { |
| 418 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 419 | result); |
| 420 | continue; |
| 421 | } |
| 422 | ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i); |
| 423 | for (const auto& extension : extensions) { |
| 424 | ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion); |
| 425 | DeviceExtension id = |
| 426 | DeviceExtensionFromName(extension.extensionName); |
| 427 | if (id == kDeviceExtensionCount) { |
| 428 | ALOGW("driver gpu[%u] extension '%s' unknown to loader", i, |
| 429 | extension.extensionName); |
| 430 | } else { |
| 431 | instance.physical_device_driver_extensions[i].set(id); |
| 432 | } |
| 433 | } |
| 434 | // Ignore driver attempts to support loader extensions |
| 435 | instance.physical_device_driver_extensions[i].reset(kKHR_swapchain); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 436 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 437 | instance.num_physical_devices = num_physical_devices; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 438 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 439 | *vkinstance = drv_instance; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 440 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 441 | return VK_SUCCESS; |
| 442 | } |
| 443 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 444 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 445 | uint32_t* pdev_count, |
| 446 | VkPhysicalDevice* pdevs) { |
| 447 | Instance& instance = GetDispatchParent(vkinstance); |
| 448 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 449 | if (pdevs) { |
| 450 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 451 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 452 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 453 | } |
| 454 | *pdev_count = count; |
| 455 | return VK_SUCCESS; |
| 456 | } |
| 457 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 458 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 459 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 460 | VkPhysicalDevice pdev, |
| 461 | const char* layer_name, |
| 462 | uint32_t* properties_count, |
| 463 | VkExtensionProperties* properties) { |
| 464 | (void)layer_name; |
| 465 | |
| 466 | Instance& instance = GetDispatchParent(pdev); |
| 467 | |
| 468 | size_t gpu_idx = 0; |
| 469 | while (instance.physical_devices[gpu_idx] != pdev) |
| 470 | gpu_idx++; |
| 471 | const DeviceExtensionSet driver_extensions = |
| 472 | instance.physical_device_driver_extensions[gpu_idx]; |
| 473 | |
| 474 | // We only support VK_KHR_swapchain if the GPU supports |
| 475 | // VK_ANDROID_native_buffer |
| 476 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 477 | alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties))); |
| 478 | uint32_t num_extensions = 0; |
| 479 | if (driver_extensions[kANDROID_native_buffer]) { |
| 480 | available[num_extensions++] = VkExtensionProperties{ |
| 481 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION}; |
| 482 | } |
| 483 | |
| 484 | if (!properties || *properties_count > num_extensions) |
| 485 | *properties_count = num_extensions; |
| 486 | if (properties) |
| 487 | std::copy(available, available + *properties_count, properties); |
| 488 | |
| 489 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 492 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 493 | const VkAllocationCallbacks* allocator) { |
| 494 | Instance& instance = GetDispatchParent(vkinstance); |
| 495 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 496 | VkAllocationCallbacks local_allocator; |
| 497 | if (!allocator) { |
| 498 | local_allocator = *instance.alloc; |
| 499 | allocator = &local_allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 500 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 501 | |
| 502 | DestroyInstance(&instance, allocator, vkinstance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 505 | // ----------------------------------------------------------------------------- |
| 506 | |
| 507 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 508 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 511 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame^] | 512 | return &driver::GetData(vkdevice).allocator; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 515 | VkInstance GetDriverInstance(VkInstance instance) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 516 | return instance; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 519 | const driver::InstanceDriverTable& GetDriverDispatch(VkInstance instance) { |
| 520 | return driver::GetData(instance).driver; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 523 | const driver::DeviceDriverTable& GetDriverDispatch(VkDevice device) { |
| 524 | return driver::GetData(device).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 527 | const driver::DeviceDriverTable& GetDriverDispatch(VkQueue queue) { |
| 528 | return driver::GetData(queue).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 531 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 532 | return GetDispatchParent(instance).debug_report_callbacks; |
| 533 | } |
| 534 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 535 | bool InitLoader(hwvulkan_device_t* dev) { |
| 536 | if (!g_hwdevice) { |
| 537 | g_hwdevice = dev; |
| 538 | if (!LoadVulkanHAL()) |
| 539 | g_hwdevice = nullptr; |
| 540 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 541 | |
| 542 | return (g_hwdevice != nullptr); |
| 543 | } |
| 544 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 545 | namespace driver { |
| 546 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 547 | VkResult EnumerateInstanceExtensionProperties( |
| 548 | const char* pLayerName, |
| 549 | uint32_t* pPropertyCount, |
| 550 | VkExtensionProperties* pProperties) { |
| 551 | (void)pLayerName; |
| 552 | |
| 553 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 554 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 555 | uint32_t num_extensions = 0; |
| 556 | |
| 557 | available[num_extensions++] = VkExtensionProperties{ |
| 558 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 559 | available[num_extensions++] = |
| 560 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 561 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 562 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 563 | available[num_extensions++] = |
| 564 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 565 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 566 | } |
| 567 | |
| 568 | if (!pProperties || *pPropertyCount > num_extensions) |
| 569 | *pPropertyCount = num_extensions; |
| 570 | if (pProperties) |
| 571 | std::copy(available, available + *pPropertyCount, pProperties); |
| 572 | |
| 573 | return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
| 574 | } |
| 575 | |
| 576 | } // namespace driver |
| 577 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 578 | } // namespace vulkan |