From 3df1670809c3b4dc1a36a728e4a3ce32b2d4182f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 4 Aug 2020 02:08:54 +1000 Subject: [PATCH] libretro: Try for a GLES context first depending on preferred renderer --- .../libretro_host_interface.cpp | 9 +++-- .../libretro_opengl_host_display.cpp | 36 +++++++++++++++---- .../libretro_opengl_host_display.h | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/duckstation-libretro/libretro_host_interface.cpp b/src/duckstation-libretro/libretro_host_interface.cpp index d60664d20..033a7658b 100644 --- a/src/duckstation-libretro/libretro_host_interface.cpp +++ b/src/duckstation-libretro/libretro_host_interface.cpp @@ -824,8 +824,13 @@ bool LibretroHostInterface::RequestHardwareRendererContext() break; case GPURenderer::HardwareOpenGL: - m_hw_render_callback_valid = LibretroOpenGLHostDisplay::RequestHardwareRendererContext(&m_hw_render_callback); - break; + { + const bool prefer_gles = + (preferred_renderer == RETRO_HW_CONTEXT_OPENGLES2 || preferred_renderer == RETRO_HW_CONTEXT_OPENGLES_VERSION); + m_hw_render_callback_valid = + LibretroOpenGLHostDisplay::RequestHardwareRendererContext(&m_hw_render_callback, prefer_gles); + } + break; default: Log_ErrorPrintf("Unhandled renderer %s", Settings::GetRendererName(renderer)); diff --git a/src/duckstation-libretro/libretro_opengl_host_display.cpp b/src/duckstation-libretro/libretro_opengl_host_display.cpp index 97b9565ba..f508bcc19 100644 --- a/src/duckstation-libretro/libretro_opengl_host_display.cpp +++ b/src/duckstation-libretro/libretro_opengl_host_display.cpp @@ -23,15 +23,10 @@ void LibretroOpenGLHostDisplay::SetVSync(bool enabled) Log_DevPrintf("Ignoring SetVSync(%u)", BoolToUInt32(enabled)); } -bool LibretroOpenGLHostDisplay::RequestHardwareRendererContext(retro_hw_render_callback* cb) +static bool TryDesktopVersions(retro_hw_render_callback* cb) { - // Prefer a desktop OpenGL context where possible. If we can't get this, try OpenGL ES. static constexpr std::array, 11> desktop_versions_to_try = { - {/*{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, {3, 3}, {3, 2}, */ {3, 1}, {3, 0}}}; - static constexpr std::array, 4> es_versions_to_try = {{{3, 2}, {3, 1}, {3, 0}}}; - - cb->cache_context = true; - cb->bottom_left_origin = true; + {/*{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, */ {3, 3}, {3, 2}, {3, 1}, {3, 0}}}; for (const auto& [major, minor] : desktop_versions_to_try) { @@ -52,6 +47,13 @@ bool LibretroOpenGLHostDisplay::RequestHardwareRendererContext(retro_hw_render_c return true; } + return false; +} + +static bool TryESVersions(retro_hw_render_callback* cb) +{ + static constexpr std::array, 4> es_versions_to_try = {{{3, 2}, {3, 1}, {3, 0}}}; + for (const auto& [major, minor] : es_versions_to_try) { if (major >= 3 && minor > 0) @@ -71,6 +73,26 @@ bool LibretroOpenGLHostDisplay::RequestHardwareRendererContext(retro_hw_render_c return true; } + return false; +} + +bool LibretroOpenGLHostDisplay::RequestHardwareRendererContext(retro_hw_render_callback* cb, bool prefer_gles) +{ + // Prefer a desktop OpenGL context where possible. If we can't get this, try OpenGL ES. + cb->cache_context = true; + cb->bottom_left_origin = true; + + if (!prefer_gles) + { + if (TryDesktopVersions(cb) || TryESVersions(cb)) + return true; + } + else + { + if (TryESVersions(cb) || TryDesktopVersions(cb)) + return true; + } + Log_ErrorPrint("Failed to set any GL HW renderer"); return false; } diff --git a/src/duckstation-libretro/libretro_opengl_host_display.h b/src/duckstation-libretro/libretro_opengl_host_display.h index 173d4c522..2f5bb2012 100644 --- a/src/duckstation-libretro/libretro_opengl_host_display.h +++ b/src/duckstation-libretro/libretro_opengl_host_display.h @@ -13,7 +13,7 @@ public: LibretroOpenGLHostDisplay(); ~LibretroOpenGLHostDisplay(); - static bool RequestHardwareRendererContext(retro_hw_render_callback* cb); + static bool RequestHardwareRendererContext(retro_hw_render_callback* cb, bool prefer_gles); RenderAPI GetRenderAPI() const override;