Changeset 245 for trunk/src


Ignore:
Timestamp:
05/27/14 16:26:53 (11 years ago)
Author:
epyon
Message:
  • gl library wgl support
  • gl context and window adoption
Location:
trunk/src
Files:
4 edited

Legend:

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

    r233 r245  
    362362
    363363
    364 gl_context::gl_context( device* a_device, void* a_win_handle )
    365         : context( a_device ), m_handle( nullptr )
     364gl_context::gl_context( device* a_device )
     365        : context( a_device )
     366{
     367}
     368
     369
     370nv::gl_context::~gl_context()
     371{
     372}
     373
     374
     375void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
     376{
     377        apply_render_state( rs );
     378        if ( count > 0 )
     379        {
     380                p->bind();
     381                va->bind();
     382                if ( va->has_index_buffer() )
     383                {
     384                        glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
     385                }
     386                else
     387                {
     388                        glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
     389                }
     390                va->unbind();
     391                p->unbind();
     392        }
     393}
     394
     395nv::sdl_gl_context::sdl_gl_context( device* a_device, void* a_sdl_win_handle )
     396        : gl_context( a_device ), m_handle( nullptr )
    366397{
    367398#if NV_SDL_VERSION == NV_SDL_20
    368         m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_win_handle ) );
     399        m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_sdl_win_handle ) );
    369400
    370401        if ( m_handle == 0 )
     
    384415        NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
    385416#if NV_SDL_VERSION == NV_SDL_20
    386 //      SDL_GL_SetSwapInterval(1);
     417        //      SDL_GL_SetSwapInterval(1);
    387418#endif
    388419
     
    393424}
    394425
    395 
    396 nv::gl_context::~gl_context()
     426nv::sdl_gl_context::~sdl_gl_context()
    397427{
    398428#if NV_SDL_VERSION == NV_SDL_20
     
    401431}
    402432
    403 
    404 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
    405 {
    406         apply_render_state( rs );
    407         if ( count > 0 )
    408         {
    409                 p->bind();
    410                 va->bind();
    411                 if ( va->has_index_buffer() )
    412                 {
    413                         glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
    414                 }
    415                 else
    416                 {
    417                         glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
    418                 }
    419                 va->unbind();
    420                 p->unbind();
    421         }
    422 }
     433nv::native_gl_context::native_gl_context( device* a_device, void* a_native_win_handle )
     434        : gl_context( a_device ), m_handle( nullptr )
     435{
     436#if NV_PLATFORM == NV_WINDOWS
     437
     438// TODO: error checking
     439        HDC hdc = (HDC)a_native_win_handle;
     440
     441        const int wgl_attrib_list[] =
     442        {
     443                WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
     444                WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
     445                WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB,
     446                WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,
     447                WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
     448                WGL_COLOR_BITS_ARB,     32,
     449                WGL_DEPTH_BITS_ARB,     24,
     450                WGL_STENCIL_BITS_ARB,   8,
     451                0, 0  //End
     452        };
     453
     454        unsigned int num_formats;
     455        int pixel_format;
     456        PIXELFORMATDESCRIPTOR pfd;
     457
     458        if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
     459        {
     460                return;
     461        }
     462
     463        if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
     464        {
     465                //int err = GetLastError();
     466                return;
     467        }
     468
     469        int attribs[] =
     470        {
     471                WGL_CONTEXT_MAJOR_VERSION_ARB,   2,
     472                WGL_CONTEXT_MINOR_VERSION_ARB,   1,
     473                WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
     474                //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
     475                0, 0  //End
     476        };
     477
     478        HGLRC handle;
     479        if ( 0 == (handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
     480        {
     481                return;
     482        }
     483
     484        if ( FALSE == dynwglMakeCurrent( hdc, handle ) )
     485        {
     486                return;
     487        }     
     488
     489        m_handle = (void*)handle;
     490#else
     491        NV_ASSERT( false, "Native GL context not implemented for this platform!" );
     492#endif
     493        force_apply_render_state( m_render_state );
     494}
     495
     496nv::native_gl_context::~native_gl_context()
     497{
     498#if NV_PLATFORM == NV_WINDOWS
     499        dynwglDeleteContext( (HGLRC)m_handle );
     500#endif
     501}
  • trunk/src/gl/gl_device.cc

    r229 r245  
    2020}
    2121
     22window* nv::gl_device::adopt_window( void* sys_w_handle, void* sys_dc )
     23{
     24        return new gl_window( this, sys_w_handle, sys_dc );
     25}
     26
     27
     28
    2229gl_device::gl_device()
    2330{
     
    3946                return; // TODO: Error report
    4047        }
    41 #endif
    42 
    43 //      bpp = m_info->vfmt->BitsPerPixel;
    44 
    45         SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    46         SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    47         SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    48         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
    49         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    50 
    51 //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
    52 //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
    53 
    54 #if NV_SDL_VERSION == NV_SDL_20
    55         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    56         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    57         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    58         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    5948#endif
    6049
  • trunk/src/gl/gl_window.cc

    r243 r245  
    3434                        int capslock = !!(ke.keysym.mod & KMOD_CAPS);
    3535                        if ((shifted ^ capslock) != 0) {
    36                                 kevent.key.ascii = SDL_toupper(ucode);
     36                                kevent.key.ascii = (char8)SDL_toupper(ucode);
    3737                        }
    3838                }
     
    204204
    205205gl_window::gl_window( device* dev, uint16 width, uint16 height, bool fullscreen )
    206         : m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr )
    207 {
     206        : m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr ), m_adopted( false )
     207{
     208        //      bpp = m_info->vfmt->BitsPerPixel;
     209
     210        SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
     211        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
     212        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
     213        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
     214        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
     215
     216        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
     217        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
     218
     219#if NV_SDL_VERSION == NV_SDL_20
     220        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
     221        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
     222        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     223        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
     224#endif
     225
     226
    208227#if NV_SDL_VERSION == NV_SDL_12
    209228        uint32 flags = SDL_OPENGL;
     
    233252
    234253
    235         m_context = new gl_context( m_device, m_handle );
     254        m_context = new sdl_gl_context( m_device, m_handle );
    236255        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
    237256}
     
    254273void gl_window::set_title( const string& title )
    255274{
     275        if ( m_adopted ) return;
    256276#if NV_SDL_VERSION == NV_SDL_20
    257277        SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.c_str() );
     
    276296void gl_window::swap_buffers()
    277297{
     298        if ( m_adopted ) return; // NOT SURE
    278299#if NV_SDL_VERSION == NV_SDL_20
    279300        SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
     
    287308        delete m_context;
    288309#if NV_SDL_VERSION == NV_SDL_20
    289         SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
    290 #endif
    291 }
     310        if ( !m_adopted ) SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
     311#endif
     312}
     313
     314nv::gl_window::gl_window( device* dev, void* handle, void* dc )
     315        : m_device( dev ), m_width( 0 ), m_height( 0 ), m_title(), m_handle( nullptr ), m_adopted( true )
     316{
     317#if NV_PLATFORM == NV_WINDOWS
     318#if NV_SDL_VERSION == NV_SDL_20
     319        nv::load_gl_no_context();
     320        m_context = new native_gl_context( m_device, dc );
     321        SDL_Window* window = SDL_CreateWindowFrom( handle );
     322        // Doesn't work :/
     323//      RECT rect;
     324//      GetClientRect( (HWND)handle, &rect );
     325//      m_width   = (uint16)rect.right;
     326//      m_height  = (uint16)rect.bottom;
     327        m_handle  = window;
     328        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
     329#else
     330        NV_ASSERT( false, "Native GL context only working with SDL 2.0!" );
     331#endif
     332#else
     333        NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
     334#endif
     335}
  • trunk/src/lib/gl.cc

    r204 r245  
    1616#endif
    1717
    18 #define NV_GL_FUN( rtype, fname, fparams ) rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
     18// for wgl support
     19#if NV_PLATFORM == NV_WINDOWS
     20#    ifndef WIN32_LEAN_AND_MEAN
     21#      define WIN32_LEAN_AND_MEAN 1
     22#    endif
     23#include <windows.h>
     24#    undef WIN32_LEAN_AND_MEAN
     25#endif
     26
     27// extern added for wgl needs only!
     28#define NV_GL_FUN( rtype, fname, fparams ) extern "C" rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
     29#define NV_GL_FUN_REN( rtype, fname, rname, fparams ) extern "C" rtype (NV_GL_APIENTRY *rname) fparams = nullptr;
    1930#define NV_GL_FUN_EXT NV_GL_FUN
    2031#include <nv/lib/detail/gl_functions.inc>
     32#if NV_PLATFORM == NV_WINDOWS
     33#include <nv/lib/detail/wgl_functions.inc>
     34#endif
     35#undef NV_GL_FUN_REN
    2136#undef NV_GL_FUN_EXT
    2237#undef NV_GL_FUN
    2338
     39static nv::library gl_library;
     40static bool gl_library_loaded = false;
     41static bool wgl_library_loaded = false;
     42
    2443bool nv::load_gl_library( const char* path )
    2544{
     45        if ( gl_library_loaded ) return true;
    2646#if defined( NV_SDL_GL )
    2747#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    2848#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    2949#else
    30         static nv::library gl_library;
    31         if ( gl_library.is_open() ) return true;
    32         gl_library.open( path );
     50        if ( !gl_library.is_open() ) gl_library.open( path );
    3351
    3452        void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
     
    5573#       undef NV_GL_LOAD
    5674#       undef NV_GL_LOAD_EXT
     75        gl_library_loaded = true;
    5776        return true;
    5877}
    5978
     79bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */ )
     80{
     81        if ( wgl_library_loaded ) return true;
     82#if NV_PLATFORM == NV_WINDOWS
     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);
     86#           define NV_GL_LOAD_REN( fname, rname )  *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
     87#else //
     88        if ( !gl_library.is_open() ) gl_library.open( path );
     89
     90        void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
     91        *(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
     92#define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
     93#define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
     94#define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
     95#endif
     96#       define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
     97#       define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
     98#       define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
     99#       include <nv/lib/detail/wgl_functions.inc>
     100#       undef NV_GL_FUN_REN
     101#       undef NV_GL_FUN_EXT
     102#       undef NV_GL_FUN
     103
     104#       undef NV_GL_LOAD
     105#       undef NV_GL_LOAD_EXT
     106#       undef NV_GL_LOAD_REN
     107        wgl_library_loaded = true;
     108        return true;
     109#else
     110        return false;
    60111#endif
     112}
     113
     114#endif // NV_DYNAMIC
     115
     116bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
     117{
     118#if NV_PLATFORM == NV_WINDOWS
     119        if ( !gl_library.is_open() ) gl_library.open( path );
     120        if ( wgl_library_loaded ) return true;
     121
     122        HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC)        = nullptr;
     123        BOOL  (NV_GL_APIENTRY *wgl_makecurrent)   (HDC, HGLRC) = nullptr;
     124        BOOL  (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC)      = nullptr;
     125
     126        *(void **) &wgl_createcontext = gl_library.get("wglCreateContext");
     127        *(void **) &wgl_makecurrent   = gl_library.get("wglMakeCurrent");
     128        *(void **) &wgl_deletecontext = gl_library.get("wglDeleteContext");
     129
     130        WNDCLASS wndClass;
     131        HINSTANCE hInstance = 0;
     132
     133        ZeroMemory(&wndClass, sizeof(WNDCLASS));
     134
     135        wndClass.cbClsExtra = 0;
     136        wndClass.cbWndExtra = 0;                       
     137        wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     138        wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     139        wndClass.hInstance = hInstance;
     140        wndClass.lpfnWndProc = (WNDPROC) DefWindowProc;
     141        wndClass.lpszClassName = TEXT("Dummy67789");
     142        wndClass.lpszMenuName = 0;
     143        wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
     144
     145        DWORD err = GetLastError();
     146        RegisterClass(&wndClass);
     147        err = GetLastError();
     148
     149
     150        HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN,
     151                0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
     152                NULL, GetModuleHandle(nullptr), NULL);
     153
     154        HDC hDC = GetDC(hWndFake);
     155
     156        PIXELFORMATDESCRIPTOR pfd;
     157        memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
     158        pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
     159        pfd.nVersion   = 1;
     160        pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
     161        pfd.iPixelType = PFD_TYPE_RGBA;
     162        pfd.cColorBits = 32;
     163        pfd.cDepthBits = 24;
     164        pfd.iLayerType = PFD_MAIN_PLANE;
     165
     166        int iPixelFormat = ChoosePixelFormat(hDC, &pfd);
     167        if (iPixelFormat == 0)return false;
     168
     169        if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
     170
     171        HGLRC hRCFake = wgl_createcontext(hDC);
     172        wgl_makecurrent(hDC, hRCFake);
     173
     174        LoadLibrary( "gdi32.dll" );
     175        bool gl_loaded = nv::load_gl_library( path );
     176        bool wgl_loaded = nv::load_wgl_library( path );
     177        bool result = gl_loaded && wgl_loaded;
     178
     179        wgl_makecurrent(NULL, NULL);
     180        wgl_deletecontext(hRCFake);
     181        DestroyWindow(hWndFake);
     182        return result;
     183#else
     184        return false;
     185#endif
     186}
     187
Note: See TracChangeset for help on using the changeset viewer.