Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | #ifndef LIBVULKAN_DRIVER_H |
| 18 | #define LIBVULKAN_DRIVER_H 1 |
| 19 | |
| 20 | #include <inttypes.h> |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 21 | #include <bitset> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 22 | #include <type_traits> |
| 23 | #include <log/log.h> |
| 24 | |
| 25 | #include <vulkan/vulkan.h> |
| 26 | #include <hardware/hwvulkan.h> |
| 27 | |
| 28 | #include "api_gen.h" |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 29 | #include "driver_gen.h" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 30 | |
| 31 | namespace vulkan { |
| 32 | |
| 33 | // This is here so that we can embed api::{Instance,Device}Data in |
| 34 | // driver::{Instance,Device}Data to avoid pointer chasing. They are |
| 35 | // considered opaque to the driver layer. |
| 36 | namespace api { |
| 37 | |
| 38 | struct InstanceData { |
| 39 | InstanceDispatchTable dispatch; |
| 40 | |
| 41 | // for VkPhysicalDevice->VkInstance mapping |
| 42 | VkInstance instance; |
| 43 | |
| 44 | // LayerChain::ActiveLayer array |
| 45 | void* layers; |
| 46 | uint32_t layer_count; |
| 47 | |
| 48 | // debug.vulkan.enable_callback |
| 49 | PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback; |
| 50 | VkDebugReportCallbackEXT debug_callback; |
| 51 | }; |
| 52 | |
| 53 | struct DeviceData { |
| 54 | DeviceDispatchTable dispatch; |
| 55 | |
| 56 | // LayerChain::ActiveLayer array |
| 57 | void* layers; |
| 58 | uint32_t layer_count; |
| 59 | }; |
| 60 | |
| 61 | } // namespace api |
| 62 | |
| 63 | namespace driver { |
| 64 | |
| 65 | struct InstanceData { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 66 | InstanceData(const VkAllocationCallbacks& alloc) |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 67 | : opaque_api_data(), |
| 68 | allocator(alloc), |
| 69 | driver(), |
| 70 | get_device_proc_addr(nullptr) { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 71 | hook_extensions.set(ProcHook::EXTENSION_CORE); |
| 72 | hal_extensions.set(ProcHook::EXTENSION_CORE); |
| 73 | } |
| 74 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 75 | api::InstanceData opaque_api_data; |
| 76 | |
| 77 | const VkAllocationCallbacks allocator; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 78 | |
| 79 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; |
| 80 | std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 81 | |
| 82 | InstanceDriverTable driver; |
| 83 | PFN_vkGetDeviceProcAddr get_device_proc_addr; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct DeviceData { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 87 | DeviceData(const VkAllocationCallbacks& alloc) |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 88 | : opaque_api_data(), allocator(alloc), driver() { |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 89 | hook_extensions.set(ProcHook::EXTENSION_CORE); |
| 90 | hal_extensions.set(ProcHook::EXTENSION_CORE); |
| 91 | } |
| 92 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 93 | api::DeviceData opaque_api_data; |
| 94 | |
| 95 | const VkAllocationCallbacks allocator; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 96 | |
| 97 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; |
| 98 | std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; |
| 99 | |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 100 | DeviceDriverTable driver; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | bool Debuggable(); |
| 104 | bool OpenHAL(); |
| 105 | const VkAllocationCallbacks& GetDefaultAllocator(); |
| 106 | |
| 107 | // clang-format off |
| 108 | VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName); |
| 109 | VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName); |
| 110 | VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); |
Chia-I Wu | ba0be41 | 2016-03-24 16:24:40 +0800 | [diff] [blame^] | 111 | |
| 112 | VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 113 | // clang-format on |
| 114 | |
| 115 | template <typename DispatchableType> |
| 116 | void StaticAssertDispatchable(DispatchableType) { |
| 117 | static_assert(std::is_same<DispatchableType, VkInstance>::value || |
| 118 | std::is_same<DispatchableType, VkPhysicalDevice>::value || |
| 119 | std::is_same<DispatchableType, VkDevice>::value || |
| 120 | std::is_same<DispatchableType, VkQueue>::value || |
| 121 | std::is_same<DispatchableType, VkCommandBuffer>::value, |
| 122 | "unrecognized dispatchable type"); |
| 123 | } |
| 124 | |
| 125 | template <typename DispatchableType> |
| 126 | bool SetDataInternal(DispatchableType dispatchable, const void* data) { |
| 127 | StaticAssertDispatchable(dispatchable); |
| 128 | |
| 129 | hwvulkan_dispatch_t* dispatch = |
| 130 | reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); |
| 131 | // must be magic or already set |
| 132 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { |
| 133 | ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | dispatch->vtbl = data; |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | template <typename DispatchableType> |
| 143 | void* GetDataInternal(DispatchableType dispatchable) { |
| 144 | StaticAssertDispatchable(dispatchable); |
| 145 | |
| 146 | const hwvulkan_dispatch_t* dispatch = |
| 147 | reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); |
| 148 | |
| 149 | return const_cast<void*>(dispatch->vtbl); |
| 150 | } |
| 151 | |
| 152 | inline bool SetData(VkInstance instance, const InstanceData& data) { |
| 153 | return SetDataInternal(instance, &data); |
| 154 | } |
| 155 | |
| 156 | inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { |
| 157 | return SetDataInternal(physical_dev, &data); |
| 158 | } |
| 159 | |
| 160 | inline bool SetData(VkDevice dev, const DeviceData& data) { |
| 161 | return SetDataInternal(dev, &data); |
| 162 | } |
| 163 | |
| 164 | inline bool SetData(VkQueue queue, const DeviceData& data) { |
| 165 | return SetDataInternal(queue, &data); |
| 166 | } |
| 167 | |
| 168 | inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { |
| 169 | return SetDataInternal(cmd, &data); |
| 170 | } |
| 171 | |
| 172 | inline InstanceData& GetData(VkInstance instance) { |
| 173 | return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); |
| 174 | } |
| 175 | |
| 176 | inline InstanceData& GetData(VkPhysicalDevice physical_dev) { |
| 177 | return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); |
| 178 | } |
| 179 | |
| 180 | inline DeviceData& GetData(VkDevice dev) { |
| 181 | return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); |
| 182 | } |
| 183 | |
| 184 | inline DeviceData& GetData(VkQueue queue) { |
| 185 | return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); |
| 186 | } |
| 187 | |
| 188 | inline DeviceData& GetData(VkCommandBuffer cmd) { |
| 189 | return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); |
| 190 | } |
| 191 | |
| 192 | } // namespace driver |
| 193 | } // namespace vulkan |
| 194 | |
| 195 | #endif // LIBVULKAN_DRIVER_H |