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