| 1 | #include <vulkan.h> |
| 2 | #include <dlfcn.h> |
| 3 | |
| 4 | #include <GL/glx.h> |
| 5 | |
| 6 | #include <string> |
| 7 | #include <memory> |
| 8 | #include <iostream> |
| 9 | #include <functional> |
| 10 | |
| 11 | extern "C" VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion); |
| 12 | |
| 13 | |
| 14 | #ifndef NV_DRIVER_PATH |
| 15 | #define NV_DRIVER_PATH "/usr/lib/x86_64-linux-gnu/nvidia/libGL.so.1:libGLX_nvidia.so.0" |
| 16 | #endif |
| 17 | #ifndef NV_BUMBLEBEE_DISPLAY |
| 18 | #define NV_BUMBLEBEE_DISPLAY ":8" |
| 19 | #endif |
| 20 | |
| 21 | typedef void* dlsym_fn(void *, const char*); |
| 22 | |
| 23 | #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 |
| 24 | #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 |
| 25 | typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*); |
| 26 | |
| 27 | typedef void *(*getproc )(const char* name); |
| 28 | |
| 29 | class BBGLXContext { |
| 30 | public: |
| 31 | Display *dpy; |
| 32 | Window win; |
| 33 | GLXContext ctx; |
| 34 | public: |
| 35 | BBGLXContext() = default; |
| 36 | BBGLXContext(const BBGLXContext ©) = delete; |
| 37 | BBGLXContext(const char* display){ |
| 38 | dpy = XOpenDisplay(display); |
| 39 | if(!dpy){ |
| 40 | std::cerr << "Can't open bumblebee display.\n"; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | int nelements; |
| 45 | GLXFBConfig *fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), 0, &nelements); |
| 46 | static int attributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None }; |
| 47 | XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy),attributeList); |
| 48 | |
| 49 | XSetWindowAttributes swa; |
| 50 | swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone); |
| 51 | swa.border_pixel = 0; |
| 52 | swa.event_mask = StructureNotifyMask; |
| 53 | win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 100, 100, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa); |
| 54 | XFree(vi); |
| 55 | |
| 56 | GLXCREATECONTEXTATTRIBSARBPROC pfn_glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB"); |
| 57 | |
| 58 | int attribs[] = { |
| 59 | GLX_CONTEXT_MAJOR_VERSION_ARB, 3, |
| 60 | GLX_CONTEXT_MINOR_VERSION_ARB, 0, |
| 61 | 0}; |
| 62 | ctx = pfn_glXCreateContextAttribsARB(dpy, *fbc, 0, true, attribs); |
| 63 | XFree(fbc); |
| 64 | } |
| 65 | ~BBGLXContext() { |
| 66 | if(dpy != nullptr) { |
| 67 | glXDestroyContext(dpy, ctx); |
| 68 | XDestroyWindow(dpy, win); |
| 69 | XCloseDisplay(dpy); |
| 70 | } |
| 71 | } |
| 72 | BBGLXContext& operator=(BBGLXContext&& other) noexcept { |
| 73 | dpy = other.dpy; |
| 74 | win = other.win; |
| 75 | ctx = other.ctx; |
| 76 | other.dpy = nullptr; |
| 77 | return *this; |
| 78 | } |
| 79 | bool isValid(){ |
| 80 | return dpy != nullptr; |
| 81 | } |
| 82 | static void deleter(int *){ |
| 83 | } |
| 84 | std::unique_ptr<void, std::function<void(void*)>> makeCurrent(){ |
| 85 | glXMakeCurrent (dpy, win, ctx); |
| 86 | return std::unique_ptr<void, std::function<void(void*)>>(this, [this](void *p){ |
| 87 | glXMakeCurrent (dpy, 0, 0); |
| 88 | }); |
| 89 | } |
| 90 | }; |
| 91 | class VulkanIcd { |
| 92 | protected: |
| 93 | VKAPI_ATTR PFN_vkVoidFunction (*instanceProcAddr) (VkInstance instance, |
| 94 | const char* pName) = nullptr; |
| 95 | VKAPI_ATTR PFN_vkVoidFunction (*phyProcAddr) (VkInstance instance, |
| 96 | const char* pName) = nullptr; |
| 97 | VKAPI_ATTR VkResult VKAPI_CALL (*negotiateVersion)(uint32_t* pSupportedVersion) = nullptr; |
| 98 | public: |
| 99 | VulkanIcd() = default; |
| 100 | virtual ~VulkanIcd() = default; |
| 101 | virtual PFN_vkVoidFunction vk_icdGetInstanceProcAddr( |
| 102 | VkInstance instance, |
| 103 | const char* pName){ |
| 104 | return instanceProcAddr(instance, pName); |
| 105 | } |
| 106 | virtual PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(VkInstance instance, |
| 107 | const char* pName) { |
| 108 | return phyProcAddr(instance, pName); |
| 109 | } |
| 110 | virtual VkResult vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion) { |
| 111 | return negotiateVersion(pSupportedVersion); |
| 112 | } |
| 113 | |
| 114 | }; |
| 115 | class ExternalVulkanIcd: public VulkanIcd { |
| 116 | void *libnv; |
| 117 | public: |
| 118 | ExternalVulkanIcd(){ |
| 119 | libnv = dlopen("libGLX_nvidia.so.0", RTLD_LOCAL | RTLD_LAZY); |
| 120 | auto fn2 = [this](const char* name) { |
| 121 | return (void *) dlsym(this->libnv, name); |
| 122 | }; |
| 123 | instanceProcAddr = (decltype(instanceProcAddr)) fn2("vk_icdGetInstanceProcAddr"); |
| 124 | phyProcAddr = (decltype(phyProcAddr)) fn2("vk_icdGetPhysicalDeviceProcAddr"); |
| 125 | negotiateVersion = (decltype(negotiateVersion)) fn2("vk_icdNegotiateLoaderICDInterfaceVersion"); |
| 126 | } |
| 127 | ~ExternalVulkanIcd(){ |
| 128 | dlclose(libnv); |
| 129 | } |
| 130 | VkResult vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion) { |
| 131 | char *prev = getenv("DISPLAY"); |
| 132 | std::string old{prev}; |
| 133 | setenv("DISPLAY", NV_BUMBLEBEE_DISPLAY, 1); |
| 134 | auto res = negotiateVersion(pSupportedVersion); |
| 135 | setenv("DISPLAY",old.c_str(), 1); |
| 136 | return res; |
| 137 | } |
| 138 | }; |
| 139 | PFN_vkVoidFunction getOverrideFn(const char *pName); |
| 140 | class InternalVulkanIcd: public VulkanIcd { |
| 141 | public: |
| 142 | PFN_vkGetDeviceProcAddr getDeviceProcAddr; |
| 143 | |
| 144 | PFN_vkCreateInstance createInstance; |
| 145 | PFN_vkDestroyInstance destroyInstance; |
| 146 | PFN_vkCreateDevice createDevice; |
| 147 | PFN_vkDestroyDevice destroyDevice; |
| 148 | PFN_vkGetDeviceQueue getDeviceQueue; |
| 149 | PFN_vkCreateSwapchainKHR createSwapchainKHR; |
| 150 | PFN_vkDestroySwapchainKHR destroySwapchainKHR; |
| 151 | PFN_vkQueuePresentKHR queuePresentKHR; |
| 152 | PFN_vkQueueSubmit queueSubmit; |
| 153 | //PFN_vkAllocateMemory; |
| 154 | //PFN_vkBindBufferMemory |
| 155 | //PFN_vkBindImageMemory; |
| 156 | //PFN_vkAcquireNextImageKHR; |
| 157 | public: |
| 158 | InternalVulkanIcd(BBGLXContext &glx){ |
| 159 | auto guard = glx.makeCurrent(); |
| 160 | getproc fn2 = (getproc) glXGetProcAddress((const GLubyte*) "glGetVkProcAddrNV"); |
| 161 | // getproc fn2 = (getproc) glXGetProcAddress((const GLubyte*) "ex7991765ed"); |
| 162 | void *glfn = fn2("vk_icdGetInstanceProcAddr"); |
| 163 | |
| 164 | instanceProcAddr = (decltype(instanceProcAddr)) fn2("vk_icdGetInstanceProcAddr"); |
| 165 | phyProcAddr = (decltype(phyProcAddr)) fn2("vk_icdGetPhysicalDeviceProcAddr"); |
| 166 | negotiateVersion = (decltype(negotiateVersion)) fn2("vk_icdNegotiateLoaderICDInterfaceVersion"); |
| 167 | |
| 168 | createInstance = (decltype(createInstance)) fn2("vkCreateInstance"); |
| 169 | destroyInstance = (decltype(destroyInstance)) fn2("vkDestroyInstance"); |
| 170 | createDevice = (decltype(createDevice)) fn2("vkCreateDevice"); |
| 171 | destroyDevice = (decltype(destroyDevice)) fn2("vkDestroyDevice"); |
| 172 | getDeviceProcAddr = (decltype(getDeviceProcAddr)) fn2("vkGetDeviceProcAddr"); |
| 173 | getDeviceQueue = (decltype(getDeviceQueue)) fn2("vkGetDeviceQueue"); |
| 174 | createSwapchainKHR = (decltype(createSwapchainKHR)) fn2("vkCreateSwapchainKHR"); |
| 175 | destroySwapchainKHR = (decltype(destroySwapchainKHR)) fn2("vkDestroySwapchainKHR"); |
| 176 | queuePresentKHR = (decltype(queuePresentKHR)) fn2("vkQueuePresentKHR"); |
| 177 | queueSubmit = (decltype(queueSubmit)) fn2("vkQueueSubmit"); |
| 178 | } |
| 179 | virtual PFN_vkVoidFunction vk_icdGetInstanceProcAddr( |
| 180 | VkInstance instance, |
| 181 | const char* pName){ |
| 182 | auto internalFn = instanceProcAddr(instance, pName); |
| 183 | if(internalFn != nullptr){ |
| 184 | auto overrideFnc = getOverrideFn(pName); |
| 185 | if(overrideFnc != nullptr){ |
| 186 | return overrideFnc; |
| 187 | } |
| 188 | } |
| 189 | return internalFn; |
| 190 | } |
| 191 | VkResult vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion) { |
| 192 | return VulkanIcd::vk_icdNegotiateLoaderICDInterfaceVersion(pSupportedVersion); |
| 193 | } |
| 194 | |
| 195 | }; |
| 196 | |
| 197 | class StaticInitialize { |
| 198 | void *nvDriver; |
| 199 | void *glLibGL; |
| 200 | public: |
| 201 | BBGLXContext glx; |
| 202 | std::unique_ptr<VulkanIcd> icd; |
| 203 | |
| 204 | public: |
| 205 | StaticInitialize(){ |
| 206 | // Load libGL from LD_LIBRARY_PATH before loading the NV-driver (unluckily also named libGL |
| 207 | // This ensures that ld.so will find this libGL before the Nvidia one, when |
| 208 | // again asked to load libGL. |
| 209 | glLibGL = dlopen("libGL.so.1", RTLD_GLOBAL | RTLD_NOW); |
| 210 | glx = BBGLXContext(NV_BUMBLEBEE_DISPLAY); |
| 211 | if(!glx.isValid()){ |
| 212 | return; |
| 213 | } |
| 214 | // icd = std::unique_ptr<ExternalVulkanIcd>(new ExternalVulkanIcd()); |
| 215 | icd = std::unique_ptr<InternalVulkanIcd>(new InternalVulkanIcd(glx)); |
| 216 | |
| 217 | } |
| 218 | ~StaticInitialize(){ |
| 219 | dlclose(glLibGL); |
| 220 | } |
| 221 | bool IsInited(){ |
| 222 | return icd != nullptr; |
| 223 | } |
| 224 | InternalVulkanIcd &internal(){ |
| 225 | return *reinterpret_cast<InternalVulkanIcd*>(icd.get()); |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | StaticInitialize init; |
| 230 | |
| 231 | template<typename PFN, PFN InternalVulkanIcd::*p, typename... Args> |
| 232 | VKAPI_ATTR auto VKAPI_CALL forward(Args... args) -> decltype((init.internal().*p)(args...)) { |
| 233 | auto guard = init.glx.makeCurrent(); |
| 234 | return (init.internal().*p)(args...); |
| 235 | } |
| 236 | |
| 237 | template<auto p, typename PFN = typename std::remove_reference<decltype(init.internal().*p)>::type> |
| 238 | auto forwarder() -> PFN { |
| 239 | return &forward<PFN, p>; |
| 240 | } |
| 241 | PFN_vkVoidFunction vk_GetDeviceProcAddr( |
| 242 | VkDevice device, |
| 243 | const char* pName){ |
| 244 | auto ret = init.internal().getDeviceProcAddr(device, pName); |
| 245 | if(ret != nullptr){ |
| 246 | auto r2 = getOverrideFn(pName); |
| 247 | if(r2 != nullptr){ |
| 248 | return r2; |
| 249 | } |
| 250 | } |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | extern "C" VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr( |
| 255 | VkInstance instance, |
| 256 | const char* pName){ |
| 257 | if (!init.IsInited()) return nullptr; |
| 258 | return init.icd->vk_icdGetInstanceProcAddr(instance, pName); |
| 259 | } |
| 260 | |
| 261 | PFN_vkVoidFunction getOverrideFn(const char *pName){ |
| 262 | std::string name = pName; |
| 263 | if(name == "vkGetInstanceProcAddr") { |
| 264 | return (PFN_vkVoidFunction) vk_icdGetInstanceProcAddr; |
| 265 | }else if(name == "vkGetDeviceProcAddr") { |
| 266 | return (PFN_vkVoidFunction) vk_GetDeviceProcAddr; |
| 267 | }else if(name == "vkCreateInstance") { |
| 268 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::createInstance>(); |
| 269 | }else if(name == "vkDestroyInstance") { |
| 270 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::destroyInstance>(); |
| 271 | }else if(name == "vkCreateDevice") { |
| 272 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::createDevice>(); |
| 273 | }else if(name == "vkDestroyDevice") { |
| 274 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::destroyDevice>(); |
| 275 | }else if(name == "vkGetDeviceQueue") { |
| 276 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::getDeviceQueue>(); |
| 277 | }else if(name == "vkCreateSwapchainKHR") { |
| 278 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::createSwapchainKHR>(); |
| 279 | }else if(name == "vkDestroySwapchainKHR") { |
| 280 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::destroySwapchainKHR>(); |
| 281 | }else if(name == "vkQueuePresentKHR") { |
| 282 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::queuePresentKHR>(); |
| 283 | }else if(name == "vkQueueSubmit") { |
| 284 | return (PFN_vkVoidFunction) forwarder<&InternalVulkanIcd::queueSubmit>(); |
| 285 | } |
| 286 | return nullptr; |
| 287 | } |
| 288 | |
| 289 | extern "C" VKAPI_ATTR PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(VkInstance instance, |
| 290 | const char* pName){ |
| 291 | if (!init.IsInited()) return nullptr; |
| 292 | return init.icd->vk_icdGetPhysicalDeviceProcAddr(instance, pName); |
| 293 | } |
| 294 | extern "C" VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion){ |
| 295 | if (!init.IsInited()) { |
| 296 | return VK_ERROR_INCOMPATIBLE_DRIVER; |
| 297 | } |
| 298 | return init.icd->vk_icdNegotiateLoaderICDInterfaceVersion(pSupportedVersion); |
| 299 | } |