Skip to content

Commit

Permalink
Fixed bugs 2570, 3145, improved OpenGL ES context support on Windows …
Browse files Browse the repository at this point in the history
…and X11

Mark Callow

The attached patch does the following for the X11 and Windows platforms, the only ones where SDL attempts to use context_create_es_profile:

- Adds SDL_HINT_OPENGL_ES_DRIVER by which the application can
  say to use the OpenGL ES driver & EGL rather than the Open GL
  driver. (For bug #2570)
- Adds code to {WIN,X11}_GL_InitExtensions to determine the maximum
  OpenGL ES version supported by the OpenGL driver (for bug #3145)
- Modifies the test that determines whether to use the OpenGL
  driver or the real OpenGL ES driver to take into account the
  hint, the requested and supported ES version and whether ES 1.X
  is being requested. (For bug #2570 & bug #3145)
- Enables the testgles2 test for __WINDOWS__ and __LINUX__ and adds
  the test to the VisualC projects.

With the fix in place I have run testdraw2, testgl and testgles2 without any issues and have run my own apps that use OpenGL, OpenGL ES 3 and OpenGL ES 1.1.
  • Loading branch information
slouken committed Jan 10, 2017
1 parent bf11cd5 commit a52d48c
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 35 deletions.
8 changes: 7 additions & 1 deletion VisualC/tests/testgles2/testgles2.vcxproj
Expand Up @@ -189,11 +189,17 @@
<CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\..\SDLtest\SDLtest.vcxproj">
<Project>{da956fd3-e143-46f2-9fe5-c77bebc56b1a}</Project>
<Private>false</Private>
<CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\test\testgles2.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
4 changes: 4 additions & 0 deletions VisualC/tests/testgles2/testgles2_VS2008.vcproj
Expand Up @@ -336,6 +336,10 @@
CopyLocalSatelliteAssemblies="false"
RelativePathToProject=".\SDLmain\SDLmain_VS2008.vcproj"
/>
<ProjectReference
ReferencedProjectIdentifier="{DA956FD3-E143-46F2-9FE5-C77BEBC56B1A}"
RelativePathToProject=".\SDLtest\SDLtest_VS2008.vcproj"
/>
</References>
<Files>
<File
Expand Down
31 changes: 31 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -744,6 +744,37 @@ extern "C" {
*/
#define SDL_HINT_RPI_VIDEO_LAYER "SDL_RPI_VIDEO_LAYER"

/**
* \brief A variable controlling what driver to use for OpenGL ES contexts.
*
* On some platforms, currently Windows and X11, OpenGL drivers may support
* creating contexts with an OpenGL ES profile. By default SDL uses these
* profiles, when available, otherwise it attempts to load an OpenGL ES
* library, e.g. that provided by the ANGLE project. This variable controls
* whether SDL follows this default behaviour or will always load an
* OpenGL ES library.
*
* Circumstances where this is useful include
* - Testing an app with a particular OpenGL ES implementation, e.g ANGLE,
* or emulator, e.g. those from ARM, Imagination or Qualcomm.
* - Resolving OpenGL ES function addresses at link time by linking with
* the OpenGL ES library instead of querying them at run time with
* SDL_GL_GetProcAddress.
*
* Caution: for an application to work with the default behaviour across
* different OpenGL drivers it \i must query the OpenGL ES function
* addresses at run time using SDL_GL_GetProcAddress.
*
* This variable is ignored on most platforms because OpenGL ES is native
* or not supported.
*
* This variable can be set to the following values:
* "0" - Use ES profile of OpenGL, if available. (Default when not set.)
* "1" - Load OpenGL ES library using the default library names.
*
*/
#define SDL_HINT_OPENGL_ES_DRIVER "SDL_OPENGL_ES_DRIVER"

/**
* \brief An enumeration of hint priorities
*/
Expand Down
2 changes: 2 additions & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -426,6 +426,8 @@ extern SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayM
extern SDL_VideoDisplay *SDL_GetDisplayForWindow(SDL_Window *window);
extern void *SDL_GetDisplayDriverData( int displayIndex );

extern void SDL_GL_DeduceMaxSupportedESProfile(int* major, int* minor);

extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);

extern void SDL_OnWindowShown(SDL_Window * window);
Expand Down
31 changes: 31 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -2886,6 +2886,37 @@ SDL_GL_ExtensionSupported(const char *extension)
#endif
}

/* Deduce supported ES profile versions from the supported
ARB_ES*_compatibility extensions. There is no direct query.
This is normally only called when the OpenGL driver supports
{GLX,WGL}_EXT_create_context_es2_profile.
*/
void
SDL_GL_DeduceMaxSupportedESProfile(int* major, int* minor)
{
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
/* XXX This is fragile; it will break in the event of release of
* new versions of OpenGL ES.
*/
if (SDL_GL_ExtensionSupported("GL_ARB_ES3_2_compatibility")) {
*major = 3;
*minor = 2;
} else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_1_compatibility")) {
*major = 3;
*minor = 1;
} else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_compatibility")) {
*major = 3;
*minor = 0;
} else {
*major = 2;
*minor = 0;
}
#else
return SDL_FALSE;
#endif
}

void
SDL_GL_ResetAttributes()
{
Expand Down
61 changes: 57 additions & 4 deletions src/video/windows/SDL_windowsopengl.c
Expand Up @@ -26,6 +26,7 @@
#include "SDL_loadso.h"
#include "SDL_windowsvideo.h"
#include "SDL_windowsopengles.h"
#include "SDL_hints.h"

/* WGL implementation of SDL OpenGL support */

Expand Down Expand Up @@ -131,6 +132,44 @@ WIN_GL_LoadLibrary(_THIS, const char *path)
return SDL_SetError("Could not retrieve OpenGL functions");
}

/* XXX Too sleazy? WIN_GL_InitExtensions looks for certain OpenGL
extensions via SDL_GL_DeduceMaxSupportedESProfile. This uses
SDL_GL_ExtensionSupported which in turn calls SDL_GL_GetProcAddress.
However SDL_GL_GetProcAddress will fail if the library is not
loaded; it checks for gl_config.driver_loaded > 0. To avoid this
test failing, increment driver_loaded around the call to
WIN_GLInitExtensions.
Successful loading of the library is normally indicated by
SDL_GL_LoadLibrary incrementing driver_loaded immediately after
this function returns 0 to it.
Alternatives to this are:
- moving SDL_GL_DeduceMaxSupportedESProfile to both the WIN and
X11 platforms while adding a function equivalent to
SDL_GL_ExtensionSupported but which directly calls
glGetProcAddress(). Having 3 copies of the
SDL_GL_ExtensionSupported makes this alternative unattractive.
- moving SDL_GL_DeduceMaxSupportedESProfile to a new file shared
by the WIN and X11 platforms while adding a function equivalent
to SDL_GL_ExtensionSupported. This is unattractive due to the
number of project files that will need updating, plus there
will be 2 copies of the SDL_GL_ExtensionSupported code.
- Add a private equivalent of SDL_GL_ExtensionSupported to
SDL_video.c.
- Move the call to WIN_GL_InitExtensions back to WIN_CreateWindow
and add a flag to gl_data to avoid multiple calls to this
expensive function. This is probably the least objectionable
alternative if this increment/decrement trick is unacceptable.
Note that the driver_loaded > 0 check needs to remain in
SDL_GL_ExtensionSupported and SDL_GL_GetProcAddress as they are
public API functions.
*/
++_this->gl_config.driver_loaded;
WIN_GL_InitExtensions(_this);
--_this->gl_config.driver_loaded;

return 0;
}

Expand Down Expand Up @@ -407,9 +446,11 @@ WIN_GL_InitExtensions(_THIS)
}

/* Check for WGL_EXT_create_context_es2_profile */
_this->gl_data->HAS_WGL_EXT_create_context_es2_profile = SDL_FALSE;
if (HasExtension("WGL_EXT_create_context_es2_profile", extensions)) {
_this->gl_data->HAS_WGL_EXT_create_context_es2_profile = SDL_TRUE;
SDL_GL_DeduceMaxSupportedESProfile(
&_this->gl_data->es_profile_max_supported_version.major,
&_this->gl_data->es_profile_max_supported_version.minor
);
}

/* Check for GLX_ARB_context_flush_control */
Expand Down Expand Up @@ -593,14 +634,26 @@ WIN_GL_SetupWindow(_THIS, SDL_Window * window)
return retval;
}

SDL_bool
WIN_GL_UseEGL(_THIS)
{
SDL_assert(_this->gl_data != NULL);
SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES);

return (SDL_GetHintBoolean(SDL_HINT_OPENGL_ES_DRIVER, SDL_FALSE)
|| _this->gl_config.major_version == 1 /* No WGL extension for OpenGL ES 1.x profiles. */
|| _this->gl_config.major_version > _this->gl_data->es_profile_max_supported_version.major
|| (_this->gl_config.major_version == _this->gl_data->es_profile_max_supported_version.major
&& _this->gl_config.minor_version > _this->gl_data->es_profile_max_supported_version.minor));
}

SDL_GLContext
WIN_GL_CreateContext(_THIS, SDL_Window * window)
{
HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
HGLRC context, share_context;

if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES &&
!_this->gl_data->HAS_WGL_EXT_create_context_es2_profile) {
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES && WIN_GL_UseEGL(_this)) {
#if SDL_VIDEO_OPENGL_EGL
/* Switch to EGL based functions */
WIN_GL_UnloadLibrary(_this);
Expand Down
13 changes: 11 additions & 2 deletions src/video/windows/SDL_windowsopengl.h
Expand Up @@ -29,10 +29,18 @@ struct SDL_GLDriverData
{
SDL_bool HAS_WGL_ARB_pixel_format;
SDL_bool HAS_WGL_EXT_swap_control_tear;
SDL_bool HAS_WGL_EXT_create_context_es2_profile;
SDL_bool HAS_WGL_ARB_context_flush_control;

void *(WINAPI * wglGetProcAddress) (const char *proc);
/* Max version of OpenGL ES context that can be created if the
implementation supports WGL_EXT_create_context_es2_profile.
major = minor = 0 when unsupported.
*/
struct {
int major;
int minor;
} es_profile_max_supported_version;

void *(WINAPI * wglGetProcAddress) (const char *proc);
HGLRC(WINAPI * wglCreateContext) (HDC hdc);
BOOL(WINAPI * wglDeleteContext) (HGLRC hglrc);
BOOL(WINAPI * wglMakeCurrent) (HDC hdc, HGLRC hglrc);
Expand All @@ -56,6 +64,7 @@ struct SDL_GLDriverData
extern int WIN_GL_LoadLibrary(_THIS, const char *path);
extern void *WIN_GL_GetProcAddress(_THIS, const char *proc);
extern void WIN_GL_UnloadLibrary(_THIS);
extern SDL_bool WIN_GL_UseEGL(_THIS);
extern int WIN_GL_SetupWindow(_THIS, SDL_Window * window);
extern SDL_GLContext WIN_GL_CreateContext(_THIS, SDL_Window * window);
extern int WIN_GL_MakeCurrent(_THIS, SDL_Window * window,
Expand Down
40 changes: 20 additions & 20 deletions src/video/windows/SDL_windowswindow.c
Expand Up @@ -263,6 +263,8 @@ SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
return 0;
}



int
WIN_CreateWindow(_THIS, SDL_Window * window)
{
Expand Down Expand Up @@ -299,38 +301,36 @@ WIN_CreateWindow(_THIS, SDL_Window * window)
return -1;
}

#if SDL_VIDEO_OPENGL_WGL
/* We need to initialize the extensions before deciding how to create ES profiles */
if (window->flags & SDL_WINDOW_OPENGL) {
WIN_GL_InitExtensions(_this);
if (!(window->flags & SDL_WINDOW_OPENGL)) {
return 0;
}
#endif

/* The rest of this macro mess is for OpenGL or OpenGL ES windows */
#if SDL_VIDEO_OPENGL_ES2
if ((window->flags & SDL_WINDOW_OPENGL) &&
_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
#if SDL_VIDEO_OPENGL_WGL
&& (!_this->gl_data || !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile)
#endif
) {
#if SDL_VIDEO_OPENGL_EGL
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
#if SDL_VIDEO_OPENGL_WGL
&& (!_this->gl_data || WIN_GL_UseEGL(_this))
#endif /* SDL_VIDEO_OPENGL_WGL */
) {
#if SDL_VIDEO_OPENGL_EGL
if (WIN_GLES_SetupWindow(_this, window) < 0) {
WIN_DestroyWindow(_this, window);
return -1;
}
return 0;
#else
return SDL_SetError("Could not create GLES window surface (no EGL support available)");
#endif /* SDL_VIDEO_OPENGL_EGL */
} else
return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
#endif /* SDL_VIDEO_OPENGL_EGL */
}
#endif /* SDL_VIDEO_OPENGL_ES2 */

#if SDL_VIDEO_OPENGL_WGL
if (window->flags & SDL_WINDOW_OPENGL) {
if (WIN_GL_SetupWindow(_this, window) < 0) {
WIN_DestroyWindow(_this, window);
return -1;
}
if (WIN_GL_SetupWindow(_this, window) < 0) {
WIN_DestroyWindow(_this, window);
return -1;
}
#else
return SDL_SetError("Could not create GL window (WGL support not configured)");
#endif

return 0;
Expand Down
26 changes: 23 additions & 3 deletions src/video/x11/SDL_x11opengl.c
Expand Up @@ -24,6 +24,7 @@

#include "SDL_x11video.h"
#include "SDL_assert.h"
#include "SDL_hints.h"

/* GLX implementation of SDL OpenGL support */

Expand Down Expand Up @@ -143,7 +144,6 @@ typedef GLXContext(*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display * dpy,

static void X11_GL_InitExtensions(_THIS);


int
X11_GL_LoadLibrary(_THIS, const char *path)
{
Expand Down Expand Up @@ -222,13 +222,17 @@ X11_GL_LoadLibrary(_THIS, const char *path)
}

/* Initialize extensions */
/* See lengthy comment about the inc/dec in
../windows/SDL_windowsopengl.c. */
++_this->gl_config.driver_loaded;
X11_GL_InitExtensions(_this);
--_this->gl_config.driver_loaded;

/* If we need a GL ES context and there's no
* GLX_EXT_create_context_es2_profile extension, switch over to X11_GLES functions
*/
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES &&
! _this->gl_data->HAS_GLX_EXT_create_context_es2_profile ) {
X11_GL_UseEGL(_this) ) {
#if SDL_VIDEO_OPENGL_EGL
X11_GL_UnloadLibrary(_this);
/* Better avoid conflicts! */
Expand Down Expand Up @@ -380,7 +384,10 @@ X11_GL_InitExtensions(_THIS)

/* Check for GLX_EXT_create_context_es2_profile */
if (HasExtension("GLX_EXT_create_context_es2_profile", extensions)) {
_this->gl_data->HAS_GLX_EXT_create_context_es2_profile = SDL_TRUE;
SDL_GL_DeduceMaxSupportedESProfile(
&_this->gl_data->es_profile_max_supported_version.major,
&_this->gl_data->es_profile_max_supported_version.minor
);
}

/* Check for GLX_ARB_context_flush_control */
Expand Down Expand Up @@ -565,6 +572,19 @@ X11_GL_ErrorHandler(Display * d, XErrorEvent * e)
return (0);
}

SDL_bool
X11_GL_UseEGL(_THIS)
{
SDL_assert(_this->gl_data != NULL);
SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES);

return (SDL_GetHintBoolean(SDL_HINT_OPENGL_ES_DRIVER, SDL_FALSE)
|| _this->gl_config.major_version == 1 /* No GLX extension for OpenGL ES 1.x profiles. */
|| _this->gl_config.major_version > _this->gl_data->es_profile_max_supported_version.major
|| (_this->gl_config.major_version == _this->gl_data->es_profile_max_supported_version.major
&& _this->gl_config.minor_version > _this->gl_data->es_profile_max_supported_version.minor));
}

SDL_GLContext
X11_GL_CreateContext(_THIS, SDL_Window * window)
{
Expand Down

0 comments on commit a52d48c

Please sign in to comment.