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

Last change on this file since 463 was 463, checked in by epyon, 10 years ago
  • 3D/2D_ARRAY texture support
File size: 10.1 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[5]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[5]6
[395]7#include "nv/common.hh"
[368]8#include "nv/stl/range.hh"
[365]9#include "nv/core/logging.hh"
[5]10#include "nv/lib/gl.hh"
11
12#if defined( NV_GL_DYNAMIC )
13
[319]14#include "nv/core/library.hh"
[5]15
16#if defined( NV_SDL_GL )
[170]17#       include "nv/lib/sdl.hh"
[5]18#endif
19
[245]20// for wgl support
21#if NV_PLATFORM == NV_WINDOWS
22#include <windows.h>
23#endif
24
[323]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;
[167]27#define NV_GL_FUN_EXT NV_GL_FUN
28#include <nv/lib/detail/gl_functions.inc>
[245]29#if NV_PLATFORM == NV_WINDOWS
30#include <nv/lib/detail/wgl_functions.inc>
31#endif
[311]32#include <nv/lib/detail/gl_ext/gl_ext_all_functions.inc>
[245]33#undef NV_GL_FUN_REN
[167]34#undef NV_GL_FUN_EXT
35#undef NV_GL_FUN
[5]36
[245]37static nv::library gl_library;
[311]38static nv::gl_extensions gl_loaded_extensions = nv::gl_extensions(0);
[323]39static void* (NV_GL_APIENTRY *gl_ext_loader) ( const char* ) = nullptr;
[245]40static bool gl_library_loaded = false;
41static bool wgl_library_loaded = false;
42
[311]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
[323]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// };
[311]56
57
[440]58static void* load_gl_ext_symbol_impl( const nv::string_view& name, nv::log_level fail_level = nv::LOG_DEBUG )
[5]59{
[440]60        void* result = gl_ext_loader( name.data() );
[365]61        NV_LOG( ( result ? nv::LOG_DEBUG : fail_level ), "load_gl_ext_symbol : ", name, ( result ? " succeded." : "failed." ) );
[311]62        return result;
63}
64
[440]65static void* load_gl_ext_symbol( const nv::string_view& name, bool iterate, const nv::string_view& ext )
[311]66{
67        void * result        = nullptr;
[440]68        nv::string128 test_name = name + ext;
69        result = load_gl_ext_symbol_impl( !ext.empty() ? test_name : name );
[311]70        if ( result ) return result;
71        if ( iterate )
72        {
[440]73                test_name = name + "ARB"_ls;
74                result = gl_ext_loader( test_name.data() );
[311]75                if ( result ) return result;
[440]76                test_name = name + "EXT"_ls;
77                result = gl_ext_loader( test_name.data() );
[311]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;
[5]86#if defined( NV_SDL_GL )
[406]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 );
[5]90#else
[245]91        if ( !gl_library.is_open() ) gl_library.open( path );
[109]92
[5]93#       if NV_PLATFORM == NV_WINDOWS
[406]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) );
[204]97#       elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
[406]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) );
[5]101#       else
[406]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) );
[5]104#       endif
105#endif
106
[167]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
[5]112
113#       undef NV_GL_LOAD
114#       undef NV_GL_LOAD_EXT
[245]115        gl_library_loaded = true;
[5]116        return true;
117}
118
[406]119
120
[311]121bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */, bool force_reload )
[245]122{
[311]123        if ( wgl_library_loaded && !force_reload ) return true;
[245]124#if NV_PLATFORM == NV_WINDOWS
125#if defined( NV_SDL_GL )
[406]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 );
[245]130#else //
131        if ( !gl_library.is_open() ) gl_library.open( path );
132
[406]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) );
[245]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;
[20]153#endif
[245]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
[406]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") );
[245]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;
[406]182        wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>( DefWindowProc );
[245]183        wndClass.lpszClassName = TEXT("Dummy67789");
184        wndClass.lpszMenuName = 0;
185        wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
186
[323]187        /*DWORD err = */GetLastError();
[245]188        RegisterClass(&wndClass);
[323]189        /*err = */GetLastError();
[245]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
[311]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        {
[365]245                NV_LOG_ERROR( "load_gl_extension used, while gl_library was closed!" );
[311]246                return false;
247        }
248
249        if ( gl_ext_loader == nullptr )
250        {
[365]251                NV_LOG_ERROR( "load_gl_extension used, while gl_ext_loader was undefined!" );
[311]252                return false;
253        }
[365]254        NV_LOG_DEBUG( "load_gl_extension - loading extension - \"", name, "\"..." );
[311]255
256        uint32 count      = 0;
257        uint32 fail_count = 0;
258
259#       define NV_GL_FUN_EXT( rtype, symbol, fparams ) \
[440]260        void_assign( symbol, load_gl_ext_symbol(#symbol, true, string_view()) ); \
[311]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;
[463]271        case GL_EXT_TEXTURE_ARRAY: {
272#include <nv/lib/detail/gl_ext/gl_ext_texture_array_functions.inc>
273        } break;
[311]274        default : {
[365]275                NV_LOG_ERROR( "load_gl_extension - unknown extension \"", name, "\"!" );
[406]276                return false;
[311]277        }
278        }
279#       undef NV_GL_FUN_EXT
280
281        if ( fail_count == 0 )
282        {
[365]283                NV_LOG_NOTICE( "load_gl_extension - extension \"", name, "\" loaded (", count, " symbols)" );
[406]284                gl_loaded_extensions = gl_extensions( gl_loaded_extensions | static_cast<unsigned>( extension ) );
[311]285                return false;
286        }
[365]287        NV_LOG_NOTICE( "load_gl_extension - failed to load extension \"", name, "\" (", count, "/", fail_count, " symbols loaded)" );
[311]288        return true;
289}
290
291nv::gl_extensions nv::load_gl_extensions( uint32 extensions )
292{
293        gl_extensions result = gl_extensions(0);
294        for ( auto ext : nv::bits( gl_extensions(extensions) ) )
295        {
296                if ( load_gl_extension(ext) ) result = gl_extensions( result | ext );
297        }
298        return result;
299}
300
301bool nv::is_gl_extension_loaded( gl_extensions extensions )
302{
303        return ( gl_loaded_extensions & extensions ) != 0;
304}
305
306bool nv::are_gl_extensions_loaded( uint32 extensions )
307{
308        for ( auto ext : nv::bits( gl_extensions(extensions) ) )
309        {
310                if ( !is_gl_extension_loaded(ext) ) return false;
311        }
312        return true;
313}
314
Note: See TracBrowser for help on using the repository browser.