[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[5] | 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 |
|
---|
[319] | 7 | #include "nv/core/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] | 37 | static nv::library gl_library;
|
---|
[311] | 38 | static nv::gl_extensions gl_loaded_extensions = nv::gl_extensions(0);
|
---|
[323] | 39 | static void* (NV_GL_APIENTRY *gl_ext_loader) ( const char* ) = nullptr;
|
---|
[245] | 40 | static bool gl_library_loaded = false;
|
---|
| 41 | static bool wgl_library_loaded = false;
|
---|
| 42 |
|
---|
[311] | 43 | static 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 |
|
---|
| 58 | static void* load_gl_ext_symbol_impl( const char* name, nv::log_level fail_level = nv::LOG_DEBUG )
|
---|
[5] | 59 | {
|
---|
[311] | 60 | void* result = gl_ext_loader( name );
|
---|
[365] | 61 | NV_LOG( ( result ? nv::LOG_DEBUG : fail_level ), "load_gl_ext_symbol : ", name, ( result ? " succeded." : "failed." ) );
|
---|
[311] | 62 | return result;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | static 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 |
|
---|
| 80 | bool nv::load_gl_library( const char* path, bool force_reload )
|
---|
| 81 | {
|
---|
| 82 | if ( gl_library_loaded && !force_reload ) return true;
|
---|
[5] | 83 | #if defined( NV_SDL_GL )
|
---|
| 84 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
| 85 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
[311] | 86 | *(void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
|
---|
[5] | 87 | #else
|
---|
[245] | 88 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
[109] | 89 |
|
---|
[5] | 90 | # if NV_PLATFORM == NV_WINDOWS
|
---|
| 91 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
[311] | 92 | *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
|
---|
| 93 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
[204] | 94 | # elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
|
---|
[5] | 95 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
[311] | 96 | *(void **) (&gl_ext_loader) = gl_library.get("glXGetProcAddress");
|
---|
| 97 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
[5] | 98 | # else
|
---|
| 99 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 100 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 101 | # endif
|
---|
| 102 | #endif
|
---|
| 103 |
|
---|
[167] | 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
|
---|
[5] | 109 |
|
---|
| 110 | # undef NV_GL_LOAD
|
---|
| 111 | # undef NV_GL_LOAD_EXT
|
---|
[245] | 112 | gl_library_loaded = true;
|
---|
[5] | 113 | return true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[311] | 116 | bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */, bool force_reload )
|
---|
[245] | 117 | {
|
---|
[311] | 118 | if ( wgl_library_loaded && !force_reload ) return true;
|
---|
[245] | 119 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 120 | #if defined( NV_SDL_GL )
|
---|
| 121 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
| 122 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
| 123 | # define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
|
---|
[311] | 124 | (void **) (&gl_ext_loader) = SDL_GL_GetProcAddress;
|
---|
[245] | 125 | #else //
|
---|
| 126 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
| 127 |
|
---|
[311] | 128 | *(void **) (&gl_ext_loader) = gl_library.get("wglGetProcAddress");
|
---|
[245] | 129 | #define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
[311] | 130 | #define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_ext_loader(#symbol);
|
---|
[245] | 131 | #define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
|
---|
| 132 | #endif
|
---|
| 133 | # define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
|
---|
| 134 | # define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
|
---|
| 135 | # define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
|
---|
| 136 | # include <nv/lib/detail/wgl_functions.inc>
|
---|
| 137 | # undef NV_GL_FUN_REN
|
---|
| 138 | # undef NV_GL_FUN_EXT
|
---|
| 139 | # undef NV_GL_FUN
|
---|
| 140 |
|
---|
| 141 | # undef NV_GL_LOAD
|
---|
| 142 | # undef NV_GL_LOAD_EXT
|
---|
| 143 | # undef NV_GL_LOAD_REN
|
---|
| 144 | wgl_library_loaded = true;
|
---|
| 145 | return true;
|
---|
| 146 | #else
|
---|
| 147 | return false;
|
---|
[20] | 148 | #endif
|
---|
[245] | 149 | }
|
---|
| 150 |
|
---|
| 151 | #endif // NV_DYNAMIC
|
---|
| 152 |
|
---|
| 153 | bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
|
---|
| 154 | {
|
---|
| 155 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 156 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
| 157 | if ( wgl_library_loaded ) return true;
|
---|
| 158 |
|
---|
| 159 | HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC) = nullptr;
|
---|
| 160 | BOOL (NV_GL_APIENTRY *wgl_makecurrent) (HDC, HGLRC) = nullptr;
|
---|
| 161 | BOOL (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC) = nullptr;
|
---|
| 162 |
|
---|
| 163 | *(void **) &wgl_createcontext = gl_library.get("wglCreateContext");
|
---|
| 164 | *(void **) &wgl_makecurrent = gl_library.get("wglMakeCurrent");
|
---|
| 165 | *(void **) &wgl_deletecontext = gl_library.get("wglDeleteContext");
|
---|
| 166 |
|
---|
| 167 | WNDCLASS wndClass;
|
---|
| 168 | HINSTANCE hInstance = 0;
|
---|
| 169 |
|
---|
| 170 | ZeroMemory(&wndClass, sizeof(WNDCLASS));
|
---|
| 171 |
|
---|
| 172 | wndClass.cbClsExtra = 0;
|
---|
| 173 | wndClass.cbWndExtra = 0;
|
---|
| 174 | wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
---|
| 175 | wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
---|
| 176 | wndClass.hInstance = hInstance;
|
---|
| 177 | wndClass.lpfnWndProc = (WNDPROC) DefWindowProc;
|
---|
| 178 | wndClass.lpszClassName = TEXT("Dummy67789");
|
---|
| 179 | wndClass.lpszMenuName = 0;
|
---|
| 180 | wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
---|
| 181 |
|
---|
[323] | 182 | /*DWORD err = */GetLastError();
|
---|
[245] | 183 | RegisterClass(&wndClass);
|
---|
[323] | 184 | /*err = */GetLastError();
|
---|
[245] | 185 |
|
---|
| 186 |
|
---|
| 187 | HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN,
|
---|
| 188 | 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
|
---|
| 189 | NULL, GetModuleHandle(nullptr), NULL);
|
---|
| 190 |
|
---|
| 191 | HDC hDC = GetDC(hWndFake);
|
---|
| 192 |
|
---|
| 193 | PIXELFORMATDESCRIPTOR pfd;
|
---|
| 194 | memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
---|
| 195 | pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
|
---|
| 196 | pfd.nVersion = 1;
|
---|
| 197 | pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
|
---|
| 198 | pfd.iPixelType = PFD_TYPE_RGBA;
|
---|
| 199 | pfd.cColorBits = 32;
|
---|
| 200 | pfd.cDepthBits = 24;
|
---|
| 201 | pfd.iLayerType = PFD_MAIN_PLANE;
|
---|
| 202 |
|
---|
| 203 | int iPixelFormat = ChoosePixelFormat(hDC, &pfd);
|
---|
| 204 | if (iPixelFormat == 0)return false;
|
---|
| 205 |
|
---|
| 206 | if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
|
---|
| 207 |
|
---|
| 208 | HGLRC hRCFake = wgl_createcontext(hDC);
|
---|
| 209 | wgl_makecurrent(hDC, hRCFake);
|
---|
| 210 |
|
---|
| 211 | LoadLibrary( "gdi32.dll" );
|
---|
| 212 | bool gl_loaded = nv::load_gl_library( path );
|
---|
| 213 | bool wgl_loaded = nv::load_wgl_library( path );
|
---|
| 214 | bool result = gl_loaded && wgl_loaded;
|
---|
| 215 |
|
---|
| 216 | wgl_makecurrent(NULL, NULL);
|
---|
| 217 | wgl_deletecontext(hRCFake);
|
---|
| 218 | DestroyWindow(hWndFake);
|
---|
| 219 | return result;
|
---|
| 220 | #else
|
---|
| 221 | return false;
|
---|
| 222 | #endif
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[311] | 225 | const char* nv::get_gl_extension_name( gl_extensions extension )
|
---|
| 226 | {
|
---|
| 227 | uint32 value = uint32( extension );
|
---|
| 228 | uint32 index = 0;
|
---|
| 229 | while ( value >>= 1 ) index++;
|
---|
| 230 | return NV_SAFE_ARRAY(gl_extension_names, index+1, gl_extension_names[0] );
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | bool nv::load_gl_extension( gl_extensions extension )
|
---|
| 234 | {
|
---|
| 235 | const char* name = get_gl_extension_name( extension );
|
---|
| 236 | // TODO: first check for support using gl mechanisms
|
---|
| 237 | // see SDL 2.0 SDL_video.c
|
---|
| 238 | if ( !gl_library.is_open() )
|
---|
| 239 | {
|
---|
[365] | 240 | NV_LOG_ERROR( "load_gl_extension used, while gl_library was closed!" );
|
---|
[311] | 241 | return false;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | if ( gl_ext_loader == nullptr )
|
---|
| 245 | {
|
---|
[365] | 246 | NV_LOG_ERROR( "load_gl_extension used, while gl_ext_loader was undefined!" );
|
---|
[311] | 247 | return false;
|
---|
| 248 | }
|
---|
[365] | 249 | NV_LOG_DEBUG( "load_gl_extension - loading extension - \"", name, "\"..." );
|
---|
[311] | 250 |
|
---|
| 251 | uint32 count = 0;
|
---|
| 252 | uint32 fail_count = 0;
|
---|
| 253 |
|
---|
| 254 | # define NV_GL_FUN_EXT( rtype, symbol, fparams ) \
|
---|
| 255 | *(void **) (&symbol) = load_gl_ext_symbol(#symbol, true, nullptr); \
|
---|
| 256 | count++; if ( !symbol ) fail_count++;
|
---|
| 257 |
|
---|
| 258 | switch ( extension )
|
---|
| 259 | {
|
---|
| 260 | case GL_EXT_FRAMEBUFFER_BLIT : {
|
---|
| 261 | #include <nv/lib/detail/gl_ext/gl_ext_framebuffer_blit_functions.inc>
|
---|
| 262 | } break;
|
---|
| 263 | case GL_EXT_FRAMEBUFFER_OBJECT : {
|
---|
| 264 | #include <nv/lib/detail/gl_ext/gl_ext_framebuffer_object_functions.inc>
|
---|
| 265 | } break;
|
---|
| 266 | default : {
|
---|
[365] | 267 | NV_LOG_ERROR( "load_gl_extension - unknown extension \"", name, "\"!" );
|
---|
[311] | 268 | return nullptr;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | # undef NV_GL_FUN_EXT
|
---|
| 272 |
|
---|
| 273 | if ( fail_count == 0 )
|
---|
| 274 | {
|
---|
[365] | 275 | NV_LOG_NOTICE( "load_gl_extension - extension \"", name, "\" loaded (", count, " symbols)" );
|
---|
[331] | 276 | gl_loaded_extensions = (gl_extensions)( gl_loaded_extensions | (unsigned)extension );
|
---|
[311] | 277 | return false;
|
---|
| 278 | }
|
---|
[365] | 279 | NV_LOG_NOTICE( "load_gl_extension - failed to load extension \"", name, "\" (", count, "/", fail_count, " symbols loaded)" );
|
---|
[311] | 280 | return true;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | nv::gl_extensions nv::load_gl_extensions( uint32 extensions )
|
---|
| 284 | {
|
---|
| 285 | gl_extensions result = gl_extensions(0);
|
---|
| 286 | for ( auto ext : nv::bits( gl_extensions(extensions) ) )
|
---|
| 287 | {
|
---|
| 288 | if ( load_gl_extension(ext) ) result = gl_extensions( result | ext );
|
---|
| 289 | }
|
---|
| 290 | return result;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | bool nv::is_gl_extension_loaded( gl_extensions extensions )
|
---|
| 294 | {
|
---|
| 295 | return ( gl_loaded_extensions & extensions ) != 0;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | bool nv::are_gl_extensions_loaded( uint32 extensions )
|
---|
| 299 | {
|
---|
| 300 | for ( auto ext : nv::bits( gl_extensions(extensions) ) )
|
---|
| 301 | {
|
---|
| 302 | if ( !is_gl_extension_loaded(ext) ) return false;
|
---|
| 303 | }
|
---|
| 304 | return true;
|
---|
| 305 | }
|
---|
| 306 |
|
---|