src/video/windows/SDL_windowsopengl.c
changeset 10806 36f40b8cc979
parent 10737 3406a0f8b041
child 11332 e3797888c6f1
     1.1 --- a/src/video/windows/SDL_windowsopengl.c	Mon Jan 09 20:37:52 2017 -0800
     1.2 +++ b/src/video/windows/SDL_windowsopengl.c	Tue Jan 10 08:54:33 2017 -0800
     1.3 @@ -26,6 +26,7 @@
     1.4  #include "SDL_loadso.h"
     1.5  #include "SDL_windowsvideo.h"
     1.6  #include "SDL_windowsopengles.h"
     1.7 +#include "SDL_hints.h"
     1.8  
     1.9  /* WGL implementation of SDL OpenGL support */
    1.10  
    1.11 @@ -131,6 +132,44 @@
    1.12          return SDL_SetError("Could not retrieve OpenGL functions");
    1.13      }
    1.14  
    1.15 +    /* XXX Too sleazy? WIN_GL_InitExtensions looks for certain OpenGL
    1.16 +       extensions via SDL_GL_DeduceMaxSupportedESProfile. This uses
    1.17 +       SDL_GL_ExtensionSupported which in turn calls SDL_GL_GetProcAddress.
    1.18 +       However SDL_GL_GetProcAddress will fail if the library is not
    1.19 +       loaded; it checks for gl_config.driver_loaded > 0. To avoid this
    1.20 +       test failing, increment driver_loaded around the call to
    1.21 +       WIN_GLInitExtensions.
    1.22 +
    1.23 +       Successful loading of the library is normally indicated by
    1.24 +       SDL_GL_LoadLibrary incrementing driver_loaded immediately after
    1.25 +       this function returns 0 to it.
    1.26 +
    1.27 +       Alternatives to this are:
    1.28 +       - moving SDL_GL_DeduceMaxSupportedESProfile to both the WIN and
    1.29 +         X11 platforms while adding a function equivalent to
    1.30 +         SDL_GL_ExtensionSupported but which directly calls
    1.31 +         glGetProcAddress(). Having 3 copies of the
    1.32 +         SDL_GL_ExtensionSupported makes this alternative unattractive.
    1.33 +       - moving SDL_GL_DeduceMaxSupportedESProfile to a new file shared
    1.34 +         by the WIN and X11 platforms while adding a function equivalent
    1.35 +         to SDL_GL_ExtensionSupported. This is unattractive due to the
    1.36 +         number of project files that will need updating, plus there
    1.37 +         will be 2 copies of the SDL_GL_ExtensionSupported code.
    1.38 +       - Add a private equivalent of SDL_GL_ExtensionSupported to
    1.39 +         SDL_video.c.
    1.40 +       - Move the call to WIN_GL_InitExtensions back to WIN_CreateWindow
    1.41 +         and add a flag to gl_data to avoid multiple calls to this
    1.42 +         expensive function. This is probably the least objectionable
    1.43 +         alternative if this increment/decrement trick is unacceptable.
    1.44 +
    1.45 +       Note that the driver_loaded > 0 check needs to remain in
    1.46 +       SDL_GL_ExtensionSupported and SDL_GL_GetProcAddress as they are
    1.47 +       public API functions.
    1.48 +    */
    1.49 +    ++_this->gl_config.driver_loaded;
    1.50 +    WIN_GL_InitExtensions(_this);
    1.51 +    --_this->gl_config.driver_loaded;
    1.52 +
    1.53      return 0;
    1.54  }
    1.55  
    1.56 @@ -407,9 +446,11 @@
    1.57      }
    1.58  
    1.59      /* Check for WGL_EXT_create_context_es2_profile */
    1.60 -    _this->gl_data->HAS_WGL_EXT_create_context_es2_profile = SDL_FALSE;
    1.61      if (HasExtension("WGL_EXT_create_context_es2_profile", extensions)) {
    1.62 -        _this->gl_data->HAS_WGL_EXT_create_context_es2_profile = SDL_TRUE;
    1.63 +        SDL_GL_DeduceMaxSupportedESProfile(
    1.64 +            &_this->gl_data->es_profile_max_supported_version.major,
    1.65 +            &_this->gl_data->es_profile_max_supported_version.minor
    1.66 +        );
    1.67      }
    1.68  
    1.69      /* Check for GLX_ARB_context_flush_control */
    1.70 @@ -593,14 +634,26 @@
    1.71      return retval;
    1.72  }
    1.73  
    1.74 +SDL_bool
    1.75 +WIN_GL_UseEGL(_THIS)
    1.76 +{
    1.77 +    SDL_assert(_this->gl_data != NULL);
    1.78 +    SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
    1.79 +
    1.80 +    return (SDL_GetHintBoolean(SDL_HINT_OPENGL_ES_DRIVER, SDL_FALSE)
    1.81 +            || _this->gl_config.major_version == 1 /* No WGL extension for OpenGL ES 1.x profiles. */
    1.82 +            || _this->gl_config.major_version > _this->gl_data->es_profile_max_supported_version.major
    1.83 +            || (_this->gl_config.major_version == _this->gl_data->es_profile_max_supported_version.major
    1.84 +                && _this->gl_config.minor_version > _this->gl_data->es_profile_max_supported_version.minor));
    1.85 +}
    1.86 +
    1.87  SDL_GLContext
    1.88  WIN_GL_CreateContext(_THIS, SDL_Window * window)
    1.89  {
    1.90      HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    1.91      HGLRC context, share_context;
    1.92  
    1.93 -    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES &&
    1.94 -        !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile) {
    1.95 +    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES && WIN_GL_UseEGL(_this)) {
    1.96  #if SDL_VIDEO_OPENGL_EGL        
    1.97          /* Switch to EGL based functions */
    1.98          WIN_GL_UnloadLibrary(_this);