ppsspp/libretro/LibretroGraphicsContext.cpp

145 lines
3.7 KiB
C++
Raw Normal View History

2018-03-24 12:44:53 +01:00
#include "libretro/LibretroGraphicsContext.h"
#include "libretro/LibretroGLContext.h"
2020-09-27 15:24:17 +02:00
#include "libretro/LibretroGLCoreContext.h"
#include "libretro/libretro.h"
2018-03-24 12:44:53 +01:00
#include "libretro/LibretroVulkanContext.h"
#ifdef _WIN32
#include "libretro/LibretroD3D11Context.h"
#endif
2018-03-24 12:44:53 +01:00
#include "Common/Log.h"
#include "Core/Config.h"
#include "Core/System.h"
#include "GPU/GPUInterface.h"
retro_video_refresh_t LibretroGraphicsContext::video_cb;
extern "C" {
retro_hw_get_proc_address_t libretro_get_proc_address;
};
2018-03-24 12:44:53 +01:00
void retro_set_video_refresh(retro_video_refresh_t cb) { LibretroGraphicsContext::video_cb = cb; }
static void context_reset() { ((LibretroHWRenderContext *)Libretro::ctx)->ContextReset(); }
static void context_destroy() { ((LibretroHWRenderContext *)Libretro::ctx)->ContextDestroy(); }
bool LibretroHWRenderContext::Init(bool cache_context) {
hw_render_.cache_context = cache_context;
if (!Libretro::environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render_))
return false;
libretro_get_proc_address = hw_render_.get_proc_address;
return true;
}
2018-03-24 12:44:53 +01:00
2018-03-26 17:37:34 +01:00
LibretroHWRenderContext::LibretroHWRenderContext(retro_hw_context_type context_type, unsigned version_major, unsigned version_minor) {
2018-03-24 12:44:53 +01:00
hw_render_.context_type = context_type;
hw_render_.version_major = version_major;
hw_render_.version_minor = version_minor;
hw_render_.context_reset = context_reset;
hw_render_.context_destroy = context_destroy;
hw_render_.depth = true;
}
2018-03-26 17:37:34 +01:00
void LibretroHWRenderContext::ContextReset() {
2018-03-24 12:44:53 +01:00
INFO_LOG(G3D, "Context reset");
// needed to restart the thread
// TODO: find a way to move this to ContextDestroy.
2018-03-26 17:37:34 +01:00
if (!hw_render_.cache_context && Libretro::useEmuThread && draw_ && Libretro::emuThreadState != Libretro::EmuThreadState::PAUSED) {
2018-03-24 12:44:53 +01:00
DestroyDrawContext();
2018-03-26 17:37:34 +01:00
}
2018-03-24 12:44:53 +01:00
2018-03-26 17:37:34 +01:00
if (!draw_) {
2018-03-24 12:44:53 +01:00
CreateDrawContext();
bool success = draw_->CreatePresets();
2020-08-16 12:48:09 +02:00
_assert_(success);
2018-03-24 12:44:53 +01:00
}
GotBackbuffer();
2018-03-26 17:37:34 +01:00
if (gpu) {
2018-03-24 12:44:53 +01:00
gpu->DeviceRestore();
2018-03-26 17:37:34 +01:00
}
2018-03-24 12:44:53 +01:00
}
2018-03-26 17:37:34 +01:00
void LibretroHWRenderContext::ContextDestroy() {
2018-03-24 12:44:53 +01:00
INFO_LOG(G3D, "Context destroy");
2018-03-26 17:37:34 +01:00
if (Libretro::useEmuThread) {
2018-03-24 12:44:53 +01:00
#if 0
Libretro::EmuThreadPause();
#else
Libretro::EmuThreadStop();
#endif
}
2018-03-26 17:37:34 +01:00
if (!hw_render_.cache_context && !Libretro::useEmuThread) {
Shutdown();
2018-03-26 17:37:34 +01:00
}
2018-03-24 12:44:53 +01:00
}
2018-03-26 17:37:34 +01:00
void LibretroGraphicsContext::GotBackbuffer() { draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight); }
2018-03-26 17:37:34 +01:00
void LibretroGraphicsContext::LostBackbuffer() { draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, -1, -1); }
2018-03-26 17:37:34 +01:00
LibretroGraphicsContext *LibretroGraphicsContext::CreateGraphicsContext() {
2018-03-24 12:44:53 +01:00
LibretroGraphicsContext *ctx;
2020-09-27 15:24:17 +02:00
retro_hw_context_type preferred;
if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER, &preferred))
preferred = RETRO_HW_CONTEXT_DUMMY;
2018-03-24 12:44:53 +01:00
2020-09-27 15:24:17 +02:00
#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;
2018-03-26 17:37:34 +01:00
}
2020-09-27 15:24:17 +02:00
#endif
2018-03-24 12:44:53 +01:00
2023-02-12 20:49:58 +08:00
if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_OPENGL || preferred == RETRO_HW_CONTEXT_OPENGLES3) {
2020-09-27 15:24:17 +02:00
ctx = new LibretroGLContext();
2018-03-24 12:44:53 +01:00
2020-09-27 15:24:17 +02:00
if (ctx->Init()) {
return ctx;
}
delete ctx;
2018-03-26 17:37:34 +01:00
}
2018-03-24 12:44:53 +01:00
2022-04-02 16:34:13 -07:00
#ifndef HAVE_LIBNX
2020-09-27 15:24:17 +02:00
if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_VULKAN) {
ctx = new LibretroVulkanContext();
2018-03-24 12:44:53 +01:00
2020-09-27 15:24:17 +02:00
if (ctx->Init()) {
return ctx;
}
delete ctx;
2018-03-26 17:37:34 +01:00
}
2022-04-02 16:34:13 -07:00
#endif
2018-03-24 12:44:53 +01:00
2020-09-27 15:24:17 +02:00
#ifdef _WIN32
if (preferred == RETRO_HW_CONTEXT_DUMMY || preferred == RETRO_HW_CONTEXT_DIRECT3D) {
ctx = new LibretroD3D11Context();
2018-03-24 12:44:53 +01:00
2020-09-27 15:24:17 +02:00
if (ctx->Init()) {
return ctx;
}
delete ctx;
ctx = new LibretroD3D9Context();
if (ctx->Init()) {
return ctx;
}
delete ctx;
2018-03-26 17:37:34 +01:00
}
2018-03-24 12:44:53 +01:00
#endif
ctx = new LibretroSoftwareContext();
ctx->Init();
return ctx;
2018-03-24 12:44:53 +01:00
}