| 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 | #include <GL/gl.h> |
| 40 | #undef GL_ARB_imaging |
| 41 | #undef GL_VERSION_1_3 |
| 42 | #include <GL/glext.h> |
| 43 | |
| 44 | #include <X11/Xwindows.h> |
| 45 | #include <os.h> |
| 46 | #include "glwindows.h" |
| 47 | #include <glx/glxserver.h> |
| 48 | |
| 49 | static HMODULE hMod = NULL; |
| 50 | |
| 51 | /* |
| 52 | Implement the __glGetProcAddress function by just using GetProcAddress() on the selected DLL |
| 53 | */ |
| 54 | void *glXGetProcAddressARB(const char *symbol) |
| 55 | { |
| 56 | void *proc; |
| 57 | |
| 58 | /* Default to the mesa GL implementation if one hasn't been selected yet */ |
| 59 | if (!hMod) |
| 60 | glWinSelectImplementation(0); |
| 61 | |
| 62 | proc = GetProcAddress(hMod, symbol); |
| 63 | |
| 64 | if (glxWinDebugSettings.enableGLcallTrace) |
| 65 | ErrorF("glXGetProcAddressARB: Resolved '%s' in %p to %p\n", symbol, hMod, proc); |
| 66 | |
| 67 | return proc; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | Select a GL implementation DLL |
| 72 | */ |
| 73 | int glWinSelectImplementation(int native) |
| 74 | { |
| 75 | const char *dllname; |
| 76 | |
| 77 | if (native) { |
| 78 | dllname = "cygnativeGLthunk.dll"; |
| 79 | } |
| 80 | else { |
| 81 | dllname = "cygGL-1.dll"; |
| 82 | } |
| 83 | |
| 84 | hMod = LoadLibraryEx(dllname, NULL, 0); |
| 85 | if (hMod == NULL) { |
| 86 | ErrorF("glWinSelectGLimplementation: Could not load '%s'\n", dllname); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | ErrorF("glWinSelectGLimplementation: Loaded '%s'\n", dllname); |
| 91 | |
| 92 | /* Connect __glGetProcAddress() to our implementation of glXGetProcAddressARB() above */ |
| 93 | __glXsetGetProcAddress((glx_gpa_proc)glXGetProcAddressARB); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | #define RESOLVE_RET(proctype, symbol, retval) \ |
| 99 | proctype proc = (proctype)glXGetProcAddressARB(symbol); \ |
| 100 | if (proc == NULL) return retval; |
| 101 | |
| 102 | #define RESOLVE(proctype, symbol) RESOLVE_RET(proctype, symbol,) |
| 103 | #define RESOLVED_PROC proc |
| 104 | |
| 105 | /* Include generated shims for direct linkage to GL functions which are in the ABI */ |
| 106 | #include "generated_gl_shim.c" |
| 107 | |
| 108 | /* |
| 109 | Special wrapper for glAddSwapHintRectWIN for copySubBuffers |
| 110 | |
| 111 | Only used with native GL if the GL_WIN_swap_hint extension is present, so we enable |
| 112 | GLX_MESA_copy_sub_buffer |
| 113 | */ |
| 114 | typedef void (__stdcall * PFNGLADDSWAPHINTRECTWIN) (GLint x, GLint y, |
| 115 | GLsizei width, |
| 116 | GLsizei height); |
| 117 | |
| 118 | void |
| 119 | glAddSwapHintRectWINWrapper(GLint x, GLint y, GLsizei width, |
| 120 | GLsizei height) |
| 121 | { |
| 122 | RESOLVE(PFNGLADDSWAPHINTRECTWIN, "glAddSwapHintRectWIN"); |
| 123 | RESOLVED_PROC(x, y, width, height); |
| 124 | } |