src/video/winrt/SDL_winrtopengles.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 15 Feb 2015 11:53:24 -0500
changeset 9350 419c75bb3cd4
parent 9347 847eb2564aa4
child 9351 483c411e8e11
permissions -rw-r--r--
WinRT: improved OpenGL ES compatibility on WinPhone/ARM and Surface RT

This change integrates initialization settings for ANGLE/WinRT, as suggested in
MSOpenTech's latest ANGLE template-projects (for MSVC).

This should fix some OpenGL initialization issues on WinPhone 8.1 on ARM, and
on the 1st-generation Surface RT.
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@8615
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     4
slouken@8582
     5
  This software is provided 'as-is', without any express or implied
slouken@8582
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     7
  arising from the use of this software.
slouken@8582
     8
slouken@8582
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
    10
  including commercial applications, and to alter it and redistribute it
slouken@8582
    11
  freely, subject to the following restrictions:
slouken@8582
    12
slouken@8582
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8582
    14
     claim that you wrote the original software. If you use this software
slouken@8582
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8582
    16
     appreciated but is not required.
slouken@8582
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8582
    18
     misrepresented as being the original software.
slouken@8582
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8582
    20
*/
slouken@8609
    21
#include "../../SDL_internal.h"
slouken@8582
    22
slouken@8582
    23
#if SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL
slouken@8582
    24
slouken@8582
    25
/* EGL implementation of SDL OpenGL support */
slouken@8582
    26
slouken@8582
    27
#include "SDL_winrtvideo_cpp.h"
slouken@8582
    28
extern "C" {
slouken@8582
    29
#include "SDL_winrtopengles.h"
dludwig@8663
    30
#include "SDL_loadso.h"
slouken@8582
    31
}
slouken@8582
    32
dludwig@8663
    33
/* Windows includes */
dludwig@8663
    34
#include <wrl/client.h>
dludwig@8663
    35
using namespace Windows::UI::Core;
dludwig@8663
    36
dludwig@8663
    37
/* ANGLE/WinRT constants */
dludwig@8663
    38
static const int ANGLE_D3D_FEATURE_LEVEL_ANY = 0;
dludwig@9350
    39
#define EGL_PLATFORM_ANGLE_ANGLE                        0x3201
dludwig@9350
    40
#define EGL_PLATFORM_ANGLE_TYPE_ANGLE                   0x3202
dludwig@9350
    41
#define EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE      0x3204
dludwig@9350
    42
#define EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE      0x3203
dludwig@9350
    43
#define EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE           0x3205
dludwig@9350
    44
#define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE             0x3207
dludwig@9350
    45
#define EGL_PLATFORM_ANGLE_USE_WARP_ANGLE               0x3208
dludwig@9350
    46
#define EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER   0x320B
dludwig@9350
    47
dludwig@8663
    48
dludwig@8663
    49
/*
dludwig@8663
    50
 * SDL/EGL top-level implementation
dludwig@8663
    51
 */
slouken@8582
    52
slouken@8582
    53
extern "C" int
dludwig@8663
    54
WINRT_GLES_LoadLibrary(_THIS, const char *path)
dludwig@8663
    55
{
dludwig@8663
    56
    SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
dludwig@8663
    57
dludwig@8663
    58
    if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY) != 0) {
dludwig@8663
    59
        return -1;
dludwig@8663
    60
    }
dludwig@8663
    61
dludwig@8663
    62
    /* Load ANGLE/WinRT-specific functions */
dludwig@9211
    63
    CreateWinrtEglWindow_Old_Function CreateWinrtEglWindow = (CreateWinrtEglWindow_Old_Function) SDL_LoadFunction(_this->egl_data->egl_dll_handle, "CreateWinrtEglWindow");
dludwig@9211
    64
    if (CreateWinrtEglWindow) {
dludwig@9211
    65
        /* 'CreateWinrtEglWindow' was found, which means that an an older
dludwig@9211
    66
         * version of ANGLE/WinRT is being used.  Continue setting up EGL,
dludwig@9211
    67
         * as appropriate to this version of ANGLE.
dludwig@9211
    68
         */
dludwig@9211
    69
dludwig@9211
    70
        /* Create an ANGLE/WinRT EGL-window */
dludwig@9211
    71
        /* TODO, WinRT: check for XAML usage before accessing the CoreWindow, as not doing so could lead to a crash */
dludwig@9211
    72
        CoreWindow ^ native_win = CoreWindow::GetForCurrentThread();
dludwig@9211
    73
        Microsoft::WRL::ComPtr<IUnknown> cpp_win = reinterpret_cast<IUnknown *>(native_win);
dludwig@9211
    74
        HRESULT result = CreateWinrtEglWindow(cpp_win, ANGLE_D3D_FEATURE_LEVEL_ANY, &(video_data->winrtEglWindow));
dludwig@9211
    75
        if (FAILED(result)) {
dludwig@9211
    76
            return -1;
dludwig@9211
    77
        }
dludwig@9211
    78
dludwig@9211
    79
        /* Call eglGetDisplay and eglInitialize as appropriate.  On other
dludwig@9211
    80
         * platforms, this would probably get done by SDL_EGL_LoadLibrary,
dludwig@9211
    81
         * however ANGLE/WinRT's current implementation (as of Mar 22, 2014) of
dludwig@9211
    82
         * eglGetDisplay requires that a C++ object be passed into it, so the
dludwig@9211
    83
         * call will be made in this file, a C++ file, instead.
dludwig@9211
    84
         */
dludwig@9211
    85
        Microsoft::WRL::ComPtr<IUnknown> cpp_display = video_data->winrtEglWindow;
dludwig@9211
    86
        _this->egl_data->egl_display = ((eglGetDisplay_Old_Function)_this->egl_data->eglGetDisplay)(cpp_display);
dludwig@9211
    87
        if (!_this->egl_data->egl_display) {
dludwig@9211
    88
            return SDL_SetError("Could not get EGL display");
dludwig@9211
    89
        }
dludwig@9350
    90
dludwig@9350
    91
        if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
dludwig@9350
    92
            return SDL_SetError("Could not initialize EGL");
dludwig@9350
    93
        }
dludwig@9211
    94
    } else {
dludwig@9350
    95
        /* Declare some ANGLE/EGL initialization property-sets, as suggested by
dludwig@9350
    96
         * MSOpenTech's ANGLE-for-WinRT template apps:
dludwig@9350
    97
         */
dludwig@9350
    98
        const EGLint defaultDisplayAttributes[] =
dludwig@9350
    99
        {
dludwig@9211
   100
            EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
dludwig@9350
   101
            EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE, 
dludwig@9350
   102
            EGL_NONE,
dludwig@9350
   103
        };
dludwig@9350
   104
dludwig@9350
   105
        const EGLint fl9_3DisplayAttributes[] =
dludwig@9350
   106
        {
dludwig@9350
   107
            EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
dludwig@9350
   108
            EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
dludwig@9350
   109
            EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
dludwig@9350
   110
            EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE, 
dludwig@9350
   111
            EGL_NONE,
dludwig@9350
   112
        };
dludwig@9350
   113
dludwig@9350
   114
        const EGLint warpDisplayAttributes[] =
dludwig@9350
   115
        {
dludwig@9350
   116
            EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
dludwig@9350
   117
            EGL_PLATFORM_ANGLE_USE_WARP_ANGLE, EGL_TRUE,
dludwig@9350
   118
            EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE, 
dludwig@9211
   119
            EGL_NONE,
dludwig@9211
   120
        };
dludwig@9211
   121
dludwig@9211
   122
        /* 'CreateWinrtEglWindow' was NOT found, which either means that a
dludwig@9211
   123
         * newer version of ANGLE/WinRT is being used, or that we don't have
dludwig@9211
   124
         * a valid copy of ANGLE.
dludwig@9211
   125
         *
dludwig@9211
   126
         * Try loading ANGLE as if it were the newer version.
dludwig@9211
   127
         */
dludwig@9211
   128
        eglGetPlatformDisplayEXT_Function eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_Function)_this->egl_data->eglGetProcAddress("eglGetPlatformDisplayEXT");
dludwig@9211
   129
        if (!eglGetPlatformDisplayEXT) {
dludwig@9211
   130
            return SDL_SetError("Could not retrieve ANGLE/WinRT display function(s)");
dludwig@9211
   131
        }
dludwig@9211
   132
dludwig@9350
   133
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
dludwig@9350
   134
        /* Try initializing EGL at D3D11 Feature Level 10_0+ (which is not
dludwig@9350
   135
         * supported on WinPhone 8.x.
dludwig@9350
   136
         */
dludwig@9350
   137
        _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes);
dludwig@9211
   138
        if (!_this->egl_data->egl_display) {
dludwig@9211
   139
            return SDL_SetError("Could not get EGL display");
dludwig@9211
   140
        }
dludwig@8663
   141
dludwig@9350
   142
        if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE)
dludwig@9350
   143
#endif
dludwig@9350
   144
        {
dludwig@9350
   145
            /* Try initializing EGL at D3D11 Feature Level 9_3, in case the
dludwig@9350
   146
             * 10_0 init fails, or we're on Windows Phone (which only supports
dludwig@9350
   147
             * 9_3).
dludwig@9350
   148
             */
dludwig@9350
   149
            _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes);
dludwig@9350
   150
            if (!_this->egl_data->egl_display) {
dludwig@9350
   151
                return SDL_SetError("Could not get 9_3 EGL display");
dludwig@9350
   152
            }
dludwig@9350
   153
dludwig@9350
   154
            if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
dludwig@9350
   155
                /* Try initializing EGL at D3D11 Feature Level 11_0 on WARP
dludwig@9350
   156
                 * (a Windows-provided, software rasterizer) if all else fails.
dludwig@9350
   157
                 */
dludwig@9350
   158
                _this->egl_data->egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes);
dludwig@9350
   159
                if (!_this->egl_data->egl_display) {
dludwig@9350
   160
                    return SDL_SetError("Could not get WARP EGL display");
dludwig@9350
   161
                }
dludwig@9350
   162
dludwig@9350
   163
                if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
dludwig@9350
   164
                    return SDL_SetError("Could not initialize EGL");
dludwig@9350
   165
                }
dludwig@9350
   166
            }
dludwig@9350
   167
        }
dludwig@8663
   168
    }
dludwig@8663
   169
dludwig@8663
   170
    return 0;
dludwig@8663
   171
}
dludwig@8663
   172
dludwig@8663
   173
extern "C" void
dludwig@8663
   174
WINRT_GLES_UnloadLibrary(_THIS)
dludwig@8663
   175
{
dludwig@8663
   176
    SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
dludwig@8663
   177
dludwig@8663
   178
    /* Release SDL's own COM reference to the ANGLE/WinRT IWinrtEglWindow */
dludwig@8663
   179
    if (video_data->winrtEglWindow) {
dludwig@8663
   180
        video_data->winrtEglWindow->Release();
dludwig@8663
   181
        video_data->winrtEglWindow = nullptr;
dludwig@8663
   182
    }
dludwig@8663
   183
dludwig@8663
   184
    /* Perform the bulk of the unloading */
dludwig@8663
   185
    SDL_EGL_UnloadLibrary(_this);
slouken@8582
   186
}
slouken@8582
   187
slouken@8582
   188
extern "C" {
slouken@8582
   189
SDL_EGL_CreateContext_impl(WINRT)
slouken@8582
   190
SDL_EGL_SwapWindow_impl(WINRT)
slouken@8582
   191
SDL_EGL_MakeCurrent_impl(WINRT)
slouken@8582
   192
}
slouken@8582
   193
slouken@8582
   194
#endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
slouken@8582
   195
slouken@8582
   196
/* vi: set ts=4 sw=4 expandtab: */
slouken@8582
   197