Jon Ashburn | 55585ed | 2016-05-11 16:57:26 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2015-2016 Valve Corporation |
| 4 | * Copyright (C) 2015-2016 LunarG, Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Jon Ashburn <jon@lunarg.com> |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #pragma once |
| 23 | #include <unordered_map> |
| 24 | #include "vulkan/vk_layer.h" |
| 25 | |
| 26 | |
| 27 | struct devExts { |
| 28 | bool wsi_swapchain_enabled; |
| 29 | }; |
| 30 | struct instExts { |
| 31 | bool wsi_surface_enabled; |
| 32 | bool wsi_surf_xcb_enabled; |
| 33 | bool wsi_surf_xlib_enabled; |
| 34 | bool wsi_surf_mir_enabled; |
| 35 | bool wsi_surf_wayland_enabled; |
| 36 | bool wsi_surf_win32_enabled; |
| 37 | bool debug_report_enabled; |
| 38 | }; |
| 39 | //TODO remove this maps and use the wrapped object Vkdevice object instead |
| 40 | static std::unordered_map<void *, struct devExts> deviceExtMap; |
| 41 | |
| 42 | struct wrapped_phys_dev_obj { |
| 43 | VkLayerInstanceDispatchTable *loader_disp; |
| 44 | struct wrapped_inst_obj *inst; // parent instance object |
| 45 | void *obj; |
| 46 | }; |
| 47 | |
| 48 | struct wrapped_inst_obj { |
| 49 | VkLayerInstanceDispatchTable *loader_disp; |
| 50 | instExts exts; |
| 51 | VkLayerInstanceDispatchTable layer_disp; //this layer's dispatch table |
| 52 | PFN_vkSetInstanceLoaderData pfn_inst_init; |
| 53 | struct wrapped_phys_dev_obj *ptr_phys_devs; // any enumerated phys devs |
| 54 | VkInstance obj; |
| 55 | }; |
| 56 | |
| 57 | struct wrapped_dev_obj { |
| 58 | VkLayerDispatchTable *disp; |
| 59 | devExts exts; //TODO use this |
| 60 | VkLayerInstanceDispatchTable *layer_disp; // TODO use this |
| 61 | PFN_vkSetDeviceLoaderData pfn_dev_init; //TODO use this |
| 62 | void *obj; |
| 63 | }; |
| 64 | |
| 65 | static inline VkInstance unwrap_instance(const VkInstance instance, wrapped_inst_obj **inst) { |
| 66 | *inst = reinterpret_cast<wrapped_inst_obj *> (instance); |
| 67 | return (*inst)->obj; |
| 68 | } |
| 69 | |
| 70 | static inline VkPhysicalDevice unwrap_phys_dev(const VkPhysicalDevice physical_device, wrapped_phys_dev_obj **phys_dev) { |
| 71 | *phys_dev = reinterpret_cast<wrapped_phys_dev_obj *> (physical_device); |
| 72 | return reinterpret_cast <VkPhysicalDevice> ((*phys_dev)->obj); |
| 73 | } |
| 74 | |
| 75 | static void create_device_register_extensions(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) { |
| 76 | uint32_t i; |
| 77 | VkLayerDispatchTable *pDisp = device_dispatch_table(device); |
| 78 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
| 79 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)gpa(device, "vkCreateSwapchainKHR"); |
| 80 | pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR)gpa(device, "vkDestroySwapchainKHR"); |
| 81 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR)gpa(device, "vkGetSwapchainImagesKHR"); |
| 82 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR)gpa(device, "vkAcquireNextImageKHR"); |
| 83 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR"); |
| 84 | |
| 85 | deviceExtMap[pDisp].wsi_swapchain_enabled = false; |
| 86 | for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
| 87 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) |
| 88 | deviceExtMap[pDisp].wsi_swapchain_enabled = true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void create_instance_register_extensions(const VkInstanceCreateInfo *pCreateInfo, VkInstance instance, struct wrapped_inst_obj *inst) { |
| 93 | uint32_t i; |
| 94 | VkLayerInstanceDispatchTable *pDisp = &inst->layer_disp; |
| 95 | PFN_vkGetInstanceProcAddr gpa = pDisp->GetInstanceProcAddr; |
| 96 | |
| 97 | // KHR_surface |
| 98 | pDisp->DestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)gpa(instance, "vkDestroySurfaceKHR"); |
| 99 | pDisp->GetPhysicalDeviceSurfaceSupportKHR = |
| 100 | (PFN_vkGetPhysicalDeviceSurfaceSupportKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceSupportKHR"); |
| 101 | pDisp->GetPhysicalDeviceSurfaceCapabilitiesKHR = |
| 102 | (PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR"); |
| 103 | pDisp->GetPhysicalDeviceSurfaceFormatsKHR = |
| 104 | (PFN_vkGetPhysicalDeviceSurfaceFormatsKHR)gpa(instance, "vkGetPhysicalDeviceSurfaceFormatsKHR"); |
| 105 | pDisp->GetPhysicalDeviceSurfacePresentModesKHR = |
| 106 | (PFN_vkGetPhysicalDeviceSurfacePresentModesKHR)gpa(instance, "vkGetPhysicalDeviceSurfacePresentModesKHR"); |
| 107 | |
| 108 | // KHR_XXX_surface |
| 109 | #ifdef VK_USE_PLATFORM_XCB_KHR |
| 110 | pDisp->CreateXcbSurfaceKHR = (PFN_vkCreateXcbSurfaceKHR)gpa(instance, "vkCreateXcbSurfaceKHR"); |
| 111 | pDisp->GetPhysicalDeviceXcbPresentationSupportKHR = |
| 112 | (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceXcbPresentationSupportKHR"); |
| 113 | #endif // VK_USE_PLATFORM_XCB_KHR |
| 114 | |
| 115 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 116 | pDisp->CreateXlibSurfaceKHR = (PFN_vkCreateXlibSurfaceKHR)gpa(instance, "vkCreateXlibSurfaceKHR"); |
| 117 | pDisp->GetPhysicalDeviceXlibPresentationSupportKHR = |
| 118 | (PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceXlibPresentationSupportKHR"); |
| 119 | #endif // VK_USE_PLATFORM_XLIB_KHR |
| 120 | |
| 121 | #ifdef VK_USE_PLATFORM_MIR_KHR |
| 122 | pDisp->CreateMirSurfaceKHR = (PFN_vkCreateMirSurfaceKHR)gpa(instance, "vkCreateMirSurfaceKHR"); |
| 123 | pDisp->GetPhysicalDeviceMirPresentationSupportKHR = |
| 124 | (PFN_vkGetPhysicalDeviceMirPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceMirPresentationSupportKHR"); |
| 125 | #endif // VK_USE_PLATFORM_MIR_KHR |
| 126 | |
| 127 | #ifdef VK_USE_PLATFORM_WAYLAND_KHR |
| 128 | pDisp->CreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)gpa(instance, "vkCreateWaylandSurfaceKHR"); |
| 129 | pDisp->GetPhysicalDeviceWaylandPresentationSupportKHR = |
| 130 | (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR"); |
| 131 | #endif // VK_USE_PLATFORM_WAYLAND_KHR |
| 132 | |
| 133 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 134 | pDisp->CreateWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)gpa(instance, "vkCreateWin32SurfaceKHR"); |
| 135 | pDisp->GetPhysicalDeviceWin32PresentationSupportKHR = |
| 136 | (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)gpa(instance, "vkGetPhysicalDeviceWin32PresentationSupportKHR"); |
| 137 | #endif // VK_USE_PLATFORM_WIN32_KHR |
| 138 | |
| 139 | //EXT_debug_report |
| 140 | pDisp->CreateDebugReportCallbackEXT = |
| 141 | (PFN_vkCreateDebugReportCallbackEXT)gpa(instance, "vkCreateDebugReportCallbackEXT"); |
| 142 | pDisp->DestroyDebugReportCallbackEXT = |
| 143 | (PFN_vkDestroyDebugReportCallbackEXT)gpa(instance, "vkDestroyDebugReportCallbackEXT"); |
| 144 | pDisp->DebugReportMessageEXT = |
| 145 | (PFN_vkDebugReportMessageEXT)gpa(instance, "vkDebugReportMessageEXT"); |
| 146 | |
| 147 | |
| 148 | inst->exts.wsi_surface_enabled = false; |
| 149 | inst->exts.wsi_surf_xcb_enabled = false; |
| 150 | inst->exts.wsi_surf_xlib_enabled = false; |
| 151 | inst->exts.wsi_surf_mir_enabled = false; |
| 152 | inst->exts.wsi_surf_wayland_enabled = false; |
| 153 | inst->exts.wsi_surf_win32_enabled = false; |
| 154 | inst->exts.debug_report_enabled = false; |
| 155 | for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) { |
| 156 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SURFACE_EXTENSION_NAME) == 0) |
| 157 | inst->exts.wsi_surface_enabled = true; |
| 158 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) |
| 159 | inst->exts.debug_report_enabled = true; |
| 160 | #ifdef VK_USE_PLATFORM_XCB_KHR |
| 161 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME) == 0) |
| 162 | inst->exts.wsi_surf_xcb_enabled = true; |
| 163 | #endif // VK_USE_PLATFORM_XCB_KHR |
| 164 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 165 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XLIB_SURFACE_EXTENSION_NAME) == 0) |
| 166 | inst->exts.wsi_surf_xlib_enabled = true; |
| 167 | |
| 168 | #endif // VK_USE_PLATFORM_XLIB_KHR |
| 169 | #ifdef VK_USE_PLATFORM_WAYLAND_KHR |
| 170 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) == 0) |
| 171 | inst->exts.wsi_surf_wayland_enabled = true; |
| 172 | |
| 173 | #endif // VK_USE_PLATFORM_WAYLAND_KHR |
| 174 | #ifdef VK_USE_PLATFORM_MIR_KHR |
| 175 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_MIR_SURFACE_EXTENSION_NAME) == 0) |
| 176 | inst->exts.wsi_surf_mir_enabled = true; |
| 177 | |
| 178 | #endif // VK_USE_PLATFORM_MIR_KHR |
| 179 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 180 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WIN32_SURFACE_EXTENSION_NAME) == 0) |
| 181 | inst->exts.wsi_surf_win32_enabled = true; |
| 182 | #endif // VK_USE_PLATFORM_WIN32_KHR |
| 183 | |
| 184 | } |
| 185 | } |