blob: 5abd3c5dbf2ae6ce911110fbd6baa55f687e0ad1 [file] [log] [blame]
Jesse Halld02edcb2015-09-08 07:44:48 -07001/*
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 Hall504db7f2016-01-14 15:53:57 -080017// #define LOG_NDEBUG 0
Michael Lentine9dbe67f2015-09-16 15:53:50 -050018
Jesse Hall04f4f472015-08-16 19:51:04 -070019// module header
20#include "loader.h"
21// standard C headers
Michael Lentine03c64b02015-08-26 18:27:26 -050022#include <dirent.h>
23#include <dlfcn.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070024#include <inttypes.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070025#include <pthread.h>
Jesse Hall03b6fe12015-11-24 12:44:21 -080026#include <stdlib.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070027#include <string.h>
Jesse Hall21597662015-12-18 13:48:24 -080028#include <sys/prctl.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070029// standard C++ headers
30#include <algorithm>
31#include <mutex>
Michael Lentine03c64b02015-08-26 18:27:26 -050032#include <sstream>
33#include <string>
34#include <unordered_map>
35#include <vector>
Jesse Hall04f4f472015-08-16 19:51:04 -070036// platform/library headers
Michael Lentine03c64b02015-08-26 18:27:26 -050037#include <cutils/properties.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070038#include <hardware/hwvulkan.h>
39#include <log/log.h>
Michael Lentine1c69b9e2015-09-14 13:26:59 -050040#include <vulkan/vulkan_loader_data.h>
Jesse Hall04f4f472015-08-16 19:51:04 -070041
42using namespace vulkan;
43
44static const uint32_t kMaxPhysicalDevices = 4;
45
Michael Lentine03c64b02015-08-26 18:27:26 -050046namespace {
47
48// These definitions are taken from the LunarG Vulkan Loader. They are used to
49// enforce compatability between the Loader and Layers.
50typedef void* (*PFN_vkGetProcAddr)(void* obj, const char* pName);
51
52typedef struct VkLayerLinkedListElem_ {
53 PFN_vkGetProcAddr get_proc_addr;
54 void* next_element;
55 void* base_object;
56} VkLayerLinkedListElem;
57
Jesse Hall1f91d392015-12-11 16:28:44 -080058// ----------------------------------------------------------------------------
Michael Lentine03c64b02015-08-26 18:27:26 -050059
Jesse Hall3fbc8562015-11-29 22:10:52 -080060// Standard-library allocator that delegates to VkAllocationCallbacks.
Jesse Hall03b6fe12015-11-24 12:44:21 -080061//
62// TODO(jessehall): This class currently always uses
Jesse Hall3fbc8562015-11-29 22:10:52 -080063// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
Jesse Hall03b6fe12015-11-24 12:44:21 -080064// parameter or a constructor parameter. The former would help catch bugs
65// where we use the wrong scope, e.g. adding a command-scope string to an
66// instance-scope vector. But that might also be pretty annoying to deal with.
Michael Lentine03c64b02015-08-26 18:27:26 -050067template <class T>
68class CallbackAllocator {
69 public:
70 typedef T value_type;
71
Jesse Hall3fbc8562015-11-29 22:10:52 -080072 CallbackAllocator(const VkAllocationCallbacks* alloc_input)
Michael Lentine03c64b02015-08-26 18:27:26 -050073 : alloc(alloc_input) {}
74
75 template <class T2>
76 CallbackAllocator(const CallbackAllocator<T2>& other)
77 : alloc(other.alloc) {}
78
79 T* allocate(std::size_t n) {
Jesse Hall3fbc8562015-11-29 22:10:52 -080080 void* mem =
81 alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
82 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine03c64b02015-08-26 18:27:26 -050083 return static_cast<T*>(mem);
84 }
85
86 void deallocate(T* array, std::size_t /*n*/) {
87 alloc->pfnFree(alloc->pUserData, array);
88 }
89
Jesse Hall3fbc8562015-11-29 22:10:52 -080090 const VkAllocationCallbacks* alloc;
Michael Lentine03c64b02015-08-26 18:27:26 -050091};
92// These are needed in order to move Strings
93template <class T>
94bool operator==(const CallbackAllocator<T>& alloc1,
95 const CallbackAllocator<T>& alloc2) {
96 return alloc1.alloc == alloc2.alloc;
97}
98template <class T>
99bool operator!=(const CallbackAllocator<T>& alloc1,
100 const CallbackAllocator<T>& alloc2) {
101 return !(alloc1 == alloc2);
102}
103
104template <class Key,
105 class T,
106 class Hash = std::hash<Key>,
Jesse Hall1f91d392015-12-11 16:28:44 -0800107 class Pred = std::equal_to<Key>>
Michael Lentine03c64b02015-08-26 18:27:26 -0500108using UnorderedMap =
109 std::unordered_map<Key,
110 T,
111 Hash,
112 Pred,
Jesse Hall1f91d392015-12-11 16:28:44 -0800113 CallbackAllocator<std::pair<const Key, T>>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500114
115template <class T>
Jesse Hall1f91d392015-12-11 16:28:44 -0800116using Vector = std::vector<T, CallbackAllocator<T>>;
Michael Lentine03c64b02015-08-26 18:27:26 -0500117
Jesse Hall1f91d392015-12-11 16:28:44 -0800118typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
119 String;
Michael Lentine03c64b02015-08-26 18:27:26 -0500120
Jesse Hall1f91d392015-12-11 16:28:44 -0800121// ----------------------------------------------------------------------------
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500122
Jesse Halle1b12782015-11-30 11:27:32 -0800123VKAPI_ATTR void* DefaultAllocate(void*,
124 size_t size,
125 size_t alignment,
126 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800127 void* ptr = nullptr;
128 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
129 // additionally requires that it be at least sizeof(void*).
130 return posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0
131 ? ptr
132 : nullptr;
133}
134
Jesse Halle1b12782015-11-30 11:27:32 -0800135VKAPI_ATTR void* DefaultReallocate(void*,
136 void* ptr,
137 size_t size,
138 size_t alignment,
139 VkSystemAllocationScope) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800140 if (size == 0) {
141 free(ptr);
142 return nullptr;
143 }
144
145 // TODO(jessehall): Right now we never shrink allocations; if the new
146 // request is smaller than the existing chunk, we just continue using it.
147 // Right now the loader never reallocs, so this doesn't matter. If that
148 // changes, or if this code is copied into some other project, this should
149 // probably have a heuristic to allocate-copy-free when doing so will save
150 // "enough" space.
151 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
152 if (size <= old_size)
153 return ptr;
154
155 void* new_ptr = nullptr;
156 if (posix_memalign(&new_ptr, alignment, size) != 0)
157 return nullptr;
158 if (ptr) {
159 memcpy(new_ptr, ptr, std::min(old_size, size));
160 free(ptr);
161 }
162 return new_ptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700163}
164
Jesse Halle1b12782015-11-30 11:27:32 -0800165VKAPI_ATTR void DefaultFree(void*, void* pMem) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700166 free(pMem);
167}
168
Jesse Hall3fbc8562015-11-29 22:10:52 -0800169const VkAllocationCallbacks kDefaultAllocCallbacks = {
Jesse Hall04f4f472015-08-16 19:51:04 -0700170 .pUserData = nullptr,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800171 .pfnAllocation = DefaultAllocate,
172 .pfnReallocation = DefaultReallocate,
Jesse Hall04f4f472015-08-16 19:51:04 -0700173 .pfnFree = DefaultFree,
174};
175
Jesse Hall1f91d392015-12-11 16:28:44 -0800176// ----------------------------------------------------------------------------
Jesse Hall80523e22016-01-06 16:47:54 -0800177// Global Data and Initialization
Jesse Hall1f91d392015-12-11 16:28:44 -0800178
Jesse Hall80523e22016-01-06 16:47:54 -0800179hwvulkan_device_t* g_hwdevice = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800180InstanceExtensionSet g_driver_instance_extensions;
181
Jesse Hall80523e22016-01-06 16:47:54 -0800182void LoadVulkanHAL() {
183 static const hwvulkan_module_t* module;
184 int result =
185 hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
186 if (result != 0) {
187 ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
188 return;
189 }
190 result = module->common.methods->open(
191 &module->common, HWVULKAN_DEVICE_0,
192 reinterpret_cast<hw_device_t**>(&g_hwdevice));
193 if (result != 0) {
194 ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
195 result);
196 module = nullptr;
197 return;
198 }
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800199
200 VkResult vkresult;
201 uint32_t count;
202 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
203 nullptr, &count, nullptr)) != VK_SUCCESS) {
204 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
205 vkresult);
206 g_hwdevice->common.close(&g_hwdevice->common);
207 g_hwdevice = nullptr;
208 module = nullptr;
209 return;
210 }
211 VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
212 alloca(count * sizeof(VkExtensionProperties)));
213 if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
214 nullptr, &count, extensions)) != VK_SUCCESS) {
215 ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
216 vkresult);
217 g_hwdevice->common.close(&g_hwdevice->common);
218 g_hwdevice = nullptr;
219 module = nullptr;
220 return;
221 }
222 ALOGV_IF(count > 0, "Driver-supported instance extensions:");
223 for (uint32_t i = 0; i < count; i++) {
224 ALOGV(" %s (v%u)", extensions[i].extensionName,
225 extensions[i].specVersion);
226 InstanceExtension id =
227 InstanceExtensionFromName(extensions[i].extensionName);
228 if (id != kInstanceExtensionCount)
229 g_driver_instance_extensions.set(id);
230 }
231 // Ignore driver attempts to support loader extensions
232 g_driver_instance_extensions.reset(kKHR_surface);
233 g_driver_instance_extensions.reset(kKHR_android_surface);
Jesse Hall80523e22016-01-06 16:47:54 -0800234}
235
Jesse Hall04f4f472015-08-16 19:51:04 -0700236bool EnsureInitialized() {
237 static std::once_flag once_flag;
Jesse Hall04f4f472015-08-16 19:51:04 -0700238 std::call_once(once_flag, []() {
Jesse Hall80523e22016-01-06 16:47:54 -0800239 LoadVulkanHAL();
240 DiscoverLayers();
Jesse Hall04f4f472015-08-16 19:51:04 -0700241 });
Jesse Hall80523e22016-01-06 16:47:54 -0800242 return g_hwdevice != nullptr;
Jesse Hall04f4f472015-08-16 19:51:04 -0700243}
244
Jesse Hall1f91d392015-12-11 16:28:44 -0800245// -----------------------------------------------------------------------------
246
247struct Instance {
248 Instance(const VkAllocationCallbacks* alloc_callbacks)
249 : dispatch_ptr(&dispatch),
250 handle(reinterpret_cast<VkInstance>(&dispatch_ptr)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800251 alloc(alloc_callbacks),
252 num_physical_devices(0),
Jesse Hall80523e22016-01-06 16:47:54 -0800253 active_layers(CallbackAllocator<LayerRef>(alloc)),
Jesse Hall1f91d392015-12-11 16:28:44 -0800254 message(VK_NULL_HANDLE) {
255 memset(&dispatch, 0, sizeof(dispatch));
256 memset(physical_devices, 0, sizeof(physical_devices));
Jesse Hall1f91d392015-12-11 16:28:44 -0800257 drv.instance = VK_NULL_HANDLE;
258 memset(&drv.dispatch, 0, sizeof(drv.dispatch));
259 drv.num_physical_devices = 0;
260 }
261
Jesse Hall80523e22016-01-06 16:47:54 -0800262 ~Instance() {}
Jesse Hall1f91d392015-12-11 16:28:44 -0800263
264 const InstanceDispatchTable* dispatch_ptr;
265 const VkInstance handle;
266 InstanceDispatchTable dispatch;
267
Jesse Hall1f91d392015-12-11 16:28:44 -0800268 const VkAllocationCallbacks* alloc;
269 uint32_t num_physical_devices;
270 VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
Jesse Hallb1471272016-01-17 21:36:58 -0800271 DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Jesse Hall1f91d392015-12-11 16:28:44 -0800272
Jesse Hall80523e22016-01-06 16:47:54 -0800273 Vector<LayerRef> active_layers;
Jesse Hall715b86a2016-01-16 16:34:29 -0800274 VkDebugReportCallbackEXT message;
275 DebugReportCallbackList debug_report_callbacks;
Jesse Hall1f91d392015-12-11 16:28:44 -0800276
277 struct {
278 VkInstance instance;
279 DriverDispatchTable dispatch;
280 uint32_t num_physical_devices;
281 } drv; // may eventually be an array
282};
283
284struct Device {
285 Device(Instance* instance_)
286 : instance(instance_),
Jesse Hall80523e22016-01-06 16:47:54 -0800287 active_layers(CallbackAllocator<LayerRef>(instance->alloc)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800288 memset(&dispatch, 0, sizeof(dispatch));
289 }
290 DeviceDispatchTable dispatch;
291 Instance* instance;
292 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Jesse Hall80523e22016-01-06 16:47:54 -0800293 Vector<LayerRef> active_layers;
Jesse Hall1f91d392015-12-11 16:28:44 -0800294};
295
296template <typename THandle>
297struct HandleTraits {};
298template <>
299struct HandleTraits<VkInstance> {
300 typedef Instance LoaderObjectType;
301};
302template <>
303struct HandleTraits<VkPhysicalDevice> {
304 typedef Instance LoaderObjectType;
305};
306template <>
307struct HandleTraits<VkDevice> {
308 typedef Device LoaderObjectType;
309};
310template <>
311struct HandleTraits<VkQueue> {
312 typedef Device LoaderObjectType;
313};
314template <>
315struct HandleTraits<VkCommandBuffer> {
316 typedef Device LoaderObjectType;
317};
318
319template <typename THandle>
320typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
321 THandle handle) {
322 // TODO(jessehall): Make Instance and Device POD types (by removing the
323 // non-default constructors), so that offsetof is actually legal to use.
324 // The specific case we're using here is safe in gcc/clang (and probably
325 // most other C++ compilers), but isn't guaranteed by C++.
326 typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
327#pragma clang diagnostic push
328#pragma clang diagnostic ignored "-Winvalid-offsetof"
329 const size_t kDispatchOffset = offsetof(ObjectType, dispatch);
330#pragma clang diagnostic pop
331
332 const auto& dispatch = GetDispatchTable(handle);
333 uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch);
334 uintptr_t object_addr = dispatch_addr - kDispatchOffset;
335 return *reinterpret_cast<ObjectType*>(object_addr);
336}
337
338// -----------------------------------------------------------------------------
339
Jesse Hall04f4f472015-08-16 19:51:04 -0700340void DestroyDevice(Device* device) {
Jesse Hall3fbc8562015-11-29 22:10:52 -0800341 const VkAllocationCallbacks* alloc = device->instance->alloc;
Jesse Hall04f4f472015-08-16 19:51:04 -0700342 device->~Device();
343 alloc->pfnFree(alloc->pUserData, device);
344}
345
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500346template <class TObject>
Jesse Hallaa410942016-01-17 13:07:10 -0800347LayerRef GetLayerRef(const char* name);
348template <>
349LayerRef GetLayerRef<Instance>(const char* name) {
350 return GetInstanceLayerRef(name);
351}
352template <>
353LayerRef GetLayerRef<Device>(const char* name) {
354 return GetDeviceLayerRef(name);
355}
356
357template <class TObject>
Jesse Hall80523e22016-01-06 16:47:54 -0800358bool ActivateLayer(TObject* object, const char* name) {
Jesse Hallaa410942016-01-17 13:07:10 -0800359 LayerRef layer(GetLayerRef<TObject>(name));
Jesse Hall80523e22016-01-06 16:47:54 -0800360 if (!layer)
361 return false;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500362 if (std::find(object->active_layers.begin(), object->active_layers.end(),
Jesse Hall80523e22016-01-06 16:47:54 -0800363 layer) == object->active_layers.end())
364 object->active_layers.push_back(std::move(layer));
365 ALOGV("activated layer '%s'", name);
366 return true;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500367}
368
Michael Lentine9da191b2015-10-13 11:08:45 -0500369struct InstanceNamesPair {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500370 Instance* instance;
Michael Lentine9da191b2015-10-13 11:08:45 -0500371 Vector<String>* layer_names;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500372};
373
Michael Lentine9da191b2015-10-13 11:08:45 -0500374void SetLayerNamesFromProperty(const char* name,
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500375 const char* value,
376 void* data) {
377 const char prefix[] = "debug.vulkan.layer.";
378 const size_t prefixlen = sizeof(prefix) - 1;
379 if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
380 return;
Michael Lentine9da191b2015-10-13 11:08:45 -0500381 const char* number_str = name + prefixlen;
382 long layer_number = strtol(number_str, nullptr, 10);
383 if (layer_number <= 0 || layer_number == LONG_MAX) {
384 ALOGW("Cannot use a layer at number %ld from string %s", layer_number,
385 number_str);
386 return;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500387 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500388 auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
389 Vector<String>* layer_names = instance_names_pair->layer_names;
390 Instance* instance = instance_names_pair->instance;
391 size_t layer_size = static_cast<size_t>(layer_number);
392 if (layer_size > layer_names->size()) {
393 layer_names->resize(layer_size,
394 String(CallbackAllocator<char>(instance->alloc)));
395 }
396 (*layer_names)[layer_size - 1] = value;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500397}
398
399template <class TInfo, class TObject>
Jesse Hall1f91d392015-12-11 16:28:44 -0800400VkResult ActivateAllLayers(TInfo create_info,
401 Instance* instance,
402 TObject* object) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500403 ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
404 create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
405 "Cannot activate layers for unknown object %p", object);
406 CallbackAllocator<char> string_allocator(instance->alloc);
407 // Load system layers
Jesse Hall21597662015-12-18 13:48:24 -0800408 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500409 char layer_prop[PROPERTY_VALUE_MAX];
410 property_get("debug.vulkan.layers", layer_prop, "");
411 String layer_name(string_allocator);
412 String layer_prop_str(layer_prop, string_allocator);
413 size_t end, start = 0;
414 while ((end = layer_prop_str.find(':', start)) != std::string::npos) {
415 layer_name = layer_prop_str.substr(start, end - start);
Jesse Hall80523e22016-01-06 16:47:54 -0800416 ActivateLayer(object, layer_name.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500417 start = end + 1;
418 }
Michael Lentine9da191b2015-10-13 11:08:45 -0500419 Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
420 InstanceNamesPair instance_names_pair = {.instance = instance,
421 .layer_names = &layer_names};
422 property_list(SetLayerNamesFromProperty,
423 static_cast<void*>(&instance_names_pair));
424 for (auto layer_name_element : layer_names) {
Jesse Hall80523e22016-01-06 16:47:54 -0800425 ActivateLayer(object, layer_name_element.c_str());
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500426 }
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500427 }
428 // Load app layers
Jesse Hall3dd678a2016-01-08 21:52:01 -0800429 for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) {
Jesse Hall80523e22016-01-06 16:47:54 -0800430 if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
Jesse Hall9a16f972015-10-28 15:59:53 -0700431 ALOGE("requested %s layer '%s' not present",
Jesse Hall1f91d392015-12-11 16:28:44 -0800432 create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
433 ? "instance"
434 : "device",
Jesse Hall80523e22016-01-06 16:47:54 -0800435 create_info->ppEnabledLayerNames[i]);
Jesse Hall9a16f972015-10-28 15:59:53 -0700436 return VK_ERROR_LAYER_NOT_PRESENT;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500437 }
438 }
Jesse Hall9a16f972015-10-28 15:59:53 -0700439 return VK_SUCCESS;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500440}
441
442template <class TCreateInfo>
443bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
444 const char* extension_name,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800445 const VkAllocationCallbacks* alloc) {
Jesse Hall3dd678a2016-01-08 21:52:01 -0800446 for (uint32_t i = 0; i < local_create_info.enabledExtensionCount; ++i) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500447 if (!strcmp(extension_name,
448 local_create_info.ppEnabledExtensionNames[i])) {
449 return false;
450 }
451 }
Jesse Hall3dd678a2016-01-08 21:52:01 -0800452 uint32_t extension_count = local_create_info.enabledExtensionCount;
453 local_create_info.enabledExtensionCount++;
Jesse Hall3fbc8562015-11-29 22:10:52 -0800454 void* mem = alloc->pfnAllocation(
Jesse Hall03b6fe12015-11-24 12:44:21 -0800455 alloc->pUserData,
Jesse Hall3dd678a2016-01-08 21:52:01 -0800456 local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
457 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500458 if (mem) {
459 const char** enabled_extensions = static_cast<const char**>(mem);
460 for (uint32_t i = 0; i < extension_count; ++i) {
461 enabled_extensions[i] =
462 local_create_info.ppEnabledExtensionNames[i];
463 }
464 enabled_extensions[extension_count] = extension_name;
465 local_create_info.ppEnabledExtensionNames = enabled_extensions;
466 } else {
467 ALOGW("%s extension cannot be enabled: memory allocation failed",
468 extension_name);
Jesse Hall3dd678a2016-01-08 21:52:01 -0800469 local_create_info.enabledExtensionCount--;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500470 return false;
471 }
472 return true;
473}
474
475template <class T>
476void FreeAllocatedCreateInfo(T& local_create_info,
Jesse Hall3fbc8562015-11-29 22:10:52 -0800477 const VkAllocationCallbacks* alloc) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500478 alloc->pfnFree(
479 alloc->pUserData,
480 const_cast<char**>(local_create_info.ppEnabledExtensionNames));
481}
482
Jesse Halle1b12782015-11-30 11:27:32 -0800483VKAPI_ATTR
Jesse Hall715b86a2016-01-16 16:34:29 -0800484VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
485 VkDebugReportObjectTypeEXT /*objectType*/,
486 uint64_t /*object*/,
Michael Lentineeb970862015-10-15 12:42:22 -0500487 size_t /*location*/,
488 int32_t message_code,
489 const char* layer_prefix,
490 const char* message,
491 void* /*user_data*/) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800492 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500493 ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
Jesse Hall715b86a2016-01-16 16:34:29 -0800494 } else if (flags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500495 ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
496 }
Michael Lentineeb970862015-10-15 12:42:22 -0500497 return false;
Michael Lentine03c64b02015-08-26 18:27:26 -0500498}
499
Jesse Hall06193802015-12-03 16:12:51 -0800500VkResult Noop() {
Michael Lentine03c64b02015-08-26 18:27:26 -0500501 return VK_SUCCESS;
502}
503
Jesse Hall1f91d392015-12-11 16:28:44 -0800504} // anonymous namespace
505
506namespace vulkan {
Michael Lentinecd6cabf2015-09-14 17:32:59 -0500507
Jesse Hall04f4f472015-08-16 19:51:04 -0700508// -----------------------------------------------------------------------------
509// "Bottom" functions. These are called at the end of the instance dispatch
510// chain.
511
Jesse Hall1f91d392015-12-11 16:28:44 -0800512VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
513 const VkAllocationCallbacks* allocator,
514 VkInstance* vkinstance) {
515 Instance& instance = GetDispatchParent(*vkinstance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700516 VkResult result;
517
Jesse Halla7ac76d2016-01-08 22:29:42 -0800518 VkInstanceCreateInfo driver_create_info = *create_info;
519 driver_create_info.enabledLayerCount = 0;
520 driver_create_info.ppEnabledLayerNames = nullptr;
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800521
522 InstanceExtensionSet enabled_extensions;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800523 driver_create_info.enabledExtensionCount = 0;
524 driver_create_info.ppEnabledExtensionNames = nullptr;
Jesse Hall715b86a2016-01-16 16:34:29 -0800525 size_t max_names =
526 std::min(static_cast<size_t>(create_info->enabledExtensionCount),
527 g_driver_instance_extensions.count());
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800528 if (max_names > 0) {
529 const char** names =
530 static_cast<const char**>(alloca(max_names * sizeof(char*)));
531 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
532 InstanceExtension id = InstanceExtensionFromName(
533 create_info->ppEnabledExtensionNames[i]);
534 if (id != kInstanceExtensionCount &&
535 g_driver_instance_extensions[id]) {
536 names[driver_create_info.enabledExtensionCount++] =
537 create_info->ppEnabledExtensionNames[i];
538 enabled_extensions.set(id);
539 }
540 }
541 driver_create_info.ppEnabledExtensionNames = names;
542 }
Jesse Halla7ac76d2016-01-08 22:29:42 -0800543
544 result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
Jesse Hall1f91d392015-12-11 16:28:44 -0800545 &instance.drv.instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700546 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800547 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700548 return result;
549 }
550
Jesse Hall1f91d392015-12-11 16:28:44 -0800551 if (!LoadDriverDispatchTable(instance.drv.instance,
552 g_hwdevice->GetInstanceProcAddr,
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800553 enabled_extensions, instance.drv.dispatch)) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800554 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700555 return VK_ERROR_INITIALIZATION_FAILED;
556 }
557
Jesse Hall1f91d392015-12-11 16:28:44 -0800558 hwvulkan_dispatch_t* drv_dispatch =
559 reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
560 if (drv_dispatch->magic == HWVULKAN_DISPATCH_MAGIC) {
561 // Skip setting drv_dispatch->vtbl, since we never call through it;
562 // we go through instance.drv.dispatch instead.
Jesse Hall04f4f472015-08-16 19:51:04 -0700563 } else {
564 ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800565 drv_dispatch->magic);
566 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700567 return VK_ERROR_INITIALIZATION_FAILED;
568 }
569
570 uint32_t num_physical_devices = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 result = instance.drv.dispatch.EnumeratePhysicalDevices(
572 instance.drv.instance, &num_physical_devices, nullptr);
Jesse Hall04f4f472015-08-16 19:51:04 -0700573 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700575 return VK_ERROR_INITIALIZATION_FAILED;
576 }
577 num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
Jesse Hall1f91d392015-12-11 16:28:44 -0800578 result = instance.drv.dispatch.EnumeratePhysicalDevices(
579 instance.drv.instance, &num_physical_devices,
580 instance.physical_devices);
Jesse Hall04f4f472015-08-16 19:51:04 -0700581 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800582 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700583 return VK_ERROR_INITIALIZATION_FAILED;
584 }
Jesse Hallb1471272016-01-17 21:36:58 -0800585
586 Vector<VkExtensionProperties> extensions(
587 Vector<VkExtensionProperties>::allocator_type(instance.alloc));
Jesse Hall04f4f472015-08-16 19:51:04 -0700588 for (uint32_t i = 0; i < num_physical_devices; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800589 hwvulkan_dispatch_t* pdev_dispatch =
590 reinterpret_cast<hwvulkan_dispatch_t*>(
591 instance.physical_devices[i]);
592 if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700593 ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -0800594 pdev_dispatch->magic);
595 DestroyInstance_Bottom(instance.handle, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700596 return VK_ERROR_INITIALIZATION_FAILED;
597 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800598 pdev_dispatch->vtbl = instance.dispatch_ptr;
Jesse Hallb1471272016-01-17 21:36:58 -0800599
600 uint32_t count;
601 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
602 instance.physical_devices[i], nullptr, &count, nullptr)) !=
603 VK_SUCCESS) {
604 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
605 result);
606 continue;
607 }
608 extensions.resize(count);
609 if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
610 instance.physical_devices[i], nullptr, &count,
611 extensions.data())) != VK_SUCCESS) {
612 ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
613 result);
614 continue;
615 }
616 ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
617 for (const auto& extension : extensions) {
618 ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
619 DeviceExtension id =
620 DeviceExtensionFromName(extension.extensionName);
621 if (id == kDeviceExtensionCount) {
622 ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
623 extension.extensionName);
624 } else {
625 instance.physical_device_driver_extensions[i].set(id);
626 }
627 }
628 // Ignore driver attempts to support loader extensions
629 instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
Jesse Hall04f4f472015-08-16 19:51:04 -0700630 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800631 instance.drv.num_physical_devices = num_physical_devices;
Jesse Hall1f91d392015-12-11 16:28:44 -0800632 instance.num_physical_devices = instance.drv.num_physical_devices;
Jesse Hallb1471272016-01-17 21:36:58 -0800633
Jesse Hall04f4f472015-08-16 19:51:04 -0700634 return VK_SUCCESS;
635}
636
Jesse Hall1f91d392015-12-11 16:28:44 -0800637PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance, const char* name) {
638 PFN_vkVoidFunction pfn;
639 if ((pfn = GetLoaderBottomProcAddr(name)))
640 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -0800641 return nullptr;
642}
643
644VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
645 uint32_t* pdev_count,
646 VkPhysicalDevice* pdevs) {
647 Instance& instance = GetDispatchParent(vkinstance);
648 uint32_t count = instance.num_physical_devices;
Jesse Hall04f4f472015-08-16 19:51:04 -0700649 if (pdevs) {
650 count = std::min(count, *pdev_count);
Jesse Hall1f91d392015-12-11 16:28:44 -0800651 std::copy(instance.physical_devices, instance.physical_devices + count,
652 pdevs);
Jesse Hall04f4f472015-08-16 19:51:04 -0700653 }
654 *pdev_count = count;
655 return VK_SUCCESS;
656}
657
Jesse Hall1f91d392015-12-11 16:28:44 -0800658void GetPhysicalDeviceProperties_Bottom(
659 VkPhysicalDevice pdev,
660 VkPhysicalDeviceProperties* properties) {
661 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
662 pdev, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700663}
664
Jesse Hall1f91d392015-12-11 16:28:44 -0800665void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
666 VkPhysicalDeviceFeatures* features) {
667 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
668 features);
669}
670
671void GetPhysicalDeviceMemoryProperties_Bottom(
672 VkPhysicalDevice pdev,
673 VkPhysicalDeviceMemoryProperties* properties) {
674 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
675 pdev, properties);
676}
677
678void GetPhysicalDeviceQueueFamilyProperties_Bottom(
679 VkPhysicalDevice pdev,
680 uint32_t* pCount,
681 VkQueueFamilyProperties* properties) {
682 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
683 pdev, pCount, properties);
684}
685
686void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
687 VkFormat format,
688 VkFormatProperties* properties) {
689 GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
Jesse Hall04f4f472015-08-16 19:51:04 -0700690 pdev, format, properties);
691}
692
Jesse Hall1f91d392015-12-11 16:28:44 -0800693VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700694 VkPhysicalDevice pdev,
695 VkFormat format,
696 VkImageType type,
697 VkImageTiling tiling,
698 VkImageUsageFlags usage,
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700699 VkImageCreateFlags flags,
Jesse Hall04f4f472015-08-16 19:51:04 -0700700 VkImageFormatProperties* properties) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800701 return GetDispatchParent(pdev)
702 .drv.dispatch.GetPhysicalDeviceImageFormatProperties(
Jesse Halla9e57032015-11-30 01:03:10 -0800703 pdev, format, type, tiling, usage, flags, properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700704}
705
Jesse Hall1f91d392015-12-11 16:28:44 -0800706void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
Jesse Hall04f4f472015-08-16 19:51:04 -0700707 VkPhysicalDevice pdev,
Jesse Hall1f91d392015-12-11 16:28:44 -0800708 VkFormat format,
709 VkImageType type,
710 VkSampleCountFlagBits samples,
711 VkImageUsageFlags usage,
712 VkImageTiling tiling,
713 uint32_t* properties_count,
714 VkSparseImageFormatProperties* properties) {
715 GetDispatchParent(pdev)
716 .drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
717 pdev, format, type, samples, usage, tiling, properties_count,
718 properties);
Jesse Hall04f4f472015-08-16 19:51:04 -0700719}
720
Jesse Halle1b12782015-11-30 11:27:32 -0800721VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800722VkResult EnumerateDeviceExtensionProperties_Bottom(
Jesse Hallb1471272016-01-17 21:36:58 -0800723 VkPhysicalDevice gpu,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800724 const char* layer_name,
Jesse Hall1f91d392015-12-11 16:28:44 -0800725 uint32_t* properties_count,
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800726 VkExtensionProperties* properties) {
727 const VkExtensionProperties* extensions = nullptr;
728 uint32_t num_extensions = 0;
729 if (layer_name) {
730 GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions);
731 } else {
Jesse Hallb1471272016-01-17 21:36:58 -0800732 Instance& instance = GetDispatchParent(gpu);
733 size_t gpu_idx = 0;
734 while (instance.physical_devices[gpu_idx] != gpu)
735 gpu_idx++;
736 const DeviceExtensionSet driver_extensions =
737 instance.physical_device_driver_extensions[gpu_idx];
738
739 // We only support VK_KHR_swapchain if the GPU supports
740 // VK_ANDROID_native_buffer
741 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
742 alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
743 if (driver_extensions[kANDROID_native_buffer]) {
744 available[num_extensions++] = VkExtensionProperties{
745 VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
746 }
747
748 // TODO(jessehall): We need to also enumerate extensions supported by
749 // implicitly-enabled layers. Currently we don't have that list of
750 // layers until instance creation.
751 extensions = available;
Jesse Hall57f7f8c2016-01-17 17:21:36 -0800752 }
753
754 if (!properties || *properties_count > num_extensions)
755 *properties_count = num_extensions;
756 if (properties)
757 std::copy(extensions, extensions + *properties_count, properties);
758 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -0700759}
760
Jesse Halle1b12782015-11-30 11:27:32 -0800761VKAPI_ATTR
Jesse Hall80523e22016-01-06 16:47:54 -0800762VkResult EnumerateDeviceLayerProperties_Bottom(VkPhysicalDevice /*pdev*/,
Jesse Hall1f91d392015-12-11 16:28:44 -0800763 uint32_t* properties_count,
Jesse Hallaa410942016-01-17 13:07:10 -0800764 VkLayerProperties* properties) {
765 uint32_t layer_count =
766 EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
767 if (!properties || *properties_count > layer_count)
768 *properties_count = layer_count;
769 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall1f91d392015-12-11 16:28:44 -0800770}
771
772VKAPI_ATTR
Jesse Hallb1471272016-01-17 21:36:58 -0800773VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
Jesse Hall1f91d392015-12-11 16:28:44 -0800774 const VkDeviceCreateInfo* create_info,
775 const VkAllocationCallbacks* allocator,
776 VkDevice* device_out) {
Jesse Hallb1471272016-01-17 21:36:58 -0800777 Instance& instance = GetDispatchParent(gpu);
Jesse Hall04f4f472015-08-16 19:51:04 -0700778 VkResult result;
779
Jesse Hall03b6fe12015-11-24 12:44:21 -0800780 if (!allocator) {
781 if (instance.alloc)
782 allocator = instance.alloc;
783 else
784 allocator = &kDefaultAllocCallbacks;
785 }
786
Jesse Hall3fbc8562015-11-29 22:10:52 -0800787 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
788 alignof(Device),
789 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Jesse Hall04f4f472015-08-16 19:51:04 -0700790 if (!mem)
791 return VK_ERROR_OUT_OF_HOST_MEMORY;
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500792 Device* device = new (mem) Device(&instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700793
Jesse Hall9a16f972015-10-28 15:59:53 -0700794 result = ActivateAllLayers(create_info, &instance, device);
795 if (result != VK_SUCCESS) {
796 DestroyDevice(device);
797 return result;
798 }
799
Jesse Hallb1471272016-01-17 21:36:58 -0800800 size_t gpu_idx = 0;
801 while (instance.physical_devices[gpu_idx] != gpu)
802 gpu_idx++;
803
804 uint32_t num_driver_extensions = 0;
805 const char** driver_extensions = static_cast<const char**>(
806 alloca(create_info->enabledExtensionCount * sizeof(const char*)));
807 for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
808 const char* name = create_info->ppEnabledExtensionNames[i];
809
810 DeviceExtension id = DeviceExtensionFromName(name);
811 if (id < kDeviceExtensionCount &&
812 (instance.physical_device_driver_extensions[gpu_idx][id] ||
813 id == kKHR_swapchain)) {
814 if (id == kKHR_swapchain)
815 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
816 driver_extensions[num_driver_extensions++] = name;
817 continue;
818 }
819
820 bool supported = false;
821 for (const auto& layer : device->active_layers) {
822 if (layer.SupportsExtension(name))
823 supported = true;
824 }
825 if (!supported) {
826 ALOGE(
827 "requested device extension '%s' not supported by driver or "
828 "any active layers",
829 name);
830 DestroyDevice(device);
831 return VK_ERROR_EXTENSION_NOT_PRESENT;
832 }
833 }
834
Jesse Halla7ac76d2016-01-08 22:29:42 -0800835 VkDeviceCreateInfo driver_create_info = *create_info;
836 driver_create_info.enabledLayerCount = 0;
837 driver_create_info.ppEnabledLayerNames = nullptr;
838 // TODO(jessehall): As soon as we enumerate device extensions supported by
839 // the driver, we need to filter the requested extension list to those
840 // supported by the driver here. Also, add the VK_ANDROID_native_buffer
841 // extension to the list iff the VK_KHR_swapchain extension was requested,
842 // instead of adding it unconditionally like we do now.
Jesse Hallb1471272016-01-17 21:36:58 -0800843 driver_create_info.enabledExtensionCount = num_driver_extensions;
844 driver_create_info.ppEnabledExtensionNames = driver_extensions;
Jesse Halla7ac76d2016-01-08 22:29:42 -0800845
Jesse Hall04f4f472015-08-16 19:51:04 -0700846 VkDevice drv_device;
Jesse Hallb1471272016-01-17 21:36:58 -0800847 result = instance.drv.dispatch.CreateDevice(gpu, &driver_create_info,
848 allocator, &drv_device);
Jesse Hall04f4f472015-08-16 19:51:04 -0700849 if (result != VK_SUCCESS) {
850 DestroyDevice(device);
851 return result;
852 }
853
Jesse Hall1f91d392015-12-11 16:28:44 -0800854 hwvulkan_dispatch_t* drv_dispatch =
Jesse Hall04f4f472015-08-16 19:51:04 -0700855 reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800856 if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
857 ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
858 drv_dispatch->magic);
Michael Lentine03c64b02015-08-26 18:27:26 -0500859 PFN_vkDestroyDevice destroy_device =
860 reinterpret_cast<PFN_vkDestroyDevice>(
Jesse Hall1f91d392015-12-11 16:28:44 -0800861 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
862 "vkDestroyDevice"));
Jesse Hall03b6fe12015-11-24 12:44:21 -0800863 destroy_device(drv_device, allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -0700864 DestroyDevice(device);
865 return VK_ERROR_INITIALIZATION_FAILED;
866 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800867 drv_dispatch->vtbl = &device->dispatch;
868 device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
869 instance.drv.dispatch.GetDeviceProcAddr(drv_device,
870 "vkGetDeviceProcAddr"));
Jesse Hall04f4f472015-08-16 19:51:04 -0700871
Michael Lentine03c64b02015-08-26 18:27:26 -0500872 void* base_object = static_cast<void*>(drv_device);
873 void* next_object = base_object;
874 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -0800875 PFN_vkGetDeviceProcAddr next_get_proc_addr = GetDeviceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -0500876 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine9dbe67f2015-09-16 15:53:50 -0500877 device->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -0500878 CallbackAllocator<VkLayerLinkedListElem>(instance.alloc));
879
880 for (size_t i = elem_list.size(); i > 0; i--) {
881 size_t idx = i - 1;
882 next_element = &elem_list[idx];
883 next_element->get_proc_addr =
884 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
885 next_element->base_object = base_object;
886 next_element->next_element = next_object;
887 next_object = static_cast<void*>(next_element);
888
Jesse Hall80523e22016-01-06 16:47:54 -0800889 next_get_proc_addr = device->active_layers[idx].GetGetDeviceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -0500890 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -0800891 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -0500892 next_get_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -0800893 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -0500894 }
895 }
896
Jesse Hall1f91d392015-12-11 16:28:44 -0800897 // This is the magic call that initializes all the layer devices and
898 // allows them to create their device_handle -> device_data mapping.
899 next_get_proc_addr(static_cast<VkDevice>(next_object),
900 "vkGetDeviceProcAddr");
901
902 // We must create all the layer devices *before* retrieving the device
903 // procaddrs, so that the layers know which extensions are enabled and
904 // therefore which functions to return procaddrs for.
905 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
906 next_get_proc_addr(drv_device, "vkCreateDevice"));
Jesse Hallb1471272016-01-17 21:36:58 -0800907 create_device(gpu, create_info, allocator, &drv_device);
Jesse Hall1f91d392015-12-11 16:28:44 -0800908
909 if (!LoadDeviceDispatchTable(static_cast<VkDevice>(base_object),
910 next_get_proc_addr, device->dispatch)) {
Michael Lentine03c64b02015-08-26 18:27:26 -0500911 DestroyDevice(device);
912 return VK_ERROR_INITIALIZATION_FAILED;
913 }
914
Jesse Hall1f91d392015-12-11 16:28:44 -0800915 *device_out = drv_device;
Jesse Hall04f4f472015-08-16 19:51:04 -0700916 return VK_SUCCESS;
917}
918
Jesse Hall1f91d392015-12-11 16:28:44 -0800919void DestroyInstance_Bottom(VkInstance vkinstance,
920 const VkAllocationCallbacks* allocator) {
921 Instance& instance = GetDispatchParent(vkinstance);
922
923 // These checks allow us to call DestroyInstance_Bottom from any error
924 // path in CreateInstance_Bottom, before the driver instance is fully
925 // initialized.
926 if (instance.drv.instance != VK_NULL_HANDLE &&
927 instance.drv.dispatch.DestroyInstance) {
928 instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
929 }
930 if (instance.message) {
Jesse Hall715b86a2016-01-16 16:34:29 -0800931 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
932 destroy_debug_report_callback =
933 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
934 vkGetInstanceProcAddr(vkinstance,
935 "vkDestroyDebugReportCallbackEXT"));
936 destroy_debug_report_callback(vkinstance, instance.message, allocator);
Jesse Hall1f91d392015-12-11 16:28:44 -0800937 }
Jesse Hall80523e22016-01-06 16:47:54 -0800938 instance.active_layers.clear();
Jesse Hall1f91d392015-12-11 16:28:44 -0800939 const VkAllocationCallbacks* alloc = instance.alloc;
940 instance.~Instance();
941 alloc->pfnFree(alloc->pUserData, &instance);
Jesse Hall04f4f472015-08-16 19:51:04 -0700942}
943
Jesse Hall1f91d392015-12-11 16:28:44 -0800944PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
945 const char* name) {
946 if (strcmp(name, "vkCreateDevice") == 0) {
947 // TODO(jessehall): Blegh, having this here is disgusting. The current
948 // layer init process can't call through the instance dispatch table's
949 // vkCreateDevice, because that goes through the instance layers rather
950 // than through the device layers. So we need to be able to get the
951 // vkCreateDevice pointer through the *device* layer chain.
952 //
953 // Because we've already created the driver device before calling
954 // through the layer vkCreateDevice functions, the loader bottom proc
955 // is a no-op.
Michael Lentine03c64b02015-08-26 18:27:26 -0500956 return reinterpret_cast<PFN_vkVoidFunction>(Noop);
957 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800958
959 // VK_ANDROID_native_buffer should be hidden from applications and layers.
960 // TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
961 PFN_vkVoidFunction pfn;
962 if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
963 strcmp(name, "vkAcquireImageANDROID") == 0 ||
964 strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
965 return nullptr;
Michael Lentine03c64b02015-08-26 18:27:26 -0500966 }
Jesse Hall1f91d392015-12-11 16:28:44 -0800967 if ((pfn = GetLoaderBottomProcAddr(name)))
968 return pfn;
969 return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
Jesse Hall04f4f472015-08-16 19:51:04 -0700970}
971
Jesse Hall04f4f472015-08-16 19:51:04 -0700972// -----------------------------------------------------------------------------
Jesse Hall1f91d392015-12-11 16:28:44 -0800973// Loader top functions. These are called directly from the loader entry
974// points or from the application (via vkGetInstanceProcAddr) without going
975// through a dispatch table.
Jesse Hall04f4f472015-08-16 19:51:04 -0700976
Jesse Hall1f91d392015-12-11 16:28:44 -0800977VkResult EnumerateInstanceExtensionProperties_Top(
Jesse Hall80523e22016-01-06 16:47:54 -0800978 const char* layer_name,
979 uint32_t* properties_count,
980 VkExtensionProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -0700981 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700982 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -0700983
Jesse Hall80523e22016-01-06 16:47:54 -0800984 const VkExtensionProperties* extensions = nullptr;
985 uint32_t num_extensions = 0;
986 if (layer_name) {
Jesse Hallaa410942016-01-17 13:07:10 -0800987 GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
Jesse Hall80523e22016-01-06 16:47:54 -0800988 } else {
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800989 VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
990 alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
991 available[num_extensions++] = VkExtensionProperties{
992 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
993 available[num_extensions++] =
994 VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
995 VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
996 if (g_driver_instance_extensions[kEXT_debug_report]) {
997 available[num_extensions++] =
998 VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
999 VK_EXT_DEBUG_REPORT_SPEC_VERSION};
1000 }
Jesse Hall80523e22016-01-06 16:47:54 -08001001 // TODO(jessehall): We need to also enumerate extensions supported by
1002 // implicitly-enabled layers. Currently we don't have that list of
1003 // layers until instance creation.
Jesse Hall6bd5dfa2016-01-16 17:13:30 -08001004 extensions = available;
Jesse Hall80523e22016-01-06 16:47:54 -08001005 }
Jesse Hall04f4f472015-08-16 19:51:04 -07001006
Jesse Hall80523e22016-01-06 16:47:54 -08001007 if (!properties || *properties_count > num_extensions)
1008 *properties_count = num_extensions;
1009 if (properties)
1010 std::copy(extensions, extensions + *properties_count, properties);
1011 return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001012}
1013
Jesse Hall80523e22016-01-06 16:47:54 -08001014VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
1015 VkLayerProperties* properties) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001016 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001017 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001018
Jesse Hall80523e22016-01-06 16:47:54 -08001019 uint32_t layer_count =
Jesse Hallaa410942016-01-17 13:07:10 -08001020 EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
Jesse Hall80523e22016-01-06 16:47:54 -08001021 if (!properties || *properties_count > layer_count)
1022 *properties_count = layer_count;
1023 return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
Jesse Hall04f4f472015-08-16 19:51:04 -07001024}
1025
Jesse Hall1f91d392015-12-11 16:28:44 -08001026VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
1027 const VkAllocationCallbacks* allocator,
1028 VkInstance* instance_out) {
Jesse Hall04f4f472015-08-16 19:51:04 -07001029 VkResult result;
1030
1031 if (!EnsureInitialized())
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001032 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Hall04f4f472015-08-16 19:51:04 -07001033
Jesse Hall03b6fe12015-11-24 12:44:21 -08001034 if (!allocator)
1035 allocator = &kDefaultAllocCallbacks;
1036
Jesse Hall04f4f472015-08-16 19:51:04 -07001037 VkInstanceCreateInfo local_create_info = *create_info;
Jesse Hall04f4f472015-08-16 19:51:04 -07001038 create_info = &local_create_info;
1039
Jesse Hall3fbc8562015-11-29 22:10:52 -08001040 void* instance_mem = allocator->pfnAllocation(
1041 allocator->pUserData, sizeof(Instance), alignof(Instance),
1042 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Jesse Hall04f4f472015-08-16 19:51:04 -07001043 if (!instance_mem)
1044 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall03b6fe12015-11-24 12:44:21 -08001045 Instance* instance = new (instance_mem) Instance(allocator);
Jesse Hall04f4f472015-08-16 19:51:04 -07001046
Jesse Hall9a16f972015-10-28 15:59:53 -07001047 result = ActivateAllLayers(create_info, instance, instance);
1048 if (result != VK_SUCCESS) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001049 DestroyInstance_Bottom(instance->handle, allocator);
Jesse Hall9a16f972015-10-28 15:59:53 -07001050 return result;
1051 }
Michael Lentine03c64b02015-08-26 18:27:26 -05001052
Jesse Hall1f91d392015-12-11 16:28:44 -08001053 void* base_object = static_cast<void*>(instance->handle);
Michael Lentine03c64b02015-08-26 18:27:26 -05001054 void* next_object = base_object;
1055 VkLayerLinkedListElem* next_element;
Jesse Hall1f91d392015-12-11 16:28:44 -08001056 PFN_vkGetInstanceProcAddr next_get_proc_addr = GetInstanceProcAddr_Bottom;
Michael Lentine03c64b02015-08-26 18:27:26 -05001057 Vector<VkLayerLinkedListElem> elem_list(
Michael Lentine1f0f5392015-09-11 14:54:34 -07001058 instance->active_layers.size(),
Michael Lentine03c64b02015-08-26 18:27:26 -05001059 CallbackAllocator<VkLayerLinkedListElem>(instance->alloc));
1060
1061 for (size_t i = elem_list.size(); i > 0; i--) {
1062 size_t idx = i - 1;
1063 next_element = &elem_list[idx];
1064 next_element->get_proc_addr =
1065 reinterpret_cast<PFN_vkGetProcAddr>(next_get_proc_addr);
1066 next_element->base_object = base_object;
1067 next_element->next_element = next_object;
1068 next_object = static_cast<void*>(next_element);
1069
Jesse Hall80523e22016-01-06 16:47:54 -08001070 next_get_proc_addr =
1071 instance->active_layers[idx].GetGetInstanceProcAddr();
Michael Lentine03c64b02015-08-26 18:27:26 -05001072 if (!next_get_proc_addr) {
Jesse Hall80523e22016-01-06 16:47:54 -08001073 next_object = next_element->next_element;
Michael Lentine03c64b02015-08-26 18:27:26 -05001074 next_get_proc_addr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Jesse Hall80523e22016-01-06 16:47:54 -08001075 next_element->get_proc_addr);
Michael Lentine03c64b02015-08-26 18:27:26 -05001076 }
1077 }
1078
Jesse Hall1f91d392015-12-11 16:28:44 -08001079 // This is the magic call that initializes all the layer instances and
1080 // allows them to create their instance_handle -> instance_data mapping.
1081 next_get_proc_addr(static_cast<VkInstance>(next_object),
1082 "vkGetInstanceProcAddr");
1083
1084 if (!LoadInstanceDispatchTable(static_cast<VkInstance>(base_object),
1085 next_get_proc_addr, instance->dispatch)) {
1086 DestroyInstance_Bottom(instance->handle, allocator);
Michael Lentine03c64b02015-08-26 18:27:26 -05001087 return VK_ERROR_INITIALIZATION_FAILED;
1088 }
1089
Michael Lentine950bb4f2015-09-14 13:26:30 -05001090 // Force enable callback extension if required
Jesse Hall21597662015-12-18 13:48:24 -08001091 bool enable_callback = false;
1092 bool enable_logging = false;
1093 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
1094 enable_callback =
1095 property_get_bool("debug.vulkan.enable_callback", false);
1096 enable_logging = enable_callback;
1097 if (enable_callback) {
1098 enable_callback = AddExtensionToCreateInfo(
Jesse Hall715b86a2016-01-16 16:34:29 -08001099 local_create_info, "VK_EXT_debug_report", instance->alloc);
Jesse Hall21597662015-12-18 13:48:24 -08001100 }
Michael Lentine950bb4f2015-09-14 13:26:30 -05001101 }
1102
Jesse Hall1f91d392015-12-11 16:28:44 -08001103 *instance_out = instance->handle;
1104 PFN_vkCreateInstance create_instance =
1105 reinterpret_cast<PFN_vkCreateInstance>(
1106 next_get_proc_addr(instance->handle, "vkCreateInstance"));
1107 result = create_instance(create_info, allocator, instance_out);
Michael Lentine9dbe67f2015-09-16 15:53:50 -05001108 if (enable_callback)
1109 FreeAllocatedCreateInfo(local_create_info, instance->alloc);
Jesse Hall04f4f472015-08-16 19:51:04 -07001110 if (result <= 0) {
1111 // For every layer, including the loader top and bottom layers:
1112 // - If a call to the next CreateInstance fails, the layer must clean
1113 // up anything it has successfully done so far, and propagate the
1114 // error upwards.
1115 // - If a layer successfully calls the next layer's CreateInstance, and
1116 // afterwards must fail for some reason, it must call the next layer's
1117 // DestroyInstance before returning.
1118 // - The layer must not call the next layer's DestroyInstance if that
1119 // layer's CreateInstance wasn't called, or returned failure.
1120
Jesse Hall1f91d392015-12-11 16:28:44 -08001121 // On failure, CreateInstance_Bottom frees the instance struct, so it's
Jesse Hall04f4f472015-08-16 19:51:04 -07001122 // already gone at this point. Nothing to do.
1123 }
1124
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001125 if (enable_logging) {
Jesse Hall715b86a2016-01-16 16:34:29 -08001126 const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
1127 .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
1128 .flags =
1129 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT,
1130 .pfnCallback = LogDebugMessageCallback,
1131 };
1132 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
1133 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
1134 GetInstanceProcAddr_Top(instance->handle,
1135 "vkCreateDebugReportCallbackEXT"));
1136 create_debug_report_callback(instance->handle, &callback_create_info,
1137 allocator, &instance->message);
Michael Lentinecd6cabf2015-09-14 17:32:59 -05001138 }
1139
Jesse Hall04f4f472015-08-16 19:51:04 -07001140 return result;
1141}
1142
Jesse Hall1f91d392015-12-11 16:28:44 -08001143PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
1144 const char* name) {
1145 // vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
1146 if (!vkinstance)
1147 return GetLoaderGlobalProcAddr(name);
1148
1149 const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
1150 PFN_vkVoidFunction pfn;
1151 // Always go through the loader-top function if there is one.
1152 if ((pfn = GetLoaderTopProcAddr(name)))
1153 return pfn;
1154 // Otherwise, look up the handler in the instance dispatch table
1155 if ((pfn = GetDispatchProcAddr(dispatch, name)))
1156 return pfn;
Jesse Hall1f91d392015-12-11 16:28:44 -08001157 // Anything not handled already must be a device-dispatched function
1158 // without a loader-top. We must return a function that will dispatch based
1159 // on the dispatchable object parameter -- which is exactly what the
1160 // exported functions do. So just return them here.
1161 return GetLoaderExportProcAddr(name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001162}
1163
Jesse Hall1f91d392015-12-11 16:28:44 -08001164void DestroyInstance_Top(VkInstance instance,
1165 const VkAllocationCallbacks* allocator) {
1166 if (!instance)
1167 return;
1168 GetDispatchTable(instance).DestroyInstance(instance, allocator);
1169}
1170
1171PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
1172 PFN_vkVoidFunction pfn;
Jesse Hall04f4f472015-08-16 19:51:04 -07001173 if (!device)
Jesse Hall1f91d392015-12-11 16:28:44 -08001174 return nullptr;
1175 if ((pfn = GetLoaderTopProcAddr(name)))
1176 return pfn;
1177 return GetDispatchProcAddr(GetDispatchTable(device), name);
Jesse Hall04f4f472015-08-16 19:51:04 -07001178}
1179
Jesse Hall1f91d392015-12-11 16:28:44 -08001180void GetDeviceQueue_Top(VkDevice vkdevice,
1181 uint32_t family,
1182 uint32_t index,
1183 VkQueue* queue_out) {
1184 const auto& table = GetDispatchTable(vkdevice);
1185 table.GetDeviceQueue(vkdevice, family, index, queue_out);
1186 hwvulkan_dispatch_t* queue_dispatch =
1187 reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
1188 if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
1189 queue_dispatch->vtbl != &table)
1190 ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
1191 queue_dispatch->magic);
1192 queue_dispatch->vtbl = &table;
Jesse Hall04f4f472015-08-16 19:51:04 -07001193}
1194
Jesse Hall1f91d392015-12-11 16:28:44 -08001195VkResult AllocateCommandBuffers_Top(
1196 VkDevice vkdevice,
1197 const VkCommandBufferAllocateInfo* alloc_info,
1198 VkCommandBuffer* cmdbufs) {
1199 const auto& table = GetDispatchTable(vkdevice);
1200 VkResult result =
1201 table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001202 if (result != VK_SUCCESS)
1203 return result;
Jesse Hall3dd678a2016-01-08 21:52:01 -08001204 for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001205 hwvulkan_dispatch_t* cmdbuf_dispatch =
Jesse Hall3fbc8562015-11-29 22:10:52 -08001206 reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
Jesse Hall1f91d392015-12-11 16:28:44 -08001207 ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
Jesse Hall3fbc8562015-11-29 22:10:52 -08001208 "invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
Jesse Hall1f91d392015-12-11 16:28:44 -08001209 cmdbuf_dispatch->magic);
1210 cmdbuf_dispatch->vtbl = &table;
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001211 }
Jesse Hallc7a6eb52015-08-31 12:52:03 -07001212 return VK_SUCCESS;
1213}
1214
Jesse Hall1f91d392015-12-11 16:28:44 -08001215void DestroyDevice_Top(VkDevice vkdevice,
1216 const VkAllocationCallbacks* /*allocator*/) {
1217 if (!vkdevice)
1218 return;
1219 Device& device = GetDispatchParent(vkdevice);
Jesse Hall1f91d392015-12-11 16:28:44 -08001220 device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
1221 DestroyDevice(&device);
Jesse Hall04f4f472015-08-16 19:51:04 -07001222}
1223
Jesse Hall1f91d392015-12-11 16:28:44 -08001224// -----------------------------------------------------------------------------
1225
1226const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
1227 return GetDispatchParent(vkinstance).alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001228}
1229
Jesse Hall1f91d392015-12-11 16:28:44 -08001230const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
1231 return GetDispatchParent(vkdevice).instance->alloc;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001232}
1233
Jesse Hall715b86a2016-01-16 16:34:29 -08001234VkInstance GetDriverInstance(VkInstance instance) {
1235 return GetDispatchParent(instance).drv.instance;
1236}
1237
1238const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
1239 return GetDispatchParent(instance).drv.dispatch;
1240}
1241
Jesse Hall1f91d392015-12-11 16:28:44 -08001242const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
1243 return GetDispatchParent(device).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001244}
1245
Jesse Hall1f91d392015-12-11 16:28:44 -08001246const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
1247 return GetDispatchParent(queue).instance->drv.dispatch;
Jesse Halld7b994a2015-09-07 14:17:37 -07001248}
1249
Jesse Hall715b86a2016-01-16 16:34:29 -08001250DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
1251 return GetDispatchParent(instance).debug_report_callbacks;
1252}
1253
Jesse Hall04f4f472015-08-16 19:51:04 -07001254} // namespace vulkan