1 /*
2 * File: glshim.c
3 * Purpose: GL shim which redirects to a specified DLL
4 *
5 * Copyright (c) Jon TURNEY 2013
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 A GL shim which redirects to a specified DLL
28
29 XWin is statically linked with this, rather than the system libGL, so that
30 GL calls can be directed to mesa cygGL-1.dll, or cygnativeGLthunk.dll
31 (which contains cdecl-to-stdcall thunks to the native openGL32.dll)
32 */
33
34 #ifdef HAVE_XWIN_CONFIG_H
35 #include <xwin-config.h>
36 #endif
37
38 #define GL_GLEXT_LEGACY
39 #define GL_GLEXT_PROTOTYPES
40 #include <GL/gl.h>
41 #undef GL_ARB_imaging
42 #undef GL_VERSION_1_3
43 #include <GL/glext.h>
44
45 #include <X11/Xwindows.h>
46 #include <os.h>
47 #include "glwindows.h"
48 #include <glx/glxserver.h>
49
50 extern void *glXGetProcAddressARB(const char *);
51
52 static HMODULE hMod = NULL;
53
54 /*
55 Implement the __glGetProcAddress function by just using GetProcAddress() on the selected DLL
56 */
glXGetProcAddressARB(const char * symbol)57 void *glXGetProcAddressARB(const char *symbol)
58 {
59 void *proc;
60
61 /* Default to the mesa GL implementation if one hasn't been selected yet */
62 if (!hMod)
63 glWinSelectImplementation(0);
64
65 proc = GetProcAddress(hMod, symbol);
66
67 if (glxWinDebugSettings.enableGLcallTrace)
68 ErrorF("glXGetProcAddressARB: Resolved '%s' in %p to %p\n", symbol, hMod, proc);
69
70 return proc;
71 }
72
73 /*
74 Select a GL implementation DLL
75 */
glWinSelectImplementation(int native)76 int glWinSelectImplementation(int native)
77 {
78 const char *dllname;
79
80 if (native) {
81 dllname = "cygnativeGLthunk.dll";
82 }
83 else {
84 dllname = "cygGL-1.dll";
85 }
86
87 hMod = LoadLibraryEx(dllname, NULL, 0);
88 if (hMod == NULL) {
89 ErrorF("glWinSelectGLimplementation: Could not load '%s'\n", dllname);
90 return -1;
91 }
92
93 ErrorF("glWinSelectGLimplementation: Loaded '%s'\n", dllname);
94
95 /* Connect __glGetProcAddress() to our implementation of glXGetProcAddressARB() above */
96 __glXsetGetProcAddress((glx_gpa_proc)glXGetProcAddressARB);
97
98 return 0;
99 }
100
101 #define RESOLVE_RET(proctype, symbol, retval) \
102 proctype proc = (proctype)glXGetProcAddressARB(symbol); \
103 if (proc == NULL) return retval;
104
105 #define RESOLVE(proctype, symbol) RESOLVE_RET(proctype, symbol,)
106 #define RESOLVED_PROC proc
107
108 /* Include generated shims for direct linkage to GL functions which are in the ABI */
109 #include "generated_gl_shim.ic"
110
111 /*
112 Special wrapper for glAddSwapHintRectWIN for copySubBuffers
113
114 Only used with native GL if the GL_WIN_swap_hint extension is present, so we enable
115 GLX_MESA_copy_sub_buffer
116 */
117 typedef void (__stdcall * PFNGLADDSWAPHINTRECTWIN) (GLint x, GLint y,
118 GLsizei width,
119 GLsizei height);
120
121 void
glAddSwapHintRectWINWrapper(GLint x,GLint y,GLsizei width,GLsizei height)122 glAddSwapHintRectWINWrapper(GLint x, GLint y, GLsizei width,
123 GLsizei height)
124 {
125 RESOLVE(PFNGLADDSWAPHINTRECTWIN, "glAddSwapHintRectWIN");
126 RESOLVED_PROC(x, y, width, height);
127 }
128