From 3eac1fc28494709dc7cbfaae81cf0b1f42f12e17 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 4 Mar 2023 12:24:31 +0100 Subject: DolphinQt: Rework TAS input threading, part 1 (buttons) This gets rid of a blocking operation, improving performance and fixing https://bugs.dolphin-emu.org/issues/12893. This also makes us no longer directly access the state of certain UI elements from the CPU thread, which probably wasn't thread-safe but doesn't seem to have caused any observable issues so far. --- Source/Core/DolphinQt/TAS/TASControlState.cpp | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Source/Core/DolphinQt/TAS/TASControlState.cpp (limited to 'Source/Core/DolphinQt/TAS/TASControlState.cpp') diff --git a/Source/Core/DolphinQt/TAS/TASControlState.cpp b/Source/Core/DolphinQt/TAS/TASControlState.cpp new file mode 100644 index 0000000000..fd0c209aa6 --- /dev/null +++ b/Source/Core/DolphinQt/TAS/TASControlState.cpp @@ -0,0 +1,58 @@ +// Copyright 2023 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "DolphinQt/TAS/TASControlState.h" + +#include + +#include "Common/CommonTypes.h" + +u16 TASControlState::GetValue() const +{ + const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed); + const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed); + + return (ui_thread_state.version != cpu_thread_state.version ? cpu_thread_state : ui_thread_state) + .value; +} + +bool TASControlState::OnControllerValueChanged(u16 new_value) +{ + const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed); + + if (cpu_thread_state.value == new_value) + { + // The CPU thread state is already up to date with the controller. No need to do anything + return false; + } + + const State new_state{static_cast(cpu_thread_state.version + 1), new_value}; + m_cpu_thread_state.store(new_state, std::memory_order_relaxed); + + return true; +} + +void TASControlState::OnUIValueChanged(u16 new_value) +{ + const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed); + + const State new_state{ui_thread_state.version, new_value}; + m_ui_thread_state.store(new_state, std::memory_order_relaxed); +} + +u16 TASControlState::ApplyControllerValueChange() +{ + const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed); + const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed); + + if (ui_thread_state.version == cpu_thread_state.version) + { + // The UI thread state is already up to date with the CPU thread. No need to do anything + return ui_thread_state.value; + } + else + { + m_ui_thread_state.store(cpu_thread_state, std::memory_order_relaxed); + return cpu_thread_state.value; + } +} -- cgit v1.2.3