1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of NV Libraries.
|
---|
5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
6 |
|
---|
7 | #include "nv/core/common.hh"
|
---|
8 | #include "nv/core/range.hh"
|
---|
9 | #include "nv/lib/gl.hh"
|
---|
10 |
|
---|
11 | #if defined( NV_GL_DYNAMIC )
|
---|
12 |
|
---|
13 | #include "nv/core/library.hh"
|
---|
14 |
|
---|
15 | #if defined( NV_SDL_GL )
|
---|
16 | # include "nv/lib/sdl.hh"
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | // for wgl support
|
---|
20 | #if NV_PLATFORM == NV_WINDOWS
|
---|
21 | # ifndef WIN32_LEAN_AND_MEAN
|
---|
22 | # define WIN32_LEAN_AND_MEAN 1
|
---|
23 | # endif
|
---|
24 | #include <windows.h>
|
---|
25 | # undef WIN32_LEAN_AND_MEAN
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | // extern added for wgl needs only!
|
---|
29 | #define NV_GL_FUN( rtype, fname, fparams ) extern "C" rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
|
---|
30 | #define NV_GL_FUN_REN( rtype, fname, rname, fparams ) extern "C" rtype (NV_GL_APIENTRY *rname) fparams = nullptr;
|
---|
31 | #define NV_GL_FUN_EXT NV_GL_FUN
|
---|
32 | #include <nv/lib/detail/gl_functions.inc>
|
---|
33 | #if NV_PLATFORM == NV_WINDOWS
|
---|
34 | #include <nv/lib/detail/wgl_functions.inc>
|
---|
35 | #endif
|
---|
36 | #include <nv/lib/detail/gl_ext/gl_ext_all_functions.inc>
|
---|
37 | #undef NV_GL_FUN_REN
|
---|
38 | #undef NV_GL_FUN_EXT
|
---|
39 | #undef NV_GL_FUN
|
---|
40 |
|
---|
41 | static nv::library gl_library;
|
---|
42 | static nv::gl_extensions gl_loaded_extensions = nv::gl_extensions(0);
|
---|
43 | extern "C" void* (NV_GL_APIENTRY *gl_ext_loader) ( const char* ) = nullptr;
|
---|
44 | static bool gl_library_loaded = false;
|
---|
45 | static bool wgl_library_loaded = false;
|
---|
46 |
|
---|
47 | static const char *gl_extension_names[] = {
|
---|
48 | "UNKNOWN",
|
---|
49 | #define NV_GL_EXTENSION( count, id, name ) "GL_EXT_"#id,
|
---|
50 | #include <nv/lib/detail/gl_ext/gl_ext_info.inc>
|
---|
51 | #undef NV_GL_EXTENSION
|
---|
52 | };
|
---|
53 |
|
---|
54 | static const char *gl_extension_ids[] = {
|
---|
55 | "UNKNOWN",
|
---|
56 | #define NV_GL_EXTENSION( count, id, name ) #id,
|
---|
57 | #include <nv/lib/detail/gl_ext/gl_ext_info.inc>
|
---|
58 | #undef NV_GL_EXTENSION
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 | static void* load_gl_ext_symbol_impl( const char* name, nv::log_level fail_level = nv::LOG_DEBUG )
|
---|
63 | {
|
---|
64 | void* result = gl_ext_loader( name );
|
---|
65 | NV_LOG( ( result ? nv::LOG_DEBUG : fail_level ), "load_gl_ext_symbol : " << name << ( result ? " succeded." : "failed." ) );
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static void* load_gl_ext_symbol( const char* name, bool iterate, const char* ext )
|
---|
70 | {
|
---|
71 | void * result = nullptr;
|
---|
72 | result = load_gl_ext_symbol_impl( ext ? ( std::string(name) + ext ).c_str() : name );
|
---|
73 | if ( result ) return result;
|
---|
74 | if ( iterate )
|
---|
75 | {
|
---|
76 | result = gl_ext_loader( (std::string(name) + "ARB").c_str() );
|
---|
77 | if ( result ) return result;
|
---|
78 | result = gl_ext_loader( (std::string(name) + "EXT").c_str() );
|
---|
79 | if ( result ) return result;
|
---|
80 | }
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool nv::load_gl_library( const char* path, bool force_reload )
|
---|
85 | {
|
---|
86 | if ( gl_library_loaded && !force_reload ) return true;
|
---|
87 | #if defined( NV_SDL_GL )
|
---|
88 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
89 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
90 | *(void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
|
---|
91 | #else
|
---|
92 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
93 |
|
---|
94 | # if NV_PLATFORM == NV_WINDOWS
|
---|
95 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
96 | *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
|
---|
97 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
98 | # elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
|
---|
99 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
100 | *(void **) (&gl_ext_loader) = gl_library.get("glXGetProcAddress");
|
---|
101 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
102 | # else
|
---|
103 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
104 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
105 | # endif
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | # define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
|
---|
109 | # define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
|
---|
110 | # include <nv/lib/detail/gl_functions.inc>
|
---|
111 | # undef NV_GL_FUN_EXT
|
---|
112 | # undef NV_GL_FUN
|
---|
113 |
|
---|
114 | # undef NV_GL_LOAD
|
---|
115 | # undef NV_GL_LOAD_EXT
|
---|
116 | gl_library_loaded = true;
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */, bool force_reload )
|
---|
121 | {
|
---|
122 | if ( wgl_library_loaded && !force_reload ) return true;
|
---|
123 | #if NV_PLATFORM == NV_WINDOWS
|
---|
124 | #if defined( NV_SDL_GL )
|
---|
125 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
126 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
127 | # define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
|
---|
128 | (void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
|
---|
129 | #else //
|
---|
130 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
131 |
|
---|
132 | *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
|
---|
133 | #define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
134 | #define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
135 | #define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
|
---|
136 | #endif
|
---|
137 | # define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
|
---|
138 | # define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
|
---|
139 | # define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
|
---|
140 | # include <nv/lib/detail/wgl_functions.inc>
|
---|
141 | # undef NV_GL_FUN_REN
|
---|
142 | # undef NV_GL_FUN_EXT
|
---|
143 | # undef NV_GL_FUN
|
---|
144 |
|
---|
145 | # undef NV_GL_LOAD
|
---|
146 | # undef NV_GL_LOAD_EXT
|
---|
147 | # undef NV_GL_LOAD_REN
|
---|
148 | wgl_library_loaded = true;
|
---|
149 | return true;
|
---|
150 | #else
|
---|
151 | return false;
|
---|
152 | #endif
|
---|
153 | }
|
---|
154 |
|
---|
155 | #endif // NV_DYNAMIC
|
---|
156 |
|
---|
157 | bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
|
---|
158 | {
|
---|
159 | #if NV_PLATFORM == NV_WINDOWS
|
---|
160 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
161 | if ( wgl_library_loaded ) return true;
|
---|
162 |
|
---|
163 | HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC) = nullptr;
|
---|
164 | BOOL (NV_GL_APIENTRY *wgl_makecurrent) (HDC, HGLRC) = nullptr;
|
---|
165 | BOOL (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC) = nullptr;
|
---|
166 |
|
---|
167 | *(void **) &wgl_createcontext = gl_library.get("wglCreateContext");
|
---|
168 | *(void **) &wgl_makecurrent = gl_library.get("wglMakeCurrent");
|
---|
169 | *(void **) &wgl_deletecontext = gl_library.get("wglDeleteContext");
|
---|
170 |
|
---|
171 | WNDCLASS wndClass;
|
---|
172 | HINSTANCE hInstance = 0;
|
---|
173 |
|
---|
174 | ZeroMemory(&wndClass, sizeof(WNDCLASS));
|
---|
175 |
|
---|
176 | wndClass.cbClsExtra = 0;
|
---|
177 | wndClass.cbWndExtra = 0;
|
---|
178 | wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
---|
179 | wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
---|
180 | wndClass.hInstance = hInstance;
|
---|
181 | wndClass.lpfnWndProc = (WNDPROC) DefWindowProc;
|
---|
182 | wndClass.lpszClassName = TEXT("Dummy67789");
|
---|
183 | wndClass.lpszMenuName = 0;
|
---|
184 | wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
---|
185 |
|
---|
186 | DWORD err = GetLastError();
|
---|
187 | RegisterClass(&wndClass);
|
---|
188 | err = GetLastError();
|
---|
189 |
|
---|
190 |
|
---|
191 | HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN,
|
---|
192 | 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
|
---|
193 | NULL, GetModuleHandle(nullptr), NULL);
|
---|
194 |
|
---|
195 | HDC hDC = GetDC(hWndFake);
|
---|
196 |
|
---|
197 | PIXELFORMATDESCRIPTOR pfd;
|
---|
198 | memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
---|
199 | pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
|
---|
200 | pfd.nVersion = 1;
|
---|
201 | pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
|
---|
202 | pfd.iPixelType = PFD_TYPE_RGBA;
|
---|
203 | pfd.cColorBits = 32;
|
---|
204 | pfd.cDepthBits = 24;
|
---|
205 | pfd.iLayerType = PFD_MAIN_PLANE;
|
---|
206 |
|
---|
207 | int iPixelFormat = ChoosePixelFormat(hDC, &pfd);
|
---|
208 | if (iPixelFormat == 0)return false;
|
---|
209 |
|
---|
210 | if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
|
---|
211 |
|
---|
212 | HGLRC hRCFake = wgl_createcontext(hDC);
|
---|
213 | wgl_makecurrent(hDC, hRCFake);
|
---|
214 |
|
---|
215 | LoadLibrary( "gdi32.dll" );
|
---|
216 | bool gl_loaded = nv::load_gl_library( path );
|
---|
217 | bool wgl_loaded = nv::load_wgl_library( path );
|
---|
218 | bool result = gl_loaded && wgl_loaded;
|
---|
219 |
|
---|
220 | wgl_makecurrent(NULL, NULL);
|
---|
221 | wgl_deletecontext(hRCFake);
|
---|
222 | DestroyWindow(hWndFake);
|
---|
223 | return result;
|
---|
224 | #else
|
---|
225 | return false;
|
---|
226 | #endif
|
---|
227 | }
|
---|
228 |
|
---|
229 | const char* nv::get_gl_extension_name( gl_extensions extension )
|
---|
230 | {
|
---|
231 | uint32 value = uint32( extension );
|
---|
232 | uint32 index = 0;
|
---|
233 | while ( value >>= 1 ) index++;
|
---|
234 | return NV_SAFE_ARRAY(gl_extension_names, index+1, gl_extension_names[0] );
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | bool nv::load_gl_extension( gl_extensions extension )
|
---|
239 | {
|
---|
240 | const char* name = get_gl_extension_name( extension );
|
---|
241 | // TODO: first check for support using gl mechanisms
|
---|
242 | // see SDL 2.0 SDL_video.c
|
---|
243 | if ( !gl_library.is_open() )
|
---|
244 | {
|
---|
245 | NV_LOG( nv::LOG_ERROR, "load_gl_extension used, while gl_library was closed!" );
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if ( gl_ext_loader == nullptr )
|
---|
250 | {
|
---|
251 | NV_LOG( nv::LOG_ERROR, "load_gl_extension used, while gl_ext_loader was undefined!" );
|
---|
252 | return false;
|
---|
253 | }
|
---|
254 | NV_LOG( nv::LOG_DEBUG, "load_gl_extension - loading extension - \"" << name << "\"..." );
|
---|
255 |
|
---|
256 | uint32 count = 0;
|
---|
257 | uint32 fail_count = 0;
|
---|
258 |
|
---|
259 | # define NV_GL_FUN_EXT( rtype, symbol, fparams ) \
|
---|
260 | *(void **) (&symbol) = load_gl_ext_symbol(#symbol, true, nullptr); \
|
---|
261 | count++; if ( !symbol ) fail_count++;
|
---|
262 |
|
---|
263 | switch ( extension )
|
---|
264 | {
|
---|
265 | case GL_EXT_FRAMEBUFFER_BLIT : {
|
---|
266 | #include <nv/lib/detail/gl_ext/gl_ext_framebuffer_blit_functions.inc>
|
---|
267 | } break;
|
---|
268 | case GL_EXT_FRAMEBUFFER_OBJECT : {
|
---|
269 | #include <nv/lib/detail/gl_ext/gl_ext_framebuffer_object_functions.inc>
|
---|
270 | } break;
|
---|
271 | default : {
|
---|
272 | NV_LOG( nv::LOG_ERROR, "load_gl_extension - unknown extension \"" << name << "\"!" );
|
---|
273 | return nullptr;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | # undef NV_GL_FUN_EXT
|
---|
277 |
|
---|
278 | if ( fail_count == 0 )
|
---|
279 | {
|
---|
280 | NV_LOG( nv::LOG_NOTICE, "load_gl_extension - extension \"" << name << "\" loaded (" << count << " symbols)" );
|
---|
281 | return false;
|
---|
282 | }
|
---|
283 | NV_LOG( nv::LOG_NOTICE, "load_gl_extension - failed to load extension \"" << name << "\" (" << count << "/" << fail_count << " symbols loaded)" );
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 |
|
---|
287 | nv::gl_extensions nv::load_gl_extensions( uint32 extensions )
|
---|
288 | {
|
---|
289 | gl_extensions result = gl_extensions(0);
|
---|
290 | for ( auto ext : nv::bits( gl_extensions(extensions) ) )
|
---|
291 | {
|
---|
292 | if ( load_gl_extension(ext) ) result = gl_extensions( result | ext );
|
---|
293 | }
|
---|
294 | return result;
|
---|
295 | }
|
---|
296 |
|
---|
297 | bool nv::is_gl_extension_loaded( gl_extensions extensions )
|
---|
298 | {
|
---|
299 | return ( gl_loaded_extensions & extensions ) != 0;
|
---|
300 | }
|
---|
301 |
|
---|
302 | bool nv::are_gl_extensions_loaded( uint32 extensions )
|
---|
303 | {
|
---|
304 | for ( auto ext : nv::bits( gl_extensions(extensions) ) )
|
---|
305 | {
|
---|
306 | if ( !is_gl_extension_loaded(ext) ) return false;
|
---|
307 | }
|
---|
308 | return true;
|
---|
309 | }
|
---|
310 |
|
---|