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

Last change on this file since 458 was 440, checked in by epyon, 10 years ago
  • massive std::string removal
  • no header depends on std::string anymore (or any other STL header)
  • still some code files do (WIP)
  • massive refactoring where std::string was used
  • lua still messy (grep for string128 - used everywhere)
  • string_twine added
File size: 10.0 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 nv::string_view& name, nv::log_level fail_level = nv::LOG_DEBUG )
59{
60        void* result = gl_ext_loader( name.data() );
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 nv::string_view& name, bool iterate, const nv::string_view& ext )
66{
67        void * result        = nullptr;
68        nv::string128 test_name = name + ext;
69        result = load_gl_ext_symbol_impl( !ext.empty() ? test_name : name );
70        if ( result ) return result;
71        if ( iterate )
72        {
73                test_name = name + "ARB"_ls;
74                result = gl_ext_loader( test_name.data() );
75                if ( result ) return result;
76                test_name = name + "EXT"_ls;
77                result = gl_ext_loader( test_name.data() );
78                if ( result ) return result;
79        }
80        return result;
81}
82
83bool nv::load_gl_library( const char* path, bool force_reload )
84{
85        if ( gl_library_loaded && !force_reload ) return true;
86#if defined( NV_SDL_GL )
87#               define NV_GL_LOAD( symbol ) void_assign( symbol, SDL_GL_GetProcAddress(#symbol) );
88#               define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, SDL_GL_GetProcAddress(#symbol) );
89        void_assign( gl_ext_loader, SDL_GL_GetProcAddress );
90#else
91        if ( !gl_library.is_open() ) gl_library.open( path );
92
93#       if NV_PLATFORM == NV_WINDOWS
94#               define NV_GL_LOAD( symbol ) void_assign( symbol, gl_library.get(#symbol) );
95                void_assign( gl_ext_loader, gl_library.get( "wglGetProcAddress" ) );
96#               define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, gl_ext_loader(#symbol) );
97#       elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
98#               define NV_GL_LOAD( symbol ) void_assign( symbol, gl_library.get(#symbol) );
99                void_assign( gl_ext_loader, gl_library.get("glXGetProcAddress") );
100#               define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, gl_ext_loader(#symbol) );
101#       else
102#               define NV_GL_LOAD( symbol ) void_assign( symbol, gl_library.get(#symbol) );
103#               define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, gl_library.get(#symbol) );
104#       endif
105#endif
106
107#       define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
108#       define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
109#       include <nv/lib/detail/gl_functions.inc>
110#       undef NV_GL_FUN_EXT
111#       undef NV_GL_FUN
112
113#       undef NV_GL_LOAD
114#       undef NV_GL_LOAD_EXT
115        gl_library_loaded = true;
116        return true;
117}
118
119
120
121bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */, bool force_reload )
122{
123        if ( wgl_library_loaded && !force_reload ) return true;
124#if NV_PLATFORM == NV_WINDOWS
125#if defined( NV_SDL_GL )
126#               define NV_GL_LOAD( symbol ) void_assign( symbol, SDL_GL_GetProcAddress(#symbol) );
127#               define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, SDL_GL_GetProcAddress(#symbol) );
128#           define NV_GL_LOAD_REN( fname, rname )  void_assign( rname, SDL_GL_GetProcAddress(#fname) );
129        void_assign( gl_ext_loader, SDL_GL_GetProcAddress );
130#else //
131        if ( !gl_library.is_open() ) gl_library.open( path );
132
133        void_assign( gl_ext_loader, gl_library.get("wglGetProcAddress") );
134#define NV_GL_LOAD( symbol ) void_assign( symbol, gl_library.get(#symbol) );
135#define NV_GL_LOAD_EXT( symbol ) void_assign( symbol, gl_ext_loader(#symbol) );
136#define NV_GL_LOAD_REN( fname, rname ) void_assign( rname, gl_library.get(#fname) );
137#endif
138#       define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
139#       define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
140#       define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
141#       include <nv/lib/detail/wgl_functions.inc>
142#       undef NV_GL_FUN_REN
143#       undef NV_GL_FUN_EXT
144#       undef NV_GL_FUN
145
146#       undef NV_GL_LOAD
147#       undef NV_GL_LOAD_EXT
148#       undef NV_GL_LOAD_REN
149        wgl_library_loaded = true;
150        return true;
151#else
152        return false;
153#endif
154}
155
156#endif // NV_DYNAMIC
157
158bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
159{
160#if NV_PLATFORM == NV_WINDOWS
161        if ( !gl_library.is_open() ) gl_library.open( path );
162        if ( wgl_library_loaded ) return true;
163
164        HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC)        = nullptr;
165        BOOL  (NV_GL_APIENTRY *wgl_makecurrent)   (HDC, HGLRC) = nullptr;
166        BOOL  (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC)      = nullptr;
167
168        void_assign( wgl_createcontext, gl_library.get("wglCreateContext") );
169        void_assign( wgl_makecurrent,   gl_library.get("wglMakeCurrent") );
170        void_assign( wgl_deletecontext, gl_library.get("wglDeleteContext") );
171
172        WNDCLASS wndClass;
173        HINSTANCE hInstance = 0;
174
175        ZeroMemory(&wndClass, sizeof(WNDCLASS));
176
177        wndClass.cbClsExtra = 0;
178        wndClass.cbWndExtra = 0;                       
179        wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
180        wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
181        wndClass.hInstance = hInstance;
182        wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>( DefWindowProc );
183        wndClass.lpszClassName = TEXT("Dummy67789");
184        wndClass.lpszMenuName = 0;
185        wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
186
187        /*DWORD err = */GetLastError();
188        RegisterClass(&wndClass);
189        /*err = */GetLastError();
190
191
192        HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN,
193                0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
194                NULL, GetModuleHandle(nullptr), NULL);
195
196        HDC hDC = GetDC(hWndFake);
197
198        PIXELFORMATDESCRIPTOR pfd;
199        memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
200        pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
201        pfd.nVersion   = 1;
202        pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
203        pfd.iPixelType = PFD_TYPE_RGBA;
204        pfd.cColorBits = 32;
205        pfd.cDepthBits = 24;
206        pfd.iLayerType = PFD_MAIN_PLANE;
207
208        int iPixelFormat = ChoosePixelFormat(hDC, &pfd);
209        if (iPixelFormat == 0)return false;
210
211        if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
212
213        HGLRC hRCFake = wgl_createcontext(hDC);
214        wgl_makecurrent(hDC, hRCFake);
215
216        LoadLibrary( "gdi32.dll" );
217        bool gl_loaded = nv::load_gl_library( path );
218        bool wgl_loaded = nv::load_wgl_library( path );
219        bool result = gl_loaded && wgl_loaded;
220
221        wgl_makecurrent(NULL, NULL);
222        wgl_deletecontext(hRCFake);
223        DestroyWindow(hWndFake);
224        return result;
225#else
226        return false;
227#endif
228}
229
230const char* nv::get_gl_extension_name( gl_extensions extension )
231{
232        uint32 value = uint32( extension );
233        uint32 index = 0;
234        while ( value >>= 1 ) index++;
235        return NV_SAFE_ARRAY(gl_extension_names, index+1, gl_extension_names[0] );
236}
237
238bool 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_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_ERROR( "load_gl_extension used, while gl_ext_loader was undefined!" );
252                return false;
253        }
254        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_assign( symbol, load_gl_ext_symbol(#symbol, true, string_view()) ); \
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_ERROR( "load_gl_extension - unknown extension \"", name, "\"!" );
273                return false;
274        }
275        }
276#       undef NV_GL_FUN_EXT
277
278        if ( fail_count == 0 )
279        {
280                NV_LOG_NOTICE( "load_gl_extension - extension \"", name, "\" loaded (", count, " symbols)" );
281                gl_loaded_extensions = gl_extensions( gl_loaded_extensions | static_cast<unsigned>( extension ) );
282                return false;
283        }
284        NV_LOG_NOTICE( "load_gl_extension - failed to load extension \"", name, "\" (", count, "/", fail_count, " symbols loaded)" );
285        return true;
286}
287
288nv::gl_extensions nv::load_gl_extensions( uint32 extensions )
289{
290        gl_extensions result = gl_extensions(0);
291        for ( auto ext : nv::bits( gl_extensions(extensions) ) )
292        {
293                if ( load_gl_extension(ext) ) result = gl_extensions( result | ext );
294        }
295        return result;
296}
297
298bool nv::is_gl_extension_loaded( gl_extensions extensions )
299{
300        return ( gl_loaded_extensions & extensions ) != 0;
301}
302
303bool nv::are_gl_extensions_loaded( uint32 extensions )
304{
305        for ( auto ext : nv::bits( gl_extensions(extensions) ) )
306        {
307                if ( !is_gl_extension_loaded(ext) ) return false;
308        }
309        return true;
310}
311
Note: See TracBrowser for help on using the repository browser.