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> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 21 | |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 22 | #include <bitset> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 23 | #include <type_traits> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 26 | |
| 27 | #include <vulkan/vulkan.h> |
| 28 | #include <hardware/hwvulkan.h> |
| 29 | |
| 30 | #include "api_gen.h" |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 31 | #include "driver_gen.h" |
Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 32 | #include "debug_report.h" |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 33 | #include "swapchain.h" |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 34 | |
| 35 | namespace vulkan { |
| 36 | |
| 37 | // This is here so that we can embed api::{Instance,Device}Data in |
| 38 | // driver::{Instance,Device}Data to avoid pointer chasing. They are |
| 39 | // considered opaque to the driver layer. |
| 40 | namespace api { |
| 41 | |
| 42 | struct InstanceData { |
| 43 | InstanceDispatchTable dispatch; |
| 44 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 45 | // LayerChain::ActiveLayer array |
| 46 | void* layers; |
| 47 | uint32_t layer_count; |
| 48 | |
| 49 | // debug.vulkan.enable_callback |
| 50 | PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback; |
| 51 | VkDebugReportCallbackEXT debug_callback; |
| 52 | }; |
| 53 | |
| 54 | struct DeviceData { |
| 55 | DeviceDispatchTable dispatch; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | } // namespace api |
| 59 | |
| 60 | namespace driver { |
| 61 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 62 | VK_DEFINE_HANDLE(InstanceDispatchable) |
| 63 | VK_DEFINE_HANDLE(DeviceDispatchable) |
| 64 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 65 | struct InstanceData { |
Chih-Hung Hsieh | d56988d | 2016-09-01 11:37:47 -0700 | [diff] [blame] | 66 | explicit 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(), |
Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 70 | get_device_proc_addr(nullptr) {} |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 71 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 72 | api::InstanceData opaque_api_data; |
| 73 | |
| 74 | const VkAllocationCallbacks allocator; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 75 | |
| 76 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; |
Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 77 | |
| 78 | InstanceDriverTable driver; |
| 79 | PFN_vkGetDeviceProcAddr get_device_proc_addr; |
Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 80 | |
| 81 | DebugReportCallbackList debug_report_callbacks; |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct DeviceData { |
Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 85 | DeviceData(const VkAllocationCallbacks& alloc, |
| 86 | const DebugReportCallbackList& debug_report_callbacks_) |
| 87 | : opaque_api_data(), |
| 88 | allocator(alloc), |
| 89 | debug_report_callbacks(debug_report_callbacks_), |
Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 90 | driver() {} |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 91 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 92 | api::DeviceData opaque_api_data; |
| 93 | |
| 94 | const VkAllocationCallbacks allocator; |
Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 95 | const DebugReportCallbackList& debug_report_callbacks; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 96 | |
| 97 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; |
Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 98 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 99 | VkDevice driver_device; |
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 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 103 | bool OpenHAL(); |
| 104 | const VkAllocationCallbacks& GetDefaultAllocator(); |
| 105 | |
Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 106 | void QueryPresentationProperties( |
Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 107 | VkPhysicalDevice physicalDevice, |
Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 108 | VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties); |
Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 109 | |
Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 110 | VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, |
| 111 | const char* pName); |
| 112 | VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, |
| 113 | const char* pName); |
| 114 | VKAPI_ATTR VkResult |
| 115 | EnumerateInstanceExtensionProperties(const char* pLayerName, |
| 116 | uint32_t* pPropertyCount, |
| 117 | VkExtensionProperties* pProperties); |
| 118 | VKAPI_ATTR VkResult |
| 119 | EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, |
| 120 | const char* pLayerName, |
| 121 | uint32_t* pPropertyCount, |
| 122 | VkExtensionProperties* pProperties); |
| 123 | VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, |
| 124 | const VkAllocationCallbacks* pAllocator, |
| 125 | VkInstance* pInstance); |
| 126 | VKAPI_ATTR void DestroyInstance(VkInstance instance, |
| 127 | const VkAllocationCallbacks* pAllocator); |
| 128 | VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, |
| 129 | const VkDeviceCreateInfo* pCreateInfo, |
| 130 | const VkAllocationCallbacks* pAllocator, |
| 131 | VkDevice* pDevice); |
| 132 | VKAPI_ATTR void DestroyDevice(VkDevice device, |
| 133 | const VkAllocationCallbacks* pAllocator); |
| 134 | VKAPI_ATTR VkResult |
| 135 | EnumeratePhysicalDevices(VkInstance instance, |
| 136 | uint32_t* pPhysicalDeviceCount, |
| 137 | VkPhysicalDevice* pPhysicalDevices); |
| 138 | VKAPI_ATTR VkResult EnumeratePhysicalDeviceGroups( |
| 139 | VkInstance instance, |
| 140 | uint32_t* pPhysicalDeviceGroupCount, |
| 141 | VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties); |
| 142 | VKAPI_ATTR void GetDeviceQueue(VkDevice device, |
| 143 | uint32_t queueFamilyIndex, |
| 144 | uint32_t queueIndex, |
| 145 | VkQueue* pQueue); |
| 146 | VKAPI_ATTR void GetDeviceQueue2(VkDevice device, |
| 147 | const VkDeviceQueueInfo2* pQueueInfo, |
| 148 | VkQueue* pQueue); |
| 149 | VKAPI_ATTR VkResult |
| 150 | AllocateCommandBuffers(VkDevice device, |
| 151 | const VkCommandBufferAllocateInfo* pAllocateInfo, |
| 152 | VkCommandBuffer* pCommandBuffers); |
| 153 | VKAPI_ATTR VkResult QueueSubmit(VkQueue queue, |
| 154 | uint32_t submitCount, |
| 155 | const VkSubmitInfo* pSubmits, |
| 156 | VkFence fence); |
| 157 | VKAPI_ATTR void GetPhysicalDeviceFeatures2( |
| 158 | VkPhysicalDevice physicalDevice, |
| 159 | VkPhysicalDeviceFeatures2* pFeatures); |
| 160 | VKAPI_ATTR void GetPhysicalDeviceProperties2( |
| 161 | VkPhysicalDevice physicalDevice, |
| 162 | VkPhysicalDeviceProperties2* pProperties); |
| 163 | VKAPI_ATTR void GetPhysicalDeviceFormatProperties2( |
| 164 | VkPhysicalDevice physicalDevice, |
| 165 | VkFormat format, |
| 166 | VkFormatProperties2* pFormatProperties); |
| 167 | VKAPI_ATTR VkResult GetPhysicalDeviceImageFormatProperties2( |
| 168 | VkPhysicalDevice physicalDevice, |
| 169 | const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, |
| 170 | VkImageFormatProperties2* pImageFormatProperties); |
| 171 | VKAPI_ATTR void GetPhysicalDeviceQueueFamilyProperties2( |
| 172 | VkPhysicalDevice physicalDevice, |
| 173 | uint32_t* pQueueFamilyPropertyCount, |
| 174 | VkQueueFamilyProperties2* pQueueFamilyProperties); |
| 175 | VKAPI_ATTR void GetPhysicalDeviceMemoryProperties2( |
| 176 | VkPhysicalDevice physicalDevice, |
| 177 | VkPhysicalDeviceMemoryProperties2* pMemoryProperties); |
| 178 | VKAPI_ATTR void GetPhysicalDeviceSparseImageFormatProperties2( |
| 179 | VkPhysicalDevice physicalDevice, |
| 180 | const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo, |
| 181 | uint32_t* pPropertyCount, |
| 182 | VkSparseImageFormatProperties2* pProperties); |
| 183 | VKAPI_ATTR void GetPhysicalDeviceExternalBufferProperties( |
| 184 | VkPhysicalDevice physicalDevice, |
| 185 | const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo, |
| 186 | VkExternalBufferProperties* pExternalBufferProperties); |
| 187 | VKAPI_ATTR void GetPhysicalDeviceExternalSemaphoreProperties( |
| 188 | VkPhysicalDevice physicalDevice, |
| 189 | const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo, |
| 190 | VkExternalSemaphoreProperties* pExternalSemaphoreProperties); |
| 191 | VKAPI_ATTR void GetPhysicalDeviceExternalFenceProperties( |
| 192 | VkPhysicalDevice physicalDevice, |
| 193 | const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, |
| 194 | VkExternalFenceProperties* pExternalFenceProperties); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 195 | |
| 196 | template <typename DispatchableType> |
| 197 | void StaticAssertDispatchable(DispatchableType) { |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 198 | static_assert( |
| 199 | std::is_same<DispatchableType, VkInstance>::value || |
| 200 | std::is_same<DispatchableType, VkPhysicalDevice>::value || |
| 201 | std::is_same<DispatchableType, VkDevice>::value || |
| 202 | std::is_same<DispatchableType, InstanceDispatchable>::value || |
| 203 | std::is_same<DispatchableType, VkQueue>::value || |
| 204 | std::is_same<DispatchableType, VkCommandBuffer>::value || |
| 205 | std::is_same<DispatchableType, DeviceDispatchable>::value, |
| 206 | "unrecognized dispatchable type"); |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | template <typename DispatchableType> |
| 210 | bool SetDataInternal(DispatchableType dispatchable, const void* data) { |
| 211 | StaticAssertDispatchable(dispatchable); |
| 212 | |
| 213 | hwvulkan_dispatch_t* dispatch = |
| 214 | reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); |
| 215 | // must be magic or already set |
| 216 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { |
| 217 | ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | dispatch->vtbl = data; |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | template <typename DispatchableType> |
| 227 | void* GetDataInternal(DispatchableType dispatchable) { |
| 228 | StaticAssertDispatchable(dispatchable); |
| 229 | |
| 230 | const hwvulkan_dispatch_t* dispatch = |
| 231 | reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); |
| 232 | |
| 233 | return const_cast<void*>(dispatch->vtbl); |
| 234 | } |
| 235 | |
| 236 | inline bool SetData(VkInstance instance, const InstanceData& data) { |
| 237 | return SetDataInternal(instance, &data); |
| 238 | } |
| 239 | |
| 240 | inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { |
| 241 | return SetDataInternal(physical_dev, &data); |
| 242 | } |
| 243 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 244 | inline bool SetData(InstanceDispatchable dispatchable, |
| 245 | const InstanceData& data) { |
| 246 | return SetDataInternal(dispatchable, &data); |
| 247 | } |
| 248 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 249 | inline bool SetData(VkDevice dev, const DeviceData& data) { |
| 250 | return SetDataInternal(dev, &data); |
| 251 | } |
| 252 | |
| 253 | inline bool SetData(VkQueue queue, const DeviceData& data) { |
| 254 | return SetDataInternal(queue, &data); |
| 255 | } |
| 256 | |
| 257 | inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { |
| 258 | return SetDataInternal(cmd, &data); |
| 259 | } |
| 260 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 261 | inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) { |
| 262 | return SetDataInternal(dispatchable, &data); |
| 263 | } |
| 264 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 265 | inline InstanceData& GetData(VkInstance instance) { |
| 266 | return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); |
| 267 | } |
| 268 | |
| 269 | inline InstanceData& GetData(VkPhysicalDevice physical_dev) { |
| 270 | return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); |
| 271 | } |
| 272 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 273 | inline InstanceData& GetData(InstanceDispatchable dispatchable) { |
| 274 | return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable)); |
| 275 | } |
| 276 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 277 | inline DeviceData& GetData(VkDevice dev) { |
| 278 | return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); |
| 279 | } |
| 280 | |
| 281 | inline DeviceData& GetData(VkQueue queue) { |
| 282 | return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); |
| 283 | } |
| 284 | |
| 285 | inline DeviceData& GetData(VkCommandBuffer cmd) { |
| 286 | return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); |
| 287 | } |
| 288 | |
Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 289 | inline DeviceData& GetData(DeviceDispatchable dispatchable) { |
| 290 | return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable)); |
| 291 | } |
| 292 | |
Chia-I Wu | bc011fc | 2016-05-03 12:19:55 +0800 | [diff] [blame] | 293 | template <typename DispatchableType> |
| 294 | const DebugReportLogger Logger(DispatchableType dispatchable) { |
| 295 | return DebugReportLogger(GetData(dispatchable).debug_report_callbacks); |
| 296 | } |
| 297 | |
Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 298 | } // namespace driver |
| 299 | } // namespace vulkan |
| 300 | |
| 301 | #endif // LIBVULKAN_DRIVER_H |