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