1 2 // Copyright Michael D. Parker 2018. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 module bindbc.opengl.gl; 8 9 import bindbc.loader; 10 import bindbc.opengl.config, 11 bindbc.opengl.context; 12 13 private { 14 SharedLib lib; 15 GLSupport contextVersion; 16 GLSupport loadedVersion; 17 } 18 19 @nogc nothrow: 20 21 GLSupport openGLContextVersion() { return contextVersion; } 22 GLSupport loadedOpenGLVersion() { return loadedVersion; } 23 bool isOpenGLLoaded() { return lib != invalidHandle; } 24 25 26 void unloadOpenGL() 27 { 28 if(lib != invalidHandle) { 29 lib.unload(); 30 } 31 } 32 33 GLSupport loadOpenGL() 34 { 35 version(Windows) { 36 const(char)[][1] libNames = ["OpenGL32.dll"]; 37 } 38 else version(OSX) { 39 const(char)[][3] libNames = [ 40 "../Frameworks/OpenGL.framework/OpenGL", 41 "/Library/Frameworks/OpenGL.framework/OpenGL", 42 "/System/Library/Frameworks/OpenGL.framework/OpenGL" 43 ]; 44 } 45 else version(Posix) { 46 const(char)[][2] libNames = [ 47 "libGL.so", 48 "libGL.so.1" 49 ]; 50 } 51 else static assert(0, "bindbc-opengl is not yet supported on this platform"); 52 53 GLSupport ret; 54 foreach(name; libNames) { 55 ret = loadOpenGL(name.ptr); 56 if(ret != GLSupport.noLibrary) break; 57 } 58 return ret; 59 } 60 61 GLSupport loadOpenGL(const(char)* libName) 62 { 63 import bindbc.opengl.bind; 64 65 // If the library isn't yet loaded, load it now. 66 if(lib == invalidHandle) { 67 lib = load(libName); 68 if(lib == invalidHandle) { 69 return GLSupport.noLibrary; 70 } 71 } 72 73 // Before attempting to load *any* symbols, make sure a context 74 // has been activated. This is only a requirement on Windows, and 75 // only in specific cases, but always checking for it makes for 76 // uniformity across platforms and no surprises when porting client 77 // code from other platforms to Windows. 78 contextVersion = getContextVersion(lib); 79 if(contextVersion < GLSupport.gl11) return contextVersion; 80 81 // Load the base library 82 if(!lib.loadGL11()) return GLSupport.badLibrary; 83 84 // Now load the context-dependent stuff. Always try to load up to 85 // OpenGL 2.1 by default. Load higher only if configured to do so. 86 auto loadedVersion = lib.loadGL21(contextVersion); 87 static if(glSupport >= GLSupport.gl30) loadedVersion = lib.loadGL30(contextVersion); 88 static if(glSupport >= GLSupport.gl31) loadedVersion = lib.loadGL31(contextVersion); 89 static if(glSupport >= GLSupport.gl32) loadedVersion = lib.loadGL32(contextVersion); 90 static if(glSupport >= GLSupport.gl33) loadedVersion = lib.loadGL33(contextVersion); 91 static if(glSupport >= GLSupport.gl40) loadedVersion = lib.loadGL40(contextVersion); 92 static if(glSupport >= GLSupport.gl41) loadedVersion = lib.loadGL41(contextVersion); 93 static if(glSupport >= GLSupport.gl42) loadedVersion = lib.loadGL42(contextVersion); 94 static if(glSupport >= GLSupport.gl43) loadedVersion = lib.loadGL43(contextVersion); 95 static if(glSupport >= GLSupport.gl44) loadedVersion = lib.loadGL44(contextVersion); 96 static if(glSupport >= GLSupport.gl45) loadedVersion = lib.loadGL45(contextVersion); 97 static if(glSupport >= GLSupport.gl46) loadedVersion = lib.loadGL46(contextVersion); 98 99 // From any GL versions higher than the one loaded, load the core ARB 100 // extensions. 101 if(loadedVersion < GLSupport.gl30) lib.loadARB30(loadedVersion); 102 if(loadedVersion < GLSupport.gl31) lib.loadARB31(loadedVersion); 103 if(loadedVersion < GLSupport.gl32) lib.loadARB32(loadedVersion); 104 if(loadedVersion < GLSupport.gl33) lib.loadARB33(loadedVersion); 105 if(loadedVersion < GLSupport.gl40) lib.loadARB40(loadedVersion); 106 if(loadedVersion < GLSupport.gl41) lib.loadARB41(loadedVersion); 107 if(loadedVersion < GLSupport.gl42) lib.loadARB42(loadedVersion); 108 if(loadedVersion < GLSupport.gl43) lib.loadARB43(loadedVersion); 109 if(loadedVersion < GLSupport.gl44) lib.loadARB44(loadedVersion); 110 if(loadedVersion < GLSupport.gl45) lib.loadARB45(loadedVersion); 111 if(loadedVersion < GLSupport.gl46) lib.loadARB46(loadedVersion); 112 113 // Load all other supported extensions 114 loadARB(lib, contextVersion); 115 116 return loadedVersion; 117 }