From b41d20dd8265b82352a760a56336459dc06ce43f Mon Sep 17 00:00:00 2001 From: barbudreadmon Date: Sun, 27 Sep 2020 15:24:17 +0200 Subject: [PATCH] glcore context switching --- libretro/CMakeLists.txt | 1 + libretro/LibretroGLContext.h | 2 - libretro/LibretroGLCoreContext.cpp | 36 +++++++++++++++++ libretro/LibretroGLCoreContext.h | 37 +++++++++++++++++ libretro/LibretroGraphicsContext.cpp | 60 +++++++++++++++++++--------- libretro/Makefile.common | 3 +- 6 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 libretro/LibretroGLCoreContext.cpp create mode 100644 libretro/LibretroGLCoreContext.h diff --git a/libretro/CMakeLists.txt b/libretro/CMakeLists.txt index b590cc25df..69efea8980 100644 --- a/libretro/CMakeLists.txt +++ b/libretro/CMakeLists.txt @@ -3,6 +3,7 @@ set(LIBRETRO_SRCS libretro.cpp LibretroGraphicsContext.cpp LibretroGLContext.cpp + LibretroGLCoreContext.cpp LibretroVulkanContext.cpp libretro_vulkan.cpp) diff --git a/libretro/LibretroGLContext.h b/libretro/LibretroGLContext.h index b85c8affb0..73c29701b7 100644 --- a/libretro/LibretroGLContext.h +++ b/libretro/LibretroGLContext.h @@ -9,8 +9,6 @@ public: LibretroGLContext() #ifdef USING_GLES2 : LibretroHWRenderContext(RETRO_HW_CONTEXT_OPENGLES2) -#elif defined(HAVE_OPENGL_CORE) - : LibretroHWRenderContext(RETRO_HW_CONTEXT_OPENGL_CORE, 3, 1) #else : LibretroHWRenderContext(RETRO_HW_CONTEXT_OPENGL) #endif diff --git a/libretro/LibretroGLCoreContext.cpp b/libretro/LibretroGLCoreContext.cpp new file mode 100644 index 0000000000..14c816f1cf --- /dev/null +++ b/libretro/LibretroGLCoreContext.cpp @@ -0,0 +1,36 @@ + +#include "Common/Log.h" +#include "Core/Config.h" +#include "Core/ConfigValues.h" +#include "Core/System.h" +#include "gfx_es2/gpu_features.h" + +#include "libretro/LibretroGLCoreContext.h" + +bool LibretroGLCoreContext::Init() { + if (!LibretroHWRenderContext::Init(true)) + return false; + + g_Config.iGPUBackend = (int)GPUBackend::OPENGL; + return true; +} + +void LibretroGLCoreContext::CreateDrawContext() { + if (!glewInitDone) { +#if !defined(IOS) && !defined(USING_GLES2) + if (glewInit() != GLEW_OK) { + ERROR_LOG(G3D, "glewInit() failed.\n"); + return; + } +#endif + glewInitDone = true; + CheckGLExtensions(); + } + draw_ = Draw::T3DCreateGLContext(); + renderManager_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); +} + +void LibretroGLCoreContext::DestroyDrawContext() { + LibretroHWRenderContext::DestroyDrawContext(); + renderManager_ = nullptr; +} diff --git a/libretro/LibretroGLCoreContext.h b/libretro/LibretroGLCoreContext.h new file mode 100644 index 0000000000..7719a867c2 --- /dev/null +++ b/libretro/LibretroGLCoreContext.h @@ -0,0 +1,37 @@ +#pragma once + +#include "gfx/gl_common.h" +#include "libretro/LibretroGraphicsContext.h" +#include "thin3d/GLRenderManager.h" + +class LibretroGLCoreContext : public LibretroHWRenderContext { +public: + LibretroGLCoreContext() + : LibretroHWRenderContext(RETRO_HW_CONTEXT_OPENGL_CORE, 3, 1) + { + hw_render_.bottom_left_origin = true; + } + + bool Init() override; + void CreateDrawContext() override; + void DestroyDrawContext() override; + void SetRenderTarget() override { + extern GLuint g_defaultFBO; + g_defaultFBO = hw_render_.get_current_framebuffer(); + } + + void ThreadStart() override { renderManager_->ThreadStart(draw_); } + bool ThreadFrame() override { return renderManager_->ThreadFrame(); } + void ThreadEnd() override { renderManager_->ThreadEnd(); } + void StopThread() override { + renderManager_->WaitUntilQueueIdle(); + renderManager_->StopThread(); + } + + GPUCore GetGPUCore() override { return GPUCORE_GLES; } + const char *Ident() override { return "OpenGL Core"; } + +private: + GLRenderManager *renderManager_ = nullptr; + bool glewInitDone = false; +}; diff --git a/libretro/LibretroGraphicsContext.cpp b/libretro/LibretroGraphicsContext.cpp index 4facea7683..01ce61068d 100644 --- a/libretro/LibretroGraphicsContext.cpp +++ b/libretro/LibretroGraphicsContext.cpp @@ -1,6 +1,7 @@ #include "libretro/LibretroGraphicsContext.h" #include "libretro/LibretroGLContext.h" +#include "libretro/LibretroGLCoreContext.h" #include "libretro/libretro.h" #include "libretro/LibretroVulkanContext.h" #ifdef _WIN32 @@ -88,34 +89,55 @@ void LibretroGraphicsContext::LostBackbuffer() { draw_->HandleEvent(Draw::Event: LibretroGraphicsContext *LibretroGraphicsContext::CreateGraphicsContext() { LibretroGraphicsContext *ctx; - ctx = new LibretroGLContext(); + retro_hw_context_type preferred; + if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER, &preferred)) + preferred = RETRO_HW_CONTEXT_DUMMY; - if (ctx->Init()) { - return ctx; +#ifndef USING_GLES2 + if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_OPENGL_CORE) { + ctx = new LibretroGLCoreContext(); + + if (ctx->Init()) { + return ctx; + } + delete ctx; } - delete ctx; +#endif - ctx = new LibretroVulkanContext(); + if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_OPENGL) { + ctx = new LibretroGLContext(); - if (ctx->Init()) { - return ctx; + if (ctx->Init()) { + return ctx; + } + delete ctx; + } + + if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_VULKAN) { + ctx = new LibretroVulkanContext(); + + if (ctx->Init()) { + return ctx; + } + delete ctx; } - delete ctx; #ifdef _WIN32 - ctx = new LibretroD3D11Context(); + if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_DIRECT3D) { + ctx = new LibretroD3D11Context(); - if (ctx->Init()) { - return ctx; + if (ctx->Init()) { + return ctx; + } + delete ctx; + + ctx = new LibretroD3D9Context(); + + if (ctx->Init()) { + return ctx; + } + delete ctx; } - delete ctx; - - ctx = new LibretroD3D9Context(); - - if (ctx->Init()) { - return ctx; - } - delete ctx; #endif #if 1 diff --git a/libretro/Makefile.common b/libretro/Makefile.common index f5c2ef547f..1aa15ddfad 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -696,7 +696,8 @@ endif SOURCES_CXX += \ $(LIBRETRODIR)/libretro.cpp \ $(LIBRETRODIR)/LibretroGraphicsContext.cpp \ - $(LIBRETRODIR)/LibretroGLContext.cpp + $(LIBRETRODIR)/LibretroGLContext.cpp \ + $(LIBRETRODIR)/LibretroGLCoreContext.cpp ifneq ($(STATIC_LINKING), 1) SOURCES_C += \