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 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 19 | // module header |
| 20 | #include "loader.h" |
| 21 | // standard C headers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <dlfcn.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
| 25 | #include <malloc.h> |
| 26 | #include <pthread.h> |
| 27 | #include <string.h> |
| 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 | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 39 | #include <vulkan/vk_debug_report_lunarg.h> |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 40 | #include <vulkan/vulkan_loader_data.h> |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace vulkan; |
| 43 | |
| 44 | static const uint32_t kMaxPhysicalDevices = 4; |
| 45 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | // These definitions are taken from the LunarG Vulkan Loader. They are used to |
| 49 | // enforce compatability between the Loader and Layers. |
| 50 | typedef void* (*PFN_vkGetProcAddr)(void* obj, const char* pName); |
| 51 | |
| 52 | typedef struct VkLayerLinkedListElem_ { |
| 53 | PFN_vkGetProcAddr get_proc_addr; |
| 54 | void* next_element; |
| 55 | void* base_object; |
| 56 | } VkLayerLinkedListElem; |
| 57 | |
| 58 | // Define Handle typedef to be void* as returned from dlopen. |
| 59 | typedef void* SharedLibraryHandle; |
| 60 | |
| 61 | // Custom versions of std classes that use the vulkan alloc callback. |
| 62 | template <class T> |
| 63 | class CallbackAllocator { |
| 64 | public: |
| 65 | typedef T value_type; |
| 66 | |
| 67 | CallbackAllocator(const VkAllocCallbacks* alloc_input) |
| 68 | : alloc(alloc_input) {} |
| 69 | |
| 70 | template <class T2> |
| 71 | CallbackAllocator(const CallbackAllocator<T2>& other) |
| 72 | : alloc(other.alloc) {} |
| 73 | |
| 74 | T* allocate(std::size_t n) { |
| 75 | void* mem = alloc->pfnAlloc(alloc->pUserData, n * sizeof(T), alignof(T), |
| 76 | VK_SYSTEM_ALLOC_TYPE_INTERNAL); |
| 77 | return static_cast<T*>(mem); |
| 78 | } |
| 79 | |
| 80 | void deallocate(T* array, std::size_t /*n*/) { |
| 81 | alloc->pfnFree(alloc->pUserData, array); |
| 82 | } |
| 83 | |
| 84 | const VkAllocCallbacks* alloc; |
| 85 | }; |
| 86 | // These are needed in order to move Strings |
| 87 | template <class T> |
| 88 | bool operator==(const CallbackAllocator<T>& alloc1, |
| 89 | const CallbackAllocator<T>& alloc2) { |
| 90 | return alloc1.alloc == alloc2.alloc; |
| 91 | } |
| 92 | template <class T> |
| 93 | bool operator!=(const CallbackAllocator<T>& alloc1, |
| 94 | const CallbackAllocator<T>& alloc2) { |
| 95 | return !(alloc1 == alloc2); |
| 96 | } |
| 97 | |
| 98 | template <class Key, |
| 99 | class T, |
| 100 | class Hash = std::hash<Key>, |
| 101 | class Pred = std::equal_to<Key> > |
| 102 | using UnorderedMap = |
| 103 | std::unordered_map<Key, |
| 104 | T, |
| 105 | Hash, |
| 106 | Pred, |
| 107 | CallbackAllocator<std::pair<const Key, T> > >; |
| 108 | |
| 109 | template <class T> |
| 110 | using Vector = std::vector<T, CallbackAllocator<T> >; |
| 111 | |
| 112 | typedef std::basic_string<char, |
| 113 | std::char_traits<char>, |
| 114 | CallbackAllocator<char> > String; |
| 115 | |
| 116 | } // namespace |
| 117 | |
| 118 | // ----------------------------------------------------------------------------- |
| 119 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 120 | namespace { |
| 121 | |
| 122 | struct LayerData { |
| 123 | String path; |
| 124 | SharedLibraryHandle handle; |
| 125 | uint32_t ref_count; |
| 126 | }; |
| 127 | |
| 128 | typedef UnorderedMap<String, LayerData>::iterator LayerMapIterator; |
| 129 | |
| 130 | } // namespace |
| 131 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 132 | struct VkInstance_T { |
| 133 | VkInstance_T(const VkAllocCallbacks* alloc_callbacks) |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 134 | : vtbl(&vtbl_storage), |
| 135 | alloc(alloc_callbacks), |
| 136 | num_physical_devices(0), |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 137 | layers(CallbackAllocator<std::pair<String, LayerData> >(alloc)), |
| 138 | active_layers(CallbackAllocator<String>(alloc)) { |
| 139 | pthread_mutex_init(&layer_lock, 0); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 140 | memset(&vtbl_storage, 0, sizeof(vtbl_storage)); |
| 141 | memset(physical_devices, 0, sizeof(physical_devices)); |
| 142 | memset(&drv.vtbl, 0, sizeof(drv.vtbl)); |
| 143 | drv.GetDeviceProcAddr = nullptr; |
| 144 | drv.num_physical_devices = 0; |
| 145 | } |
| 146 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 147 | ~VkInstance_T() { pthread_mutex_destroy(&layer_lock); } |
| 148 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 149 | InstanceVtbl* vtbl; |
| 150 | InstanceVtbl vtbl_storage; |
| 151 | |
| 152 | const VkAllocCallbacks* alloc; |
| 153 | uint32_t num_physical_devices; |
| 154 | VkPhysicalDevice physical_devices[kMaxPhysicalDevices]; |
| 155 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 156 | pthread_mutex_t layer_lock; |
| 157 | // Map of layer names to layer data |
| 158 | UnorderedMap<String, LayerData> layers; |
| 159 | // Vector of layers active for this instance |
| 160 | Vector<LayerMapIterator> active_layers; |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 161 | VkDbgMsgCallback message; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 162 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 163 | struct Driver { |
| 164 | // Pointers to driver entry points. Used explicitly by the loader; not |
| 165 | // set as the dispatch table for any objects. |
| 166 | InstanceVtbl vtbl; |
| 167 | |
| 168 | // Pointer to the driver's get_device_proc_addr, must be valid for any |
| 169 | // of the driver's physical devices. Not part of the InstanceVtbl since |
| 170 | // it's not an Instance/PhysicalDevice function. |
| 171 | PFN_vkGetDeviceProcAddr GetDeviceProcAddr; |
| 172 | |
| 173 | // Number of physical devices owned by this driver. |
| 174 | uint32_t num_physical_devices; |
| 175 | } drv; // may eventually be an array |
| 176 | }; |
| 177 | |
| 178 | // ----------------------------------------------------------------------------- |
| 179 | |
| 180 | namespace { |
| 181 | |
| 182 | typedef VkInstance_T Instance; |
| 183 | |
| 184 | struct Device { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 185 | Device(Instance* instance_input) |
| 186 | : instance(instance_input), |
| 187 | active_layers(CallbackAllocator<LayerMapIterator>(instance->alloc)) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 188 | memset(&vtbl_storage, 0, sizeof(vtbl_storage)); |
| 189 | vtbl_storage.device = this; |
| 190 | } |
| 191 | DeviceVtbl vtbl_storage; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 192 | Instance* instance; |
| 193 | // Vector of layers active for this device |
| 194 | Vector<LayerMapIterator> active_layers; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | // ----------------------------------------------------------------------------- |
| 198 | // Utility Code |
| 199 | |
| 200 | inline const InstanceVtbl* GetVtbl(VkPhysicalDevice physicalDevice) { |
| 201 | return *reinterpret_cast<InstanceVtbl**>(physicalDevice); |
| 202 | } |
| 203 | |
| 204 | inline const DeviceVtbl* GetVtbl(VkDevice device) { |
| 205 | return *reinterpret_cast<DeviceVtbl**>(device); |
| 206 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 207 | inline const DeviceVtbl* GetVtbl(VkQueue queue) { |
| 208 | return *reinterpret_cast<DeviceVtbl**>(queue); |
| 209 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 210 | |
| 211 | void* DefaultAlloc(void*, size_t size, size_t alignment, VkSystemAllocType) { |
| 212 | return memalign(alignment, size); |
| 213 | } |
| 214 | |
| 215 | void DefaultFree(void*, void* pMem) { |
| 216 | free(pMem); |
| 217 | } |
| 218 | |
| 219 | const VkAllocCallbacks kDefaultAllocCallbacks = { |
| 220 | .pUserData = nullptr, |
| 221 | .pfnAlloc = DefaultAlloc, |
| 222 | .pfnFree = DefaultFree, |
| 223 | }; |
| 224 | |
| 225 | hwvulkan_device_t* g_hwdevice; |
| 226 | bool EnsureInitialized() { |
| 227 | static std::once_flag once_flag; |
| 228 | static const hwvulkan_module_t* module; |
| 229 | |
| 230 | std::call_once(once_flag, []() { |
| 231 | int result; |
| 232 | result = hw_get_module("vulkan", |
| 233 | reinterpret_cast<const hw_module_t**>(&module)); |
| 234 | if (result != 0) { |
| 235 | ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), |
| 236 | result); |
| 237 | return; |
| 238 | } |
| 239 | result = module->common.methods->open( |
| 240 | &module->common, HWVULKAN_DEVICE_0, |
| 241 | reinterpret_cast<hw_device_t**>(&g_hwdevice)); |
| 242 | if (result != 0) { |
| 243 | ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result), |
| 244 | result); |
| 245 | module = nullptr; |
| 246 | return; |
| 247 | } |
| 248 | }); |
| 249 | |
| 250 | return module != nullptr && g_hwdevice != nullptr; |
| 251 | } |
| 252 | |
| 253 | void DestroyDevice(Device* device) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 254 | const VkAllocCallbacks* alloc = device->instance->alloc; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 255 | device->~Device(); |
| 256 | alloc->pfnFree(alloc->pUserData, device); |
| 257 | } |
| 258 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 259 | void FindLayersInDirectory(Instance& instance, const String& dir_name) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 260 | DIR* directory; |
| 261 | struct dirent* entry; |
| 262 | if ((directory = opendir(dir_name.c_str()))) { |
| 263 | Vector<VkLayerProperties> properties( |
| 264 | CallbackAllocator<VkLayerProperties>(instance.alloc)); |
| 265 | while ((entry = readdir(directory))) { |
| 266 | size_t length = strlen(entry->d_name); |
| 267 | if (strncmp(entry->d_name, "libVKLayer", 10) != 0 || |
| 268 | strncmp(entry->d_name + length - 3, ".so", 3) != 0) |
| 269 | continue; |
| 270 | // Open so |
| 271 | SharedLibraryHandle layer_handle = dlopen( |
| 272 | (dir_name + entry->d_name).c_str(), RTLD_NOW | RTLD_LOCAL); |
| 273 | if (!layer_handle) { |
| 274 | ALOGE("%s failed to load with error %s; Skipping", |
| 275 | entry->d_name, dlerror()); |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | // Get Layers in so |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 280 | PFN_vkEnumerateInstanceLayerProperties get_layer_properties = |
| 281 | reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>( |
| 282 | dlsym(layer_handle, "vkEnumerateInstanceLayerProperties")); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 283 | if (!get_layer_properties) { |
| 284 | ALOGE( |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 285 | "%s failed to find vkEnumerateInstanceLayerProperties with " |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 286 | "error %s; Skipping", |
| 287 | entry->d_name, dlerror()); |
| 288 | dlclose(layer_handle); |
| 289 | continue; |
| 290 | } |
| 291 | uint32_t count; |
| 292 | get_layer_properties(&count, nullptr); |
| 293 | |
| 294 | properties.resize(count); |
| 295 | get_layer_properties(&count, &properties[0]); |
| 296 | |
| 297 | // Add Layers to potential list |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 298 | for (uint32_t i = 0; i < count; ++i) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 299 | String layer_name(properties[i].layerName, |
| 300 | CallbackAllocator<char>(instance.alloc)); |
| 301 | LayerData layer_data = {dir_name + entry->d_name, 0, 0}; |
| 302 | instance.layers.insert(std::make_pair(layer_name, layer_data)); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 303 | ALOGV("Found layer %s", properties[i].layerName); |
| 304 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 305 | dlclose(layer_handle); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 306 | } |
| 307 | closedir(directory); |
| 308 | } else { |
| 309 | ALOGE("Failed to Open Directory %s: %s (%d)", dir_name.c_str(), |
| 310 | strerror(errno), errno); |
| 311 | } |
| 312 | } |
| 313 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 314 | template <class TObject> |
| 315 | void ActivateLayer(TObject* object, Instance* instance, const String& name) { |
| 316 | // If object has layer, do nothing |
| 317 | auto element = instance->layers.find(name); |
| 318 | if (std::find(object->active_layers.begin(), object->active_layers.end(), |
| 319 | element) != object->active_layers.end()) { |
| 320 | ALOGW("Layer %s already activated; skipping", name.c_str()); |
| 321 | return; |
| 322 | } |
| 323 | // If layer is not open, open it |
| 324 | LayerData& layer_data = element->second; |
| 325 | pthread_mutex_lock(&instance->layer_lock); |
| 326 | if (layer_data.ref_count == 0) { |
| 327 | SharedLibraryHandle layer_handle = |
| 328 | dlopen(layer_data.path.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 329 | if (!layer_handle) { |
| 330 | pthread_mutex_unlock(&instance->layer_lock); |
| 331 | ALOGE("%s failed to load with error %s; Skipping", |
| 332 | layer_data.path.c_str(), dlerror()); |
| 333 | return; |
| 334 | } |
| 335 | layer_data.handle = layer_handle; |
| 336 | } |
| 337 | layer_data.ref_count++; |
| 338 | pthread_mutex_unlock(&instance->layer_lock); |
| 339 | ALOGV("Activating layer %s", name.c_str()); |
| 340 | object->active_layers.push_back(element); |
| 341 | } |
| 342 | |
| 343 | template <class TObject> |
| 344 | void DeactivateLayer(TObject* object, |
| 345 | Instance* instance, |
| 346 | Vector<LayerMapIterator>::iterator& element) { |
| 347 | LayerMapIterator& layer_map_data = *element; |
| 348 | object->active_layers.erase(element); |
| 349 | LayerData& layer_data = layer_map_data->second; |
| 350 | pthread_mutex_lock(&instance->layer_lock); |
| 351 | layer_data.ref_count--; |
| 352 | if (!layer_data.ref_count) { |
| 353 | dlclose(layer_data.handle); |
| 354 | } |
| 355 | pthread_mutex_unlock(&instance->layer_lock); |
| 356 | } |
| 357 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 358 | struct InstanceNamesPair { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 359 | Instance* instance; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 360 | Vector<String>* layer_names; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 361 | }; |
| 362 | |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 363 | void SetLayerNamesFromProperty(const char* name, |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 364 | const char* value, |
| 365 | void* data) { |
| 366 | const char prefix[] = "debug.vulkan.layer."; |
| 367 | const size_t prefixlen = sizeof(prefix) - 1; |
| 368 | if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0) |
| 369 | return; |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 370 | const char* number_str = name + prefixlen; |
| 371 | long layer_number = strtol(number_str, nullptr, 10); |
| 372 | if (layer_number <= 0 || layer_number == LONG_MAX) { |
| 373 | ALOGW("Cannot use a layer at number %ld from string %s", layer_number, |
| 374 | number_str); |
| 375 | return; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 376 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 377 | auto instance_names_pair = static_cast<InstanceNamesPair*>(data); |
| 378 | Vector<String>* layer_names = instance_names_pair->layer_names; |
| 379 | Instance* instance = instance_names_pair->instance; |
| 380 | size_t layer_size = static_cast<size_t>(layer_number); |
| 381 | if (layer_size > layer_names->size()) { |
| 382 | layer_names->resize(layer_size, |
| 383 | String(CallbackAllocator<char>(instance->alloc))); |
| 384 | } |
| 385 | (*layer_names)[layer_size - 1] = value; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | template <class TInfo, class TObject> |
| 389 | void ActivateAllLayers(TInfo create_info, Instance* instance, TObject* object) { |
| 390 | ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO || |
| 391 | create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 392 | "Cannot activate layers for unknown object %p", object); |
| 393 | CallbackAllocator<char> string_allocator(instance->alloc); |
| 394 | // Load system layers |
| 395 | { |
| 396 | char layer_prop[PROPERTY_VALUE_MAX]; |
| 397 | property_get("debug.vulkan.layers", layer_prop, ""); |
| 398 | String layer_name(string_allocator); |
| 399 | String layer_prop_str(layer_prop, string_allocator); |
| 400 | size_t end, start = 0; |
| 401 | while ((end = layer_prop_str.find(':', start)) != std::string::npos) { |
| 402 | layer_name = layer_prop_str.substr(start, end - start); |
| 403 | auto element = instance->layers.find(layer_name); |
| 404 | if (element != instance->layers.end()) { |
| 405 | ActivateLayer(object, instance, layer_name); |
| 406 | } |
| 407 | start = end + 1; |
| 408 | } |
Michael Lentine | 9da191b | 2015-10-13 11:08:45 -0500 | [diff] [blame] | 409 | Vector<String> layer_names(CallbackAllocator<String>(instance->alloc)); |
| 410 | InstanceNamesPair instance_names_pair = {.instance = instance, |
| 411 | .layer_names = &layer_names}; |
| 412 | property_list(SetLayerNamesFromProperty, |
| 413 | static_cast<void*>(&instance_names_pair)); |
| 414 | for (auto layer_name_element : layer_names) { |
| 415 | ActivateLayer(object, instance, layer_name_element); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 416 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 417 | } |
| 418 | // Load app layers |
| 419 | for (uint32_t i = 0; i < create_info->layerCount; ++i) { |
| 420 | String layer_name(create_info->ppEnabledLayerNames[i], |
| 421 | string_allocator); |
| 422 | auto element = instance->layers.find(layer_name); |
| 423 | if (element == instance->layers.end()) { |
| 424 | ALOGW("Cannot activate layer %s as it was not found.", |
| 425 | layer_name.c_str()); |
| 426 | } else { |
| 427 | ActivateLayer(object, instance, layer_name); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | template <class TCreateInfo> |
| 433 | bool AddExtensionToCreateInfo(TCreateInfo& local_create_info, |
| 434 | const char* extension_name, |
| 435 | const VkAllocCallbacks* alloc) { |
| 436 | for (uint32_t i = 0; i < local_create_info.extensionCount; ++i) { |
| 437 | if (!strcmp(extension_name, |
| 438 | local_create_info.ppEnabledExtensionNames[i])) { |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | uint32_t extension_count = local_create_info.extensionCount; |
| 443 | local_create_info.extensionCount++; |
| 444 | void* mem = alloc->pfnAlloc( |
| 445 | alloc->pUserData, local_create_info.extensionCount * sizeof(char*), |
| 446 | alignof(char*), VK_SYSTEM_ALLOC_TYPE_INTERNAL); |
| 447 | if (mem) { |
| 448 | const char** enabled_extensions = static_cast<const char**>(mem); |
| 449 | for (uint32_t i = 0; i < extension_count; ++i) { |
| 450 | enabled_extensions[i] = |
| 451 | local_create_info.ppEnabledExtensionNames[i]; |
| 452 | } |
| 453 | enabled_extensions[extension_count] = extension_name; |
| 454 | local_create_info.ppEnabledExtensionNames = enabled_extensions; |
| 455 | } else { |
| 456 | ALOGW("%s extension cannot be enabled: memory allocation failed", |
| 457 | extension_name); |
| 458 | local_create_info.extensionCount--; |
| 459 | return false; |
| 460 | } |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | template <class T> |
| 465 | void FreeAllocatedCreateInfo(T& local_create_info, |
| 466 | const VkAllocCallbacks* alloc) { |
| 467 | alloc->pfnFree( |
| 468 | alloc->pUserData, |
| 469 | const_cast<char**>(local_create_info.ppEnabledExtensionNames)); |
| 470 | } |
| 471 | |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame^] | 472 | VkBool32 LogDebugMessageCallback(VkFlags message_flags, |
| 473 | VkDbgObjectType /*obj_type*/, |
| 474 | uint64_t /*src_object*/, |
| 475 | size_t /*location*/, |
| 476 | int32_t message_code, |
| 477 | const char* layer_prefix, |
| 478 | const char* message, |
| 479 | void* /*user_data*/) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 480 | if (message_flags & VK_DBG_REPORT_ERROR_BIT) { |
| 481 | ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 482 | } else if (message_flags & VK_DBG_REPORT_WARN_BIT) { |
| 483 | ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message); |
| 484 | } |
Michael Lentine | eb97086 | 2015-10-15 12:42:22 -0500 | [diff] [blame^] | 485 | return false; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | VkResult CreateDeviceNoop(VkPhysicalDevice, |
| 489 | const VkDeviceCreateInfo*, |
| 490 | VkDevice*) { |
| 491 | return VK_SUCCESS; |
| 492 | } |
| 493 | |
| 494 | PFN_vkVoidFunction GetLayerDeviceProcAddr(VkDevice device, const char* name) { |
| 495 | if (strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 496 | return reinterpret_cast<PFN_vkVoidFunction>(GetLayerDeviceProcAddr); |
| 497 | } |
| 498 | if (strcmp(name, "vkCreateDevice") == 0) { |
| 499 | return reinterpret_cast<PFN_vkVoidFunction>(CreateDeviceNoop); |
| 500 | } |
| 501 | if (!device) |
| 502 | return GetGlobalDeviceProcAddr(name); |
| 503 | Device* loader_device = reinterpret_cast<Device*>(GetVtbl(device)->device); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 504 | return loader_device->instance->drv.GetDeviceProcAddr(device, name); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 505 | } |
| 506 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 507 | // ----------------------------------------------------------------------------- |
| 508 | // "Bottom" functions. These are called at the end of the instance dispatch |
| 509 | // chain. |
| 510 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 511 | void DestroyInstanceBottom(VkInstance instance) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 512 | // These checks allow us to call DestroyInstanceBottom from any error path |
| 513 | // in CreateInstanceBottom, before the driver instance is fully initialized. |
| 514 | if (instance->drv.vtbl.instance != VK_NULL_HANDLE && |
| 515 | instance->drv.vtbl.DestroyInstance) { |
| 516 | instance->drv.vtbl.DestroyInstance(instance->drv.vtbl.instance); |
| 517 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 518 | for (auto it = instance->active_layers.begin(); |
| 519 | it != instance->active_layers.end(); ++it) { |
| 520 | DeactivateLayer(instance, instance, it); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 521 | } |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 522 | if (instance->message) { |
| 523 | PFN_vkDbgDestroyMsgCallback DebugDestroyMessageCallback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 524 | DebugDestroyMessageCallback = |
| 525 | reinterpret_cast<PFN_vkDbgDestroyMsgCallback>( |
| 526 | vkGetInstanceProcAddr(instance, "vkDbgDestroyMsgCallback")); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 527 | DebugDestroyMessageCallback(instance, instance->message); |
| 528 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 529 | const VkAllocCallbacks* alloc = instance->alloc; |
| 530 | instance->~VkInstance_T(); |
| 531 | alloc->pfnFree(alloc->pUserData, instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | VkResult CreateInstanceBottom(const VkInstanceCreateInfo* create_info, |
| 535 | VkInstance* instance_ptr) { |
| 536 | Instance* instance = *instance_ptr; |
| 537 | VkResult result; |
| 538 | |
| 539 | result = |
| 540 | g_hwdevice->CreateInstance(create_info, &instance->drv.vtbl.instance); |
| 541 | if (result != VK_SUCCESS) { |
| 542 | DestroyInstanceBottom(instance); |
| 543 | return result; |
| 544 | } |
| 545 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 546 | if (!LoadInstanceVtbl( |
| 547 | instance->drv.vtbl.instance, instance->drv.vtbl.instance, |
| 548 | g_hwdevice->GetInstanceProcAddr, instance->drv.vtbl)) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 549 | DestroyInstanceBottom(instance); |
| 550 | return VK_ERROR_INITIALIZATION_FAILED; |
| 551 | } |
| 552 | |
| 553 | // vkGetDeviceProcAddr has a bootstrapping problem. We require that it be |
| 554 | // queryable from the Instance, and that the resulting function work for any |
| 555 | // VkDevice created from the instance. |
| 556 | instance->drv.GetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 557 | g_hwdevice->GetInstanceProcAddr(instance->drv.vtbl.instance, |
| 558 | "vkGetDeviceProcAddr")); |
| 559 | if (!instance->drv.GetDeviceProcAddr) { |
| 560 | ALOGE("missing instance proc: \"%s\"", "vkGetDeviceProcAddr"); |
| 561 | DestroyInstanceBottom(instance); |
| 562 | return VK_ERROR_INITIALIZATION_FAILED; |
| 563 | } |
| 564 | |
| 565 | hwvulkan_dispatch_t* dispatch = |
| 566 | reinterpret_cast<hwvulkan_dispatch_t*>(instance->drv.vtbl.instance); |
| 567 | if (dispatch->magic == HWVULKAN_DISPATCH_MAGIC) { |
| 568 | // Skip setting dispatch->vtbl on the driver instance handle, since we |
| 569 | // never intentionally call through it; we go through Instance::drv.vtbl |
| 570 | // instead. |
| 571 | } else { |
| 572 | ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR, |
| 573 | dispatch->magic); |
| 574 | DestroyInstanceBottom(instance); |
| 575 | return VK_ERROR_INITIALIZATION_FAILED; |
| 576 | } |
| 577 | |
| 578 | uint32_t num_physical_devices = 0; |
| 579 | result = instance->drv.vtbl.EnumeratePhysicalDevices( |
| 580 | instance->drv.vtbl.instance, &num_physical_devices, nullptr); |
| 581 | if (result != VK_SUCCESS) { |
| 582 | DestroyInstanceBottom(instance); |
| 583 | return VK_ERROR_INITIALIZATION_FAILED; |
| 584 | } |
| 585 | num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices); |
| 586 | result = instance->drv.vtbl.EnumeratePhysicalDevices( |
| 587 | instance->drv.vtbl.instance, &num_physical_devices, |
| 588 | instance->physical_devices); |
| 589 | if (result != VK_SUCCESS) { |
| 590 | DestroyInstanceBottom(instance); |
| 591 | return VK_ERROR_INITIALIZATION_FAILED; |
| 592 | } |
| 593 | for (uint32_t i = 0; i < num_physical_devices; i++) { |
| 594 | dispatch = reinterpret_cast<hwvulkan_dispatch_t*>( |
| 595 | instance->physical_devices[i]); |
| 596 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 597 | ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR, |
| 598 | dispatch->magic); |
| 599 | DestroyInstanceBottom(instance); |
| 600 | return VK_ERROR_INITIALIZATION_FAILED; |
| 601 | } |
| 602 | dispatch->vtbl = instance->vtbl; |
| 603 | } |
| 604 | instance->drv.num_physical_devices = num_physical_devices; |
| 605 | |
| 606 | instance->num_physical_devices = instance->drv.num_physical_devices; |
| 607 | return VK_SUCCESS; |
| 608 | } |
| 609 | |
| 610 | VkResult EnumeratePhysicalDevicesBottom(VkInstance instance, |
| 611 | uint32_t* pdev_count, |
| 612 | VkPhysicalDevice* pdevs) { |
| 613 | uint32_t count = instance->num_physical_devices; |
| 614 | if (pdevs) { |
| 615 | count = std::min(count, *pdev_count); |
| 616 | std::copy(instance->physical_devices, |
| 617 | instance->physical_devices + count, pdevs); |
| 618 | } |
| 619 | *pdev_count = count; |
| 620 | return VK_SUCCESS; |
| 621 | } |
| 622 | |
| 623 | VkResult GetPhysicalDeviceFeaturesBottom(VkPhysicalDevice pdev, |
| 624 | VkPhysicalDeviceFeatures* features) { |
| 625 | return GetVtbl(pdev) |
| 626 | ->instance->drv.vtbl.GetPhysicalDeviceFeatures(pdev, features); |
| 627 | } |
| 628 | |
| 629 | VkResult GetPhysicalDeviceFormatPropertiesBottom( |
| 630 | VkPhysicalDevice pdev, |
| 631 | VkFormat format, |
| 632 | VkFormatProperties* properties) { |
| 633 | return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceFormatProperties( |
| 634 | pdev, format, properties); |
| 635 | } |
| 636 | |
| 637 | VkResult GetPhysicalDeviceImageFormatPropertiesBottom( |
| 638 | VkPhysicalDevice pdev, |
| 639 | VkFormat format, |
| 640 | VkImageType type, |
| 641 | VkImageTiling tiling, |
| 642 | VkImageUsageFlags usage, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 643 | VkImageCreateFlags flags, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 644 | VkImageFormatProperties* properties) { |
| 645 | return GetVtbl(pdev) |
| 646 | ->instance->drv.vtbl.GetPhysicalDeviceImageFormatProperties( |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 647 | pdev, format, type, tiling, usage, flags, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | VkResult GetPhysicalDevicePropertiesBottom( |
| 651 | VkPhysicalDevice pdev, |
| 652 | VkPhysicalDeviceProperties* properties) { |
| 653 | return GetVtbl(pdev) |
| 654 | ->instance->drv.vtbl.GetPhysicalDeviceProperties(pdev, properties); |
| 655 | } |
| 656 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 657 | VkResult GetPhysicalDeviceQueueFamilyPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 658 | VkPhysicalDevice pdev, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 659 | uint32_t* pCount, |
| 660 | VkQueueFamilyProperties* properties) { |
| 661 | return GetVtbl(pdev) |
| 662 | ->instance->drv.vtbl.GetPhysicalDeviceQueueFamilyProperties( |
| 663 | pdev, pCount, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | VkResult GetPhysicalDeviceMemoryPropertiesBottom( |
| 667 | VkPhysicalDevice pdev, |
| 668 | VkPhysicalDeviceMemoryProperties* properties) { |
| 669 | return GetVtbl(pdev)->instance->drv.vtbl.GetPhysicalDeviceMemoryProperties( |
| 670 | pdev, properties); |
| 671 | } |
| 672 | |
| 673 | VkResult CreateDeviceBottom(VkPhysicalDevice pdev, |
| 674 | const VkDeviceCreateInfo* create_info, |
| 675 | VkDevice* out_device) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 676 | Instance& instance = *static_cast<Instance*>(GetVtbl(pdev)->instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 677 | VkResult result; |
| 678 | |
| 679 | void* mem = instance.alloc->pfnAlloc(instance.alloc->pUserData, |
| 680 | sizeof(Device), alignof(Device), |
| 681 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 682 | if (!mem) |
| 683 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 684 | Device* device = new (mem) Device(&instance); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 685 | |
| 686 | VkDevice drv_device; |
| 687 | result = instance.drv.vtbl.CreateDevice(pdev, create_info, &drv_device); |
| 688 | if (result != VK_SUCCESS) { |
| 689 | DestroyDevice(device); |
| 690 | return result; |
| 691 | } |
| 692 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 693 | hwvulkan_dispatch_t* dispatch = |
| 694 | reinterpret_cast<hwvulkan_dispatch_t*>(drv_device); |
| 695 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 696 | ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR, dispatch->magic); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 697 | PFN_vkDestroyDevice destroy_device = |
| 698 | reinterpret_cast<PFN_vkDestroyDevice>( |
| 699 | instance.drv.GetDeviceProcAddr(drv_device, "vkDestroyDevice")); |
| 700 | destroy_device(drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 701 | DestroyDevice(device); |
| 702 | return VK_ERROR_INITIALIZATION_FAILED; |
| 703 | } |
| 704 | dispatch->vtbl = &device->vtbl_storage; |
| 705 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 706 | device->vtbl_storage.GetSurfacePropertiesKHR = GetSurfacePropertiesKHR; |
| 707 | device->vtbl_storage.GetSurfaceFormatsKHR = GetSurfaceFormatsKHR; |
| 708 | device->vtbl_storage.GetSurfacePresentModesKHR = GetSurfacePresentModesKHR; |
| 709 | device->vtbl_storage.CreateSwapchainKHR = CreateSwapchainKHR; |
| 710 | device->vtbl_storage.DestroySwapchainKHR = DestroySwapchainKHR; |
| 711 | device->vtbl_storage.GetSwapchainImagesKHR = GetSwapchainImagesKHR; |
| 712 | device->vtbl_storage.AcquireNextImageKHR = AcquireNextImageKHR; |
| 713 | device->vtbl_storage.QueuePresentKHR = QueuePresentKHR; |
| 714 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 715 | ActivateAllLayers(create_info, &instance, device); |
| 716 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 717 | void* base_object = static_cast<void*>(drv_device); |
| 718 | void* next_object = base_object; |
| 719 | VkLayerLinkedListElem* next_element; |
| 720 | PFN_vkGetDeviceProcAddr next_get_proc_addr = GetLayerDeviceProcAddr; |
| 721 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 722 | device->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 723 | CallbackAllocator<VkLayerLinkedListElem>(instance.alloc)); |
| 724 | |
| 725 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 726 | size_t idx = i - 1; |
| 727 | next_element = &elem_list[idx]; |
| 728 | next_element->get_proc_addr = |
| 729 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 730 | next_element->base_object = base_object; |
| 731 | next_element->next_element = next_object; |
| 732 | next_object = static_cast<void*>(next_element); |
| 733 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 734 | auto& name = device->active_layers[idx]->first; |
| 735 | auto& handle = device->active_layers[idx]->second.handle; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 736 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 737 | dlsym(handle, (name + "GetDeviceProcAddr").c_str())); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 738 | if (!next_get_proc_addr) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 739 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 740 | dlsym(handle, "vkGetDeviceProcAddr")); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 741 | if (!next_get_proc_addr) { |
| 742 | ALOGE("Cannot find vkGetDeviceProcAddr for %s, error is %s", |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 743 | name.c_str(), dlerror()); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 744 | next_object = next_element->next_element; |
| 745 | next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 746 | next_element->get_proc_addr); |
| 747 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | if (!LoadDeviceVtbl(static_cast<VkDevice>(base_object), |
| 752 | static_cast<VkDevice>(next_object), next_get_proc_addr, |
| 753 | device->vtbl_storage)) { |
| 754 | DestroyDevice(device); |
| 755 | return VK_ERROR_INITIALIZATION_FAILED; |
| 756 | } |
| 757 | |
| 758 | PFN_vkCreateDevice layer_createDevice = |
| 759 | reinterpret_cast<PFN_vkCreateDevice>( |
| 760 | device->vtbl_storage.GetDeviceProcAddr(drv_device, |
| 761 | "vkCreateDevice")); |
| 762 | layer_createDevice(pdev, create_info, &drv_device); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 763 | |
| 764 | *out_device = drv_device; |
| 765 | return VK_SUCCESS; |
| 766 | } |
| 767 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 768 | VkResult EnumerateDeviceExtensionPropertiesBottom( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 769 | VkPhysicalDevice pdev, |
| 770 | const char* layer_name, |
| 771 | uint32_t* properties_count, |
| 772 | VkExtensionProperties* properties) { |
| 773 | // TODO: what are we supposed to do with layer_name here? |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 774 | return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceExtensionProperties( |
| 775 | pdev, layer_name, properties_count, properties); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 778 | VkResult EnumerateDeviceLayerPropertiesBottom(VkPhysicalDevice pdev, |
| 779 | uint32_t* properties_count, |
| 780 | VkLayerProperties* properties) { |
| 781 | return GetVtbl(pdev)->instance->drv.vtbl.EnumerateDeviceLayerProperties( |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 782 | pdev, properties_count, properties); |
| 783 | } |
| 784 | |
| 785 | VkResult GetPhysicalDeviceSparseImageFormatPropertiesBottom( |
| 786 | VkPhysicalDevice pdev, |
| 787 | VkFormat format, |
| 788 | VkImageType type, |
| 789 | uint32_t samples, |
| 790 | VkImageUsageFlags usage, |
| 791 | VkImageTiling tiling, |
| 792 | uint32_t* properties_count, |
| 793 | VkSparseImageFormatProperties* properties) { |
| 794 | return GetVtbl(pdev) |
| 795 | ->instance->drv.vtbl.GetPhysicalDeviceSparseImageFormatProperties( |
| 796 | pdev, format, type, samples, usage, tiling, properties_count, |
| 797 | properties); |
| 798 | } |
| 799 | |
| 800 | PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char*); |
| 801 | |
| 802 | const InstanceVtbl kBottomInstanceFunctions = { |
| 803 | // clang-format off |
| 804 | .instance = nullptr, |
| 805 | .CreateInstance = CreateInstanceBottom, |
| 806 | .DestroyInstance = DestroyInstanceBottom, |
| 807 | .GetInstanceProcAddr = GetInstanceProcAddrBottom, |
| 808 | .EnumeratePhysicalDevices = EnumeratePhysicalDevicesBottom, |
| 809 | .GetPhysicalDeviceFeatures = GetPhysicalDeviceFeaturesBottom, |
| 810 | .GetPhysicalDeviceFormatProperties = GetPhysicalDeviceFormatPropertiesBottom, |
| 811 | .GetPhysicalDeviceImageFormatProperties = GetPhysicalDeviceImageFormatPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 812 | .GetPhysicalDeviceProperties = GetPhysicalDevicePropertiesBottom, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 813 | .GetPhysicalDeviceQueueFamilyProperties = GetPhysicalDeviceQueueFamilyPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 814 | .GetPhysicalDeviceMemoryProperties = GetPhysicalDeviceMemoryPropertiesBottom, |
| 815 | .CreateDevice = CreateDeviceBottom, |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 816 | .EnumerateDeviceExtensionProperties = EnumerateDeviceExtensionPropertiesBottom, |
| 817 | .EnumerateDeviceLayerProperties = EnumerateDeviceLayerPropertiesBottom, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 818 | .GetPhysicalDeviceSparseImageFormatProperties = GetPhysicalDeviceSparseImageFormatPropertiesBottom, |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 819 | .GetPhysicalDeviceSurfaceSupportKHR = GetPhysicalDeviceSurfaceSupportKHR, |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 820 | // clang-format on |
| 821 | }; |
| 822 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 823 | VkResult Noop(...) { |
| 824 | return VK_SUCCESS; |
| 825 | } |
| 826 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 827 | PFN_vkVoidFunction GetInstanceProcAddrBottom(VkInstance, const char* name) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 828 | // TODO: Possibly move this into the instance table |
| 829 | // TODO: Possibly register the callbacks in the loader |
| 830 | if (strcmp(name, "vkDbgCreateMsgCallback") == 0 || |
| 831 | strcmp(name, "vkDbgDestroyMsgCallback") == 0) { |
| 832 | return reinterpret_cast<PFN_vkVoidFunction>(Noop); |
| 833 | } |
| 834 | if (strcmp(name, "vkCreateInstance") == 0) { |
| 835 | return reinterpret_cast<PFN_vkVoidFunction>(CreateInstanceBottom); |
| 836 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 837 | return GetSpecificInstanceProcAddr(&kBottomInstanceFunctions, name); |
| 838 | } |
| 839 | |
| 840 | } // namespace |
| 841 | |
| 842 | // ----------------------------------------------------------------------------- |
| 843 | // Global functions. These are called directly from the loader entry points, |
| 844 | // without going through a dispatch table. |
| 845 | |
| 846 | namespace vulkan { |
| 847 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 848 | VkResult EnumerateInstanceExtensionProperties( |
| 849 | const char* /*layer_name*/, |
| 850 | uint32_t* count, |
| 851 | VkExtensionProperties* /*properties*/) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 852 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 853 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 854 | |
| 855 | // TODO: not yet implemented |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 856 | ALOGW("vkEnumerateInstanceExtensionProperties not implemented"); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 857 | |
| 858 | *count = 0; |
| 859 | return VK_SUCCESS; |
| 860 | } |
| 861 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 862 | VkResult EnumerateInstanceLayerProperties(uint32_t* count, |
| 863 | VkLayerProperties* /*properties*/) { |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 864 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 865 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 866 | |
| 867 | // TODO: not yet implemented |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 868 | ALOGW("vkEnumerateInstanceLayerProperties not implemented"); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 869 | |
| 870 | *count = 0; |
| 871 | return VK_SUCCESS; |
| 872 | } |
| 873 | |
| 874 | VkResult CreateInstance(const VkInstanceCreateInfo* create_info, |
| 875 | VkInstance* out_instance) { |
| 876 | VkResult result; |
| 877 | |
| 878 | if (!EnsureInitialized()) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 879 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 880 | |
| 881 | VkInstanceCreateInfo local_create_info = *create_info; |
| 882 | if (!local_create_info.pAllocCb) |
| 883 | local_create_info.pAllocCb = &kDefaultAllocCallbacks; |
| 884 | create_info = &local_create_info; |
| 885 | |
| 886 | void* instance_mem = create_info->pAllocCb->pfnAlloc( |
| 887 | create_info->pAllocCb->pUserData, sizeof(Instance), alignof(Instance), |
| 888 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
| 889 | if (!instance_mem) |
| 890 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 891 | Instance* instance = new (instance_mem) Instance(create_info->pAllocCb); |
| 892 | |
| 893 | instance->vtbl_storage = kBottomInstanceFunctions; |
| 894 | instance->vtbl_storage.instance = instance; |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 895 | instance->message = VK_NULL_HANDLE; |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 896 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 897 | // Scan layers |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 898 | CallbackAllocator<char> string_allocator(instance->alloc); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 899 | |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 900 | String dir_name("/data/local/tmp/vulkan/", string_allocator); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 901 | FindLayersInDirectory(*instance, dir_name); |
Michael Lentine | 1c69b9e | 2015-09-14 13:26:59 -0500 | [diff] [blame] | 902 | const std::string& path = LoaderData::GetInstance().layer_path; |
| 903 | dir_name.assign(path.c_str(), path.size()); |
| 904 | dir_name.append("/"); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 905 | FindLayersInDirectory(*instance, dir_name); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 906 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 907 | ActivateAllLayers(create_info, instance, instance); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 908 | |
| 909 | void* base_object = static_cast<void*>(instance); |
| 910 | void* next_object = base_object; |
| 911 | VkLayerLinkedListElem* next_element; |
| 912 | PFN_vkGetInstanceProcAddr next_get_proc_addr = |
| 913 | kBottomInstanceFunctions.GetInstanceProcAddr; |
| 914 | Vector<VkLayerLinkedListElem> elem_list( |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 915 | instance->active_layers.size(), |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 916 | CallbackAllocator<VkLayerLinkedListElem>(instance->alloc)); |
| 917 | |
| 918 | for (size_t i = elem_list.size(); i > 0; i--) { |
| 919 | size_t idx = i - 1; |
| 920 | next_element = &elem_list[idx]; |
| 921 | next_element->get_proc_addr = |
| 922 | reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr); |
| 923 | next_element->base_object = base_object; |
| 924 | next_element->next_element = next_object; |
| 925 | next_object = static_cast<void*>(next_element); |
| 926 | |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 927 | auto& name = instance->active_layers[idx]->first; |
| 928 | auto& handle = instance->active_layers[idx]->second.handle; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 929 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 930 | dlsym(handle, (name + "GetInstanceProcAddr").c_str())); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 931 | if (!next_get_proc_addr) { |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 932 | next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 933 | dlsym(handle, "vkGetInstanceProcAddr")); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 934 | if (!next_get_proc_addr) { |
| 935 | ALOGE("Cannot find vkGetInstanceProcAddr for %s, error is %s", |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 936 | name.c_str(), dlerror()); |
Michael Lentine | 1f0f539 | 2015-09-11 14:54:34 -0700 | [diff] [blame] | 937 | next_object = next_element->next_element; |
| 938 | next_get_proc_addr = |
| 939 | reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
| 940 | next_element->get_proc_addr); |
| 941 | } |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | if (!LoadInstanceVtbl(static_cast<VkInstance>(base_object), |
| 946 | static_cast<VkInstance>(next_object), |
| 947 | next_get_proc_addr, instance->vtbl_storage)) { |
| 948 | DestroyInstanceBottom(instance); |
| 949 | return VK_ERROR_INITIALIZATION_FAILED; |
| 950 | } |
| 951 | |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 952 | // Force enable callback extension if required |
| 953 | bool enable_callback = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 954 | property_get_bool("debug.vulkan.enable_callback", false); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 955 | bool enable_logging = enable_callback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 956 | const char* extension_name = "DEBUG_REPORT"; |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 957 | if (enable_callback) { |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 958 | enable_callback = AddExtensionToCreateInfo( |
| 959 | local_create_info, extension_name, instance->alloc); |
Michael Lentine | 950bb4f | 2015-09-14 13:26:30 -0500 | [diff] [blame] | 960 | } |
| 961 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 962 | *out_instance = instance; |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 963 | result = instance->vtbl_storage.CreateInstance(create_info, out_instance); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 964 | if (enable_callback) |
| 965 | FreeAllocatedCreateInfo(local_create_info, instance->alloc); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 966 | if (result <= 0) { |
| 967 | // For every layer, including the loader top and bottom layers: |
| 968 | // - If a call to the next CreateInstance fails, the layer must clean |
| 969 | // up anything it has successfully done so far, and propagate the |
| 970 | // error upwards. |
| 971 | // - If a layer successfully calls the next layer's CreateInstance, and |
| 972 | // afterwards must fail for some reason, it must call the next layer's |
| 973 | // DestroyInstance before returning. |
| 974 | // - The layer must not call the next layer's DestroyInstance if that |
| 975 | // layer's CreateInstance wasn't called, or returned failure. |
| 976 | |
| 977 | // On failure, CreateInstanceBottom frees the instance struct, so it's |
| 978 | // already gone at this point. Nothing to do. |
| 979 | } |
| 980 | |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 981 | if (enable_logging) { |
| 982 | PFN_vkDbgCreateMsgCallback DebugCreateMessageCallback; |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 983 | DebugCreateMessageCallback = |
| 984 | reinterpret_cast<PFN_vkDbgCreateMsgCallback>( |
| 985 | vkGetInstanceProcAddr(instance, "vkDbgCreateMsgCallback")); |
| 986 | DebugCreateMessageCallback( |
| 987 | instance, VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
| 988 | LogDebugMessageCallback, NULL, &instance->message); |
Michael Lentine | cd6cabf | 2015-09-14 17:32:59 -0500 | [diff] [blame] | 989 | } |
| 990 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 991 | return result; |
| 992 | } |
| 993 | |
| 994 | PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) { |
| 995 | if (!instance) |
| 996 | return GetGlobalInstanceProcAddr(name); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 997 | // TODO: Possibly move this into the instance table |
| 998 | if (strcmp(name, "vkDbgCreateMsgCallback") == 0 || |
| 999 | strcmp(name, "vkDbgDestroyMsgCallback") == 0) { |
| 1000 | if (!instance->vtbl) |
| 1001 | return NULL; |
| 1002 | PFN_vkGetInstanceProcAddr gpa = instance->vtbl->GetInstanceProcAddr; |
| 1003 | return reinterpret_cast<PFN_vkVoidFunction>(gpa(instance, name)); |
| 1004 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1005 | // For special-case functions we always return the loader entry |
| 1006 | if (strcmp(name, "vkGetInstanceProcAddr") == 0 || |
| 1007 | strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 1008 | return GetGlobalInstanceProcAddr(name); |
| 1009 | } |
| 1010 | return GetSpecificInstanceProcAddr(instance->vtbl, name); |
| 1011 | } |
| 1012 | |
| 1013 | PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) { |
| 1014 | if (!device) |
| 1015 | return GetGlobalDeviceProcAddr(name); |
Michael Lentine | 03c64b0 | 2015-08-26 18:27:26 -0500 | [diff] [blame] | 1016 | if (strcmp(name, "vkGetDeviceProcAddr") == 0) { |
| 1017 | return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr); |
| 1018 | } |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1019 | if (strcmp(name, "vkGetDeviceQueue") == 0) { |
| 1020 | return reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue); |
| 1021 | } |
| 1022 | if (strcmp(name, "vkCreateCommandBuffer") == 0) { |
| 1023 | return reinterpret_cast<PFN_vkVoidFunction>(CreateCommandBuffer); |
| 1024 | } |
| 1025 | if (strcmp(name, "vkDestroyDevice") == 0) { |
| 1026 | return reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice); |
| 1027 | } |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1028 | // For special-case functions we always return the loader entry |
| 1029 | if (strcmp(name, "vkGetDeviceQueue") == 0 || |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1030 | strcmp(name, "vkCreateCommandBuffer") == 0 || |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1031 | strcmp(name, "vkDestroyDevice") == 0) { |
| 1032 | return GetGlobalDeviceProcAddr(name); |
| 1033 | } |
| 1034 | return GetSpecificDeviceProcAddr(GetVtbl(device), name); |
| 1035 | } |
| 1036 | |
| 1037 | VkResult GetDeviceQueue(VkDevice drv_device, |
| 1038 | uint32_t family, |
| 1039 | uint32_t index, |
| 1040 | VkQueue* out_queue) { |
| 1041 | VkResult result; |
| 1042 | VkQueue queue; |
| 1043 | const DeviceVtbl* vtbl = GetVtbl(drv_device); |
| 1044 | result = vtbl->GetDeviceQueue(drv_device, family, index, &queue); |
| 1045 | if (result != VK_SUCCESS) |
| 1046 | return result; |
| 1047 | hwvulkan_dispatch_t* dispatch = |
| 1048 | reinterpret_cast<hwvulkan_dispatch_t*>(queue); |
| 1049 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != &vtbl) { |
| 1050 | ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR, dispatch->magic); |
| 1051 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1052 | } |
| 1053 | dispatch->vtbl = vtbl; |
| 1054 | *out_queue = queue; |
| 1055 | return VK_SUCCESS; |
| 1056 | } |
| 1057 | |
Jesse Hall | c7a6eb5 | 2015-08-31 12:52:03 -0700 | [diff] [blame] | 1058 | VkResult CreateCommandBuffer(VkDevice drv_device, |
| 1059 | const VkCmdBufferCreateInfo* create_info, |
| 1060 | VkCmdBuffer* out_cmdbuf) { |
| 1061 | const DeviceVtbl* vtbl = GetVtbl(drv_device); |
| 1062 | VkCmdBuffer cmdbuf; |
| 1063 | VkResult result = |
| 1064 | vtbl->CreateCommandBuffer(drv_device, create_info, &cmdbuf); |
| 1065 | if (result != VK_SUCCESS) |
| 1066 | return result; |
| 1067 | hwvulkan_dispatch_t* dispatch = |
| 1068 | reinterpret_cast<hwvulkan_dispatch_t*>(cmdbuf); |
| 1069 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC) { |
| 1070 | ALOGE("invalid VkCmdBuffer dispatch magic: 0x%" PRIxPTR, |
| 1071 | dispatch->magic); |
| 1072 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1073 | } |
| 1074 | dispatch->vtbl = vtbl; |
| 1075 | *out_cmdbuf = cmdbuf; |
| 1076 | return VK_SUCCESS; |
| 1077 | } |
| 1078 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1079 | VkResult DestroyDevice(VkDevice drv_device) { |
| 1080 | const DeviceVtbl* vtbl = GetVtbl(drv_device); |
| 1081 | Device* device = static_cast<Device*>(vtbl->device); |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1082 | for (auto it = device->active_layers.begin(); |
| 1083 | it != device->active_layers.end(); ++it) { |
| 1084 | DeactivateLayer(device, device->instance, it); |
| 1085 | } |
| 1086 | device->active_layers.clear(); |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1087 | vtbl->DestroyDevice(drv_device); |
| 1088 | DestroyDevice(device); |
| 1089 | return VK_SUCCESS; |
| 1090 | } |
| 1091 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1092 | void* AllocDeviceMem(VkDevice device, |
| 1093 | size_t size, |
| 1094 | size_t align, |
| 1095 | VkSystemAllocType type) { |
| 1096 | const VkAllocCallbacks* alloc_cb = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1097 | static_cast<Device*>(GetVtbl(device)->device)->instance->alloc; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1098 | return alloc_cb->pfnAlloc(alloc_cb->pUserData, size, align, type); |
| 1099 | } |
| 1100 | |
| 1101 | void FreeDeviceMem(VkDevice device, void* ptr) { |
| 1102 | const VkAllocCallbacks* alloc_cb = |
Michael Lentine | 9dbe67f | 2015-09-16 15:53:50 -0500 | [diff] [blame] | 1103 | static_cast<Device*>(GetVtbl(device)->device)->instance->alloc; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1104 | alloc_cb->pfnFree(alloc_cb->pUserData, ptr); |
| 1105 | } |
| 1106 | |
| 1107 | const DeviceVtbl& GetDriverVtbl(VkDevice device) { |
| 1108 | // TODO(jessehall): This actually returns the API-level vtbl for the |
| 1109 | // device, not the driver entry points. Given the current use -- getting |
| 1110 | // the driver's private swapchain-related functions -- that works, but is |
| 1111 | // misleading and likely to cause bugs. Fix as part of separating the |
| 1112 | // loader->driver interface from the app->loader interface. |
| 1113 | return static_cast<Device*>(GetVtbl(device)->device)->vtbl_storage; |
| 1114 | } |
| 1115 | |
| 1116 | const DeviceVtbl& GetDriverVtbl(VkQueue queue) { |
| 1117 | // TODO(jessehall): This actually returns the API-level vtbl for the |
| 1118 | // device, not the driver entry points. Given the current use -- getting |
| 1119 | // the driver's private swapchain-related functions -- that works, but is |
| 1120 | // misleading and likely to cause bugs. Fix as part of separating the |
| 1121 | // loader->driver interface from the app->loader interface. |
| 1122 | return static_cast<Device*>(GetVtbl(queue)->device)->vtbl_storage; |
| 1123 | } |
| 1124 | |
Jesse Hall | 04f4f47 | 2015-08-16 19:51:04 -0700 | [diff] [blame] | 1125 | } // namespace vulkan |