From bf5f9fdcb03868d25389526a0f803c9074c0b97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 21 Aug 2024 15:31:06 +0200 Subject: [PATCH] Expose threaded rendering as a setting --- src/gui/game/GameController.cpp | 4 ++-- src/gui/game/GameController.h | 2 +- src/gui/game/GameModel.cpp | 6 ++++++ src/gui/game/GameModel.h | 6 ++++++ src/gui/game/GameView.cpp | 6 +++--- src/gui/options/OptionsController.cpp | 5 +++++ src/gui/options/OptionsController.h | 1 + src/gui/options/OptionsModel.cpp | 12 ++++++++++++ src/gui/options/OptionsModel.h | 2 ++ src/gui/options/OptionsView.cpp | 4 ++++ src/gui/options/OptionsView.h | 1 + src/lua/LuaRenderer.cpp | 13 +++++++++++++ 12 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 2b4e91349..a42cb808a 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -1728,7 +1728,7 @@ void GameController::AfterSimDraw() commandInterface->HandleEvent(AfterSimDrawEvent{}); } -bool GameController::HaveSimGraphicsEventHandlers() +bool GameController::ThreadedRenderingAllowed() { - return commandInterface->HaveSimGraphicsEventHandlers(); + return gameModel->GetThreadedRendering() && !commandInterface->HaveSimGraphicsEventHandlers(); } diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index 94b33652b..fc19e7ccd 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -205,5 +205,5 @@ public: void BeforeSimDraw(); void AfterSimDraw(); - bool HaveSimGraphicsEventHandlers(); + bool ThreadedRenderingAllowed(); }; diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index 4ef2ab21a..8225c4500 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -100,6 +100,7 @@ GameModel::GameModel(): rendererSettings.gravityFieldEnabled = prefs.Get("Renderer.GravityField", false); rendererSettings.decorations_enable = prefs.Get("Renderer.Decorations", true); + threadedRendering = prefs.Get("Renderer.SeparateThread", false); //Load config into simulation edgeMode = prefs.Get("Simulation.EdgeMode", NUM_EDGEMODES, EDGE_VOID); @@ -537,6 +538,11 @@ void GameModel::SetTemperatureScale(int temperatureScale) this->temperatureScale = temperatureScale; } +void GameModel::SetThreadedRendering(bool newThreadedRendering) +{ + threadedRendering = newThreadedRendering; +} + void GameModel::SetAmbientAirTemperature(float ambientAirTemp) { this->ambientAirTemp = ambientAirTemp; diff --git a/src/gui/game/GameModel.h b/src/gui/game/GameModel.h index 444db9386..684c7ff6b 100644 --- a/src/gui/game/GameModel.h +++ b/src/gui/game/GameModel.h @@ -130,6 +130,7 @@ private: void SaveToSimParameters(const GameSave &saveData); std::optional queuedVote; + bool threadedRendering = false; public: GameModel(); @@ -144,6 +145,11 @@ public: { return temperatureScale; } + void SetThreadedRendering(bool newThreadedRendering); + bool GetThreadedRendering() const + { + return threadedRendering; + } void SetAmbientAirTemperature(float ambientAirTemp); float GetAmbientAirTemperature(); void SetDecoSpace(int decoSpace); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 39be2546b..ef161f6b6 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -2153,8 +2153,8 @@ void GameView::OnDraw() { Graphics * g = GetGraphics(); - auto wantRendererThread = !c->HaveSimGraphicsEventHandlers(); - if (wantRendererThread) + auto threadedRenderingAllowed = c->ThreadedRenderingAllowed(); + if (threadedRenderingAllowed) { StartRendererThread(); WaitForRendererThread(); @@ -2526,7 +2526,7 @@ void GameView::OnDraw() fpsInfo << " [FIND]"; if (showDebug) { - if (wantRendererThread) + if (threadedRenderingAllowed) { fpsInfo << " [SRT]"; } diff --git a/src/gui/options/OptionsController.cpp b/src/gui/options/OptionsController.cpp index fc92a5bfa..56dfb8200 100644 --- a/src/gui/options/OptionsController.cpp +++ b/src/gui/options/OptionsController.cpp @@ -72,6 +72,11 @@ void OptionsController::SetTemperatureScale(int temperatureScale) model->SetTemperatureScale(temperatureScale); } +void OptionsController::SetThreadedRendering(bool newThreadedRendering) +{ + model->SetThreadedRendering(newThreadedRendering); +} + void OptionsController::SetFullscreen(bool fullscreen) { model->SetFullscreen(fullscreen); diff --git a/src/gui/options/OptionsController.h b/src/gui/options/OptionsController.h index fa9467eaa..65a6de9a4 100644 --- a/src/gui/options/OptionsController.h +++ b/src/gui/options/OptionsController.h @@ -24,6 +24,7 @@ public: void SetAmbientAirTemperature(float ambientAirTemp); void SetEdgeMode(int edgeMode); void SetTemperatureScale(int temperatureScale); + void SetThreadedRendering(bool newThreadedRendering); void SetFullscreen(bool fullscreen); void SetChangeResolution(bool newChangeResolution); void SetForceIntegerScaling(bool forceIntegerScaling); diff --git a/src/gui/options/OptionsModel.cpp b/src/gui/options/OptionsModel.cpp index b99963621..cfcffc27f 100644 --- a/src/gui/options/OptionsModel.cpp +++ b/src/gui/options/OptionsModel.cpp @@ -95,6 +95,18 @@ void OptionsModel::SetTemperatureScale(int temperatureScale) notifySettingsChanged(); } +int OptionsModel::GetThreadedRendering() +{ + return gModel->GetThreadedRendering(); +} + +void OptionsModel::SetThreadedRendering(bool newThreadedRendering) +{ + GlobalPrefs::Ref().Set("Renderer.SeparateThread", newThreadedRendering); + gModel->SetThreadedRendering(newThreadedRendering); + notifySettingsChanged(); +} + float OptionsModel::GetAmbientAirTemperature() { return gModel->GetSimulation()->air->ambientAirTemp; diff --git a/src/gui/options/OptionsModel.h b/src/gui/options/OptionsModel.h index bb155f75b..1e41a30d2 100644 --- a/src/gui/options/OptionsModel.h +++ b/src/gui/options/OptionsModel.h @@ -31,6 +31,8 @@ public: void SetEdgeMode(int edgeMode); int GetTemperatureScale(); void SetTemperatureScale(int temperatureScale); + int GetThreadedRendering(); + void SetThreadedRendering(bool newThreadedRendering); int GetGravityMode(); void SetGravityMode(int gravityMode); float GetCustomGravityX(); diff --git a/src/gui/options/OptionsView.cpp b/src/gui/options/OptionsView.cpp index 6dfe86de8..b622ed0be 100644 --- a/src/gui/options/OptionsView.cpp +++ b/src/gui/options/OptionsView.cpp @@ -318,6 +318,9 @@ OptionsView::OptionsView() : ui::Window(ui::Point(-1, -1), ui::Point(320, 340)) } currentY += 4; // and then undo the undo } + threadedRendering = addCheckbox(0, "Separate rendering thread", "May increase framerate when fancy effects are in use", [this] { + c->SetThreadedRendering(threadedRendering->GetChecked()); + }); decoSpace = addDropDown("Colour space used by decoration tools", { { "sRGB", DECOSPACE_SRGB }, { "Linear", DECOSPACE_LINEAR }, @@ -490,6 +493,7 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender) includePressure->SetChecked(sender->GetIncludePressure()); perfectCircle->SetChecked(sender->GetPerfectCircle()); graveExitsConsole->SetChecked(sender->GetGraveExitsConsole()); + threadedRendering->SetChecked(sender->GetThreadedRendering()); momentumScroll->SetChecked(sender->GetMomentumScroll()); } diff --git a/src/gui/options/OptionsView.h b/src/gui/options/OptionsView.h index b9f30dbcb..1daf2d5fb 100644 --- a/src/gui/options/OptionsView.h +++ b/src/gui/options/OptionsView.h @@ -41,6 +41,7 @@ class OptionsView: public ui::Window ui::Checkbox *perfectCircle{}; ui::Checkbox *graveExitsConsole{}; ui::Checkbox *nativeClipoard{}; + ui::Checkbox *threadedRendering{}; ui::ScrollPanel *scrollPanel{}; float customGravityX, customGravityY; void UpdateAmbientAirTempPreview(float airTemp, bool isValid); diff --git a/src/lua/LuaRenderer.cpp b/src/lua/LuaRenderer.cpp index 791abac57..5a76fc704 100644 --- a/src/lua/LuaRenderer.cpp +++ b/src/lua/LuaRenderer.cpp @@ -216,6 +216,18 @@ static int zoomScope(lua_State *L) return 0; } +static int separateThread(lua_State *L) +{ + auto *lsi = GetLSI(); + if (lua_gettop(L)) + { + lsi->gameModel->SetThreadedRendering(lua_toboolean(L, 1)); + return 0; + } + lua_pushboolean(L, lsi->gameModel->GetThreadedRendering()); + return 1; +} + void LuaRenderer::Open(lua_State *L) { static const luaL_Reg reg[] = { @@ -234,6 +246,7 @@ void LuaRenderer::Open(lua_State *L) LFUNC(zoomScope), LFUNC(fireSize), LFUNC(useDisplayPreset), + LFUNC(separateThread), #undef LFUNC { NULL, NULL } };