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 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 42 | // #define ENABLE_ALLOC_CALLSTACKS 1 |
| 43 | #if ENABLE_ALLOC_CALLSTACKS |
| 44 | #include <utils/CallStack.h> |
| 45 | #define ALOGD_CALLSTACK(...) \ |
| 46 | do { \ |
| 47 | ALOGD(__VA_ARGS__); \ |
| 48 | android::CallStack callstack; \ |
| 49 | callstack.update(); \ |
| 50 | callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \ |
| 51 | } while (false) |
| 52 | #else |
| 53 | #define ALOGD_CALLSTACK(...) \ |
| 54 | do { \ |
| 55 | } while (false) |
| 56 | #endif |
| 57 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 58 | using namespace vulkan; |
| 59 | |
| 60 | static const uint32_t kMaxPhysicalDevices = 4; |
| 61 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 62 | namespace { |
| 63 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 64 | // ---------------------------------------------------------------------------- |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 65 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 66 | // Standard-library allocator that delegates to VkAllocationCallbacks. |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 67 | // |
| 68 | // TODO(jessehall): This class currently always uses |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 69 | // 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] | 70 | // parameter or a constructor parameter. The former would help catch bugs |
| 71 | // where we use the wrong scope, e.g. adding a command-scope string to an |
| 72 | // 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] | 73 | template <class T> |
| 74 | class CallbackAllocator { |
| 75 | public: |
| 76 | typedef T value_type; |
| 77 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 78 | CallbackAllocator(const VkAllocationCallbacks* alloc_input) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 79 | : alloc(alloc_input) {} |
| 80 | |
| 81 | template <class T2> |
| 82 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 83 | : alloc(other.alloc) {} |
| 84 | |
| 85 | T* allocate(std::size_t n) { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 86 | void* mem = |
| 87 | alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T), |
| 88 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 89 | if (!mem) |
| 90 | throw std::bad_alloc(); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 91 | return static_cast<T*>(mem); |
| 92 | } |
| 93 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 94 | void deallocate(T* array, std::size_t /*n*/) noexcept { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 95 | alloc->pfnFree(alloc->pUserData, array); |
| 96 | } |
| 97 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 98 | const VkAllocationCallbacks* alloc; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 99 | }; |
| 100 | // These are needed in order to move Strings |
| 101 | template <class T> |
| 102 | bool operator==(const CallbackAllocator<T>& alloc1, |
| 103 | const CallbackAllocator<T>& alloc2) { |
| 104 | return alloc1.alloc == alloc2.alloc; |
| 105 | } |
| 106 | template <class T> |
| 107 | bool operator!=(const CallbackAllocator<T>& alloc1, |
| 108 | const CallbackAllocator<T>& alloc2) { |
| 109 | return !(alloc1 == alloc2); |
| 110 | } |
| 111 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 112 | template <class T> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 113 | using Vector = std::vector<T, CallbackAllocator<T>>; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 114 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 115 | typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>> |
| 116 | String; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 117 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 118 | // ---------------------------------------------------------------------------- |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 119 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 120 | VKAPI_ATTR void* DefaultAllocate(void*, |
| 121 | size_t size, |
| 122 | size_t alignment, |
| 123 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 124 | void* ptr = nullptr; |
| 125 | // Vulkan requires 'alignment' to be a power of two, but posix_memalign |
| 126 | // additionally requires that it be at least sizeof(void*). |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 127 | int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size); |
| 128 | ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment, |
| 129 | ret, ptr); |
| 130 | return ret == 0 ? ptr : nullptr; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 133 | VKAPI_ATTR void* DefaultReallocate(void*, |
| 134 | void* ptr, |
| 135 | size_t size, |
| 136 | size_t alignment, |
| 137 | VkSystemAllocationScope) { |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 138 | if (size == 0) { |
| 139 | free(ptr); |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | // TODO(jessehall): Right now we never shrink allocations; if the new |
| 144 | // request is smaller than the existing chunk, we just continue using it. |
| 145 | // Right now the loader never reallocs, so this doesn't matter. If that |
| 146 | // changes, or if this code is copied into some other project, this should |
| 147 | // probably have a heuristic to allocate-copy-free when doing so will save |
| 148 | // "enough" space. |
| 149 | size_t old_size = ptr ? malloc_usable_size(ptr) : 0; |
| 150 | if (size <= old_size) |
| 151 | return ptr; |
| 152 | |
| 153 | void* new_ptr = nullptr; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 154 | if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0) |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 155 | return nullptr; |
| 156 | if (ptr) { |
| 157 | memcpy(new_ptr, ptr, std::min(old_size, size)); |
| 158 | free(ptr); |
| 159 | } |
| 160 | return new_ptr; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 163 | VKAPI_ATTR void DefaultFree(void*, void* ptr) { |
| 164 | ALOGD_CALLSTACK("Free: %p", ptr); |
| 165 | free(ptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 168 | const VkAllocationCallbacks kDefaultAllocCallbacks = { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 169 | .pUserData = nullptr, |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 170 | .pfnAllocation = DefaultAllocate, |
| 171 | .pfnReallocation = DefaultReallocate, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 172 | .pfnFree = DefaultFree, |
| 173 | }; |
| 174 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 175 | // ---------------------------------------------------------------------------- |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 176 | // Global Data and Initialization |
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 | hwvulkan_device_t* g_hwdevice = nullptr; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 179 | InstanceExtensionSet g_driver_instance_extensions; |
| 180 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 181 | bool LoadVulkanHAL() { |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 182 | VkResult vkresult; |
| 183 | uint32_t count; |
| 184 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 185 | nullptr, &count, nullptr)) != VK_SUCCESS) { |
| 186 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 187 | vkresult); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 188 | return false; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 189 | } |
| 190 | VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>( |
| 191 | alloca(count * sizeof(VkExtensionProperties))); |
| 192 | if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties( |
| 193 | nullptr, &count, extensions)) != VK_SUCCESS) { |
| 194 | ALOGE("driver EnumerateInstanceExtensionProperties failed: %d", |
| 195 | vkresult); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 196 | return false; |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 197 | } |
| 198 | ALOGV_IF(count > 0, "Driver-supported instance extensions:"); |
| 199 | for (uint32_t i = 0; i < count; i++) { |
| 200 | ALOGV(" %s (v%u)", extensions[i].extensionName, |
| 201 | extensions[i].specVersion); |
| 202 | InstanceExtension id = |
| 203 | InstanceExtensionFromName(extensions[i].extensionName); |
| 204 | if (id != kInstanceExtensionCount) |
| 205 | g_driver_instance_extensions.set(id); |
| 206 | } |
| 207 | // Ignore driver attempts to support loader extensions |
| 208 | g_driver_instance_extensions.reset(kKHR_surface); |
| 209 | g_driver_instance_extensions.reset(kKHR_android_surface); |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 210 | |
| 211 | return true; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 214 | // ----------------------------------------------------------------------------- |
| 215 | |
| 216 | struct Instance { |
| 217 | Instance(const VkAllocationCallbacks* alloc_callbacks) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 218 | : base{{}, *alloc_callbacks}, |
| 219 | alloc(&base.allocator), |
| 220 | num_physical_devices(0) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 221 | memset(physical_devices, 0, sizeof(physical_devices)); |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 222 | enabled_extensions.reset(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 223 | memset(&drv.dispatch, 0, sizeof(drv.dispatch)); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 226 | ~Instance() {} |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 227 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 228 | driver::InstanceData base; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 229 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 230 | const VkAllocationCallbacks* alloc; |
| 231 | uint32_t num_physical_devices; |
Courtney Goeltzenleuchter | 84cd4c2 | 2016-03-16 12:20:13 -0600 | [diff] [blame] | 232 | VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 233 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 234 | DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices]; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 235 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 236 | DebugReportCallbackList debug_report_callbacks; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 237 | InstanceExtensionSet enabled_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 238 | |
| 239 | struct { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 240 | DriverDispatchTable dispatch; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 241 | } drv; // may eventually be an array |
| 242 | }; |
| 243 | |
| 244 | struct Device { |
| 245 | Device(Instance* instance_) |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 246 | : base{{}, *instance_->alloc}, instance(instance_) { |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 247 | enabled_extensions.reset(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 248 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 249 | |
| 250 | driver::DeviceData base; |
| 251 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 252 | Instance* instance; |
| 253 | PFN_vkGetDeviceProcAddr get_device_proc_addr; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 254 | DeviceExtensionSet enabled_extensions; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | template <typename THandle> |
| 258 | struct HandleTraits {}; |
| 259 | template <> |
| 260 | struct HandleTraits<VkInstance> { |
| 261 | typedef Instance LoaderObjectType; |
| 262 | }; |
| 263 | template <> |
| 264 | struct HandleTraits<VkPhysicalDevice> { |
| 265 | typedef Instance LoaderObjectType; |
| 266 | }; |
| 267 | template <> |
| 268 | struct HandleTraits<VkDevice> { |
| 269 | typedef Device LoaderObjectType; |
| 270 | }; |
| 271 | template <> |
| 272 | struct HandleTraits<VkQueue> { |
| 273 | typedef Device LoaderObjectType; |
| 274 | }; |
| 275 | template <> |
| 276 | struct HandleTraits<VkCommandBuffer> { |
| 277 | typedef Device LoaderObjectType; |
| 278 | }; |
| 279 | |
| 280 | template <typename THandle> |
| 281 | typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent( |
| 282 | THandle handle) { |
| 283 | // TODO(jessehall): Make Instance and Device POD types (by removing the |
| 284 | // non-default constructors), so that offsetof is actually legal to use. |
| 285 | // The specific case we're using here is safe in gcc/clang (and probably |
| 286 | // most other C++ compilers), but isn't guaranteed by C++. |
| 287 | typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType; |
| 288 | #pragma clang diagnostic push |
| 289 | #pragma clang diagnostic ignored "-Winvalid-offsetof" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 290 | const size_t kBaseOffset = offsetof(ObjectType, base); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 291 | #pragma clang diagnostic pop |
| 292 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 293 | const auto& base = driver::GetData(handle); |
| 294 | uintptr_t base_addr = reinterpret_cast<uintptr_t>(&base); |
| 295 | uintptr_t object_addr = base_addr - kBaseOffset; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 296 | return *reinterpret_cast<ObjectType*>(object_addr); |
| 297 | } |
| 298 | |
| 299 | // ----------------------------------------------------------------------------- |
| 300 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 301 | void DestroyDevice(Device* device, VkDevice vkdevice) { |
| 302 | const auto& instance = *device->instance; |
| 303 | |
| 304 | if (vkdevice != VK_NULL_HANDLE) |
| 305 | instance.drv.dispatch.DestroyDevice(vkdevice, instance.alloc); |
| 306 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 307 | device->~Device(); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 308 | instance.alloc->pfnFree(instance.alloc->pUserData, device); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 311 | /* |
| 312 | * This function will return the pNext pointer of any |
| 313 | * CreateInfo extensions that are not loader extensions. |
| 314 | * This is used to skip past the loader extensions prepended |
| 315 | * to the list during CreateInstance and CreateDevice. |
| 316 | */ |
| 317 | void* StripCreateExtensions(const void* pNext) { |
| 318 | VkLayerInstanceCreateInfo* create_info = |
| 319 | const_cast<VkLayerInstanceCreateInfo*>( |
| 320 | static_cast<const VkLayerInstanceCreateInfo*>(pNext)); |
| 321 | |
| 322 | while ( |
| 323 | create_info && |
| 324 | (create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO || |
| 325 | create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) { |
| 326 | create_info = const_cast<VkLayerInstanceCreateInfo*>( |
| 327 | static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext)); |
| 328 | } |
| 329 | |
| 330 | return create_info; |
| 331 | } |
| 332 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 333 | // Clean up and deallocate an Instance; called from both the failure paths in |
| 334 | // CreateInstance_Top as well as from DestroyInstance_Top. This function does |
| 335 | // not call down the dispatch chain; that should be done before calling this |
| 336 | // function, iff the lower vkCreateInstance call has been made and returned |
| 337 | // successfully. |
| 338 | void DestroyInstance(Instance* instance, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 339 | const VkAllocationCallbacks* allocator, |
| 340 | VkInstance vkinstance) { |
| 341 | if (vkinstance != VK_NULL_HANDLE && instance->drv.dispatch.DestroyInstance) |
| 342 | instance->drv.dispatch.DestroyInstance(vkinstance, allocator); |
| 343 | |
Jesse Hall | fee7143 | 2016-03-05 22:27:02 -0800 | [diff] [blame] | 344 | instance->~Instance(); |
| 345 | allocator->pfnFree(allocator->pUserData, instance); |
Courtney Goeltzenleuchter | e6e6968 | 2016-01-28 17:26:17 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 348 | } // anonymous namespace |
| 349 | |
| 350 | namespace vulkan { |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 351 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 352 | // ----------------------------------------------------------------------------- |
| 353 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 354 | // chain. |
| 355 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 356 | VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info, |
| 357 | const VkAllocationCallbacks* allocator, |
| 358 | VkInstance* vkinstance) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 359 | VkResult result; |
| 360 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 361 | if (!allocator) |
| 362 | allocator = &kDefaultAllocCallbacks; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 363 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 364 | void* instance_mem = allocator->pfnAllocation( |
| 365 | allocator->pUserData, sizeof(Instance), alignof(Instance), |
| 366 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 367 | if (!instance_mem) |
| 368 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 369 | Instance& instance = *new (instance_mem) Instance(allocator); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 370 | |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 371 | // Check that all enabled extensions are supported |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 372 | uint32_t num_driver_extensions = 0; |
| 373 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 374 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 375 | InstanceExtension id = InstanceExtensionFromName(name); |
| 376 | if (id != kInstanceExtensionCount) { |
| 377 | if (g_driver_instance_extensions[id]) { |
| 378 | num_driver_extensions++; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 379 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 380 | continue; |
| 381 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 382 | if (id == kKHR_surface || id == kKHR_android_surface) { |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 383 | instance.enabled_extensions.set(id); |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 384 | continue; |
| 385 | } |
Courtney Goeltzenleuchter | 6fecdd5 | 2016-02-03 15:14:46 -0700 | [diff] [blame] | 386 | // The loader natively supports debug report. |
| 387 | if (id == kEXT_debug_report) { |
| 388 | continue; |
| 389 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 390 | } |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 393 | VkInstanceCreateInfo driver_create_info = *create_info; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 394 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 395 | driver_create_info.enabledLayerCount = 0; |
| 396 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 397 | driver_create_info.enabledExtensionCount = 0; |
| 398 | driver_create_info.ppEnabledExtensionNames = nullptr; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 399 | if (num_driver_extensions > 0) { |
| 400 | const char** names = static_cast<const char**>( |
| 401 | alloca(num_driver_extensions * sizeof(char*))); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 402 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 403 | const char* name = create_info->ppEnabledExtensionNames[i]; |
| 404 | InstanceExtension id = InstanceExtensionFromName(name); |
| 405 | if (id != kInstanceExtensionCount) { |
| 406 | if (g_driver_instance_extensions[id]) { |
| 407 | names[driver_create_info.enabledExtensionCount++] = name; |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 408 | continue; |
| 409 | } |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | driver_create_info.ppEnabledExtensionNames = names; |
Jesse Hall | 4b62e4f | 2016-01-21 09:49:49 -0800 | [diff] [blame] | 413 | ALOG_ASSERT( |
| 414 | driver_create_info.enabledExtensionCount == num_driver_extensions, |
| 415 | "counted enabled driver instance extensions twice and got " |
| 416 | "different answers!"); |
Jesse Hall | 6bd5dfa | 2016-01-16 17:13:30 -0800 | [diff] [blame] | 417 | } |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 418 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 419 | VkInstance drv_instance; |
Jesse Hall | a7ac76d | 2016-01-08 22:29:42 -0800 | [diff] [blame] | 420 | result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc, |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 421 | &drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 422 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 423 | DestroyInstance(&instance, allocator, VK_NULL_HANDLE); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 424 | return result; |
| 425 | } |
| 426 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 427 | if (!driver::SetData(drv_instance, instance.base)) { |
| 428 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 429 | return VK_ERROR_INITIALIZATION_FAILED; |
| 430 | } |
| 431 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 432 | if (!LoadDriverDispatchTable(drv_instance, g_hwdevice->GetInstanceProcAddr, |
| 433 | instance.enabled_extensions, |
| 434 | instance.drv.dispatch)) { |
| 435 | DestroyInstance(&instance, allocator, drv_instance); |
Courtney Goeltzenleuchter | aa6c872 | 2016-01-29 08:57:16 -0700 | [diff] [blame] | 436 | return VK_ERROR_INITIALIZATION_FAILED; |
| 437 | } |
| 438 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 439 | uint32_t num_physical_devices = 0; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 440 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 441 | drv_instance, &num_physical_devices, nullptr); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 442 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 443 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 444 | return VK_ERROR_INITIALIZATION_FAILED; |
| 445 | } |
| 446 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 447 | result = instance.drv.dispatch.EnumeratePhysicalDevices( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 448 | drv_instance, &num_physical_devices, instance.physical_devices); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 449 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 450 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 451 | return VK_ERROR_INITIALIZATION_FAILED; |
| 452 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 453 | |
| 454 | Vector<VkExtensionProperties> extensions( |
| 455 | Vector<VkExtensionProperties>::allocator_type(instance.alloc)); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 456 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 457 | if (!driver::SetData(instance.physical_devices[i], instance.base)) { |
| 458 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 459 | return VK_ERROR_INITIALIZATION_FAILED; |
| 460 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 461 | |
| 462 | uint32_t count; |
| 463 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 464 | instance.physical_devices[i], nullptr, &count, nullptr)) != |
| 465 | VK_SUCCESS) { |
| 466 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 467 | result); |
| 468 | continue; |
| 469 | } |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 470 | try { |
| 471 | extensions.resize(count); |
| 472 | } catch (std::bad_alloc&) { |
| 473 | ALOGE("instance creation failed: out of memory"); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 474 | DestroyInstance(&instance, allocator, drv_instance); |
Jesse Hall | 26cecff | 2016-01-21 19:52:25 -0800 | [diff] [blame] | 475 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 476 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 477 | if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties( |
| 478 | instance.physical_devices[i], nullptr, &count, |
| 479 | extensions.data())) != VK_SUCCESS) { |
| 480 | ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i, |
| 481 | result); |
| 482 | continue; |
| 483 | } |
| 484 | ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i); |
| 485 | for (const auto& extension : extensions) { |
| 486 | ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion); |
| 487 | DeviceExtension id = |
| 488 | DeviceExtensionFromName(extension.extensionName); |
| 489 | if (id == kDeviceExtensionCount) { |
| 490 | ALOGW("driver gpu[%u] extension '%s' unknown to loader", i, |
| 491 | extension.extensionName); |
| 492 | } else { |
| 493 | instance.physical_device_driver_extensions[i].set(id); |
| 494 | } |
| 495 | } |
| 496 | // Ignore driver attempts to support loader extensions |
| 497 | instance.physical_device_driver_extensions[i].reset(kKHR_swapchain); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 498 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 499 | instance.num_physical_devices = num_physical_devices; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 500 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 501 | *vkinstance = drv_instance; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 502 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 503 | return VK_SUCCESS; |
| 504 | } |
| 505 | |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 506 | VkResult CreateAndroidSurfaceKHR_Disabled(VkInstance, |
| 507 | const VkAndroidSurfaceCreateInfoKHR*, |
| 508 | const VkAllocationCallbacks*, |
| 509 | VkSurfaceKHR*) { |
| 510 | ALOGE( |
| 511 | "VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not " |
| 512 | "executed."); |
| 513 | |
| 514 | return VK_SUCCESS; |
| 515 | } |
| 516 | |
| 517 | void DestroySurfaceKHR_Disabled(VkInstance, |
| 518 | VkSurfaceKHR, |
| 519 | const VkAllocationCallbacks*) { |
| 520 | ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed."); |
| 521 | } |
| 522 | |
| 523 | VkResult GetPhysicalDeviceSurfaceSupportKHR_Disabled(VkPhysicalDevice, |
| 524 | uint32_t, |
| 525 | VkSurfaceKHR, |
| 526 | VkBool32*) { |
| 527 | ALOGE( |
| 528 | "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not " |
| 529 | "executed."); |
| 530 | |
| 531 | return VK_SUCCESS; |
| 532 | } |
| 533 | |
| 534 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled( |
| 535 | VkPhysicalDevice, |
| 536 | VkSurfaceKHR, |
| 537 | VkSurfaceCapabilitiesKHR*) { |
| 538 | ALOGE( |
| 539 | "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceapabilitiesKHR " |
| 540 | "not executed."); |
| 541 | |
| 542 | return VK_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | VkResult GetPhysicalDeviceSurfaceFormatsKHR_Disabled(VkPhysicalDevice, |
| 546 | VkSurfaceKHR, |
| 547 | uint32_t*, |
| 548 | VkSurfaceFormatKHR*) { |
| 549 | ALOGE( |
| 550 | "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not " |
| 551 | "executed."); |
| 552 | |
| 553 | return VK_SUCCESS; |
| 554 | } |
| 555 | |
| 556 | VkResult GetPhysicalDeviceSurfacePresentModesKHR_Disabled(VkPhysicalDevice, |
| 557 | VkSurfaceKHR, |
| 558 | uint32_t*, |
| 559 | VkPresentModeKHR*) { |
| 560 | ALOGE( |
| 561 | "VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR " |
| 562 | "not executed."); |
| 563 | |
| 564 | return VK_SUCCESS; |
| 565 | } |
| 566 | |
| 567 | PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance vkinstance, |
| 568 | const char* name) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 569 | PFN_vkVoidFunction pfn; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 570 | |
| 571 | if (vkinstance) { |
| 572 | Instance& instance = GetDispatchParent(vkinstance); |
| 573 | if (!instance.enabled_extensions[kKHR_android_surface]) { |
| 574 | // KHR_android_surface is not enabled, use error stubs instead |
| 575 | if (strcmp(name, "vkCreateAndroidSurfaceKHR") == 0) { |
| 576 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 577 | CreateAndroidSurfaceKHR_Disabled); |
| 578 | } |
| 579 | } |
| 580 | if (!instance.enabled_extensions[kKHR_surface]) { |
| 581 | // KHR_surface is not enabled, use error stubs instead |
| 582 | if (strcmp(name, "vkDestroySurfaceKHR") == 0) { |
| 583 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 584 | DestroySurfaceKHR_Disabled); |
| 585 | } |
| 586 | if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0) { |
| 587 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 588 | GetPhysicalDeviceSurfaceSupportKHR_Disabled); |
| 589 | } |
| 590 | if (strcmp(name, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") == |
| 591 | 0) { |
| 592 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 593 | GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled); |
| 594 | } |
| 595 | if (strcmp(name, "vkGetPhysicalDeviceSurfaceFormatsKHR") == 0) { |
| 596 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 597 | GetPhysicalDeviceSurfaceFormatsKHR_Disabled); |
| 598 | } |
| 599 | if (strcmp(name, "vkGetPhysicalDeviceSurfacePresentModesKHR") == |
| 600 | 0) { |
| 601 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 602 | GetPhysicalDeviceSurfacePresentModesKHR_Disabled); |
| 603 | } |
| 604 | } |
| 605 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 606 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 607 | return pfn; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 608 | return g_hwdevice->GetInstanceProcAddr(vkinstance, name); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance, |
| 612 | uint32_t* pdev_count, |
| 613 | VkPhysicalDevice* pdevs) { |
| 614 | Instance& instance = GetDispatchParent(vkinstance); |
| 615 | uint32_t count = instance.num_physical_devices; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 616 | if (pdevs) { |
| 617 | count = std::min(count, *pdev_count); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 618 | std::copy(instance.physical_devices, instance.physical_devices + count, |
| 619 | pdevs); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 620 | } |
| 621 | *pdev_count = count; |
| 622 | return VK_SUCCESS; |
| 623 | } |
| 624 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 625 | void GetPhysicalDeviceProperties_Bottom( |
| 626 | VkPhysicalDevice pdev, |
| 627 | VkPhysicalDeviceProperties* properties) { |
| 628 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties( |
| 629 | pdev, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 632 | void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev, |
| 633 | VkPhysicalDeviceFeatures* features) { |
| 634 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev, |
| 635 | features); |
| 636 | } |
| 637 | |
| 638 | void GetPhysicalDeviceMemoryProperties_Bottom( |
| 639 | VkPhysicalDevice pdev, |
| 640 | VkPhysicalDeviceMemoryProperties* properties) { |
| 641 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties( |
| 642 | pdev, properties); |
| 643 | } |
| 644 | |
| 645 | void GetPhysicalDeviceQueueFamilyProperties_Bottom( |
| 646 | VkPhysicalDevice pdev, |
| 647 | uint32_t* pCount, |
| 648 | VkQueueFamilyProperties* properties) { |
| 649 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties( |
| 650 | pdev, pCount, properties); |
| 651 | } |
| 652 | |
| 653 | void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev, |
| 654 | VkFormat format, |
| 655 | VkFormatProperties* properties) { |
| 656 | GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 657 | pdev, format, properties); |
| 658 | } |
| 659 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 660 | VkResult GetPhysicalDeviceImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 661 | VkPhysicalDevice pdev, |
| 662 | VkFormat format, |
| 663 | VkImageType type, |
| 664 | VkImageTiling tiling, |
| 665 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 666 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 667 | VkImageFormatProperties* properties) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 668 | return GetDispatchParent(pdev) |
| 669 | .drv.dispatch.GetPhysicalDeviceImageFormatProperties( |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 670 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 673 | void GetPhysicalDeviceSparseImageFormatProperties_Bottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 674 | VkPhysicalDevice pdev, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 675 | VkFormat format, |
| 676 | VkImageType type, |
| 677 | VkSampleCountFlagBits samples, |
| 678 | VkImageUsageFlags usage, |
| 679 | VkImageTiling tiling, |
| 680 | uint32_t* properties_count, |
| 681 | VkSparseImageFormatProperties* properties) { |
| 682 | GetDispatchParent(pdev) |
| 683 | .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties( |
| 684 | pdev, format, type, samples, usage, tiling, properties_count, |
| 685 | properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 688 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 689 | VkResult EnumerateDeviceExtensionProperties_Bottom( |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 690 | VkPhysicalDevice pdev, |
| 691 | const char* layer_name, |
| 692 | uint32_t* properties_count, |
| 693 | VkExtensionProperties* properties) { |
| 694 | (void)layer_name; |
| 695 | |
| 696 | Instance& instance = GetDispatchParent(pdev); |
| 697 | |
| 698 | size_t gpu_idx = 0; |
| 699 | while (instance.physical_devices[gpu_idx] != pdev) |
| 700 | gpu_idx++; |
| 701 | const DeviceExtensionSet driver_extensions = |
| 702 | instance.physical_device_driver_extensions[gpu_idx]; |
| 703 | |
| 704 | // We only support VK_KHR_swapchain if the GPU supports |
| 705 | // VK_ANDROID_native_buffer |
| 706 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 707 | alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties))); |
| 708 | uint32_t num_extensions = 0; |
| 709 | if (driver_extensions[kANDROID_native_buffer]) { |
| 710 | available[num_extensions++] = VkExtensionProperties{ |
| 711 | VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION}; |
| 712 | } |
| 713 | |
| 714 | if (!properties || *properties_count > num_extensions) |
| 715 | *properties_count = num_extensions; |
| 716 | if (properties) |
| 717 | std::copy(available, available + *properties_count, properties); |
| 718 | |
| 719 | return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Courtney Goeltzenleuchter | 1cc0d37 | 2016-02-05 17:10:59 -0700 | [diff] [blame] | 722 | // This is a no-op, the Top function returns the aggregate layer property |
| 723 | // data. This is to keep the dispatch generator happy. |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 724 | VKAPI_ATTR |
Courtney Goeltzenleuchter | 1cc0d37 | 2016-02-05 17:10:59 -0700 | [diff] [blame] | 725 | VkResult EnumerateDeviceLayerProperties_Bottom( |
| 726 | VkPhysicalDevice /*pdev*/, |
| 727 | uint32_t* /*properties_count*/, |
| 728 | VkLayerProperties* /*properties*/) { |
| 729 | return VK_SUCCESS; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | VKAPI_ATTR |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 733 | VkResult CreateDevice_Bottom(VkPhysicalDevice gpu, |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 734 | const VkDeviceCreateInfo* create_info, |
| 735 | const VkAllocationCallbacks* allocator, |
| 736 | VkDevice* device_out) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 737 | Instance& instance = GetDispatchParent(gpu); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 738 | |
| 739 | // FIXME(jessehall): We don't have good conventions or infrastructure yet to |
| 740 | // do better than just using the instance allocator and scope for |
| 741 | // everything. See b/26732122. |
| 742 | if (true /*!allocator*/) |
| 743 | allocator = instance.alloc; |
| 744 | |
| 745 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device), |
| 746 | alignof(Device), |
| 747 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 748 | if (!mem) |
| 749 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 750 | Device* device = new (mem) Device(&instance); |
| 751 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 752 | size_t gpu_idx = 0; |
| 753 | while (instance.physical_devices[gpu_idx] != gpu) |
| 754 | gpu_idx++; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 755 | |
| 756 | VkDeviceCreateInfo driver_create_info = *create_info; |
| 757 | driver_create_info.pNext = StripCreateExtensions(create_info->pNext); |
| 758 | driver_create_info.enabledLayerCount = 0; |
| 759 | driver_create_info.ppEnabledLayerNames = nullptr; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 760 | |
| 761 | uint32_t num_driver_extensions = 0; |
| 762 | const char** driver_extensions = static_cast<const char**>( |
| 763 | alloca(create_info->enabledExtensionCount * sizeof(const char*))); |
| 764 | for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) { |
| 765 | const char* name = create_info->ppEnabledExtensionNames[i]; |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 766 | DeviceExtension id = DeviceExtensionFromName(name); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 767 | if (id != kDeviceExtensionCount) { |
| 768 | if (instance.physical_device_driver_extensions[gpu_idx][id]) { |
| 769 | driver_extensions[num_driver_extensions++] = name; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 770 | device->enabled_extensions.set(id); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 771 | continue; |
| 772 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 773 | // Add the VK_ANDROID_native_buffer extension to the list iff |
| 774 | // the VK_KHR_swapchain extension was requested |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 775 | if (id == kKHR_swapchain && |
| 776 | instance.physical_device_driver_extensions |
| 777 | [gpu_idx][kANDROID_native_buffer]) { |
| 778 | driver_extensions[num_driver_extensions++] = |
| 779 | VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME; |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 780 | device->enabled_extensions.set(id); |
Jesse Hall | ae3b70d | 2016-01-17 22:05:29 -0800 | [diff] [blame] | 781 | continue; |
| 782 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 783 | } |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 786 | driver_create_info.enabledExtensionCount = num_driver_extensions; |
| 787 | driver_create_info.ppEnabledExtensionNames = driver_extensions; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 788 | VkDevice drv_device; |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 789 | VkResult result = instance.drv.dispatch.CreateDevice( |
| 790 | gpu, &driver_create_info, allocator, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 791 | if (result != VK_SUCCESS) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 792 | DestroyDevice(device, VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 793 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 796 | if (!driver::SetData(drv_device, device->base)) { |
| 797 | DestroyDevice(device, drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 798 | return VK_ERROR_INITIALIZATION_FAILED; |
| 799 | } |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 800 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 801 | device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 802 | instance.drv.dispatch.GetDeviceProcAddr(drv_device, |
| 803 | "vkGetDeviceProcAddr")); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 804 | *device_out = drv_device; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 805 | return VK_SUCCESS; |
| 806 | } |
| 807 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 808 | void DestroyInstance_Bottom(VkInstance vkinstance, |
| 809 | const VkAllocationCallbacks* allocator) { |
| 810 | Instance& instance = GetDispatchParent(vkinstance); |
| 811 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 812 | VkAllocationCallbacks local_allocator; |
| 813 | if (!allocator) { |
| 814 | local_allocator = *instance.alloc; |
| 815 | allocator = &local_allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 816 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 817 | |
| 818 | DestroyInstance(&instance, allocator, vkinstance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 821 | VkResult CreateSwapchainKHR_Disabled(VkDevice, |
| 822 | const VkSwapchainCreateInfoKHR*, |
| 823 | const VkAllocationCallbacks*, |
| 824 | VkSwapchainKHR*) { |
| 825 | ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed."); |
| 826 | |
| 827 | return VK_SUCCESS; |
| 828 | } |
| 829 | |
| 830 | void DestroySwapchainKHR_Disabled(VkDevice, |
| 831 | VkSwapchainKHR, |
| 832 | const VkAllocationCallbacks*) { |
| 833 | ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed."); |
| 834 | } |
| 835 | |
| 836 | VkResult GetSwapchainImagesKHR_Disabled(VkDevice, |
| 837 | VkSwapchainKHR, |
| 838 | uint32_t*, |
| 839 | VkImage*) { |
| 840 | ALOGE( |
| 841 | "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed."); |
| 842 | |
| 843 | return VK_SUCCESS; |
| 844 | } |
| 845 | |
| 846 | VkResult AcquireNextImageKHR_Disabled(VkDevice, |
| 847 | VkSwapchainKHR, |
| 848 | uint64_t, |
| 849 | VkSemaphore, |
| 850 | VkFence, |
| 851 | uint32_t*) { |
| 852 | ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed."); |
| 853 | |
| 854 | return VK_SUCCESS; |
| 855 | } |
| 856 | |
| 857 | VkResult QueuePresentKHR_Disabled(VkQueue, const VkPresentInfoKHR*) { |
| 858 | ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed."); |
| 859 | |
| 860 | return VK_SUCCESS; |
| 861 | } |
| 862 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 863 | PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice, |
| 864 | const char* name) { |
| 865 | if (strcmp(name, "vkCreateDevice") == 0) { |
Courtney Goeltzenleuchter | a90ce61 | 2016-02-08 20:48:05 -0700 | [diff] [blame] | 866 | return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 867 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 868 | |
Courtney Goeltzenleuchter | a76e8ff | 2016-03-22 08:09:58 -0600 | [diff] [blame] | 869 | Device& device = GetDispatchParent(vkdevice); |
| 870 | if (!device.enabled_extensions[kKHR_swapchain]) { |
| 871 | if (strcmp(name, "vkCreateSwapchainKHR") == 0) { |
| 872 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 873 | CreateSwapchainKHR_Disabled); |
| 874 | } |
| 875 | if (strcmp(name, "vkDestroySwapchainKHR") == 0) { |
| 876 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 877 | DestroySwapchainKHR_Disabled); |
| 878 | } |
| 879 | if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) { |
| 880 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 881 | GetSwapchainImagesKHR_Disabled); |
| 882 | } |
| 883 | if (strcmp(name, "vkAcquireNextSwapchainImageKHR") == 0) { |
| 884 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 885 | AcquireNextImageKHR_Disabled); |
| 886 | } |
| 887 | if (strcmp(name, "vkQueuePresentKHR") == 0) { |
| 888 | return reinterpret_cast<PFN_vkVoidFunction>( |
| 889 | QueuePresentKHR_Disabled); |
| 890 | } |
| 891 | } |
| 892 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 893 | // VK_ANDROID_native_buffer should be hidden from applications and layers. |
| 894 | // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr. |
| 895 | PFN_vkVoidFunction pfn; |
| 896 | if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 || |
| 897 | strcmp(name, "vkAcquireImageANDROID") == 0 || |
| 898 | strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) { |
| 899 | return nullptr; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 900 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 901 | if ((pfn = GetLoaderBottomProcAddr(name))) |
| 902 | return pfn; |
| 903 | return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 906 | void DestroyDevice_Bottom(VkDevice vkdevice, const VkAllocationCallbacks*) { |
| 907 | DestroyDevice(&GetDispatchParent(vkdevice), vkdevice); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 910 | void GetDeviceQueue_Bottom(VkDevice vkdevice, |
| 911 | uint32_t family, |
| 912 | uint32_t index, |
| 913 | VkQueue* queue_out) { |
| 914 | const auto& device = GetDispatchParent(vkdevice); |
| 915 | const auto& instance = *device.instance; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 916 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 917 | instance.drv.dispatch.GetDeviceQueue(vkdevice, family, index, queue_out); |
| 918 | driver::SetData(*queue_out, device.base); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 921 | VkResult AllocateCommandBuffers_Bottom( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 922 | VkDevice vkdevice, |
| 923 | const VkCommandBufferAllocateInfo* alloc_info, |
| 924 | VkCommandBuffer* cmdbufs) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 925 | const auto& device = GetDispatchParent(vkdevice); |
| 926 | const auto& instance = *device.instance; |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 927 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 928 | VkResult result = instance.drv.dispatch.AllocateCommandBuffers( |
| 929 | vkdevice, alloc_info, cmdbufs); |
| 930 | if (result == VK_SUCCESS) { |
| 931 | for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) |
| 932 | driver::SetData(cmdbufs[i], device.base); |
| 933 | } |
| 934 | |
| 935 | return result; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 938 | // ----------------------------------------------------------------------------- |
| 939 | |
| 940 | const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) { |
| 941 | return GetDispatchParent(vkinstance).alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 944 | const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) { |
| 945 | return GetDispatchParent(vkdevice).instance->alloc; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 948 | VkInstance GetDriverInstance(VkInstance instance) { |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 949 | return instance; |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | const DriverDispatchTable& GetDriverDispatch(VkInstance instance) { |
| 953 | return GetDispatchParent(instance).drv.dispatch; |
| 954 | } |
| 955 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 956 | const DriverDispatchTable& GetDriverDispatch(VkDevice device) { |
| 957 | return GetDispatchParent(device).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 960 | const DriverDispatchTable& GetDriverDispatch(VkQueue queue) { |
| 961 | return GetDispatchParent(queue).instance->drv.dispatch; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 964 | DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) { |
| 965 | return GetDispatchParent(instance).debug_report_callbacks; |
| 966 | } |
| 967 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 968 | bool InitLoader(hwvulkan_device_t* dev) { |
| 969 | if (!g_hwdevice) { |
| 970 | g_hwdevice = dev; |
| 971 | if (!LoadVulkanHAL()) |
| 972 | g_hwdevice = nullptr; |
| 973 | } |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 974 | |
| 975 | return (g_hwdevice != nullptr); |
| 976 | } |
| 977 | |
Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame^] | 978 | namespace driver { |
| 979 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 980 | const VkAllocationCallbacks& GetDefaultAllocator() { |
| 981 | return kDefaultAllocCallbacks; |
| 982 | } |
| 983 | |
| 984 | PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) { |
| 985 | return GetInstanceProcAddr_Bottom(instance, pName); |
| 986 | } |
| 987 | |
| 988 | PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) { |
| 989 | return GetDeviceProcAddr_Bottom(device, pName); |
| 990 | } |
| 991 | |
| 992 | VkResult EnumerateInstanceExtensionProperties( |
| 993 | const char* pLayerName, |
| 994 | uint32_t* pPropertyCount, |
| 995 | VkExtensionProperties* pProperties) { |
| 996 | (void)pLayerName; |
| 997 | |
| 998 | VkExtensionProperties* available = static_cast<VkExtensionProperties*>( |
| 999 | alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties))); |
| 1000 | uint32_t num_extensions = 0; |
| 1001 | |
| 1002 | available[num_extensions++] = VkExtensionProperties{ |
| 1003 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION}; |
| 1004 | available[num_extensions++] = |
| 1005 | VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, |
| 1006 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}; |
| 1007 | if (g_driver_instance_extensions[kEXT_debug_report]) { |
| 1008 | available[num_extensions++] = |
| 1009 | VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 1010 | VK_EXT_DEBUG_REPORT_SPEC_VERSION}; |
| 1011 | } |
| 1012 | |
| 1013 | if (!pProperties || *pPropertyCount > num_extensions) |
| 1014 | *pPropertyCount = num_extensions; |
| 1015 | if (pProperties) |
| 1016 | std::copy(available, available + *pPropertyCount, pProperties); |
| 1017 | |
| 1018 | return *pPropertyCount < num_extensions ? VK_INCOMPLETE : VK_SUCCESS; |
| 1019 | } |
| 1020 | |
| 1021 | } // namespace driver |
| 1022 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1023 | } // namespace vulkan |