Skip to content

Commit

Permalink
video_core: Refactor GPU interface (#7272)
Browse files Browse the repository at this point in the history
* video_core: Refactor GPU interface

* citra_qt: Better debug widget lifetime
  • Loading branch information
raphaelthegreat authored Dec 28, 2023
1 parent 602f4f6 commit 2bb7f89
Show file tree
Hide file tree
Showing 167 changed files with 4,169 additions and 4,863 deletions.
18 changes: 8 additions & 10 deletions CMakeModules/GenerateSCMRev.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ set(HASH_FILES
"${VIDEO_CORE}/shader/generator/spv_fs_shader_gen.h"
"${VIDEO_CORE}/shader/shader.cpp"
"${VIDEO_CORE}/shader/shader.h"
"${VIDEO_CORE}/pica.cpp"
"${VIDEO_CORE}/pica.h"
"${VIDEO_CORE}/regs_framebuffer.h"
"${VIDEO_CORE}/regs_lighting.h"
"${VIDEO_CORE}/regs_pipeline.h"
"${VIDEO_CORE}/regs_rasterizer.h"
"${VIDEO_CORE}/regs_shader.h"
"${VIDEO_CORE}/regs_texturing.h"
"${VIDEO_CORE}/regs.cpp"
"${VIDEO_CORE}/regs.h"
"${VIDEO_CORE}/pica/regs_framebuffer.h"
"${VIDEO_CORE}/pica/regs_lighting.h"
"${VIDEO_CORE}/pica/regs_pipeline.h"
"${VIDEO_CORE}/pica/regs_rasterizer.h"
"${VIDEO_CORE}/pica/regs_shader.h"
"${VIDEO_CORE}/pica/regs_texturing.h"
"${VIDEO_CORE}/pica/regs_internal.cpp"
"${VIDEO_CORE}/pica/regs_internal.h"
)
set(COMBINED "")
foreach (F IN LISTS HASH_FILES)
Expand Down
1 change: 0 additions & 1 deletion src/android/app/src/main/jni/emu_window/emu_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "jni/input_manager.h"
#include "network/network.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"

static bool IsPortraitMode() {
return JNI_FALSE != IDCache::GetEnvForThread()->CallStaticBooleanMethod(
Expand Down
4 changes: 4 additions & 0 deletions src/android/app/src/main/jni/emu_window/emu_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <vector>
#include "core/frontend/emu_window.h"

namespace Core {
class System;
}

class EmuWindow_Android : public Frontend::EmuWindow {
public:
EmuWindow_Android(ANativeWindow* surface);
Expand Down
16 changes: 9 additions & 7 deletions src/android/app/src/main/jni/emu_window/emu_window_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

#include "common/logging/log.h"
#include "common/settings.h"
#include "core/core.h"
#include "input_common/main.h"
#include "jni/emu_window/emu_window_gl.h"
#include "video_core/gpu.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"

static constexpr std::array<EGLint, 15> egl_attribs{EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
Expand Down Expand Up @@ -71,8 +72,8 @@ class SharedContext_Android : public Frontend::GraphicsContext {
EGLContext egl_context{};
};

EmuWindow_Android_OpenGL::EmuWindow_Android_OpenGL(ANativeWindow* surface)
: EmuWindow_Android{surface} {
EmuWindow_Android_OpenGL::EmuWindow_Android_OpenGL(Core::System& system_, ANativeWindow* surface)
: EmuWindow_Android{surface}, system{system_} {
if (egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); egl_display == EGL_NO_DISPLAY) {
LOG_CRITICAL(Frontend, "eglGetDisplay() failed");
return;
Expand Down Expand Up @@ -199,6 +200,9 @@ void EmuWindow_Android_OpenGL::StopPresenting() {
}

void EmuWindow_Android_OpenGL::TryPresenting() {
if (!system.IsPoweredOn()) {
return;
}
if (presenting_state == PresentingState::Initial) [[unlikely]] {
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
Expand All @@ -208,8 +212,6 @@ void EmuWindow_Android_OpenGL::TryPresenting() {
return;
}
eglSwapInterval(egl_display, Settings::values.use_vsync_new ? 1 : 0);
if (VideoCore::g_renderer) {
VideoCore::g_renderer->TryPresent(0);
eglSwapBuffers(egl_display, egl_surface);
}
system.GPU().Renderer().TryPresent(0);
eglSwapBuffers(egl_display, egl_surface);
}
7 changes: 6 additions & 1 deletion src/android/app/src/main/jni/emu_window/emu_window_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

#include "jni/emu_window/emu_window.h"

namespace Core {
class System;
}

struct ANativeWindow;

class EmuWindow_Android_OpenGL : public EmuWindow_Android {
public:
EmuWindow_Android_OpenGL(ANativeWindow* surface);
EmuWindow_Android_OpenGL(Core::System& system, ANativeWindow* surface);
~EmuWindow_Android_OpenGL() override = default;

void TryPresenting() override;
Expand All @@ -30,6 +34,7 @@ class EmuWindow_Android_OpenGL : public EmuWindow_Android {
void DestroyContext() override;

private:
Core::System& system;
EGLConfig egl_config;
EGLSurface egl_surface{};
EGLContext egl_context{};
Expand Down
1 change: 0 additions & 1 deletion src/android/app/src/main/jni/emu_window/emu_window_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "common/logging/log.h"
#include "common/settings.h"
#include "jni/emu_window/emu_window_vk.h"
#include "video_core/video_core.h"

class GraphicsContext_Android final : public Frontend::GraphicsContext {
public:
Expand Down
30 changes: 18 additions & 12 deletions src/android/app/src/main/jni/native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
#include "jni/id_cache.h"
#include "jni/input_manager.h"
#include "jni/ndk_motion.h"
#include "video_core/debug_utils/debug_utils.h"
#include "video_core/gpu.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"

#if CITRA_ARCH(arm64)
#include <adrenotools/driver.h>
Expand Down Expand Up @@ -126,7 +127,7 @@ static bool CheckMicPermission() {

static Core::System::ResultStatus RunCitra(const std::string& filepath) {
// Citra core only supports a single running instance
std::lock_guard<std::mutex> lock(running_mutex);
std::scoped_lock lock(running_mutex);

LOG_INFO(Frontend, "Citra starting...");

Expand All @@ -137,10 +138,12 @@ static Core::System::ResultStatus RunCitra(const std::string& filepath) {
return Core::System::ResultStatus::ErrorLoader;
}

Core::System& system{Core::System::GetInstance()};

const auto graphics_api = Settings::values.graphics_api.GetValue();
switch (graphics_api) {
case Settings::GraphicsAPI::OpenGL:
window = std::make_unique<EmuWindow_Android_OpenGL>(s_surf);
window = std::make_unique<EmuWindow_Android_OpenGL>(system, s_surf);
break;
case Settings::GraphicsAPI::Vulkan:
window = std::make_unique<EmuWindow_Android_Vulkan>(s_surf, vulkan_library);
Expand All @@ -150,8 +153,6 @@ static Core::System::ResultStatus RunCitra(const std::string& filepath) {
window = std::make_unique<EmuWindow_Android_Vulkan>(s_surf, vulkan_library);
}

Core::System& system{Core::System::GetInstance()};

// Forces a config reload on game boot, if the user changed settings in the UI
Config{};
// Replace with game-specific settings
Expand Down Expand Up @@ -179,6 +180,7 @@ static Core::System::ResultStatus RunCitra(const std::string& filepath) {
// Register microphone permission check
system.RegisterMicPermissionCheck(&CheckMicPermission);

Pica::g_debug_context = Pica::DebugContext::Construct();
InputManager::Init();

window->MakeCurrent();
Expand All @@ -196,7 +198,7 @@ static Core::System::ResultStatus RunCitra(const std::string& filepath) {
LoadDiskCacheProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);

std::unique_ptr<Frontend::GraphicsContext> cpu_context;
system.Renderer().Rasterizer()->LoadDiskResources(stop_run, &LoadDiskCacheProgress);
system.GPU().Renderer().Rasterizer()->LoadDiskResources(stop_run, &LoadDiskCacheProgress);

LoadDiskCacheProgress(VideoCore::LoadCallbackStage::Complete, 0, 0);

Expand Down Expand Up @@ -275,8 +277,10 @@ void Java_org_citra_citra_1emu_NativeLibrary_surfaceChanged(JNIEnv* env,
if (window) {
window->OnSurfaceChanged(s_surf);
}
if (VideoCore::g_renderer) {
VideoCore::g_renderer->NotifySurfaceChanged();

auto& system = Core::System::GetInstance();
if (system.IsPoweredOn()) {
system.GPU().Renderer().NotifySurfaceChanged();
}

LOG_INFO(Frontend, "Surface changed");
Expand Down Expand Up @@ -311,8 +315,9 @@ void Java_org_citra_citra_1emu_NativeLibrary_notifyOrientationChange([[maybe_unu
jint layout_option,
jint rotation) {
Settings::values.layout_option = static_cast<Settings::LayoutOption>(layout_option);
if (VideoCore::g_renderer) {
VideoCore::g_renderer->UpdateCurrentFramebufferLayout(!(rotation % 2));
auto& system = Core::System::GetInstance();
if (system.IsPoweredOn()) {
system.GPU().Renderer().UpdateCurrentFramebufferLayout(!(rotation % 2));
}
InputManager::screen_rotation = rotation;
Camera::NDK::g_rotation = rotation;
Expand All @@ -322,8 +327,9 @@ void Java_org_citra_citra_1emu_NativeLibrary_swapScreens([[maybe_unused]] JNIEnv
[[maybe_unused]] jobject obj,
jboolean swap_screens, jint rotation) {
Settings::values.swap_screen = swap_screens;
if (VideoCore::g_renderer) {
VideoCore::g_renderer->UpdateCurrentFramebufferLayout(!(rotation % 2));
auto& system = Core::System::GetInstance();
if (system.IsPoweredOn()) {
system.GPU().Renderer().UpdateCurrentFramebufferLayout(!(rotation % 2));
}
InputManager::screen_rotation = rotation;
Camera::NDK::g_rotation = rotation;
Expand Down
8 changes: 5 additions & 3 deletions src/citra/citra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "core/telemetry_session.h"
#include "input_common/main.h"
#include "network/network.h"
#include "video_core/gpu.h"
#include "video_core/renderer_base.h"

#ifdef __unix__
Expand Down Expand Up @@ -438,9 +439,10 @@ int main(int argc, char** argv) {
movie.StartRecording(movie_record, movie_record_author);
}
if (!dump_video.empty() && DynamicLibrary::FFmpeg::LoadFFmpeg()) {
auto& renderer = system.GPU().Renderer();
const auto layout{
Layout::FrameLayoutFromResolutionScale(system.Renderer().GetResolutionScaleFactor())};
auto dumper = std::make_shared<VideoDumper::FFmpegBackend>();
Layout::FrameLayoutFromResolutionScale(renderer.GetResolutionScaleFactor())};
auto dumper = std::make_shared<VideoDumper::FFmpegBackend>(renderer);
if (dumper->StartDumping(dump_video, layout)) {
system.RegisterVideoDumper(dumper);
}
Expand All @@ -458,7 +460,7 @@ int main(int argc, char** argv) {
});

std::atomic_bool stop_run;
system.Renderer().Rasterizer()->LoadDiskResources(
system.GPU().Renderer().Rasterizer()->LoadDiskResources(
stop_run, [](VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total) {
LOG_DEBUG(Frontend, "Loading stage {} progress {} {}", static_cast<u32>(stage), value,
total);
Expand Down
5 changes: 3 additions & 2 deletions src/citra/emu_window/emu_window_sdl2_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#include "citra/emu_window/emu_window_sdl2_gl.h"
#include "common/scm_rev.h"
#include "common/settings.h"
#include "core/core.h"
#include "video_core/gpu.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"

class SDLGLContext : public Frontend::GraphicsContext {
public:
Expand Down Expand Up @@ -159,7 +160,7 @@ void EmuWindow_SDL2_GL::Present() {
SDL_GL_MakeCurrent(render_window, window_context);
SDL_GL_SetSwapInterval(1);
while (IsOpen()) {
VideoCore::g_renderer->TryPresent(100, is_secondary);
system.GPU().Renderer().TryPresent(100, is_secondary);
SDL_GL_SwapWindow(render_window);
}
SDL_GL_MakeCurrent(render_window, nullptr);
Expand Down
3 changes: 2 additions & 1 deletion src/citra/emu_window/emu_window_sdl2_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "common/settings.h"
#include "core/core.h"
#include "core/frontend/emu_window.h"
#include "video_core/gpu.h"
#include "video_core/renderer_software/renderer_software.h"

class DummyContext : public Frontend::GraphicsContext {};
Expand Down Expand Up @@ -94,7 +95,7 @@ void EmuWindow_SDL2_SW::Present() {
}

SDL_Surface* EmuWindow_SDL2_SW::LoadFramebuffer(VideoCore::ScreenId screen_id) {
const auto& renderer = static_cast<SwRenderer::RendererSoftware&>(system.Renderer());
const auto& renderer = static_cast<SwRenderer::RendererSoftware&>(system.GPU().Renderer());
const auto& info = renderer.Screen(screen_id);
const int width = static_cast<int>(info.width);
const int height = static_cast<int>(info.height);
Expand Down
15 changes: 7 additions & 8 deletions src/citra_qt/bootmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include "input_common/main.h"
#include "input_common/motion_emu.h"
#include "video_core/custom_textures/custom_tex_manager.h"
#include "video_core/gpu.h"
#include "video_core/renderer_base.h"
#include "video_core/renderer_software/renderer_software.h"
#include "video_core/video_core.h"

#ifdef HAS_OPENGL
#include <glad/glad.h>
Expand Down Expand Up @@ -73,7 +73,7 @@ void EmuThread::run() {

emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);

system.Renderer().Rasterizer()->LoadDiskResources(
system.GPU().Renderer().Rasterizer()->LoadDiskResources(
stop_run, [this](VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total) {
emit LoadProgress(stage, value, total);
});
Expand Down Expand Up @@ -284,9 +284,7 @@ class OpenGLRenderWidget : public RenderWidget {
}
context->MakeCurrent();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
if (VideoCore::g_renderer) {
VideoCore::g_renderer->TryPresent(100, is_secondary);
}
system.GPU().Renderer().TryPresent(100, is_secondary);
context->SwapBuffers();
glFinish();
}
Expand Down Expand Up @@ -367,7 +365,7 @@ struct SoftwareRenderWidget : public RenderWidget {
}

QImage LoadFramebuffer(VideoCore::ScreenId screen_id) {
const auto& renderer = static_cast<SwRenderer::RendererSoftware&>(system.Renderer());
const auto& renderer = static_cast<SwRenderer::RendererSoftware&>(system.GPU().Renderer());
const auto& info = renderer.Screen(screen_id);
const int width = static_cast<int>(info.width);
const int height = static_cast<int>(info.height);
Expand Down Expand Up @@ -678,13 +676,14 @@ void GRenderWindow::ReleaseRenderTarget() {
}

void GRenderWindow::CaptureScreenshot(u32 res_scale, const QString& screenshot_path) {
auto& renderer = system.GPU().Renderer();
if (res_scale == 0) {
res_scale = system.Renderer().GetResolutionScaleFactor();
res_scale = renderer.GetResolutionScaleFactor();
}

const auto layout{Layout::FrameLayoutFromResolutionScale(res_scale, is_secondary)};
screenshot_image = QImage(QSize(layout.width, layout.height), QImage::Format_RGB32);
system.Renderer().RequestScreenshot(
renderer.RequestScreenshot(
screenshot_image.bits(),
[this, screenshot_path](bool invert_y) {
const std::string std_screenshot_path = screenshot_path.toStdString();
Expand Down
1 change: 0 additions & 1 deletion src/citra_qt/configuration/configure_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ ConfigureGraphics::ConfigureGraphics(std::span<const QString> physical_devices,
ui->physical_device_combo->addItem(name);
}

ui->toggle_vsync_new->setEnabled(!is_powered_on);
ui->graphics_api_combo->setEnabled(!is_powered_on);
ui->physical_device_combo->setEnabled(!is_powered_on);
ui->toggle_async_shaders->setEnabled(!is_powered_on);
Expand Down
Loading

0 comments on commit 2bb7f89

Please sign in to comment.