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 | memset(&drv.dispatch, 0, sizeof(drv.dispatch)); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 153 | ~Instance() {} |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 154 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 155 | driver::InstanceData base; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 156 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 157 | const VkAllocationCallbacks* alloc; |
| 158 | uint32_t num_physical_devices; |
Courtney Goeltzenleuchter | 84cd4c2 | 2016-03-16 12:20:13 -0600 | [diff] [blame] | 159 | VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 160 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 161 | DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 162 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 163 | DebugReportCallbackList debug_report_callbacks; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 164 | InstanceExtensionSet enabled_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 165 | |
| 166 | struct { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 167 | DriverDispatchTable dispatch; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 168 | } drv; // may eventually be an array |
| 169 | }; |
| 170 | |
| 171 | struct Device { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 172 | Device(Instance* instance_) : base(*instance_->alloc), instance(instance_) { |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 173 | enabled_extensions.reset(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 174 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 175 | |
| 176 | driver::DeviceData base; |
| 177 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 178 | Instance* instance; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 179 | DeviceExtensionSet enabled_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | template <typename THandle> |
| 183 | struct HandleTraits {}; |
| 184 | template <> |
| 185 | struct HandleTraits<VkInstance> { |
| 186 | typedef Instance LoaderObjectType; |
| 187 | }; |
| 188 | template <> |
| 189 | struct HandleTraits<VkPhysicalDevice> { |
| 190 | typedef Instance LoaderObjectType; |
| 191 | }; |
| 192 | template <> |
| 193 | struct HandleTraits<VkDevice> { |
| 194 | typedef Device LoaderObjectType; |
| 195 | }; |
| 196 | template <> |
| 197 | struct HandleTraits<VkQueue> { |
| 198 | typedef Device LoaderObjectType; |
| 199 | }; |
| 200 | template <> |
| 201 | struct HandleTraits<VkCommandBuffer> { |
| 202 | typedef Device LoaderObjectType; |
| 203 | }; |
| 204 | |
| 205 | template <typename THandle> |
| 206 | typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent( |
| 207 | THandle handle) { |
| 208 | // TODO(jessehall): Make Instance and Device POD types (by removing the |
| 209 | // non-default constructors), so that offsetof is actually legal to use. |
| 210 | // The specific case we're using here is safe in gcc/clang (and probably |
| 211 | // most other C++ compilers), but isn't guaranteed by C++. |
| 212 | typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType; |
| 213 | #pragma clang diagnostic push |
| 214 | #pragma clang diagnostic ignored "-Winvalid-offsetof" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 215 | const size_t kBaseOffset = offsetof(ObjectType, base); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 216 | #pragma clang diagnostic pop |
| 217 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 218 | const auto& base = driver::GetData(handle); |
| 219 | uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base); |
| 220 | uintptr_t object_addr = base_addr - kBaseOffset; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 221 | return *reinterpret_cast<ObjectType*>(object_addr); |
| 222 | } |
| 223 | |
| 224 | // ----------------------------------------------------------------------------- |
| 225 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 226 | void DestroyDevice(Device* device, VkDevice vkdevice) { |
| 227 | const auto& instance = *device->instance; |
| 228 | |
| 229 | if (vkdevice != VK_NULL_HANDLE) |
| 230 | instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc); |
| 231 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 232 | device->~Device(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 233 | instance.alloc->pfnFree(instance.alloc->pUserData, device); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 236 | /* |
| 237 | * This function will return the pNext pointer of any |
| 238 | * CreateInfo extensions that are not loader extensions. |
| 239 | * This is used to skip past the loader extensions prepended |
| 240 | * to the list during CreateInstance and CreateDevice. |
| 241 | */ |
| 242 | void* StripCreateExtensions(const void* pNext) { |
| 243 | VkLayerInstanceCreateInfo* create_info = |
| 244 | const_cast<VkLayerInstanceCreateInfo*>( |
| 245 | static_cast<const VkLayerInstanceCreateInfo*>(pNext)); |
| 246 | |
| 247 | while ( |
| 248 | create_info && |
| 249 | (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO || |
| 250 | create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) { |
| 251 | create_info = const_cast<VkLayerInstanceCreateInfo*>( |
| 252 | static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext)); |
| 253 | } |
| 254 | |
| 255 | return create_info; |
| 256 | } |
| 257 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 258 | // Clean up and deallocate an Instance; called from both the failure paths in |
| 259 | // CreateInstance_Top as well as from DestroyInstance_Top. This function does |
| 260 | // not call down the dispatch chain; that should be done before calling this |
| 261 | // function, iff the lower vkCreateInstance call has been made and returned |
| 262 | // successfully. |
| 263 | void DestroyInstance(Instance* instance, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 264 | const VkAllocationCallbacks* allocator, |
| 265 | VkInstance vkinstance) { |
| 266 | if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance) |
| 267 | instance->drv.dispatch.DestroyInstance(vkinstance, allocator); |
| 268 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 269 | instance->~Instance(); |
| 270 | allocator->pfnFree(allocator->pUserData, instance); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 273 | driver::ProcHook::Extension InstanceExtensionToProcHookExtension( |
| 274 | InstanceExtension id) { |
| 275 | switch (id) { |
| 276 | case kKHR_surface: |
| 277 | return driver::ProcHook::KHR_surface; |
| 278 | case kKHR_android_surface: |
| 279 | return driver::ProcHook::KHR_android_surface; |
| 280 | case kEXT_debug_report: |
| 281 | return driver::ProcHook::EXT_debug_report; |
| 282 | default: |
| 283 | return driver::ProcHook::EXTENSION_UNKNOWN; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | driver::ProcHook::Extension DeviceExtensionToProcHookExtension( |
| 288 | DeviceExtension id) { |
| 289 | switch (id) { |
| 290 | case kKHR_swapchain: |
| 291 | return driver::ProcHook::KHR_swapchain; |
| 292 | case kANDROID_native_buffer: |
| 293 | return driver::ProcHook::ANDROID_native_buffer; |
| 294 | default: |
| 295 | return driver::ProcHook::EXTENSION_UNKNOWN; |
| 296 | } |
| 297 | } |
| 298 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 299 | } // anonymous namespace |
| 300 | |
| 301 | namespace vulkan { |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 302 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 303 | // ----------------------------------------------------------------------------- |
| 304 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 305 | // chain. |
| 306 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 307 | VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info, |
| 308 | const VkAllocationCallbacks* allocator, |
| 309 | VkInstance* vkinstance) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 310 | VkResult result; |
| 311 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 312 | if (!allocator) |
Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 313 | allocator = &driver::GetDefaultAllocator(); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 314 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 315 | void* instance_mem = allocator->pfnAllocation( |
| 316 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 317 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 318 | if (!instance_mem) |
| 319 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 320 | Instance& instance = *new (instance_mem) Instance(allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 321 | |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 322 | // Check that all enabled extensions are supported |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 323 | uint32_t num_driver_extensions = 0; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 324 | bool enable_kEXT_debug_report = false; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 325 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 326 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 327 | InstanceExtension id = InstanceExtensionFromName(name); |
| 328 | if (id != kInstanceExtensionCount) { |
| 329 | if (g_driver_instance_extensions[id]) { |
| 330 | num_driver_extensions++; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 331 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 332 | continue; |
| 333 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 334 | if (id == kKHR_surface || id == kKHR_android_surface) { |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 335 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 336 | continue; |
| 337 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 338 | // The loader natively supports debug report. |
| 339 | if (id == kEXT_debug_report) { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 340 | enable_kEXT_debug_report = true; |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 341 | continue; |
| 342 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 343 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 346 | auto& hal_exts = instance.base.hal_extensions; |
| 347 | for (size_t i = 0; i < instance.enabled_extensions.size(); i++) { |
| 348 | if (instance.enabled_extensions[i]) { |
| 349 | auto bit = InstanceExtensionToProcHookExtension( |
| 350 | static_cast<InstanceExtension>(i)); |
| 351 | if (bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 352 | hal_exts.set(bit); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | auto& hook_exts = instance.base.hook_extensions; |
| 357 | hook_exts = hal_exts; |
| 358 | if (enable_kEXT_debug_report) |
| 359 | hook_exts.set(driver::ProcHook::EXT_debug_report); |
| 360 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 361 | VkInstanceCreateInfo driver_create_info = *create_info; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 362 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 363 | driver_create_info.enabledLayerCount = 0; |
| 364 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 365 | driver_create_info.enabledExtensionCount = 0; |
| 366 | driver_create_info.ppEnabledExtensionNames = nullptr; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 367 | if (num_driver_extensions > 0) { |
| 368 | const char** names = static_cast<const char**>( |
| 369 | alloca(num_driver_extensions * sizeof(char*))); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 370 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 371 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 372 | InstanceExtension id = InstanceExtensionFromName(name); |
| 373 | if (id != kInstanceExtensionCount) { |
| 374 | if (g_driver_instance_extensions[id]) { |
| 375 | names[driver_create_info.enabledExtensionCount++] = name; |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 376 | continue; |
| 377 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | driver_create_info.ppEnabledExtensionNames = names; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 381 | ALOG_ASSERT( |
| 382 | driver_create_info.enabledExtensionCount == num_driver_extensions, |
| 383 | "counted enabled driver instance extensions twice and got " |
| 384 | "different answers!"); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 385 | } |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 386 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 387 | VkInstance drv_instance; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 388 | result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 389 | &drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 390 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 391 | DestroyInstance(&instance, allocator, VK_NULL_HANDLE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 395 | if (!driver::SetData(drv_instance, 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 | } |
| 399 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 400 | if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr, |
| 401 | instance.enabled_extensions, |
| 402 | instance.drv.dispatch)) { |
| 403 | DestroyInstance(&instance, allocator, drv_instance); |
Courtney Goeltzenleuchter | aa6c872 | 2016-01-29 08:57:16 -0700 | [diff] [blame] | 404 | return VK_ERROR_INITIALIZATION_FAILED; |
| 405 | } |
| 406 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 407 | uint32_t num_physical_devices = 0; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 408 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 409 | drv_instance, &num_physical_devices, nullptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 410 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 411 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 412 | return VK_ERROR_INITIALIZATION_FAILED; |
| 413 | } |
| 414 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 415 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 416 | drv_instance, &num_physical_devices, instance.physical_devices); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 417 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 418 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 419 | return VK_ERROR_INITIALIZATION_FAILED; |
| 420 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 421 | |
| 422 | Vector<VkExtensionProperties> extensions( |
| 423 | Vector<VkExtensionProperties>::allocator_type(instance.alloc)); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 424 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 425 | if (!driver::SetData(instance.physical_devices[i], instance.base)) { |
| 426 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 427 | return VK_ERROR_INITIALIZATION_FAILED; |
| 428 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 429 | |
| 430 | uint32_t count; |
| 431 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 432 | instance.physical_devices[i], nullptr, &count, nullptr)) != |
| 433 | VK_SUCCESS) { |
| 434 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 435 | result); |
| 436 | continue; |
| 437 | } |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 438 | try { |
| 439 | extensions.resize(count); |
| 440 | } catch (std::bad_alloc&) { |
| 441 | ALOGE("instance creation failed: out of memory"); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 442 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 443 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 444 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 445 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 446 | instance.physical_devices[i], nullptr, &count, |
| 447 | extensions.data())) != VK_SUCCESS) { |
| 448 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 449 | result); |
| 450 | continue; |
| 451 | } |
| 452 | ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i); |
| 453 | for (const auto& extension : extensions) { |
| 454 | ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion); |
| 455 | DeviceExtension id = |
| 456 | DeviceExtensionFromName(extension.extensionName); |
| 457 | if (id == kDeviceExtensionCount) { |
| 458 | ALOGW("driver gpu[%u] extension '%s' unknown to loader", i, |
| 459 | extension.extensionName); |
| 460 | } else { |
| 461 | instance.physical_device_driver_extensions[i].set(id); |
| 462 | } |
| 463 | } |
| 464 | // Ignore driver attempts to support loader extensions |
| 465 | instance.physical_device_driver_extensions[i].reset(kKHR_swapchain); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 466 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 467 | instance.num_physical_devices = num_physical_devices; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 468 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 469 | *vkinstance = drv_instance; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 470 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 471 | return VK_SUCCESS; |
| 472 | } |
| 473 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 474 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 475 | uint32_t* pdev_count, |
| 476 | VkPhysicalDevice* pdevs) { |
| 477 | Instance& instance = GetDispatchParent(vkinstance); |
| 478 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 479 | if (pdevs) { |
| 480 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 481 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 482 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 483 | } |
| 484 | *pdev_count = count; |
| 485 | return VK_SUCCESS; |
| 486 | } |
| 487 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 488 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 489 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 490 | VkPhysicalDevice pdev, |
| 491 | const char* layer_name, |
| 492 | uint32_t* properties_count, |
| 493 | VkExtensionProperties* properties) { |
| 494 | (void)layer_name; |
| 495 | |
| 496 | Instance& instance = GetDispatchParent(pdev); |
| 497 | |
| 498 | size_t gpu_idx = 0; |
| 499 | while (instance.physical_devices[gpu_idx] != pdev) |
| 500 | gpu_idx++; |
| 501 | const DeviceExtensionSet driver_extensions = |
| 502 | instance.physical_device_driver_extensions[gpu_idx]; |
| 503 | |
| 504 | // We only support VK_KHR_swapchain if the GPU supports |
| 505 | // VK_ANDROID_native_buffer |
| 506 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 507 | alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties))); |
| 508 | uint32_t num_extensions = 0; |
| 509 | if (driver_extensions[kANDROID_native_buffer]) { |
| 510 | available[num_extensions++] = VkExtensionProperties{ |
| 511 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION}; |
| 512 | } |
| 513 | |
| 514 | if (!properties || *properties_count > num_extensions) |
| 515 | *properties_count = num_extensions; |
| 516 | if (properties) |
| 517 | std::copy(available, available + *properties_count, properties); |
| 518 | |
| 519 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 522 | VKAPI_ATTR |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 523 | VkResult CreateDevice_Bottom(VkPhysicalDevice gpu, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 524 | const VkDeviceCreateInfo* create_info, |
| 525 | const VkAllocationCallbacks* allocator, |
| 526 | VkDevice* device_out) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 527 | Instance& instance = GetDispatchParent(gpu); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 528 | |
| 529 | // FIXME(jessehall): We don't have good conventions or infrastructure yet to |
| 530 | // do better than just using the instance allocator and scope for |
| 531 | // everything. See b/26732122. |
| 532 | if (true /*!allocator*/) |
| 533 | allocator = instance.alloc; |
| 534 | |
| 535 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device), |
| 536 | alignof(Device), |
| 537 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 538 | if (!mem) |
| 539 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 540 | Device* device = new (mem) Device(&instance); |
| 541 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 542 | size_t gpu_idx = 0; |
| 543 | while (instance.physical_devices[gpu_idx] != gpu) |
| 544 | gpu_idx++; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 545 | |
| 546 | VkDeviceCreateInfo driver_create_info = *create_info; |
| 547 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
| 548 | driver_create_info.enabledLayerCount = 0; |
| 549 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 550 | |
| 551 | uint32_t num_driver_extensions = 0; |
| 552 | const char** driver_extensions = static_cast<const char**>( |
| 553 | alloca(create_info->enabledExtensionCount * sizeof(const char*))); |
| 554 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 555 | const char* name = create_info->ppEnabledExtensionNames[i]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 556 | DeviceExtension id = DeviceExtensionFromName(name); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 557 | if (id != kDeviceExtensionCount) { |
| 558 | if (instance.physical_device_driver_extensions[gpu_idx][id]) { |
| 559 | driver_extensions[num_driver_extensions++] = name; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 560 | device->enabled_extensions.set(id); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 561 | continue; |
| 562 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 563 | // Add the VK_ANDROID_native_buffer extension to the list iff |
| 564 | // the VK_KHR_swapchain extension was requested |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 565 | if (id == kKHR_swapchain && |
| 566 | instance.physical_device_driver_extensions |
| 567 | [gpu_idx][kANDROID_native_buffer]) { |
| 568 | driver_extensions[num_driver_extensions++] = |
| 569 | VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 570 | device->enabled_extensions.set(id); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 571 | continue; |
| 572 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 573 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 576 | // Unlike instance->enabled_extensions, device->enabled_extensions maps to |
| 577 | // hook extensions. |
| 578 | auto& hook_exts = device->base.hook_extensions; |
| 579 | for (size_t i = 0; i < device->enabled_extensions.size(); i++) { |
| 580 | if (device->enabled_extensions[i]) { |
| 581 | auto bit = DeviceExtensionToProcHookExtension( |
| 582 | static_cast<DeviceExtension>(i)); |
| 583 | if (bit != driver::ProcHook::EXTENSION_UNKNOWN) |
| 584 | hook_exts.set(bit); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | auto& hal_exts = device->base.hal_extensions; |
| 589 | hal_exts = hook_exts; |
| 590 | // map VK_KHR_swapchain to VK_ANDROID_native_buffer |
| 591 | if (hal_exts[driver::ProcHook::KHR_swapchain]) { |
| 592 | hal_exts.reset(driver::ProcHook::KHR_swapchain); |
| 593 | hal_exts.set(driver::ProcHook::ANDROID_native_buffer); |
| 594 | } |
| 595 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 596 | driver_create_info.enabledExtensionCount = num_driver_extensions; |
| 597 | driver_create_info.ppEnabledExtensionNames = driver_extensions; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 598 | VkDevice drv_device; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 599 | VkResult result = instance.drv.dispatch.CreateDevice( |
| 600 | gpu, &driver_create_info, allocator, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 601 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 602 | DestroyDevice(device, VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 603 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 606 | if (!driver::SetData(drv_device, device->base)) { |
| 607 | DestroyDevice(device, drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 608 | return VK_ERROR_INITIALIZATION_FAILED; |
| 609 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 610 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame^] | 611 | device->base.get_device_proc_addr = |
| 612 | reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 613 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 614 | "vkGetDeviceProcAddr")); |
| 615 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 616 | *device_out = drv_device; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 617 | return VK_SUCCESS; |
| 618 | } |
| 619 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 620 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 621 | const VkAllocationCallbacks* allocator) { |
| 622 | Instance& instance = GetDispatchParent(vkinstance); |
| 623 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 624 | VkAllocationCallbacks local_allocator; |
| 625 | if (!allocator) { |
| 626 | local_allocator = *instance.alloc; |
| 627 | allocator = &local_allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 628 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 629 | |
| 630 | DestroyInstance(&instance, allocator, vkinstance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 633 | void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) { |
| 634 | DestroyDevice(&GetDispatchParent(vkdevice), vkdevice); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 637 | void GetDeviceQueue_Bottom(VkDevice vkdevice, |
| 638 | uint32_t family, |
| 639 | uint32_t index, |
| 640 | VkQueue* queue_out) { |
| 641 | const auto& device = GetDispatchParent(vkdevice); |
| 642 | const auto& instance = *device.instance; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 643 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 644 | instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out); |
| 645 | driver::SetData(*queue_out, device.base); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 648 | VkResult AllocateCommandBuffers_Bottom( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 649 | VkDevice vkdevice, |
| 650 | const VkCommandBufferAllocateInfo* alloc_info, |
| 651 | VkCommandBuffer* cmdbufs) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 652 | const auto& device = GetDispatchParent(vkdevice); |
| 653 | const auto& instance = *device.instance; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 654 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 655 | VkResult result = instance.drv.dispatch.AllocateCommandBuffers( |
| 656 | vkdevice, alloc_info, cmdbufs); |
| 657 | if (result == VK_SUCCESS) { |
| 658 | for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) |
| 659 | driver::SetData(cmdbufs[i], device.base); |
| 660 | } |
| 661 | |
| 662 | return result; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 665 | // ----------------------------------------------------------------------------- |
| 666 | |
| 667 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 668 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 671 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
| 672 | return GetDispatchParent(vkdevice).instance->alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 675 | VkInstance GetDriverInstance(VkInstance instance) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 676 | return instance; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | const DriverDispatchTable& GetDriverDispatch(VkInstance instance) { |
| 680 | return GetDispatchParent(instance).drv.dispatch; |
| 681 | } |
| 682 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 683 | const DriverDispatchTable& GetDriverDispatch(VkDevice device) { |
| 684 | return GetDispatchParent(device).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 687 | const DriverDispatchTable& GetDriverDispatch(VkQueue queue) { |
| 688 | return GetDispatchParent(queue).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 691 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 692 | return GetDispatchParent(instance).debug_report_callbacks; |
| 693 | } |
| 694 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 695 | bool InitLoader(hwvulkan_device_t* dev) { |
| 696 | if (!g_hwdevice) { |
| 697 | g_hwdevice = dev; |
| 698 | if (!LoadVulkanHAL()) |
| 699 | g_hwdevice = nullptr; |
| 700 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 701 | |
| 702 | return (g_hwdevice != nullptr); |
| 703 | } |
| 704 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 705 | namespace driver { |
| 706 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 707 | VkResult EnumerateInstanceExtensionProperties( |
| 708 | const char* pLayerName, |
| 709 | uint32_t* pPropertyCount, |
| 710 | VkExtensionProperties* pProperties) { |
| 711 | (void)pLayerName; |
| 712 | |
| 713 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 714 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 715 | uint32_t num_extensions = 0; |
| 716 | |
| 717 | available[num_extensions++] = VkExtensionProperties{ |
| 718 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 719 | available[num_extensions++] = |
| 720 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 721 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 722 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 723 | available[num_extensions++] = |
| 724 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 725 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 726 | } |
| 727 | |
| 728 | if (!pProperties || *pPropertyCount > num_extensions) |
| 729 | *pPropertyCount = num_extensions; |
| 730 | if (pProperties) |
| 731 | std::copy(available, available + *pPropertyCount, pProperties); |
| 732 | |
| 733 | return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
| 734 | } |
| 735 | |
| 736 | } // namespace driver |
| 737 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 738 | } // namespace vulkan |