Get emulator rendering working with Qt

This commit is contained in:
wheremyfoodat 2023-10-01 01:28:08 +03:00
parent 4329976dbc
commit 5155682e0f
12 changed files with 98 additions and 22 deletions

View file

@ -46,7 +46,7 @@ void RendererGL::reset() {
}
}
void RendererGL::initGraphicsContext(SDL_Window* window) {
void RendererGL::initGraphicsContextInternal() {
gl.reset();
auto gl_resources = cmrc::RendererGL::get_filesystem();
@ -168,6 +168,10 @@ void RendererGL::initGraphicsContext(SDL_Window* window) {
reset();
}
// The OpenGL renderer doesn't need to do anything with the GL context (For Qt frontend) or the SDL window (For SDL frontend)
// So we just call initGraphicsContextInternal for both
void RendererGL::initGraphicsContext([[maybe_unused]] SDL_Window* window) { initGraphicsContextInternal(); }
// Set up the OpenGL blending context to match the emulated PICA
void RendererGL::setupBlending() {
// Map of PICA blending equations to OpenGL blending equations. The unused blending equations are equivalent to equation 0 (add)

View file

@ -41,6 +41,8 @@ Emulator::Emulator()
}
#endif
// Only create SDL Window for SDL frontend
#ifdef PANDA3DS_FRONTEND_SDL
if (needOpenGL) {
// Demand 3.3 core for software renderer, or 4.1 core for OpenGL renderer (max available on MacOS)
// MacOS gets mad if we don't explicitly demand a core profile
@ -71,6 +73,7 @@ Emulator::Emulator()
Helpers::warn("Window creation failed: %s", SDL_GetError());
}
}
#endif
#endif
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER)) {
@ -126,6 +129,8 @@ void Emulator::reset(ReloadOption reload) {
void Emulator::step() {}
void Emulator::render() {}
// Main loop for the SDL frontend. TODO: Move it to a dedicated file
#ifdef PANDA3DS_FRONTEND_SDL
void Emulator::run() {
programRunning = true;
@ -403,6 +408,7 @@ void Emulator::run() {
SDL_GL_SwapWindow(window);
}
}
#endif
// Only resume if a ROM is properly loaded
void Emulator::resume() { running = (romType != ROMType::None); }
@ -562,9 +568,6 @@ bool Emulator::loadELF(std::ifstream& file) {
return true;
}
// Reset our graphics context and initialize the GPU's graphics context
void Emulator::initGraphicsContext() { gpu.initGraphicsContext(window); }
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
void Emulator::updateDiscord() {
if (config.discordRpcEnabled) {

View file

@ -4,7 +4,7 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
setWindowTitle("Alber");
// Enable drop events for loading ROMs
setAcceptDrops(true);
resize(320, 240);
resize(400, 240 * 2);
screen.show();
// Set our menu bar up
@ -25,9 +25,42 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
themeSelect->setGeometry(40, 40, 100, 50);
themeSelect->show();
connect(themeSelect, &QComboBox::currentIndexChanged, this, [&](int index) { setTheme(static_cast<Theme>(index)); });
emu = new Emulator();
// The emulator graphics context for the thread should be initialized in the emulator thread due to how GL contexts work
emuThread = std::thread([&]() {
const RendererType rendererType = emu->getConfig().rendererType;
if (rendererType == RendererType::OpenGL || rendererType == RendererType::Software) {
// Make GL context current for this thread, enable VSync
GL::Context* glContext = screen.getGLContext();
glContext->MakeCurrent();
glContext->SetSwapInterval(1);
emu->initGraphicsContext(glContext);
} else if (rendererType != RendererType::Null) {
Helpers::panic("Unsupported renderer type for the Qt backend! Vulkan on Qt is currently WIP, try the SDL frontend instead!");
}
bool success = emu->loadROM("OoT.3ds");
if (!success) {
Helpers::panic("Failed to load ROM");
}
while (true) {
emu->runFrame();
screen.getGLContext()->SwapBuffers();
}
});
}
MainWindow::~MainWindow() { delete menuBar; }
// Cleanup when the main window closes
MainWindow::~MainWindow() {
delete emu;
delete menuBar;
delete themeSelect;
}
void MainWindow::setTheme(Theme theme) {
currentTheme = theme;

View file

@ -20,7 +20,7 @@
#ifdef PANDA3DS_ENABLE_OPENGL
ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) {
// Create a native window for use with our graphics API of choice
resize(320, 240);
resize(400, 240 * 2);
setAutoFillBackground(false);
setAttribute(Qt::WA_NativeWindow, true);
@ -33,18 +33,6 @@ ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) {
if (!createGLContext()) {
Helpers::panic("Failed to create GL context for display");
}
// Make our context current to use it
glContext->MakeCurrent();
// Enable VSync for now
glContext->SetSwapInterval(1);
OpenGL::setViewport(320, 240);
OpenGL::setClearColor(1.0, 0.0, 0.0, 1.0);
OpenGL::clearColor();
// Swap buffers to display our red as a test
glContext->SwapBuffers();
}
bool ScreenWidget::createGLContext() {