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