source: trunk/src/lib/gl.cc @ 406

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