Changeset 311 for trunk/src/lib


Ignore:
Timestamp:
08/14/14 21:21:39 (11 years ago)
Author:
epyon
Message:
  • full OpenGL extension loading mechanisms
  • framebuffer_object and framebuffer_blit extension defs added
  • minor fixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/gl.cc

    r245 r311  
    66
    77#include "nv/common.hh"
     8#include "nv/range.hh"
    89#include "nv/lib/gl.hh"
    910
     
    3334#include <nv/lib/detail/wgl_functions.inc>
    3435#endif
     36#include <nv/lib/detail/gl_ext/gl_ext_all_functions.inc>
    3537#undef NV_GL_FUN_REN
    3638#undef NV_GL_FUN_EXT
     
    3840
    3941static nv::library gl_library;
     42static nv::gl_extensions gl_loaded_extensions = nv::gl_extensions(0);
     43extern "C" void* (NV_GL_APIENTRY *gl_ext_loader) ( const char* ) = nullptr;
    4044static bool gl_library_loaded = false;
    4145static bool wgl_library_loaded = false;
    4246
    43 bool nv::load_gl_library( const char* path )
    44 {
    45         if ( gl_library_loaded ) return true;
     47static 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
     54static 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
     62static 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
     69static 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
     84bool nv::load_gl_library( const char* path, bool force_reload )
     85{
     86        if ( gl_library_loaded && !force_reload ) return true;
    4687#if defined( NV_SDL_GL )
    4788#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    4889#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
     90        *(void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
    4991#else
    5092        if ( !gl_library.is_open() ) gl_library.open( path );
    5193
    52         void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
    5394#       if NV_PLATFORM == NV_WINDOWS
    5495#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
    55                 *(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
    56 #               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
     96                *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
     97#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
    5798#       elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
    5899#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
    59                 *(void **) (&ext_loader) = gl_library.get("glXGetProcAddress");
    60 #               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
     100                *(void **) (&gl_ext_loader) = gl_library.get("glXGetProcAddress");
     101#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
    61102#       else
    62103#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
     
    77118}
    78119
    79 bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */ )
    80 {
    81         if ( wgl_library_loaded ) return true;
     120bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */, bool force_reload )
     121{
     122        if ( wgl_library_loaded && !force_reload ) return true;
    82123#if NV_PLATFORM == NV_WINDOWS
    83124#if defined( NV_SDL_GL )
     
    85126#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    86127#           define NV_GL_LOAD_REN( fname, rname )  *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
     128        (void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
    87129#else //
    88130        if ( !gl_library.is_open() ) gl_library.open( path );
    89131
    90         void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
    91         *(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
     132        *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
    92133#define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
    93 #define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
     134#define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
    94135#define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
    95136#endif
     
    186227}
    187228
     229const 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
     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( 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
     287nv::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
     297bool nv::is_gl_extension_loaded( gl_extensions extensions )
     298{
     299        return ( gl_loaded_extensions & extensions ) != 0;
     300}
     301
     302bool 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
Note: See TracChangeset for help on using the changeset viewer.