Move more logic from frontend to base HostInterface

This commit is contained in:
Connor McLaughlin 2020-02-16 00:14:28 +09:00
parent f0578bb932
commit fd8ed08307
17 changed files with 539 additions and 619 deletions

View File

@ -54,44 +54,87 @@ HostInterface::HostInterface()
m_game_list->SetDatabaseFilename(GetGameListDatabaseFileName()); m_game_list->SetDatabaseFilename(GetGameListDatabaseFileName());
} }
HostInterface::~HostInterface() = default; HostInterface::~HostInterface()
bool HostInterface::CreateSystem()
{ {
m_system = System::Create(this); // system should be shut down prior to the destructor
Assert(!m_system && !m_audio_stream && !m_display);
}
// Pull in any invalid settings which have been reset. bool HostInterface::BootSystemFromFile(const char* filename)
m_settings = m_system->GetSettings(); {
m_paused = true; if (!AcquireHostDisplay())
{
ReportFormattedError("Failed to acquire host display");
return false;
}
// set host display settings
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
// create the audio stream. this will never fail, since we'll just fall back to null
m_audio_stream = CreateAudioStream(m_settings.audio_backend);
if (!m_audio_stream || !m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4))
{
ReportFormattedError("Failed to create or configure audio stream, falling back to null output.");
m_audio_stream.reset();
m_audio_stream = AudioStream::CreateNullAudioStream();
m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4);
}
m_system = System::Create(this);
if (!m_system->Boot(filename))
{
ReportFormattedError("System failed to boot. The log may contain more information.");
DestroySystem();
return false;
}
OnSystemCreated();
m_paused = m_settings.start_paused;
m_audio_stream->PauseOutput(m_paused);
UpdateSpeedLimiterState(); UpdateSpeedLimiterState();
if (m_paused)
OnSystemPaused(true);
return true; return true;
} }
bool HostInterface::BootSystem(const char* filename, const char* state_filename) bool HostInterface::BootSystemFromBIOS()
{ {
if (!m_system->Boot(filename)) return BootSystemFromFile(nullptr);
return false; }
m_paused = m_settings.start_paused; void HostInterface::PauseSystem(bool paused)
{
if (paused == m_paused)
return;
m_paused = paused;
m_audio_stream->PauseOutput(m_paused);
OnSystemPaused(paused);
UpdateSpeedLimiterState(); UpdateSpeedLimiterState();
if (state_filename && !LoadState(state_filename))
return false;
return true;
} }
void HostInterface::ResetSystem() void HostInterface::ResetSystem()
{ {
m_system->Reset(); m_system->Reset();
m_system->ResetPerformanceCounters();
AddOSDMessage("System reset."); AddOSDMessage("System reset.");
} }
void HostInterface::DestroySystem() void HostInterface::DestroySystem()
{ {
if (!m_system)
return;
m_system.reset(); m_system.reset();
m_paused = false; m_paused = false;
UpdateSpeedLimiterState(); m_audio_stream.reset();
ReleaseHostDisplay();
OnSystemDestroyed();
OnRunningGameChanged();
} }
void HostInterface::ReportError(const char* message) void HostInterface::ReportError(const char* message)
@ -283,11 +326,6 @@ void HostInterface::DrawDebugWindows()
m_system->GetMDEC()->DrawDebugStateWindow(); m_system->GetMDEC()->DrawDebugStateWindow();
} }
void HostInterface::ClearImGuiFocus()
{
ImGui::SetWindowFocus(nullptr);
}
std::optional<std::vector<u8>> HostInterface::GetBIOSImage(ConsoleRegion region) std::optional<std::vector<u8>> HostInterface::GetBIOSImage(ConsoleRegion region)
{ {
// Try the other default filenames in the directory of the configured BIOS. // Try the other default filenames in the directory of the configured BIOS.
@ -349,28 +387,48 @@ bool HostInterface::LoadState(const char* filename)
if (!stream) if (!stream)
return false; return false;
AddFormattedOSDMessage(2.0f, "Loading state from %s...", filename); AddFormattedOSDMessage(2.0f, "Loading state from '%s'...", filename);
const bool result = m_system->LoadState(stream.get()); if (m_system)
if (!result)
{ {
ReportFormattedError("Loading state from %s failed. Resetting.", filename); if (!m_system->LoadState(stream.get()))
m_system->Reset(); {
ReportFormattedError("Loading state from '%s' failed. Resetting.", filename);
m_system->Reset();
return false;
}
m_system->ResetPerformanceCounters();
}
else
{
if (!BootSystemFromFile(nullptr))
{
ReportFormattedError("Failed to boot system to load state from '%s'.", filename);
return false;
}
if (!m_system->LoadState(stream.get()))
{
ReportFormattedError("Failed to load state. The log may contain more information. Shutting down system.");
DestroySystem();
return false;
}
} }
return result; return true;
} }
bool HostInterface::LoadState(bool global, u32 slot) bool HostInterface::LoadState(bool global, u32 slot)
{ {
const std::string& code = m_system->GetRunningCode(); if (!global && (!m_system || m_system->GetRunningCode().empty()))
if (!global && code.empty())
{ {
ReportFormattedError("Can't save per-game state without a running game code."); ReportFormattedError("Can't save per-game state without a running game code.");
return false; return false;
} }
std::string save_path = global ? GetGlobalSaveStateFileName(slot) : GetGameSaveStateFileName(code.c_str(), slot); std::string save_path =
global ? GetGlobalSaveStateFileName(slot) : GetGameSaveStateFileName(m_system->GetRunningCode().c_str(), slot);
return LoadState(save_path.c_str()); return LoadState(save_path.c_str());
} }
@ -385,12 +443,12 @@ bool HostInterface::SaveState(const char* filename)
const bool result = m_system->SaveState(stream.get()); const bool result = m_system->SaveState(stream.get());
if (!result) if (!result)
{ {
ReportFormattedError("Saving state to %s failed.", filename); ReportFormattedError("Saving state to '%s' failed.", filename);
stream->Discard(); stream->Discard();
} }
else else
{ {
AddFormattedOSDMessage(2.0f, "State saved to %s.", filename); AddFormattedOSDMessage(2.0f, "State saved to '%s'.", filename);
stream->Commit(); stream->Commit();
} }
@ -431,7 +489,17 @@ void HostInterface::UpdateSpeedLimiterState()
m_system->ResetPerformanceCounters(); m_system->ResetPerformanceCounters();
} }
void HostInterface::SwitchGPURenderer() {} void HostInterface::OnSystemCreated() {}
void HostInterface::OnSystemPaused(bool paused)
{
ReportFormattedMessage("System %s.", paused ? "paused" : "resumed");
}
void HostInterface::OnSystemDestroyed()
{
ReportFormattedMessage("System shut down.");
}
void HostInterface::OnSystemPerformanceCountersUpdated() {} void HostInterface::OnSystemPerformanceCountersUpdated() {}
@ -644,16 +712,16 @@ void HostInterface::UpdateSettings(const std::function<void()>& apply_callback)
apply_callback(); apply_callback();
if (m_settings.gpu_renderer != old_gpu_renderer) if (m_settings.gpu_renderer != old_gpu_renderer)
SwitchGPURenderer(); RecreateSystem();
if (m_settings.video_sync_enabled != old_vsync_enabled || m_settings.audio_sync_enabled != old_audio_sync_enabled ||
m_settings.speed_limiter_enabled != old_speed_limiter_enabled)
{
UpdateSpeedLimiterState();
}
if (m_system) if (m_system)
{ {
if (m_settings.video_sync_enabled != old_vsync_enabled || m_settings.audio_sync_enabled != old_audio_sync_enabled ||
m_settings.speed_limiter_enabled != old_speed_limiter_enabled)
{
UpdateSpeedLimiterState();
}
if (m_settings.emulation_speed != old_emulation_speed) if (m_settings.emulation_speed != old_emulation_speed)
{ {
m_system->UpdateThrottlePeriod(); m_system->UpdateThrottlePeriod();
@ -672,7 +740,7 @@ void HostInterface::UpdateSettings(const std::function<void()>& apply_callback)
} }
} }
if (m_settings.display_linear_filtering != old_display_linear_filtering) if (m_display && m_settings.display_linear_filtering != old_display_linear_filtering)
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering); m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
} }
@ -704,3 +772,30 @@ void HostInterface::ModifyResolutionScale(s32 increment)
GPU::VRAM_WIDTH * m_settings.gpu_resolution_scale, GPU::VRAM_WIDTH * m_settings.gpu_resolution_scale,
GPU::VRAM_HEIGHT * m_settings.gpu_resolution_scale); GPU::VRAM_HEIGHT * m_settings.gpu_resolution_scale);
} }
void HostInterface::RecreateSystem()
{
std::unique_ptr<ByteStream> stream = ByteStream_CreateGrowableMemoryStream(nullptr, 8 * 1024);
if (!m_system->SaveState(stream.get()) || !stream->SeekAbsolute(0))
{
ReportError("Failed to save state before system recreation. Shutting down.");
DestroySystem();
return;
}
DestroySystem();
if (!BootSystemFromFile(nullptr))
{
ReportError("Failed to boot system after recreation.");
return;
}
if (!m_system->LoadState(stream.get()))
{
ReportError("Failed to load state after system recreation. Shutting down.");
DestroySystem();
return;
}
m_system->ResetPerformanceCounters();
}

View File

@ -12,6 +12,7 @@
#include <vector> #include <vector>
class AudioStream; class AudioStream;
class ByteStream;
class CDImage; class CDImage;
class HostDisplay; class HostDisplay;
class GameList; class GameList;
@ -27,22 +28,31 @@ public:
virtual ~HostInterface(); virtual ~HostInterface();
/// Access to host display. /// Access to host display.
ALWAYS_INLINE HostDisplay* GetDisplay() const { return m_display.get(); } ALWAYS_INLINE HostDisplay* GetDisplay() const { return m_display; }
/// Access to host audio stream. /// Access to host audio stream.
AudioStream* GetAudioStream() const { return m_audio_stream.get(); } ALWAYS_INLINE AudioStream* GetAudioStream() const { return m_audio_stream.get(); }
/// Returns a settings object which can be modified. /// Returns a settings object which can be modified.
Settings& GetSettings() { return m_settings; } ALWAYS_INLINE Settings& GetSettings() { return m_settings; }
/// Returns the game list. /// Returns the game list.
const GameList* GetGameList() const { return m_game_list.get(); } ALWAYS_INLINE const GameList* GetGameList() const { return m_game_list.get(); }
bool CreateSystem(); bool BootSystemFromFile(const char* filename);
bool BootSystem(const char* filename, const char* state_filename); bool BootSystemFromBIOS();
void PauseSystem(bool paused);
void ResetSystem(); void ResetSystem();
void DestroySystem(); void DestroySystem();
/// Loads the current emulation state from file. Specifying a slot of -1 loads the "resume" game state.
bool LoadState(bool global, u32 slot);
bool LoadState(const char* filename);
/// Saves the current emulation state to a file. Specifying a slot of -1 saves the "resume" save state.
bool SaveState(bool global, u32 slot);
bool SaveState(const char* filename);
virtual void ReportError(const char* message); virtual void ReportError(const char* message);
virtual void ReportMessage(const char* message); virtual void ReportMessage(const char* message);
@ -53,12 +63,8 @@ public:
void AddOSDMessage(const char* message, float duration = 2.0f); void AddOSDMessage(const char* message, float duration = 2.0f);
void AddFormattedOSDMessage(float duration, const char* format, ...); void AddFormattedOSDMessage(float duration, const char* format, ...);
/// Loads the BIOS image for the specified region.
virtual std::optional<std::vector<u8>> GetBIOSImage(ConsoleRegion region);
/// Returns the base user directory path. /// Returns the base user directory path.
const std::string& GetUserDirectory() const { return m_user_directory; } ALWAYS_INLINE const std::string& GetUserDirectory() const { return m_user_directory; }
/// Returns a path relative to the user directory. /// Returns a path relative to the user directory.
std::string GetUserDirectoryRelativePath(const char* format, ...) const; std::string GetUserDirectoryRelativePath(const char* format, ...) const;
@ -89,7 +95,13 @@ protected:
bool global; bool global;
}; };
virtual void SwitchGPURenderer(); virtual bool AcquireHostDisplay() = 0;
virtual void ReleaseHostDisplay() = 0;
virtual std::unique_ptr<AudioStream> CreateAudioStream(AudioBackend backend) = 0;
virtual void OnSystemCreated();
virtual void OnSystemPaused(bool paused);
virtual void OnSystemDestroyed();
virtual void OnSystemPerformanceCountersUpdated(); virtual void OnSystemPerformanceCountersUpdated();
virtual void OnRunningGameChanged(); virtual void OnRunningGameChanged();
@ -122,13 +134,8 @@ protected:
/// Returns a list of save states for the specified game code. /// Returns a list of save states for the specified game code.
std::vector<SaveStateInfo> GetAvailableSaveStates(const char* game_code) const; std::vector<SaveStateInfo> GetAvailableSaveStates(const char* game_code) const;
/// Loads the current emulation state from file. Specifying a slot of -1 loads the "resume" game state. /// Loads the BIOS image for the specified region.
bool LoadState(bool global, u32 slot); std::optional<std::vector<u8>> GetBIOSImage(ConsoleRegion region);
bool LoadState(const char* filename);
/// Saves the current emulation state to a file. Specifying a slot of -1 saves the "resume" save state.
bool SaveState(bool global, u32 slot);
bool SaveState(const char* filename);
/// Restores all settings to defaults. /// Restores all settings to defaults.
void SetDefaultSettings(); void SetDefaultSettings();
@ -143,14 +150,16 @@ protected:
/// Adjusts the internal (render) resolution of the hardware backends. /// Adjusts the internal (render) resolution of the hardware backends.
void ModifyResolutionScale(s32 increment); void ModifyResolutionScale(s32 increment);
/// Switches the GPU renderer by saving state, recreating the display window, and restoring state (if needed).
void RecreateSystem();
void UpdateSpeedLimiterState(); void UpdateSpeedLimiterState();
void DrawFPSWindow(); void DrawFPSWindow();
void DrawOSDMessages(); void DrawOSDMessages();
void DrawDebugWindows(); void DrawDebugWindows();
void ClearImGuiFocus();
std::unique_ptr<HostDisplay> m_display; HostDisplay* m_display = nullptr;
std::unique_ptr<AudioStream> m_audio_stream; std::unique_ptr<AudioStream> m_audio_stream;
std::unique_ptr<System> m_system; std::unique_ptr<System> m_system;
std::unique_ptr<GameList> m_game_list; std::unique_ptr<GameList> m_game_list;

View File

@ -169,6 +169,11 @@ void D3D11DisplayWindow::onWindowResized(int width, int height)
Panic("Failed to recreate swap chain RTV after resize"); Panic("Failed to recreate swap chain RTV after resize");
} }
bool D3D11DisplayWindow::hasDeviceContext() const
{
return static_cast<bool>(m_device);
}
bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device) bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device)
{ {
ComPtr<IDXGIFactory> dxgi_factory; ComPtr<IDXGIFactory> dxgi_factory;

View File

@ -21,6 +21,7 @@ public:
HostDisplay* getHostDisplayInterface() override; HostDisplay* getHostDisplayInterface() override;
bool hasDeviceContext() const override;
bool createDeviceContext(QThread* worker_thread, bool debug_device) override; bool createDeviceContext(QThread* worker_thread, bool debug_device) override;
bool initializeDeviceContext(bool debug_device) override; bool initializeDeviceContext(bool debug_device) override;
void destroyDeviceContext() override; void destroyDeviceContext() override;

View File

@ -1,8 +1,10 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "common/assert.h"
#include "core/game_list.h" #include "core/game_list.h"
#include "core/settings.h" #include "core/settings.h"
#include "gamelistsettingswidget.h" #include "gamelistsettingswidget.h"
#include "gamelistwidget.h" #include "gamelistwidget.h"
#include "qtdisplaywindow.h"
#include "qthostinterface.h" #include "qthostinterface.h"
#include "qtsettingsinterface.h" #include "qtsettingsinterface.h"
#include "settingsdialog.h" #include "settingsdialog.h"
@ -28,8 +30,7 @@ MainWindow::MainWindow(QtHostInterface* host_interface) : QMainWindow(nullptr),
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete m_display_widget; Assert(!m_display_widget);
m_host_interface->displayWidgetDestroyed();
} }
void MainWindow::reportError(QString message) void MainWindow::reportError(QString message)
@ -42,31 +43,40 @@ void MainWindow::reportMessage(QString message)
m_ui.statusBar->showMessage(message, 2000); m_ui.statusBar->showMessage(message, 2000);
} }
void MainWindow::onEmulationStarting() void MainWindow::createDisplayWindow(QThread* worker_thread, bool use_debug_device)
{ {
DebugAssert(!m_display_widget);
QtDisplayWindow* display_window = m_host_interface->createDisplayWindow();
DebugAssert(display_window);
m_display_widget = QWidget::createWindowContainer(display_window, m_ui.mainContainer);
DebugAssert(m_display_widget);
m_display_widget->setFocusPolicy(Qt::StrongFocus);
m_ui.mainContainer->insertWidget(1, m_display_widget);
// we need the surface visible.. this might be able to be replaced with something else
switchToEmulationView(); switchToEmulationView();
updateEmulationActions(true, false);
// we need the surface visible..
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
display_window->createDeviceContext(worker_thread, use_debug_device);
} }
void MainWindow::onEmulationStarted() void MainWindow::destroyDisplayWindow()
{ {
m_emulation_running = true; DebugAssert(m_display_widget);
updateEmulationActions(false, true);
} const bool was_fullscreen = m_display_widget->isFullScreen();
if (was_fullscreen)
toggleFullscreen();
void MainWindow::onEmulationStopped()
{
m_emulation_running = false;
updateEmulationActions(false, false);
switchToGameListView(); switchToGameListView();
}
void MainWindow::onEmulationPaused(bool paused) // recreate the display widget using the potentially-new renderer
{ m_ui.mainContainer->removeWidget(m_display_widget);
m_ui.actionPause->setChecked(paused); delete m_display_widget;
m_display_widget = nullptr;
} }
void MainWindow::toggleFullscreen() void MainWindow::toggleFullscreen()
@ -91,36 +101,22 @@ void MainWindow::toggleFullscreen()
m_ui.actionFullscreen->setChecked(fullscreen); m_ui.actionFullscreen->setChecked(fullscreen);
} }
void MainWindow::recreateDisplayWidget(bool create_device_context) void MainWindow::onEmulationStarted()
{ {
const bool was_fullscreen = m_display_widget->isFullScreen(); m_emulation_running = true;
if (was_fullscreen) updateEmulationActions(false, true);
toggleFullscreen(); }
void MainWindow::onEmulationStopped()
{
m_emulation_running = false;
updateEmulationActions(false, false);
switchToGameListView(); switchToGameListView();
}
// recreate the display widget using the potentially-new renderer void MainWindow::onEmulationPaused(bool paused)
m_ui.mainContainer->removeWidget(m_display_widget); {
m_host_interface->displayWidgetDestroyed(); m_ui.actionPause->setChecked(paused);
delete m_display_widget;
m_display_widget = m_host_interface->createDisplayWidget(m_ui.mainContainer);
m_ui.mainContainer->insertWidget(1, m_display_widget);
if (create_device_context)
switchToEmulationView();
// we need the surface visible..
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
if (create_device_context && !m_host_interface->createDisplayDeviceContext())
{
QMessageBox::critical(this, tr("DuckStation Error"),
tr("Failed to create new device context on renderer switch. Cannot continue."));
QCoreApplication::exit();
return;
}
updateDebugMenuGPURenderer();
} }
void MainWindow::onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time, void MainWindow::onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time,
@ -149,7 +145,7 @@ void MainWindow::onStartDiscActionTriggered()
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
m_host_interface->bootSystem(std::move(filename), QString()); m_host_interface->bootSystemFromFile(std::move(filename));
} }
void MainWindow::onChangeDiscFromFileActionTriggered() void MainWindow::onChangeDiscFromFileActionTriggered()
@ -168,11 +164,6 @@ void MainWindow::onChangeDiscFromGameListActionTriggered()
switchToGameListView(); switchToGameListView();
} }
void MainWindow::onStartBiosActionTriggered()
{
m_host_interface->bootSystem(QString(), QString());
}
static void OpenURL(QWidget* parent, const char* url) static void OpenURL(QWidget* parent, const char* url)
{ {
const QUrl qurl(QUrl::fromEncoded(QByteArray(url, static_cast<int>(std::strlen(url))))); const QUrl qurl(QUrl::fromEncoded(QByteArray(url, static_cast<int>(std::strlen(url)))));
@ -200,10 +191,6 @@ void MainWindow::setupAdditionalUi()
m_game_list_widget = new GameListWidget(m_ui.mainContainer); m_game_list_widget = new GameListWidget(m_ui.mainContainer);
m_game_list_widget->initialize(m_host_interface); m_game_list_widget->initialize(m_host_interface);
m_ui.mainContainer->insertWidget(0, m_game_list_widget); m_ui.mainContainer->insertWidget(0, m_game_list_widget);
m_display_widget = m_host_interface->createDisplayWidget(m_ui.mainContainer);
m_ui.mainContainer->insertWidget(1, m_display_widget);
m_ui.mainContainer->setCurrentIndex(0); m_ui.mainContainer->setCurrentIndex(0);
m_status_speed_widget = new QLabel(m_ui.statusBar); m_status_speed_widget = new QLabel(m_ui.statusBar);
@ -304,14 +291,14 @@ void MainWindow::connectSignals()
onEmulationPaused(false); onEmulationPaused(false);
connect(m_ui.actionStartDisc, &QAction::triggered, this, &MainWindow::onStartDiscActionTriggered); connect(m_ui.actionStartDisc, &QAction::triggered, this, &MainWindow::onStartDiscActionTriggered);
connect(m_ui.actionStartBios, &QAction::triggered, this, &MainWindow::onStartBiosActionTriggered); connect(m_ui.actionStartBios, &QAction::triggered, m_host_interface, &QtHostInterface::bootSystemFromBIOS);
connect(m_ui.actionChangeDisc, &QAction::triggered, [this] { m_ui.menuChangeDisc->exec(QCursor::pos()); }); connect(m_ui.actionChangeDisc, &QAction::triggered, [this] { m_ui.menuChangeDisc->exec(QCursor::pos()); });
connect(m_ui.actionChangeDiscFromFile, &QAction::triggered, this, &MainWindow::onChangeDiscFromFileActionTriggered); connect(m_ui.actionChangeDiscFromFile, &QAction::triggered, this, &MainWindow::onChangeDiscFromFileActionTriggered);
connect(m_ui.actionChangeDiscFromGameList, &QAction::triggered, this, connect(m_ui.actionChangeDiscFromGameList, &QAction::triggered, this,
&MainWindow::onChangeDiscFromGameListActionTriggered); &MainWindow::onChangeDiscFromGameListActionTriggered);
connect(m_ui.actionAddGameDirectory, &QAction::triggered, connect(m_ui.actionAddGameDirectory, &QAction::triggered,
[this]() { getSettingsDialog()->getGameListSettingsWidget()->addSearchDirectory(this); }); [this]() { getSettingsDialog()->getGameListSettingsWidget()->addSearchDirectory(this); });
connect(m_ui.actionPowerOff, &QAction::triggered, [this]() { m_host_interface->powerOffSystem(true, false); }); connect(m_ui.actionPowerOff, &QAction::triggered, [this]() { m_host_interface->destroySystem(true, false); });
connect(m_ui.actionReset, &QAction::triggered, m_host_interface, &QtHostInterface::resetSystem); connect(m_ui.actionReset, &QAction::triggered, m_host_interface, &QtHostInterface::resetSystem);
connect(m_ui.actionPause, &QAction::toggled, m_host_interface, &QtHostInterface::pauseSystem); connect(m_ui.actionPause, &QAction::toggled, m_host_interface, &QtHostInterface::pauseSystem);
connect(m_ui.actionLoadState, &QAction::triggered, this, [this]() { m_ui.menuLoadState->exec(QCursor::pos()); }); connect(m_ui.actionLoadState, &QAction::triggered, this, [this]() { m_ui.menuLoadState->exec(QCursor::pos()); });
@ -336,14 +323,14 @@ void MainWindow::connectSignals()
connect(m_host_interface, &QtHostInterface::errorReported, this, &MainWindow::reportError, connect(m_host_interface, &QtHostInterface::errorReported, this, &MainWindow::reportError,
Qt::BlockingQueuedConnection); Qt::BlockingQueuedConnection);
connect(m_host_interface, &QtHostInterface::createDisplayWindowRequested, this, &MainWindow::createDisplayWindow,
Qt::BlockingQueuedConnection);
connect(m_host_interface, &QtHostInterface::destroyDisplayWindowRequested, this, &MainWindow::destroyDisplayWindow);
connect(m_host_interface, &QtHostInterface::toggleFullscreenRequested, this, &MainWindow::toggleFullscreen);
connect(m_host_interface, &QtHostInterface::messageReported, this, &MainWindow::reportMessage); connect(m_host_interface, &QtHostInterface::messageReported, this, &MainWindow::reportMessage);
connect(m_host_interface, &QtHostInterface::emulationStarting, this, &MainWindow::onEmulationStarting);
connect(m_host_interface, &QtHostInterface::emulationStarted, this, &MainWindow::onEmulationStarted); connect(m_host_interface, &QtHostInterface::emulationStarted, this, &MainWindow::onEmulationStarted);
connect(m_host_interface, &QtHostInterface::emulationStopped, this, &MainWindow::onEmulationStopped); connect(m_host_interface, &QtHostInterface::emulationStopped, this, &MainWindow::onEmulationStopped);
connect(m_host_interface, &QtHostInterface::emulationPaused, this, &MainWindow::onEmulationPaused); connect(m_host_interface, &QtHostInterface::emulationPaused, this, &MainWindow::onEmulationPaused);
connect(m_host_interface, &QtHostInterface::toggleFullscreenRequested, this, &MainWindow::toggleFullscreen);
connect(m_host_interface, &QtHostInterface::recreateDisplayWidgetRequested, this, &MainWindow::recreateDisplayWidget,
Qt::BlockingQueuedConnection);
connect(m_host_interface, &QtHostInterface::systemPerformanceCountersUpdated, this, connect(m_host_interface, &QtHostInterface::systemPerformanceCountersUpdated, this,
&MainWindow::onSystemPerformanceCountersUpdated); &MainWindow::onSystemPerformanceCountersUpdated);
connect(m_host_interface, &QtHostInterface::runningGameChanged, this, &MainWindow::onRunningGameChanged); connect(m_host_interface, &QtHostInterface::runningGameChanged, this, &MainWindow::onRunningGameChanged);
@ -353,7 +340,7 @@ void MainWindow::connectSignals()
QString path = QString::fromStdString(entry->path); QString path = QString::fromStdString(entry->path);
if (!m_emulation_running) if (!m_emulation_running)
{ {
m_host_interface->bootSystem(path, QString()); m_host_interface->bootSystemFromFile(path);
} }
else else
{ {
@ -446,6 +433,6 @@ void MainWindow::updateDebugMenuGPURenderer()
void MainWindow::closeEvent(QCloseEvent* event) void MainWindow::closeEvent(QCloseEvent* event)
{ {
m_host_interface->powerOffSystem(true, true); m_host_interface->destroySystem(true, true);
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);
} }

View File

@ -8,6 +8,7 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
class QLabel; class QLabel;
class QThread;
class GameListWidget; class GameListWidget;
class QtHostInterface; class QtHostInterface;
@ -23,12 +24,12 @@ public:
private Q_SLOTS: private Q_SLOTS:
void reportError(QString message); void reportError(QString message);
void reportMessage(QString message); void reportMessage(QString message);
void onEmulationStarting(); void createDisplayWindow(QThread* worker_thread, bool use_debug_device);
void destroyDisplayWindow();
void toggleFullscreen();
void onEmulationStarted(); void onEmulationStarted();
void onEmulationStopped(); void onEmulationStopped();
void onEmulationPaused(bool paused); void onEmulationPaused(bool paused);
void toggleFullscreen();
void recreateDisplayWidget(bool create_device_context);
void onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time, void onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time,
float worst_frame_time); float worst_frame_time);
void onRunningGameChanged(QString filename, QString game_code, QString game_title); void onRunningGameChanged(QString filename, QString game_code, QString game_title);
@ -36,7 +37,6 @@ private Q_SLOTS:
void onStartDiscActionTriggered(); void onStartDiscActionTriggered();
void onChangeDiscFromFileActionTriggered(); void onChangeDiscFromFileActionTriggered();
void onChangeDiscFromGameListActionTriggered(); void onChangeDiscFromGameListActionTriggered();
void onStartBiosActionTriggered();
void onGitHubRepositoryActionTriggered(); void onGitHubRepositoryActionTriggered();
void onIssueTrackerActionTriggered(); void onIssueTrackerActionTriggered();
void onAboutActionTriggered(); void onAboutActionTriggered();

View File

@ -223,6 +223,11 @@ static void APIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLen
} }
} }
bool OpenGLDisplayWindow::hasDeviceContext() const
{
return static_cast<bool>(m_gl_context);
}
bool OpenGLDisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device) bool OpenGLDisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device)
{ {
m_gl_context = std::make_unique<QOpenGLContext>(); m_gl_context = std::make_unique<QOpenGLContext>();

View File

@ -27,6 +27,7 @@ public:
HostDisplay* getHostDisplayInterface() override; HostDisplay* getHostDisplayInterface() override;
bool hasDeviceContext() const override;
bool createDeviceContext(QThread* worker_thread, bool debug_device) override; bool createDeviceContext(QThread* worker_thread, bool debug_device) override;
bool initializeDeviceContext(bool debug_device) override; bool initializeDeviceContext(bool debug_device) override;
void destroyDeviceContext() override; void destroyDeviceContext() override;

View File

@ -18,6 +18,11 @@ HostDisplay* QtDisplayWindow::getHostDisplayInterface()
return nullptr; return nullptr;
} }
bool QtDisplayWindow::hasDeviceContext() const
{
return true;
}
bool QtDisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device) bool QtDisplayWindow::createDeviceContext(QThread* worker_thread, bool debug_device)
{ {
return true; return true;

View File

@ -18,6 +18,7 @@ public:
virtual HostDisplay* getHostDisplayInterface(); virtual HostDisplay* getHostDisplayInterface();
virtual bool hasDeviceContext() const;
virtual bool createDeviceContext(QThread* worker_thread, bool debug_device); virtual bool createDeviceContext(QThread* worker_thread, bool debug_device);
virtual bool initializeDeviceContext(bool debug_device); virtual bool initializeDeviceContext(bool debug_device);
virtual void destroyDeviceContext(); virtual void destroyDeviceContext();

View File

@ -151,8 +151,10 @@ void QtHostInterface::refreshGameList(bool invalidate_cache /* = false */, bool
emit gameListRefreshed(); emit gameListRefreshed();
} }
QWidget* QtHostInterface::createDisplayWidget(QWidget* parent) QtDisplayWindow* QtHostInterface::createDisplayWindow()
{ {
Assert(!m_display_window);
#ifdef WIN32 #ifdef WIN32
if (m_settings.gpu_renderer == GPURenderer::HardwareOpenGL) if (m_settings.gpu_renderer == GPURenderer::HardwareOpenGL)
m_display_window = new OpenGLDisplayWindow(this, nullptr); m_display_window = new OpenGLDisplayWindow(this, nullptr);
@ -162,40 +164,29 @@ QWidget* QtHostInterface::createDisplayWidget(QWidget* parent)
m_display_window = new OpenGLDisplayWindow(this, nullptr); m_display_window = new OpenGLDisplayWindow(this, nullptr);
#endif #endif
connect(m_display_window, &QtDisplayWindow::windowResizedEvent, this, &QtHostInterface::onDisplayWindowResized); connect(m_display_window, &QtDisplayWindow::windowResizedEvent, this, &QtHostInterface::onDisplayWindowResized);
return m_display_window;
m_display.release();
m_display = std::unique_ptr<HostDisplay>(m_display_window->getHostDisplayInterface());
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
QWidget* widget = QWidget::createWindowContainer(m_display_window, parent);
widget->setFocusPolicy(Qt::StrongFocus);
return widget;
} }
bool QtHostInterface::createDisplayDeviceContext() void QtHostInterface::bootSystemFromFile(QString filename)
{ {
return m_display_window->createDeviceContext(m_worker_thread, m_settings.gpu_use_debug_device); if (!isOnWorkerThread())
}
void QtHostInterface::displayWidgetDestroyed()
{
m_display.release();
m_display_window = nullptr;
}
void QtHostInterface::bootSystem(QString initial_filename, QString initial_save_state_filename)
{
Assert(!isOnWorkerThread());
emit emulationStarting();
if (!createDisplayDeviceContext())
{ {
emit emulationStopped(); QMetaObject::invokeMethod(this, "bootSystemFromFile", Qt::QueuedConnection, Q_ARG(QString, filename));
return; return;
} }
QMetaObject::invokeMethod(this, "doBootSystem", Qt::QueuedConnection, Q_ARG(QString, initial_filename), HostInterface::BootSystemFromFile(filename.toStdString().c_str());
Q_ARG(QString, initial_save_state_filename)); }
void QtHostInterface::bootSystemFromBIOS()
{
if (!isOnWorkerThread())
{
QMetaObject::invokeMethod(this, "bootSystemFromBIOS", Qt::QueuedConnection);
return;
}
HostInterface::BootSystemFromBIOS();
} }
void QtHostInterface::handleKeyEvent(int key, bool pressed) void QtHostInterface::handleKeyEvent(int key, bool pressed)
@ -223,51 +214,82 @@ void QtHostInterface::onDisplayWindowResized(int width, int height)
m_display_window->onWindowResized(width, height); m_display_window->onWindowResized(width, height);
} }
void QtHostInterface::SwitchGPURenderer() bool QtHostInterface::AcquireHostDisplay()
{ {
// Due to the GPU class owning textures, we have to shut the system down. DebugAssert(!m_display_window);
std::unique_ptr<ByteStream> stream;
if (m_system)
{
stream = ByteStream_CreateGrowableMemoryStream(nullptr, 8 * 1024);
if (!m_system->SaveState(stream.get()) || !stream->SeekAbsolute(0))
ReportError("Failed to save state before GPU renderer switch");
DestroySystem(); emit createDisplayWindowRequested(m_worker_thread, m_settings.gpu_use_debug_device);
m_audio_stream->PauseOutput(true); if (!m_display_window->hasDeviceContext())
{
m_display_window = nullptr;
emit destroyDisplayWindowRequested();
return false;
}
if (!m_display_window->initializeDeviceContext(m_settings.gpu_use_debug_device))
{
m_display_window->destroyDeviceContext(); m_display_window->destroyDeviceContext();
m_display_window = nullptr;
emit destroyDisplayWindowRequested();
return false;
} }
const bool restore_state = static_cast<bool>(stream); m_display = m_display_window->getHostDisplayInterface();
emit recreateDisplayWidgetRequested(restore_state); return true;
Assert(m_display_window != nullptr); }
if (restore_state) void QtHostInterface::ReleaseHostDisplay()
{
DebugAssert(m_display_window && m_display == m_display_window->getHostDisplayInterface());
m_display = nullptr;
m_display_window->disconnect(this);
m_display_window->destroyDeviceContext();
m_display_window = nullptr;
emit destroyDisplayWindowRequested();
}
std::unique_ptr<AudioStream> QtHostInterface::CreateAudioStream(AudioBackend backend)
{
switch (backend)
{ {
if (!m_display_window->initializeDeviceContext(m_settings.gpu_use_debug_device)) case AudioBackend::Default:
{ case AudioBackend::Cubeb:
emit runningGameChanged(QString(), QString(), QString()); return AudioStream::CreateCubebAudioStream();
emit emulationStopped();
return;
}
CreateSystem(); case AudioBackend::Null:
if (!BootSystem(nullptr, nullptr) || !m_system->LoadState(stream.get())) return AudioStream::CreateNullAudioStream();
{
ReportError("Failed to load state after GPU renderer switch, resetting");
m_system->Reset();
}
if (!m_paused) default:
{ return nullptr;
m_audio_stream->PauseOutput(false);
UpdateSpeedLimiterState();
}
m_system->ResetPerformanceCounters();
} }
} }
void QtHostInterface::OnSystemCreated()
{
HostInterface::OnSystemCreated();
wakeThread();
emit emulationStarted();
}
void QtHostInterface::OnSystemPaused(bool paused)
{
HostInterface::OnSystemPaused(paused);
if (!paused)
wakeThread();
emit emulationPaused(paused);
}
void QtHostInterface::OnSystemDestroyed()
{
HostInterface::OnSystemDestroyed();
emit emulationStopped();
}
void QtHostInterface::OnSystemPerformanceCountersUpdated() void QtHostInterface::OnSystemPerformanceCountersUpdated()
{ {
HostInterface::OnSystemPerformanceCountersUpdated(); HostInterface::OnSystemPerformanceCountersUpdated();
@ -457,11 +479,11 @@ void QtHostInterface::addButtonToInputMap(const QString& binding, InputButtonHan
} }
} }
void QtHostInterface::powerOffSystem(bool save_resume_state /* = false */, bool block_until_done /* = false */) void QtHostInterface::destroySystem(bool save_resume_state /* = false */, bool block_until_done /* = false */)
{ {
if (!isOnWorkerThread()) if (!isOnWorkerThread())
{ {
QMetaObject::invokeMethod(this, "powerOffSystem", QMetaObject::invokeMethod(this, "destroySystem",
block_until_done ? Qt::BlockingQueuedConnection : Qt::QueuedConnection, block_until_done ? Qt::BlockingQueuedConnection : Qt::QueuedConnection,
Q_ARG(bool, save_resume_state), Q_ARG(bool, block_until_done)); Q_ARG(bool, save_resume_state), Q_ARG(bool, block_until_done));
return; return;
@ -470,15 +492,7 @@ void QtHostInterface::powerOffSystem(bool save_resume_state /* = false */, bool
if (!m_system) if (!m_system)
return; return;
if (save_resume_state)
Log_InfoPrintf("TODO: Save resume state");
DestroySystem(); DestroySystem();
m_audio_stream->PauseOutput(true);
m_display_window->destroyDeviceContext();
emit runningGameChanged(QString(), QString(), QString());
emit emulationStopped();
} }
void QtHostInterface::resetSystem() void QtHostInterface::resetSystem()
@ -515,59 +529,6 @@ void QtHostInterface::pauseSystem(bool paused)
void QtHostInterface::changeDisc(QString new_disc_filename) {} void QtHostInterface::changeDisc(QString new_disc_filename) {}
void QtHostInterface::doBootSystem(QString initial_filename, QString initial_save_state_filename)
{
if (!m_display_window->initializeDeviceContext(m_settings.gpu_use_debug_device))
{
emit emulationStopped();
return;
}
std::string initial_filename_str = initial_filename.toStdString();
std::string initial_save_state_filename_str = initial_save_state_filename.toStdString();
std::lock_guard<std::mutex> lock(m_qsettings_mutex);
if (!CreateSystem() ||
!BootSystem(initial_filename_str.empty() ? nullptr : initial_filename_str.c_str(),
initial_save_state_filename_str.empty() ? nullptr : initial_save_state_filename_str.c_str()))
{
DestroySystem();
m_display_window->destroyDeviceContext();
emit emulationStopped();
return;
}
wakeThread();
m_audio_stream->PauseOutput(false);
UpdateSpeedLimiterState();
emit emulationStarted();
}
void QtHostInterface::createAudioStream()
{
switch (m_settings.audio_backend)
{
case AudioBackend::Default:
case AudioBackend::Cubeb:
m_audio_stream = AudioStream::CreateCubebAudioStream();
break;
case AudioBackend::Null:
default:
m_audio_stream = AudioStream::CreateNullAudioStream();
break;
}
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4))
{
qWarning() << "Failed to configure audio stream, falling back to null output";
// fall back to null output
m_audio_stream.reset();
m_audio_stream = AudioStream::CreateNullAudioStream();
m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4);
}
}
void QtHostInterface::populateSaveStateMenus(const char* game_code, QMenu* load_menu, QMenu* save_menu) void QtHostInterface::populateSaveStateMenus(const char* game_code, QMenu* load_menu, QMenu* save_menu)
{ {
const std::vector<SaveStateInfo> available_states(GetAvailableSaveStates(game_code)); const std::vector<SaveStateInfo> available_states(GetAvailableSaveStates(game_code));
@ -626,10 +587,7 @@ void QtHostInterface::loadState(QString filename)
return; return;
} }
if (m_system) LoadState(filename.toStdString().c_str());
LoadState(filename.toStdString().c_str());
else
doBootSystem(QString(), filename);
} }
void QtHostInterface::loadState(bool global, qint32 slot) void QtHostInterface::loadState(bool global, qint32 slot)
@ -640,19 +598,7 @@ void QtHostInterface::loadState(bool global, qint32 slot)
return; return;
} }
if (m_system) LoadState(global, slot);
{
LoadState(slot, global);
return;
}
if (!global)
{
// can't load a non-global system without a game code
return;
}
loadState(QString::fromStdString(GetGlobalSaveStateFileName(slot)));
} }
void QtHostInterface::saveState(bool global, qint32 slot, bool block_until_done /* = false */) void QtHostInterface::saveState(bool global, qint32 slot, bool block_until_done /* = false */)
@ -694,8 +640,6 @@ void QtHostInterface::threadEntryPoint()
{ {
m_worker_thread_event_loop = new QEventLoop(); m_worker_thread_event_loop = new QEventLoop();
createAudioStream();
// TODO: Event which flags the thread as ready // TODO: Event which flags the thread as ready
while (!m_shutdown_flag.load()) while (!m_shutdown_flag.load())
{ {

View File

@ -45,11 +45,7 @@ public:
bool isOnWorkerThread() const { return QThread::currentThread() == m_worker_thread; } bool isOnWorkerThread() const { return QThread::currentThread() == m_worker_thread; }
QWidget* createDisplayWidget(QWidget* parent); QtDisplayWindow* createDisplayWindow();
bool createDisplayDeviceContext();
void displayWidgetDestroyed();
void bootSystem(QString initial_filename, QString initial_save_state_filename);
void updateInputMap(); void updateInputMap();
void handleKeyEvent(int key, bool pressed); void handleKeyEvent(int key, bool pressed);
@ -67,20 +63,22 @@ public:
Q_SIGNALS: Q_SIGNALS:
void errorReported(QString message); void errorReported(QString message);
void messageReported(QString message); void messageReported(QString message);
void emulationStarting();
void emulationStarted(); void emulationStarted();
void emulationStopped(); void emulationStopped();
void emulationPaused(bool paused); void emulationPaused(bool paused);
void gameListRefreshed(); void gameListRefreshed();
void createDisplayWindowRequested(QThread* worker_thread, bool use_debug_device);
void destroyDisplayWindowRequested();
void toggleFullscreenRequested(); void toggleFullscreenRequested();
void recreateDisplayWidgetRequested(bool create_device_context);
void systemPerformanceCountersUpdated(float speed, float fps, float vps, float avg_frame_time, void systemPerformanceCountersUpdated(float speed, float fps, float vps, float avg_frame_time,
float worst_frame_time); float worst_frame_time);
void runningGameChanged(QString filename, QString game_code, QString game_title); void runningGameChanged(QString filename, QString game_code, QString game_title);
public Q_SLOTS: public Q_SLOTS:
void applySettings(); void applySettings();
void powerOffSystem(bool save_resume_state = false, bool block_until_done = false); void bootSystemFromFile(QString filename);
void bootSystemFromBIOS();
void destroySystem(bool save_resume_state = false, bool block_until_done = false);
void resetSystem(); void resetSystem();
void pauseSystem(bool paused); void pauseSystem(bool paused);
void changeDisc(QString new_disc_filename); void changeDisc(QString new_disc_filename);
@ -90,13 +88,18 @@ public Q_SLOTS:
private Q_SLOTS: private Q_SLOTS:
void doStopThread(); void doStopThread();
void doBootSystem(QString initial_filename, QString initial_save_state_filename);
void doUpdateInputMap(); void doUpdateInputMap();
void doHandleKeyEvent(int key, bool pressed); void doHandleKeyEvent(int key, bool pressed);
void onDisplayWindowResized(int width, int height); void onDisplayWindowResized(int width, int height);
protected: protected:
void SwitchGPURenderer() override; bool AcquireHostDisplay() override;
void ReleaseHostDisplay() override;
std::unique_ptr<AudioStream> CreateAudioStream(AudioBackend backend) override;
void OnSystemCreated() override;
void OnSystemPaused(bool paused) override;
void OnSystemDestroyed() override;
void OnSystemPerformanceCountersUpdated() override; void OnSystemPerformanceCountersUpdated() override;
void OnRunningGameChanged() override; void OnRunningGameChanged() override;
@ -122,7 +125,6 @@ private:
void updateControllerInputMap(); void updateControllerInputMap();
void updateHotkeyInputMap(); void updateHotkeyInputMap();
void addButtonToInputMap(const QString& binding, InputButtonHandler handler); void addButtonToInputMap(const QString& binding, InputButtonHandler handler);
void createAudioStream();
void createThread(); void createThread();
void stopThread(); void stopThread();
void threadEntryPoint(); void threadEntryPoint();

View File

@ -69,5 +69,5 @@ private:
D3D11::StreamBuffer m_display_uniform_buffer; D3D11::StreamBuffer m_display_uniform_buffer;
bool m_allow_tearing_supported = false; bool m_allow_tearing_supported = false;
bool m_vsync = false; bool m_vsync = true;
}; };

View File

@ -15,28 +15,26 @@ static int Run(int argc, char* argv[])
} }
// parameters // parameters
const char* filename = nullptr; std::optional<s32> state_index;
const char* exp1_filename = nullptr; const char* boot_filename = nullptr;
std::string state_filename;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
#define CHECK_ARG(str) !std::strcmp(argv[i], str) #define CHECK_ARG(str) !std::strcmp(argv[i], str)
#define CHECK_ARG_PARAM(str) (!std::strcmp(argv[i], str) && ((i + 1) < argc)) #define CHECK_ARG_PARAM(str) (!std::strcmp(argv[i], str) && ((i + 1) < argc))
if (CHECK_ARG_PARAM("-state")) if (CHECK_ARG_PARAM("-state"))
state_filename = SDLHostInterface::GetSaveStateFilename(std::strtoul(argv[++i], nullptr, 10)); state_index = std::atoi(argv[++i]);
else if (CHECK_ARG_PARAM("-exp1")) if (CHECK_ARG_PARAM("-resume"))
exp1_filename = argv[++i]; state_index = -1;
else else
filename = argv[i]; boot_filename = argv[i];
#undef CHECK_ARG #undef CHECK_ARG
#undef CHECK_ARG_PARAM #undef CHECK_ARG_PARAM
} }
// create display and host interface // create display and host interface
std::unique_ptr<SDLHostInterface> host_interface = std::unique_ptr<SDLHostInterface> host_interface = SDLHostInterface::Create();
SDLHostInterface::Create(filename, exp1_filename, state_filename.empty() ? nullptr : state_filename.c_str());
if (!host_interface) if (!host_interface)
{ {
Panic("Failed to create host interface"); Panic("Failed to create host interface");
@ -44,6 +42,17 @@ static int Run(int argc, char* argv[])
return -1; return -1;
} }
// boot/load state
if (boot_filename)
{
if (host_interface->BootSystemFromFile(boot_filename) && state_index.has_value())
host_interface->LoadState(false, state_index.value());
}
else if (state_index.has_value())
{
host_interface->LoadState(true, state_index.value());
}
// run // run
host_interface->Run(); host_interface->Run();

View File

@ -262,6 +262,8 @@ bool OpenGLHostDisplay::CreateGLContext(bool debug_device)
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
} }
// start with vsync on
SDL_GL_SetSwapInterval(1);
return true; return true;
} }

View File

@ -33,17 +33,20 @@ SDLHostInterface::SDLHostInterface()
timeBeginPeriod(1); timeBeginPeriod(1);
#endif #endif
m_switch_gpu_renderer_event_id = SDL_RegisterEvents(1); m_update_settings_event_id = SDL_RegisterEvents(1);
} }
SDLHostInterface::~SDLHostInterface() SDLHostInterface::~SDLHostInterface()
{ {
CloseGameControllers(); CloseGameControllers();
m_display.reset(); if (m_display)
ImGui::DestroyContext(); {
DestroyDisplay();
ImGui::DestroyContext();
}
if (m_window) if (m_window)
SDL_DestroyWindow(m_window); DestroySDLWindow();
#ifdef WIN32 #ifdef WIN32
timeEndPeriod(1); timeEndPeriod(1);
@ -87,30 +90,31 @@ void SDLHostInterface::DestroySDLWindow()
bool SDLHostInterface::CreateDisplay() bool SDLHostInterface::CreateDisplay()
{ {
const bool debug_device = m_settings.gpu_use_debug_device; const bool debug_device = m_settings.gpu_use_debug_device;
std::unique_ptr<HostDisplay> display;
#ifdef WIN32 #ifdef WIN32
m_display = UseOpenGLRenderer() ? OpenGLHostDisplay::Create(m_window, debug_device) : display = UseOpenGLRenderer() ? OpenGLHostDisplay::Create(m_window, debug_device) :
D3D11HostDisplay::Create(m_window, debug_device); D3D11HostDisplay::Create(m_window, debug_device);
#else #else
m_display = OpenGLHostDisplay::Create(m_window, debug_device); display = OpenGLHostDisplay::Create(m_window, debug_device);
#endif #endif
if (!m_display) if (!display)
return false; return false;
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
m_app_icon_texture = m_app_icon_texture =
m_display->CreateTexture(APP_ICON_WIDTH, APP_ICON_HEIGHT, APP_ICON_DATA, APP_ICON_WIDTH * sizeof(u32)); display->CreateTexture(APP_ICON_WIDTH, APP_ICON_HEIGHT, APP_ICON_DATA, APP_ICON_WIDTH * sizeof(u32));
if (!m_app_icon_texture) if (!display)
return false; return false;
m_display = display.release();
return true; return true;
} }
void SDLHostInterface::DestroyDisplay() void SDLHostInterface::DestroyDisplay()
{ {
m_app_icon_texture.reset(); m_app_icon_texture.reset();
m_display.reset(); delete m_display;
m_display = nullptr;
} }
void SDLHostInterface::CreateImGuiContext() void SDLHostInterface::CreateImGuiContext()
@ -123,101 +127,93 @@ void SDLHostInterface::CreateImGuiContext()
ImGui::AddRobotoRegularFont(); ImGui::AddRobotoRegularFont();
} }
void SDLHostInterface::CreateAudioStream() bool SDLHostInterface::AcquireHostDisplay()
{
// Handle renderer switch if required on Windows.
#ifdef WIN32
const HostDisplay::RenderAPI render_api = m_display->GetRenderAPI();
const bool render_api_is_gl =
render_api == HostDisplay::RenderAPI::OpenGL || render_api == HostDisplay::RenderAPI::OpenGLES;
const bool render_api_wants_gl = UseOpenGLRenderer();
if (render_api_is_gl != render_api_wants_gl)
{
ImGui::EndFrame();
DestroyDisplay();
DestroySDLWindow();
if (!CreateSDLWindow())
Panic("Failed to recreate SDL window on GPU renderer switch");
if (!CreateDisplay())
Panic("Failed to recreate display on GPU renderer switch");
ImGui::NewFrame();
}
#endif
return true;
}
void SDLHostInterface::ReleaseHostDisplay()
{
// restore vsync, since we don't want to burn cycles at the menu
m_display->SetVSync(true);
}
std::unique_ptr<AudioStream> SDLHostInterface::CreateAudioStream(AudioBackend backend)
{ {
switch (m_settings.audio_backend) switch (m_settings.audio_backend)
{ {
case AudioBackend::Null: case AudioBackend::Null:
m_audio_stream = AudioStream::CreateNullAudioStream(); return AudioStream::CreateNullAudioStream();
break;
case AudioBackend::Cubeb: case AudioBackend::Cubeb:
m_audio_stream = AudioStream::CreateCubebAudioStream(); return AudioStream::CreateCubebAudioStream();
break;
case AudioBackend::Default: case AudioBackend::Default:
default: return std::make_unique<SDLAudioStream>();
m_audio_stream = std::make_unique<SDLAudioStream>();
break;
}
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS)) default:
{ return nullptr;
ReportError("Failed to recreate audio stream, falling back to null");
m_audio_stream.reset();
m_audio_stream = AudioStream::CreateNullAudioStream();
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS))
Panic("Failed to reconfigure null audio stream");
} }
} }
void SDLHostInterface::OnSystemCreated()
{
HostInterface::OnSystemCreated();
UpdateKeyboardControllerMapping();
UpdateControllerControllerMapping();
ClearImGuiFocus();
}
void SDLHostInterface::OnSystemPaused(bool paused)
{
HostInterface::OnSystemPaused(paused);
if (!paused)
ClearImGuiFocus();
}
void SDLHostInterface::OnSystemDestroyed()
{
HostInterface::OnSystemDestroyed();
}
void SDLHostInterface::SaveSettings() void SDLHostInterface::SaveSettings()
{ {
SDLSettingsInterface si(GetSettingsFileName().c_str()); SDLSettingsInterface si(GetSettingsFileName().c_str());
m_settings.Save(si); m_settings.Save(si);
} }
void SDLHostInterface::QueueSwitchGPURenderer() void SDLHostInterface::QueueUpdateSettings()
{ {
SDL_Event ev = {}; SDL_Event ev = {};
ev.type = SDL_USEREVENT; ev.type = SDL_USEREVENT;
ev.user.code = m_switch_gpu_renderer_event_id; ev.user.code = m_update_settings_event_id;
SDL_PushEvent(&ev); SDL_PushEvent(&ev);
} }
void SDLHostInterface::SwitchGPURenderer()
{
// Due to the GPU class owning textures, we have to shut the system down.
std::unique_ptr<ByteStream> stream;
if (m_system)
{
stream = ByteStream_CreateGrowableMemoryStream(nullptr, 8 * 1024);
if (!m_system->SaveState(stream.get()) || !stream->SeekAbsolute(0))
ReportError("Failed to save state before GPU renderer switch");
DestroySystem();
}
ImGui::EndFrame();
DestroyDisplay();
DestroySDLWindow();
if (!CreateSDLWindow())
Panic("Failed to recreate SDL window on GPU renderer switch");
if (!CreateDisplay())
Panic("Failed to recreate display on GPU renderer switch");
ImGui::NewFrame();
if (stream)
{
CreateSystem();
if (!BootSystem(nullptr, nullptr) || !m_system->LoadState(stream.get()))
{
ReportError("Failed to load state after GPU renderer switch, resetting");
m_system->Reset();
}
}
UpdateFullscreen();
if (m_system)
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
}
void SDLHostInterface::SwitchAudioBackend()
{
m_audio_stream.reset();
CreateAudioStream();
if (m_system)
{
m_audio_stream->PauseOutput(false);
UpdateSpeedLimiterState();
}
}
void SDLHostInterface::UpdateFullscreen() void SDLHostInterface::UpdateFullscreen()
{ {
SDL_SetWindowFullscreen(m_window, m_settings.display_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); SDL_SetWindowFullscreen(m_window, m_settings.display_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
@ -227,15 +223,7 @@ void SDLHostInterface::UpdateFullscreen()
m_settings.display_fullscreen ? 0 : static_cast<int>(20.0f * ImGui::GetIO().DisplayFramebufferScale.x)); m_settings.display_fullscreen ? 0 : static_cast<int>(20.0f * ImGui::GetIO().DisplayFramebufferScale.x));
} }
void SDLHostInterface::UpdateControllerMapping() std::unique_ptr<SDLHostInterface> SDLHostInterface::Create()
{
UpdateKeyboardControllerMapping();
UpdateControllerControllerMapping();
}
std::unique_ptr<SDLHostInterface> SDLHostInterface::Create(const char* filename /* = nullptr */,
const char* exp1_filename /* = nullptr */,
const char* save_state_filename /* = nullptr */)
{ {
std::unique_ptr<SDLHostInterface> intf = std::make_unique<SDLHostInterface>(); std::unique_ptr<SDLHostInterface> intf = std::make_unique<SDLHostInterface>();
@ -256,34 +244,13 @@ std::unique_ptr<SDLHostInterface> SDLHostInterface::Create(const char* filename
return nullptr; return nullptr;
} }
intf->CreateAudioStream();
ImGui::NewFrame(); ImGui::NewFrame();
intf->UpdateSpeedLimiterState();
const bool boot = (filename != nullptr || exp1_filename != nullptr || save_state_filename != nullptr);
if (boot)
{
if (!intf->CreateSystem() || !intf->BootSystem(filename, exp1_filename))
return nullptr;
if (save_state_filename)
intf->LoadState(save_state_filename);
intf->UpdateControllerMapping();
}
intf->UpdateFullscreen(); intf->UpdateFullscreen();
return intf; return intf;
} }
std::string SDLHostInterface::GetSaveStateFilename(u32 index)
{
return StringUtil::StdStringFromFormat("savestate_%u.bin", index);
}
void SDLHostInterface::ReportError(const char* message) void SDLHostInterface::ReportError(const char* message)
{ {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "DuckStation Error", message, m_window); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "DuckStation Error", message, m_window);
@ -291,7 +258,7 @@ void SDLHostInterface::ReportError(const char* message)
void SDLHostInterface::ReportMessage(const char* message) void SDLHostInterface::ReportMessage(const char* message)
{ {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "DuckStation Information", message, m_window); AddOSDMessage(message, 2.0f);
} }
void SDLHostInterface::HandleSDLEvent(const SDL_Event* event) void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
@ -352,8 +319,13 @@ void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
case SDL_USEREVENT: case SDL_USEREVENT:
{ {
if (static_cast<u32>(event->user.code) == m_switch_gpu_renderer_event_id) if (static_cast<u32>(event->user.code) == m_update_settings_event_id)
SwitchGPURenderer(); {
UpdateSettings([this]() {
SDLSettingsInterface si(GetSettingsFileName().c_str());
m_settings.Load(si);
});
}
} }
break; break;
} }
@ -381,9 +353,9 @@ void SDLHostInterface::HandleSDLKeyEvent(const SDL_Event* event)
{ {
const u32 index = event->key.keysym.scancode - SDL_SCANCODE_F1 + 1; const u32 index = event->key.keysym.scancode - SDL_SCANCODE_F1 + 1;
if (event->key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) if (event->key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT))
DoSaveState(index); SaveState(true, index);
else else
DoLoadState(index); LoadState(true, index);
} }
} }
break; break;
@ -408,7 +380,7 @@ void SDLHostInterface::HandleSDLKeyEvent(const SDL_Event* event)
case SDL_SCANCODE_PAUSE: case SDL_SCANCODE_PAUSE:
{ {
if (pressed) if (pressed)
DoTogglePause(); PauseSystem(!m_paused);
} }
break; break;
@ -792,51 +764,78 @@ void SDLHostInterface::DrawMainMenuBar()
if (ImGui::BeginMenu("System")) if (ImGui::BeginMenu("System"))
{ {
if (ImGui::MenuItem("Start Disc", nullptr, false, !system_enabled)) if (ImGui::MenuItem("Start Disc", nullptr, false, !system_enabled))
{
DoStartDisc(); DoStartDisc();
ClearImGuiFocus();
}
if (ImGui::MenuItem("Start BIOS", nullptr, false, !system_enabled)) if (ImGui::MenuItem("Start BIOS", nullptr, false, !system_enabled))
DoStartBIOS(); {
BootSystemFromBIOS();
ClearImGuiFocus();
}
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Power Off", nullptr, false, system_enabled)) if (ImGui::MenuItem("Power Off", nullptr, false, system_enabled))
DoPowerOff(); {
DestroySystem();
ClearImGuiFocus();
}
if (ImGui::MenuItem("Reset", nullptr, false, system_enabled)) if (ImGui::MenuItem("Reset", nullptr, false, system_enabled))
{
ResetSystem(); ResetSystem();
ClearImGuiFocus();
}
if (ImGui::MenuItem("Pause", nullptr, m_paused, system_enabled)) if (ImGui::MenuItem("Pause", nullptr, m_paused, system_enabled))
DoTogglePause(); {
PauseSystem(!m_paused);
ClearImGuiFocus();
}
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Change Disc", nullptr, false, system_enabled)) if (ImGui::MenuItem("Change Disc", nullptr, false, system_enabled))
{
DoChangeDisc(); DoChangeDisc();
ClearImGuiFocus();
}
if (ImGui::MenuItem("Frame Step", nullptr, false, system_enabled)) if (ImGui::MenuItem("Frame Step", nullptr, false, system_enabled))
{
DoFrameStep(); DoFrameStep();
ClearImGuiFocus();
}
ImGui::Separator(); ImGui::Separator();
if (ImGui::BeginMenu("Load State")) if (ImGui::BeginMenu("Load State"))
{ {
for (u32 i = 1; i <= NUM_QUICK_SAVE_STATES; i++) for (u32 i = 1; i <= GLOBAL_SAVE_STATE_SLOTS; i++)
{ {
char buf[16]; char buf[16];
std::snprintf(buf, sizeof(buf), "State %u", i); std::snprintf(buf, sizeof(buf), "State %u", i);
if (ImGui::MenuItem(buf)) if (ImGui::MenuItem(buf))
DoLoadState(i); {
LoadState(true, i);
ClearImGuiFocus();
}
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Save State", system_enabled)) if (ImGui::BeginMenu("Save State", system_enabled))
{ {
for (u32 i = 1; i <= NUM_QUICK_SAVE_STATES; i++) for (u32 i = 1; i <= GLOBAL_SAVE_STATE_SLOTS; i++)
{ {
char buf[16]; char buf[16];
std::snprintf(buf, sizeof(buf), "State %u", i); std::snprintf(buf, sizeof(buf), "State %u", i);
if (ImGui::MenuItem(buf)) if (ImGui::MenuItem(buf))
DoSaveState(i); {
SaveState(true, i);
ClearImGuiFocus();
}
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
@ -916,12 +915,7 @@ void SDLHostInterface::DrawMainMenuBar()
void SDLHostInterface::DrawQuickSettingsMenu() void SDLHostInterface::DrawQuickSettingsMenu()
{ {
bool settings_changed = false; bool settings_changed = false;
bool gpu_settings_changed = false; settings_changed |= ImGui::MenuItem("Enable Speed Limiter", nullptr, &m_settings.speed_limiter_enabled);
if (ImGui::MenuItem("Enable Speed Limiter", nullptr, &m_settings.speed_limiter_enabled))
{
settings_changed = true;
UpdateSpeedLimiterState();
}
ImGui::Separator(); ImGui::Separator();
@ -935,8 +929,6 @@ void SDLHostInterface::DrawQuickSettingsMenu()
{ {
m_settings.cpu_execution_mode = static_cast<CPUExecutionMode>(i); m_settings.cpu_execution_mode = static_cast<CPUExecutionMode>(i);
settings_changed = true; settings_changed = true;
if (m_system)
m_system->SetCPUExecutionMode(m_settings.cpu_execution_mode);
} }
} }
@ -955,7 +947,6 @@ void SDLHostInterface::DrawQuickSettingsMenu()
{ {
m_settings.gpu_renderer = static_cast<GPURenderer>(i); m_settings.gpu_renderer = static_cast<GPURenderer>(i);
settings_changed = true; settings_changed = true;
QueueSwitchGPURenderer();
} }
} }
@ -968,11 +959,7 @@ void SDLHostInterface::DrawQuickSettingsMenu()
UpdateFullscreen(); UpdateFullscreen();
} }
if (ImGui::MenuItem("VSync", nullptr, &m_settings.video_sync_enabled)) settings_changed |= ImGui::MenuItem("VSync", nullptr, &m_settings.video_sync_enabled);
{
settings_changed = true;
UpdateSpeedLimiterState();
}
ImGui::Separator(); ImGui::Separator();
@ -987,26 +974,22 @@ void SDLHostInterface::DrawQuickSettingsMenu()
if (ImGui::MenuItem(buf, nullptr, current_internal_resolution == scale)) if (ImGui::MenuItem(buf, nullptr, current_internal_resolution == scale))
{ {
m_settings.gpu_resolution_scale = scale; m_settings.gpu_resolution_scale = scale;
gpu_settings_changed = true; settings_changed = true;
} }
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
gpu_settings_changed |= ImGui::MenuItem("True (24-Bit) Color", nullptr, &m_settings.gpu_true_color); settings_changed |= ImGui::MenuItem("True (24-Bit) Color", nullptr, &m_settings.gpu_true_color);
gpu_settings_changed |= ImGui::MenuItem("Texture Filtering", nullptr, &m_settings.gpu_texture_filtering); settings_changed |= ImGui::MenuItem("Texture Filtering", nullptr, &m_settings.gpu_texture_filtering);
if (ImGui::MenuItem("Display Linear Filtering", nullptr, &m_settings.display_linear_filtering)) settings_changed |= ImGui::MenuItem("Display Linear Filtering", nullptr, &m_settings.display_linear_filtering);
if (settings_changed)
{ {
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
settings_changed = true;
}
if (settings_changed || gpu_settings_changed)
SaveSettings(); SaveSettings();
QueueUpdateSettings();
if (gpu_settings_changed && m_system) }
m_system->GetGPU()->UpdateSettings();
} }
void SDLHostInterface::DrawDebugMenu() void SDLHostInterface::DrawDebugMenu()
@ -1068,18 +1051,23 @@ void SDLHostInterface::DrawPoweredOffWindow()
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF575757); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF575757);
ImGui::SetCursorPosX(button_left); ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Resume", button_size)) ImGui::Button("Resume", button_size);
DoResume();
ImGui::NewLine(); ImGui::NewLine();
ImGui::SetCursorPosX(button_left); ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Start Disc", button_size)) if (ImGui::Button("Start Disc", button_size))
{
DoStartDisc(); DoStartDisc();
ClearImGuiFocus();
}
ImGui::NewLine(); ImGui::NewLine();
ImGui::SetCursorPosX(button_left); ImGui::SetCursorPosX(button_left);
if (ImGui::Button("Start BIOS", button_size)) if (ImGui::Button("Start BIOS", button_size))
DoStartBIOS(); {
BootSystemFromFile(nullptr);
ClearImGuiFocus();
}
ImGui::NewLine(); ImGui::NewLine();
ImGui::SetCursorPosX(button_left); ImGui::SetCursorPosX(button_left);
@ -1087,12 +1075,15 @@ void SDLHostInterface::DrawPoweredOffWindow()
ImGui::OpenPopup("PowerOffWindow_LoadStateMenu"); ImGui::OpenPopup("PowerOffWindow_LoadStateMenu");
if (ImGui::BeginPopup("PowerOffWindow_LoadStateMenu")) if (ImGui::BeginPopup("PowerOffWindow_LoadStateMenu"))
{ {
for (u32 i = 1; i <= NUM_QUICK_SAVE_STATES; i++) for (u32 i = 1; i <= GLOBAL_SAVE_STATE_SLOTS; i++)
{ {
char buf[16]; char buf[16];
std::snprintf(buf, sizeof(buf), "State %u", i); std::snprintf(buf, sizeof(buf), "State %u", i);
if (ImGui::MenuItem(buf)) if (ImGui::MenuItem(buf))
DoLoadState(i); {
LoadState(true, i);
ClearImGuiFocus();
}
} }
ImGui::EndPopup(); ImGui::EndPopup();
} }
@ -1133,7 +1124,6 @@ void SDLHostInterface::DrawSettingsWindow()
} }
bool settings_changed = false; bool settings_changed = false;
bool gpu_settings_changed = false;
if (ImGui::BeginTabBar("SettingsTabBar", 0)) if (ImGui::BeginTabBar("SettingsTabBar", 0))
{ {
@ -1173,19 +1163,8 @@ void SDLHostInterface::DrawSettingsWindow()
ImGui::Text("Emulation Speed:"); ImGui::Text("Emulation Speed:");
ImGui::SameLine(indent); ImGui::SameLine(indent);
if (ImGui::SliderFloat("##speed", &m_settings.emulation_speed, 0.25f, 5.0f)) settings_changed |= ImGui::SliderFloat("##speed", &m_settings.emulation_speed, 0.25f, 5.0f);
{ settings_changed |= ImGui::Checkbox("Enable Speed Limiter", &m_settings.speed_limiter_enabled);
settings_changed = true;
if (m_system)
m_system->UpdateThrottlePeriod();
}
if (ImGui::Checkbox("Enable Speed Limiter", &m_settings.speed_limiter_enabled))
{
settings_changed = true;
UpdateSpeedLimiterState();
}
settings_changed |= ImGui::Checkbox("Pause On Start", &m_settings.start_paused); settings_changed |= ImGui::Checkbox("Pause On Start", &m_settings.start_paused);
} }
@ -1206,14 +1185,9 @@ void SDLHostInterface::DrawSettingsWindow()
{ {
m_settings.audio_backend = static_cast<AudioBackend>(backend); m_settings.audio_backend = static_cast<AudioBackend>(backend);
settings_changed = true; settings_changed = true;
SwitchAudioBackend();
} }
if (ImGui::Checkbox("Output Sync", &m_settings.audio_sync_enabled)) settings_changed |= ImGui::Checkbox("Output Sync", &m_settings.audio_sync_enabled);
{
settings_changed = true;
UpdateSpeedLimiterState();
}
} }
ImGui::EndTabItem(); ImGui::EndTabItem();
@ -1242,11 +1216,6 @@ void SDLHostInterface::DrawSettingsWindow()
{ {
m_settings.controller_types[i] = static_cast<ControllerType>(controller_type); m_settings.controller_types[i] = static_cast<ControllerType>(controller_type);
settings_changed = true; settings_changed = true;
if (m_system)
{
m_system->UpdateControllers();
UpdateControllerControllerMapping();
}
} }
} }
@ -1255,19 +1224,12 @@ void SDLHostInterface::DrawSettingsWindow()
std::string* path_ptr = &m_settings.memory_card_paths[i]; std::string* path_ptr = &m_settings.memory_card_paths[i];
std::snprintf(buf, sizeof(buf), "##memcard_%c_path", 'a' + i); std::snprintf(buf, sizeof(buf), "##memcard_%c_path", 'a' + i);
if (DrawFileChooser(buf, path_ptr)) settings_changed |= DrawFileChooser(buf, path_ptr);
{
settings_changed = true;
if (m_system)
m_system->UpdateMemoryCards();
}
if (ImGui::Button("Eject Memory Card")) if (ImGui::Button("Eject Memory Card"))
{ {
path_ptr->clear(); path_ptr->clear();
settings_changed = true; settings_changed = true;
if (m_system)
m_system->UpdateMemoryCards();
} }
ImGui::NewLine(); ImGui::NewLine();
@ -1292,8 +1254,6 @@ void SDLHostInterface::DrawSettingsWindow()
{ {
m_settings.cpu_execution_mode = static_cast<CPUExecutionMode>(execution_mode); m_settings.cpu_execution_mode = static_cast<CPUExecutionMode>(execution_mode);
settings_changed = true; settings_changed = true;
if (m_system)
m_system->SetCPUExecutionMode(m_settings.cpu_execution_mode);
} }
ImGui::EndTabItem(); ImGui::EndTabItem();
@ -1317,7 +1277,6 @@ void SDLHostInterface::DrawSettingsWindow()
{ {
m_settings.gpu_renderer = static_cast<GPURenderer>(gpu_renderer); m_settings.gpu_renderer = static_cast<GPURenderer>(gpu_renderer);
settings_changed = true; settings_changed = true;
QueueSwitchGPURenderer();
} }
} }
@ -1331,17 +1290,8 @@ void SDLHostInterface::DrawSettingsWindow()
settings_changed = true; settings_changed = true;
} }
if (ImGui::Checkbox("Linear Filtering", &m_settings.display_linear_filtering)) settings_changed |= ImGui::Checkbox("Linear Filtering", &m_settings.display_linear_filtering);
{ settings_changed |= ImGui::Checkbox("VSync", &m_settings.video_sync_enabled);
m_display->SetDisplayLinearFiltering(m_settings.display_linear_filtering);
settings_changed = true;
}
if (ImGui::Checkbox("VSync", &m_settings.video_sync_enabled))
{
settings_changed = true;
UpdateSpeedLimiterState();
}
} }
ImGui::NewLine(); ImGui::NewLine();
@ -1375,12 +1325,12 @@ void SDLHostInterface::DrawSettingsWindow()
static_cast<int>(resolutions.size()))) static_cast<int>(resolutions.size())))
{ {
m_settings.gpu_resolution_scale = static_cast<u32>(current_resolution_index + 1); m_settings.gpu_resolution_scale = static_cast<u32>(current_resolution_index + 1);
gpu_settings_changed = true; settings_changed = true;
} }
gpu_settings_changed |= ImGui::Checkbox("True 24-bit Color (disables dithering)", &m_settings.gpu_true_color); settings_changed |= ImGui::Checkbox("True 24-bit Color (disables dithering)", &m_settings.gpu_true_color);
gpu_settings_changed |= ImGui::Checkbox("Texture Filtering", &m_settings.gpu_texture_filtering); settings_changed |= ImGui::Checkbox("Texture Filtering", &m_settings.gpu_texture_filtering);
gpu_settings_changed |= ImGui::Checkbox("Force Progressive Scan", &m_settings.gpu_force_progressive_scan); settings_changed |= ImGui::Checkbox("Force Progressive Scan", &m_settings.gpu_force_progressive_scan);
} }
ImGui::EndTabItem(); ImGui::EndTabItem();
@ -1397,11 +1347,11 @@ void SDLHostInterface::DrawSettingsWindow()
ImGui::End(); ImGui::End();
if (settings_changed || gpu_settings_changed) if (settings_changed)
{
SaveSettings(); SaveSettings();
QueueUpdateSettings();
if (gpu_settings_changed && m_system) }
m_system->GetGPU()->UpdateSettings();
} }
void SDLHostInterface::DrawAboutWindow() void SDLHostInterface::DrawAboutWindow()
@ -1454,26 +1404,9 @@ bool SDLHostInterface::DrawFileChooser(const char* label, std::string* path, con
return result; return result;
} }
void SDLHostInterface::DoPowerOff() void SDLHostInterface::ClearImGuiFocus()
{ {
Assert(m_system); ImGui::SetWindowFocus(nullptr);
DestroySystem();
AddOSDMessage("System powered off.");
}
void SDLHostInterface::DoResume()
{
Assert(!m_system);
if (!CreateSystem() || !BootSystem(nullptr, RESUME_SAVESTATE_FILENAME))
{
DestroySystem();
return;
}
UpdateControllerMapping();
if (m_system)
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
} }
void SDLHostInterface::DoStartDisc() void SDLHostInterface::DoStartDisc()
@ -1485,33 +1418,7 @@ void SDLHostInterface::DoStartDisc()
return; return;
AddFormattedOSDMessage(2.0f, "Starting disc from '%s'...", path); AddFormattedOSDMessage(2.0f, "Starting disc from '%s'...", path);
if (!CreateSystem() || !BootSystem(path, nullptr)) BootSystemFromFile(path);
{
DestroySystem();
return;
}
UpdateControllerMapping();
if (m_system)
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
}
void SDLHostInterface::DoStartBIOS()
{
Assert(!m_system);
AddOSDMessage("Starting BIOS...");
if (!CreateSystem() || !BootSystem(nullptr, nullptr))
{
DestroySystem();
return;
}
UpdateControllerMapping();
if (m_system)
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
} }
void SDLHostInterface::DoChangeDisc() void SDLHostInterface::DoChangeDisc()
@ -1527,47 +1434,7 @@ void SDLHostInterface::DoChangeDisc()
else else
AddOSDMessage("Failed to switch CD. The log may contain further information."); AddOSDMessage("Failed to switch CD. The log may contain further information.");
if (m_system) m_system->ResetPerformanceCounters();
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
}
void SDLHostInterface::DoLoadState(u32 index)
{
if (HasSystem())
{
LoadState(GetSaveStateFilename(index).c_str());
}
else
{
if (!CreateSystem() || !BootSystem(nullptr, GetSaveStateFilename(index).c_str()))
{
DestroySystem();
return;
}
}
UpdateControllerMapping();
if (m_system)
m_system->ResetPerformanceCounters();
ClearImGuiFocus();
}
void SDLHostInterface::DoSaveState(u32 index)
{
Assert(m_system);
SaveState(GetSaveStateFilename(index).c_str());
ClearImGuiFocus();
}
void SDLHostInterface::DoTogglePause()
{
if (!m_system)
return;
m_paused = !m_paused;
if (!m_paused)
m_system->ResetPerformanceCounters();
} }
void SDLHostInterface::DoFrameStep() void SDLHostInterface::DoFrameStep()
@ -1587,8 +1454,6 @@ void SDLHostInterface::DoToggleFullscreen()
void SDLHostInterface::Run() void SDLHostInterface::Run()
{ {
m_audio_stream->PauseOutput(false);
while (!m_quit_request) while (!m_quit_request)
{ {
for (;;) for (;;)
@ -1636,10 +1501,5 @@ void SDLHostInterface::Run()
// Save state on exit so it can be resumed // Save state on exit so it can be resumed
if (m_system) if (m_system)
{
if (!SaveState(RESUME_SAVESTATE_FILENAME))
ReportError("Saving state failed, you will not be able to resume this session.");
DestroySystem(); DestroySystem();
}
} }

View File

@ -22,16 +22,22 @@ public:
SDLHostInterface(); SDLHostInterface();
~SDLHostInterface(); ~SDLHostInterface();
static std::unique_ptr<SDLHostInterface> Create(const char* filename = nullptr, const char* exp1_filename = nullptr, static std::unique_ptr<SDLHostInterface> Create();
const char* save_state_filename = nullptr);
static std::string GetSaveStateFilename(u32 index);
void ReportError(const char* message) override; void ReportError(const char* message) override;
void ReportMessage(const char* message) override; void ReportMessage(const char* message) override;
void Run(); void Run();
protected:
bool AcquireHostDisplay() override;
void ReleaseHostDisplay() override;
std::unique_ptr<AudioStream> CreateAudioStream(AudioBackend backend) override;
void OnSystemCreated() override;
void OnSystemPaused(bool paused) override;
void OnSystemDestroyed();
private: private:
enum class KeyboardControllerAction enum class KeyboardControllerAction
{ {
@ -62,9 +68,6 @@ private:
float last_rumble_strength; float last_rumble_strength;
}; };
static constexpr u32 NUM_QUICK_SAVE_STATES = 10;
static constexpr char RESUME_SAVESTATE_FILENAME[] = "savestate_resume.bin";
bool HasSystem() const { return static_cast<bool>(m_system); } bool HasSystem() const { return static_cast<bool>(m_system); }
#ifdef WIN32 #ifdef WIN32
@ -78,26 +81,16 @@ private:
bool CreateDisplay(); bool CreateDisplay();
void DestroyDisplay(); void DestroyDisplay();
void CreateImGuiContext(); void CreateImGuiContext();
void CreateAudioStream();
void SaveSettings(); void SaveSettings();
void QueueUpdateSettings();
void QueueSwitchGPURenderer();
void SwitchGPURenderer();
void SwitchAudioBackend();
void UpdateFullscreen(); void UpdateFullscreen();
void UpdateControllerMapping();
// We only pass mouse input through if it's grabbed // We only pass mouse input through if it's grabbed
void DrawImGui(); void DrawImGui();
void DoPowerOff();
void DoResume();
void DoStartDisc(); void DoStartDisc();
void DoStartBIOS();
void DoChangeDisc(); void DoChangeDisc();
void DoLoadState(u32 index);
void DoSaveState(u32 index);
void DoTogglePause();
void DoFrameStep(); void DoFrameStep();
void DoToggleFullscreen(); void DoToggleFullscreen();
@ -122,6 +115,7 @@ private:
void DrawSettingsWindow(); void DrawSettingsWindow();
void DrawAboutWindow(); void DrawAboutWindow();
bool DrawFileChooser(const char* label, std::string* path, const char* filter = nullptr); bool DrawFileChooser(const char* label, std::string* path, const char* filter = nullptr);
void ClearImGuiFocus();
SDL_Window* m_window = nullptr; SDL_Window* m_window = nullptr;
std::unique_ptr<HostDisplayTexture> m_app_icon_texture; std::unique_ptr<HostDisplayTexture> m_app_icon_texture;
@ -132,7 +126,7 @@ private:
std::array<s32, SDL_CONTROLLER_AXIS_MAX> m_controller_axis_mapping{}; std::array<s32, SDL_CONTROLLER_AXIS_MAX> m_controller_axis_mapping{};
std::array<s32, SDL_CONTROLLER_BUTTON_MAX> m_controller_button_mapping{}; std::array<s32, SDL_CONTROLLER_BUTTON_MAX> m_controller_button_mapping{};
u32 m_switch_gpu_renderer_event_id = 0; u32 m_update_settings_event_id = 0;
bool m_quit_request = false; bool m_quit_request = false;
bool m_frame_step_request = false; bool m_frame_step_request = false;