1016 lines
24 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-10-23 07:59:53 +02:00
#include <math.h>
2015-01-07 18:06:50 +01:00
/* Win32/WGL context. */
2015-01-07 18:06:50 +01:00
/* necessary for mingw32 multimon defines: */
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 //_WIN32_WINNT_WIN2K
#endif
2016-12-02 23:36:23 -05:00
#include <tchar.h>
#include <wchar.h>
2015-09-05 14:10:16 +02:00
#include <string.h>
#include <math.h>
2015-09-05 14:10:16 +02:00
#include <windows.h>
#include <commdlg.h>
2015-09-05 14:38:55 +02:00
#include <dynamic/dylib.h>
2017-05-19 16:20:30 +02:00
#include <string/stdstring.h>
2015-09-05 14:38:55 +02:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2017-01-10 19:51:06 +01:00
#include "../../configuration.h"
2015-04-10 06:33:18 +02:00
#include "../../dynamic.h"
#include "../../retroarch.h"
2019-02-02 22:02:24 +01:00
#include "../../verbosity.h"
#include "../../frontend/frontend_driver.h"
2015-11-17 08:01:33 +01:00
2015-04-09 05:16:02 +02:00
#include "../common/win32_common.h"
2015-11-17 08:01:33 +01:00
2019-12-05 17:53:47 +08:00
#ifdef HAVE_EGL
#include "../common/egl_common.h"
2019-12-06 15:21:54 +08:00
#ifdef HAVE_ANGLE
2019-12-05 17:53:47 +08:00
#include "../common/angle_common.h"
#endif
2019-12-06 15:21:54 +08:00
#endif
2019-12-05 17:53:47 +08:00
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
2016-05-16 08:34:56 +02:00
#include "../common/gl_common.h"
#elif defined(HAVE_OPENGL_CORE)
#include "../common/gl_core_common.h"
#elif defined(HAVE_OPENGL1)
#include "../common/gl1_common.h"
2016-05-16 08:34:56 +02:00
#endif
#ifdef HAVE_VULKAN
#include "../common/vulkan_common.h"
#endif
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE) || defined(HAVE_VULKAN)
#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#endif
#ifndef WGL_CONTEXT_MINOR_VERSION_ARB
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif
#ifndef WGL_CONTEXT_PROFILE_MASK_ARB
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#endif
#ifndef WGL_CONTEXT_CORE_PROFILE_BIT_ARB
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x0001
#endif
#ifndef WGL_CONTEXT_FLAGS_ARB
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#endif
#ifndef WGL_CONTEXT_DEBUG_BIT_ARB
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
2015-09-05 14:10:16 +02:00
#endif
2016-05-16 08:34:56 +02:00
#endif
2019-10-03 17:45:59 +02:00
static void gfx_ctx_wgl_destroy(void *data);
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)
2015-11-20 14:32:46 +01:00
typedef HGLRC (APIENTRY *wglCreateContextAttribsProc)(HDC, HGLRC, const int*);
2016-05-16 08:34:56 +02:00
static wglCreateContextAttribsProc pcreate_context;
#endif
2015-11-20 14:32:46 +01:00
static BOOL (APIENTRY *p_swap_interval)(int);
static HGLRC win32_hrc;
static HGLRC win32_hw_hrc;
static HDC win32_hdc;
static bool win32_use_hw_ctx = false;
static bool win32_core_hw_context_enable = false;
static bool wgl_adaptive_vsync = false;
2015-11-11 05:32:00 +01:00
#ifdef HAVE_VULKAN
static gfx_ctx_vulkan_data_t win32_vk;
#endif
2019-12-05 17:53:47 +08:00
#ifdef HAVE_EGL
static egl_ctx_data_t win32_egl;
#endif
static unsigned win32_major = 0;
static unsigned win32_minor = 0;
2018-09-12 00:07:43 +02:00
static int win32_interval = 0;
static enum gfx_ctx_api win32_api = GFX_CTX_NONE;
2018-01-24 02:12:58 +01:00
#ifdef HAVE_DYNAMIC
2019-12-05 17:53:47 +08:00
static dylib_t dll_handle = NULL; /* Handle to OpenGL32.dll/libGLESv2.dll */
2018-01-24 02:12:58 +01:00
#endif
2015-02-08 01:24:44 +01:00
2018-03-01 21:16:54 +01:00
typedef struct gfx_ctx_cgl_data
{
void *empty;
2018-03-01 21:16:54 +01:00
} gfx_ctx_wgl_data_t;
2017-05-16 22:46:56 +02:00
static gfx_ctx_proc_t gfx_ctx_wgl_get_proc_address(const char *symbol)
{
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
2017-05-16 22:46:56 +02:00
{
gfx_ctx_proc_t func = (gfx_ctx_proc_t)wglGetProcAddress(symbol);
if (func)
return func;
}
#endif
break;
default:
break;
}
2018-01-24 02:12:58 +01:00
#ifdef HAVE_DYNAMIC
2017-05-16 22:46:56 +02:00
return (gfx_ctx_proc_t)GetProcAddress((HINSTANCE)dll_handle, symbol);
2018-01-24 02:12:58 +01:00
#else
return NULL;
#endif
2017-05-16 22:46:56 +02:00
}
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
2020-01-04 19:05:05 +01:00
static bool wgl_has_extension(const char *extension, const char *extensions)
{
const char *start = NULL;
const char *terminator = NULL;
const char *where = strchr(extension, ' ');
if (where || *extension == '\0')
return false;
if (!extensions)
return false;
start = extensions;
for (;;)
{
where = strstr(start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0')
return true;
start = terminator;
}
return false;
}
static void create_gl_context(HWND hwnd, bool *quit)
{
struct retro_hw_render_callback *hwr = video_driver_get_hw_context();
bool debug = hwr->debug_context;
bool core_context = (win32_major * 1000 + win32_minor) >= 3001;
win32_hdc = GetDC(hwnd);
2019-08-28 23:58:15 +02:00
win32_setup_pixel_format(win32_hdc, true);
2014-04-19 16:21:37 +02:00
#ifdef GL_DEBUG
debug = true;
2014-04-19 16:21:37 +02:00
#endif
if (win32_hrc)
{
RARCH_LOG("[WGL]: Using cached GL context.\n");
video_driver_set_video_cache_context_ack();
}
else
2014-04-19 16:21:37 +02:00
{
win32_hrc = wglCreateContext(win32_hdc);
2014-10-27 14:35:23 +01:00
/* We'll create shared context later if not. */
if (win32_hrc && !core_context && !debug)
2014-04-19 16:21:37 +02:00
{
win32_hw_hrc = wglCreateContext(win32_hdc);
if (win32_hw_hrc)
2014-04-19 16:21:37 +02:00
{
if (!wglShareLists(win32_hrc, win32_hw_hrc))
2014-04-19 16:21:37 +02:00
{
RARCH_LOG("[WGL]: Failed to share contexts.\n");
2015-11-17 23:14:59 +01:00
*quit = true;
2014-04-19 16:21:37 +02:00
}
}
else
2015-11-17 23:14:59 +01:00
*quit = true;
2014-04-19 16:21:37 +02:00
}
}
if (win32_hrc)
{
if (wglMakeCurrent(win32_hdc, win32_hrc))
2018-05-12 19:03:39 +02:00
g_win32_inited = true;
else
2018-05-12 19:03:39 +02:00
*quit = true;
}
else
2013-06-23 13:05:33 +02:00
{
2015-11-17 23:14:59 +01:00
*quit = true;
return;
2013-06-23 13:05:33 +02:00
}
if (core_context || debug)
2013-06-23 13:05:33 +02:00
{
int attribs[16] = {0};
int *aptr = attribs;
if (core_context)
{
*aptr++ = WGL_CONTEXT_MAJOR_VERSION_ARB;
*aptr++ = win32_major;
*aptr++ = WGL_CONTEXT_MINOR_VERSION_ARB;
*aptr++ = win32_minor;
2014-10-27 14:35:23 +01:00
/* Technically, we don't have core/compat until 3.2.
* Version 3.1 is either compat or not depending
2014-10-27 14:35:23 +01:00
* on GL_ARB_compatibility.
*/
if ((win32_major * 1000 + win32_minor) >= 3002)
{
*aptr++ = WGL_CONTEXT_PROFILE_MASK_ARB;
*aptr++ = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
}
}
if (debug)
{
*aptr++ = WGL_CONTEXT_FLAGS_ARB;
*aptr++ = WGL_CONTEXT_DEBUG_BIT_ARB;
}
*aptr = 0;
2013-06-23 13:05:33 +02:00
if (!pcreate_context)
2017-05-16 22:46:09 +02:00
pcreate_context = (wglCreateContextAttribsProc)gfx_ctx_wgl_get_proc_address("wglCreateContextAttribsARB");
2013-06-23 13:05:33 +02:00
/* In order to support the core info "required_hw_api" field correctly, we should try to init the highest available
* version GL context possible. This means trying successively lower versions until it works, because GL has
* no facility for determining the highest possible supported version.
*/
2013-06-23 13:05:33 +02:00
if (pcreate_context)
{
int i;
int gl_versions[][2] = {{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, {3, 3}, {3, 2}, {3, 1}, {3, 0}};
int gl_version_rows = ARRAY_SIZE(gl_versions);
int (*versions)[2];
int version_rows = 0;
HGLRC context = NULL;
versions = gl_versions;
version_rows = gl_version_rows;
/* only try higher versions when core_context is true */
if (!core_context)
version_rows = 1;
/* try versions from highest down to requested version */
for (i = 0; i < version_rows; i++)
2013-06-23 13:05:33 +02:00
{
if (core_context)
{
attribs[1] = versions[i][0];
attribs[3] = versions[i][1];
}
context = pcreate_context(win32_hdc, NULL, attribs);
if (context)
2014-04-19 16:21:37 +02:00
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(win32_hrc);
win32_hrc = context;
if (!wglMakeCurrent(win32_hdc, win32_hrc))
{
*quit = true;
break;
}
if (win32_use_hw_ctx)
{
win32_hw_hrc = pcreate_context(win32_hdc, context, attribs);
if (!win32_hw_hrc)
{
RARCH_ERR("[WGL]: Failed to create shared context.\n");
*quit = true;
break;
}
}
/* found a suitable version that is high enough, we can stop now */
break;
}
2019-08-07 14:46:47 -04:00
else if (versions[i][0] == win32_major && versions[i][1] == win32_minor)
{
2019-08-07 14:46:47 -04:00
/* The requested version was tried and is not supported, go ahead and fail since everything else will be lower than that. */
break;
}
}
if (!context)
{
RARCH_ERR("[WGL]: Failed to create core context. Falling back to legacy context.\n");
*quit = true;
2014-04-19 16:21:37 +02:00
}
2013-06-23 13:05:33 +02:00
}
else
RARCH_ERR("[WGL]: wglCreateContextAttribsARB not supported.\n");
}
{
const char *(WINAPI * wglGetExtensionsStringARB) (HDC) = 0;
const char *extensions = NULL;
wglGetExtensionsStringARB = (const char *(WINAPI *) (HDC))
gfx_ctx_wgl_get_proc_address("wglGetExtensionsStringARB");
if (wglGetExtensionsStringARB)
{
extensions = wglGetExtensionsStringARB(win32_hdc);
RARCH_LOG("[WGL] extensions: %s\n", extensions);
if (wgl_has_extension("WGL_EXT_swap_control_tear", extensions))
{
RARCH_LOG("[WGL]: Adaptive VSync supported.\n");
wgl_adaptive_vsync = true;
}
}
}
}
2016-05-16 08:34:56 +02:00
#endif
2019-12-05 17:53:47 +08:00
#if defined(HAVE_OPENGLES) && defined(HAVE_EGL)
static void create_gles_context(HWND hwnd, bool *quit)
{
EGLint n, major, minor;
EGLint format;
EGLint attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
2019-12-06 15:21:54 +08:00
#ifdef HAVE_ANGLE
2019-12-05 17:53:47 +08:00
if (!angle_init_context(&win32_egl, EGL_DEFAULT_DISPLAY,
&major, &minor, &n, attribs, NULL))
2019-12-06 15:21:54 +08:00
#else
if (!egl_init_context(&win32_egl, EGL_NONE, EGL_DEFAULT_DISPLAY,
&major, &minor, &n, attribs, NULL))
#endif
2019-12-05 17:53:47 +08:00
{
egl_report_error();
goto error;
}
if (!egl_get_native_visual_id(&win32_egl, &format))
goto error;
if (!egl_create_context(&win32_egl, context_attributes))
{
egl_report_error();
goto error;
}
if (!egl_create_surface(&win32_egl, hwnd))
goto error;
g_win32_inited = true;
return;
error:
*quit = true;
return;
}
#endif
void create_graphics_context(HWND hwnd, bool *quit)
{
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
create_gl_context(hwnd, quit);
2016-05-16 08:34:56 +02:00
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#if defined (HAVE_OPENGLES)
create_gles_context(hwnd, quit);
#endif
break;
case GFX_CTX_VULKAN_API:
{
#ifdef HAVE_VULKAN
RECT rect;
HINSTANCE instance;
unsigned width = 0;
unsigned height = 0;
GetClientRect(hwnd, &rect);
instance = GetModuleHandle(NULL);
width = rect.right - rect.left;
height = rect.bottom - rect.top;
if (!vulkan_surface_create(&win32_vk, VULKAN_WSI_WIN32,
&instance, &hwnd,
width, height, win32_interval))
*quit = true;
2018-05-12 19:03:39 +02:00
g_win32_inited = true;
#endif
}
2016-05-16 08:34:56 +02:00
break;
case GFX_CTX_NONE:
default:
break;
}
}
2015-11-11 18:29:06 +01:00
void *dinput_wgl;
2013-11-05 20:49:04 +01:00
2018-09-12 00:07:43 +02:00
static void gfx_ctx_wgl_swap_interval(void *data, int interval)
{
2014-03-09 17:11:06 +01:00
(void)data;
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
win32_interval = interval;
if (!win32_hrc)
return;
if (!p_swap_interval)
return;
RARCH_LOG("[WGL]: wglSwapInterval(%i)\n", win32_interval);
if (!p_swap_interval(win32_interval))
RARCH_WARN("[WGL]: wglSwapInterval() failed.\n");
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_EGL
if (win32_interval != interval)
{
win32_interval = interval;
egl_set_swap_interval(&win32_egl, win32_interval);
}
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
if (win32_interval != interval)
{
win32_interval = interval;
if (win32_vk.swapchain)
win32_vk.need_new_swapchain = true;
}
#endif
break;
case GFX_CTX_NONE:
default:
win32_interval = interval;
break;
}
}
static void gfx_ctx_wgl_check_window(void *data, bool *quit,
2020-03-06 20:29:15 +01:00
bool *resize, unsigned *width, unsigned *height)
{
win32_check_window(quit, resize, width, height);
switch (win32_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
if (win32_vk.need_new_swapchain)
*resize = true;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static void gfx_ctx_wgl_swap_buffers(void *data)
{
2014-03-09 17:11:06 +01:00
(void)data;
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2017-08-22 17:44:40 +02:00
SwapBuffers(win32_hdc);
2016-05-16 08:34:56 +02:00
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
vulkan_present(&win32_vk, win32_vk.context.current_swapchain_index);
vulkan_acquire_next_image(&win32_vk);
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#if defined(HAVE_EGL)
egl_swap_buffers(&win32_egl);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static bool gfx_ctx_wgl_set_resize(void *data,
unsigned width, unsigned height)
{
2014-03-09 17:11:06 +01:00
(void)data;
(void)width;
(void)height;
switch (win32_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
if (!vulkan_create_swapchain(&win32_vk, width, height, win32_interval))
{
RARCH_ERR("[Win32/Vulkan]: Failed to update swapchain.\n");
return false;
}
if (win32_vk.created_new_swapchain)
vulkan_acquire_next_image(&win32_vk);
win32_vk.context.invalid_swapchain = true;
win32_vk.need_new_swapchain = false;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
return false;
}
static void gfx_ctx_wgl_update_title(void *data)
{
char title[128];
title[0] = '\0';
video_driver_get_window_title(title, sizeof(title));
if (title[0])
{
const ui_window_t *window = ui_companion_driver_get_window_ptr();
if (window)
window->set_title(&main_window, title);
}
}
2016-02-14 16:39:27 +01:00
static void gfx_ctx_wgl_get_video_size(void *data,
unsigned *width, unsigned *height)
{
2015-11-17 09:41:18 +01:00
HWND window = win32_get_window();
2017-08-12 18:15:26 +02:00
(void)data;
2015-11-17 09:41:18 +01:00
if (!window)
{
2015-01-16 16:22:19 +01:00
RECT mon_rect;
MONITORINFOEX current_mon;
unsigned mon_id = 0;
HMONITOR hm_to_use = NULL;
win32_monitor_info(&current_mon, &hm_to_use, &mon_id);
2015-01-16 16:22:19 +01:00
mon_rect = current_mon.rcMonitor;
*width = mon_rect.right - mon_rect.left;
*height = mon_rect.bottom - mon_rect.top;
}
else
{
2018-05-12 19:03:39 +02:00
*width = g_win32_resize_width;
*height = g_win32_resize_height;
}
}
2020-03-07 00:39:06 +01:00
static void *gfx_ctx_wgl_init(void *video_driver)
{
2018-03-01 21:16:54 +01:00
WNDCLASSEX wndclass = {0};
gfx_ctx_wgl_data_t *wgl = (gfx_ctx_wgl_data_t*)calloc(1, sizeof(*wgl));
2018-03-01 21:16:54 +01:00
if (!wgl)
return NULL;
2018-05-12 19:03:39 +02:00
if (g_win32_inited)
2019-10-03 17:45:59 +02:00
gfx_ctx_wgl_destroy(NULL);
2018-01-24 02:12:58 +01:00
#ifdef HAVE_DYNAMIC
2019-12-05 17:53:47 +08:00
#ifdef HAVE_OPENGL
dll_handle = dylib_load("OpenGL32.dll");
#else
dll_handle = dylib_load("libGLESv2.dll");
2019-12-05 17:53:47 +08:00
#endif
2018-01-24 02:12:58 +01:00
#endif
2015-11-17 10:16:16 +01:00
win32_window_reset();
win32_monitor_init();
2015-11-28 21:50:28 +01:00
2020-03-05 18:45:41 +01:00
wndclass.lpfnWndProc = WndProcWGL;
2015-11-19 08:30:37 +01:00
if (!win32_window_init(&wndclass, true, NULL))
2018-03-01 21:16:54 +01:00
goto error;
switch (win32_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
if (!vulkan_context_init(&win32_vk, VULKAN_WSI_WIN32))
2018-03-01 21:16:54 +01:00
goto error;
#endif
break;
2016-05-16 08:25:14 +02:00
case GFX_CTX_NONE:
default:
break;
}
2018-03-01 21:16:54 +01:00
return wgl;
error:
if (wgl)
free(wgl);
return NULL;
}
static void gfx_ctx_wgl_destroy(void *data)
{
2018-03-01 21:16:54 +01:00
HWND window = win32_get_window();
gfx_ctx_wgl_data_t *wgl = (gfx_ctx_wgl_data_t*)data;
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
if (win32_hrc)
{
glFinish();
wglMakeCurrent(NULL, NULL);
if (!video_driver_is_video_cache_context())
{
if (win32_hw_hrc)
wglDeleteContext(win32_hw_hrc);
wglDeleteContext(win32_hrc);
win32_hrc = NULL;
win32_hw_hrc = NULL;
}
}
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN
vulkan_context_destroy(&win32_vk, win32_vk.vk_surface != VK_NULL_HANDLE);
if (win32_vk.context.queue_lock)
slock_free(win32_vk.context.queue_lock);
memset(&win32_vk, 0, sizeof(win32_vk));
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_EGL
egl_destroy(&win32_egl);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
if (window && win32_hdc)
{
ReleaseDC(window, win32_hdc);
win32_hdc = NULL;
}
2015-11-17 09:41:18 +01:00
if (window)
{
2016-06-04 05:54:33 +02:00
win32_monitor_from_window();
2015-11-17 09:44:26 +01:00
win32_destroy_window();
}
2018-05-12 19:03:39 +02:00
if (g_win32_restore_desktop)
{
win32_monitor_get_info();
2018-05-12 19:03:39 +02:00
g_win32_restore_desktop = false;
}
2018-01-24 02:12:58 +01:00
#ifdef HAVE_DYNAMIC
2017-07-03 08:57:26 +02:00
dylib_close(dll_handle);
2018-01-24 02:12:58 +01:00
#endif
2018-03-01 21:16:54 +01:00
if (wgl)
free(wgl);
wgl_adaptive_vsync = false;
win32_core_hw_context_enable = false;
2018-05-12 19:03:39 +02:00
g_win32_inited = false;
win32_major = 0;
win32_minor = 0;
p_swap_interval = NULL;
}
2015-11-20 14:32:46 +01:00
static bool gfx_ctx_wgl_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
2018-09-09 01:28:09 +02:00
#ifdef HAVE_VULKAN
win32_vk.fullscreen = fullscreen;
2018-09-09 01:28:09 +02:00
#endif
if (!win32_set_video_mode(NULL, width, height, fullscreen))
2016-03-29 17:57:13 +02:00
{
RARCH_ERR("[WGL]: win32_set_video_mode failed.\n");
2015-11-20 14:32:46 +01:00
goto error;
2016-03-29 17:57:13 +02:00
}
2015-11-20 14:32:46 +01:00
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2017-05-16 22:46:09 +02:00
p_swap_interval = (BOOL (APIENTRY *)(int))gfx_ctx_wgl_get_proc_address("wglSwapIntervalEXT");
break;
case GFX_CTX_NONE:
default:
break;
}
2015-11-20 14:32:46 +01:00
gfx_ctx_wgl_swap_interval(data, win32_interval);
2015-11-20 14:32:46 +01:00
return true;
error:
gfx_ctx_wgl_destroy(data);
return false;
}
static void gfx_ctx_wgl_input_driver(void *data,
const char *joypad_name,
input_driver_t **input, void **input_data)
{
2020-03-02 20:10:24 +01:00
settings_t *settings = config_get_ptr();
#if _WIN32_WINNT >= 0x0501
2020-03-02 20:10:24 +01:00
const char *input_driver = settings->arrays.input_driver;
/* winraw only available since XP */
2020-03-02 20:10:24 +01:00
if (string_is_equal(input_driver, "raw"))
{
*input_data = input_winraw.init(joypad_name);
if (*input_data)
{
*input = &input_winraw;
dinput_wgl = NULL;
return;
}
}
#endif
2018-03-16 21:03:43 +00:00
#ifdef HAVE_DINPUT
dinput_wgl = input_dinput.init(joypad_name);
*input = dinput_wgl ? &input_dinput : NULL;
*input_data = dinput_wgl;
2018-03-16 21:03:43 +00:00
#endif
}
2018-03-01 21:16:54 +01:00
static enum gfx_ctx_api gfx_ctx_wgl_get_api(void *data)
{
return win32_api;
}
static bool gfx_ctx_wgl_bind_api(void *data,
enum gfx_ctx_api api, unsigned major, unsigned minor)
{
2014-03-09 17:11:06 +01:00
(void)data;
win32_major = major;
win32_minor = minor;
win32_api = api;
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)
2016-05-16 08:34:56 +02:00
if (api == GFX_CTX_OPENGL_API)
return true;
#endif
2019-12-05 17:53:47 +08:00
#if defined(HAVE_OPENGLES)
if (api == GFX_CTX_OPENGL_ES_API)
return true;
#endif
2016-05-16 08:34:56 +02:00
#if defined(HAVE_VULKAN)
if (api == GFX_CTX_VULKAN_API)
return true;
#endif
return false;
}
static void gfx_ctx_wgl_bind_hw_render(void *data, bool enable)
2014-04-19 16:21:37 +02:00
{
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
2019-12-05 17:53:47 +08:00
#if (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)) && !defined(HAVE_OPENGLES)
win32_use_hw_ctx = enable;
if (win32_hdc)
wglMakeCurrent(win32_hdc, enable ? win32_hw_hrc : win32_hrc);
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_EGL
egl_bind_hw_render(&win32_egl, enable);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
2014-04-19 16:21:37 +02:00
}
#ifdef HAVE_VULKAN
static void *gfx_ctx_wgl_get_context_data(void *data)
{
(void)data;
return &win32_vk.context;
2014-04-19 16:21:37 +02:00
}
#endif
2014-04-19 16:21:37 +02:00
static uint32_t gfx_ctx_wgl_get_flags(void *data)
{
uint32_t flags = 0;
switch (win32_api)
2016-05-05 17:35:28 +02:00
{
case GFX_CTX_OPENGL_API:
if (wgl_adaptive_vsync)
BIT32_SET(flags, GFX_CTX_FLAGS_ADAPTIVE_VSYNC);
if (win32_core_hw_context_enable)
BIT32_SET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT);
if (string_is_equal(video_driver_get_ident(), "gl1")) { }
else if (string_is_equal(video_driver_get_ident(), "glcore"))
{
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_SLANG);
#endif
}
else
{
#ifdef HAVE_CG
if (!win32_core_hw_context_enable)
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_CG);
#endif
#ifdef HAVE_GLSL
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_GLSL);
#endif
}
break;
case GFX_CTX_VULKAN_API:
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_SLANG);
#endif
break;
2019-12-05 17:53:47 +08:00
case GFX_CTX_OPENGL_ES_API:
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_SLANG);
#endif
#ifdef HAVE_GLSL
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_GLSL);
#endif
break;
case GFX_CTX_NONE:
default:
break;
2016-05-05 17:35:28 +02:00
}
2016-05-05 17:50:26 +02:00
return flags;
2016-05-05 17:35:28 +02:00
}
static void gfx_ctx_wgl_set_flags(void *data, uint32_t flags)
{
switch (win32_api)
{
case GFX_CTX_OPENGL_API:
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)
if (BIT32_GET(flags, GFX_CTX_FLAGS_ADAPTIVE_VSYNC))
wgl_adaptive_vsync = true;
if (BIT32_GET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT))
win32_core_hw_context_enable = true;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static void gfx_ctx_wgl_get_video_output_size(void *data,
unsigned *width, unsigned *height)
{
win32_get_video_output_size(width, height);
}
static void gfx_ctx_wgl_get_video_output_prev(void *data)
{
}
static void gfx_ctx_wgl_get_video_output_next(void *data)
{
}
const gfx_ctx_driver_t gfx_ctx_wgl = {
gfx_ctx_wgl_init,
gfx_ctx_wgl_destroy,
2018-03-01 21:16:54 +01:00
gfx_ctx_wgl_get_api,
gfx_ctx_wgl_bind_api,
gfx_ctx_wgl_swap_interval,
gfx_ctx_wgl_set_video_mode,
gfx_ctx_wgl_get_video_size,
win32_get_refresh_rate,
gfx_ctx_wgl_get_video_output_size,
gfx_ctx_wgl_get_video_output_prev,
gfx_ctx_wgl_get_video_output_next,
win32_get_metrics,
NULL,
2017-01-18 17:41:27 +01:00
gfx_ctx_wgl_update_title,
gfx_ctx_wgl_check_window,
gfx_ctx_wgl_set_resize,
win32_has_focus,
win32_suppress_screensaver,
true, /* has_windowed */
gfx_ctx_wgl_swap_buffers,
gfx_ctx_wgl_input_driver,
gfx_ctx_wgl_get_proc_address,
NULL,
NULL,
win32_show_cursor,
"wgl",
gfx_ctx_wgl_get_flags,
2016-05-05 17:35:28 +02:00
gfx_ctx_wgl_set_flags,
gfx_ctx_wgl_bind_hw_render,
#ifdef HAVE_VULKAN
gfx_ctx_wgl_get_context_data,
#else
NULL,
#endif
NULL
};