From 7135bdc3bdb5b6c26ead27c2438df748c79abb9d Mon Sep 17 00:00:00 2001 From: Merry Date: Fri, 15 Sep 2023 17:34:47 +0100 Subject: [PATCH 1/6] vcpkg: Add boost.heap --- vcpkg.json | 1 + 1 file changed, 1 insertion(+) diff --git a/vcpkg.json b/vcpkg.json index 7d9e631a15..203fc9708b 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -12,6 +12,7 @@ "boost-context", "boost-crc", "boost-functional", + "boost-heap", "boost-icl", "boost-intrusive", "boost-mpl", From 3ad7eec9de119afa6f5ca2db073070a8cdf62f69 Mon Sep 17 00:00:00 2001 From: Merry Date: Wed, 13 Sep 2023 22:49:41 +0100 Subject: [PATCH 2/6] core_timing: Use a fibonacci heap --- src/core/core_timing.cpp | 54 +++++++++++++++++++++------------------- src/core/core_timing.h | 12 ++++----- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index b98a0cb335..65f0df1155 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -32,6 +32,7 @@ struct CoreTiming::Event { std::uintptr_t user_data; std::weak_ptr type; s64 reschedule_time; + heap_t::handle_type handle{}; // Sort by time, unless the times are the same, in which case sort by // the order added to the queue @@ -122,9 +123,9 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, std::scoped_lock scope{basic_lock}; const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future}; - event_queue.emplace_back( - Event{next_time.count(), event_fifo_id++, user_data, event_type, 0}); - std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + auto h{event_queue.emplace( + Event{next_time.count(), event_fifo_id++, user_data, event_type, 0})}; + (*h).handle = h; } event.Set(); @@ -138,10 +139,9 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, std::scoped_lock scope{basic_lock}; const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; - event_queue.emplace_back( - Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); - - std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, user_data, event_type, + resched_time.count()})}; + (*h).handle = h; } event.Set(); @@ -151,15 +151,17 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, std::uintptr_t user_data, bool wait) { { std::scoped_lock lk{basic_lock}; - const auto itr = - std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { - return e.type.lock().get() == event_type.get() && e.user_data == user_data; - }); - // Removing random items breaks the invariant so we have to re-establish it. - if (itr != event_queue.end()) { - event_queue.erase(itr, event_queue.end()); - std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + std::vector to_remove; + for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) { + const Event& e = *itr; + if (e.type.lock().get() == event_type.get() && e.user_data == user_data) { + to_remove.push_back(itr->handle); + } + } + + for (auto h : to_remove) { + event_queue.erase(h); } } @@ -200,10 +202,9 @@ std::optional CoreTiming::Advance() { std::scoped_lock lock{advance_lock, basic_lock}; global_timer = GetGlobalTimeNs().count(); - while (!event_queue.empty() && event_queue.front().time <= global_timer) { - Event evt = std::move(event_queue.front()); - std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); - event_queue.pop_back(); + while (!event_queue.empty() && event_queue.top().time <= global_timer) { + Event evt = event_queue.top(); + event_queue.pop(); if (const auto event_type{evt.type.lock()}) { basic_lock.unlock(); @@ -219,16 +220,16 @@ std::optional CoreTiming::Advance() { ? new_schedule_time.value().count() : evt.reschedule_time}; - // If this event was scheduled into a pause, its time now is going to be way behind. - // Re-set this event to continue from the end of the pause. + // If this event was scheduled into a pause, its time now is going to be way + // behind. Re-set this event to continue from the end of the pause. auto next_time{evt.time + next_schedule_time}; if (evt.time < pause_end_time) { next_time = pause_end_time + next_schedule_time; } - event_queue.emplace_back( - Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time}); - std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + auto h{event_queue.emplace(Event{next_time, event_fifo_id++, evt.user_data, + evt.type, next_schedule_time})}; + (*h).handle = h; } } @@ -236,7 +237,7 @@ std::optional CoreTiming::Advance() { } if (!event_queue.empty()) { - return event_queue.front().time; + return event_queue.top().time; } else { return std::nullopt; } @@ -274,7 +275,8 @@ void CoreTiming::ThreadLoop() { #endif } } else { - // Queue is empty, wait until another event is scheduled and signals us to continue. + // Queue is empty, wait until another event is scheduled and signals us to + // continue. wait_set = true; event.Wait(); } diff --git a/src/core/core_timing.h b/src/core/core_timing.h index c20e906fba..26a8b93a76 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h @@ -11,7 +11,8 @@ #include #include #include -#include + +#include #include "common/common_types.h" #include "common/thread.h" @@ -151,11 +152,10 @@ private: s64 timer_resolution_ns; #endif - // The queue is a min-heap using std::make_heap/push_heap/pop_heap. - // We don't use std::priority_queue because we need to be able to serialize, unserialize and - // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't - // accommodated by the standard adaptor class. - std::vector event_queue; + using heap_t = + boost::heap::fibonacci_heap>>; + + heap_t event_queue; u64 event_fifo_id = 0; std::shared_ptr ev_lost; From f70bafff1a97eb363a81666d187593584b9b7fe7 Mon Sep 17 00:00:00 2001 From: Merry Date: Wed, 13 Sep 2023 22:55:54 +0100 Subject: [PATCH 3/6] core_timing: Attempt to reduce heap sifting --- src/core/core_timing.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 65f0df1155..e671b270f1 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -203,19 +203,31 @@ std::optional CoreTiming::Advance() { global_timer = GetGlobalTimeNs().count(); while (!event_queue.empty() && event_queue.top().time <= global_timer) { - Event evt = event_queue.top(); - event_queue.pop(); + const Event& evt = event_queue.top(); if (const auto event_type{evt.type.lock()}) { - basic_lock.unlock(); + if (evt.reschedule_time == 0) { + const auto evt_user_data = evt.user_data; + const auto evt_time = evt.time; - const auto new_schedule_time{event_type->callback( - evt.user_data, evt.time, - std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; + event_queue.pop(); - basic_lock.lock(); + basic_lock.unlock(); + + event_type->callback( + evt_user_data, evt_time, + std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time}); + + basic_lock.lock(); + } else { + basic_lock.unlock(); + + const auto new_schedule_time{event_type->callback( + evt.user_data, evt.time, + std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; + + basic_lock.lock(); - if (evt.reschedule_time != 0) { const auto next_schedule_time{new_schedule_time.has_value() ? new_schedule_time.value().count() : evt.reschedule_time}; @@ -227,9 +239,8 @@ std::optional CoreTiming::Advance() { next_time = pause_end_time + next_schedule_time; } - auto h{event_queue.emplace(Event{next_time, event_fifo_id++, evt.user_data, - evt.type, next_schedule_time})}; - (*h).handle = h; + event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.user_data, + evt.type, next_schedule_time, evt.handle}); } } From 8992a62da4d48b440500d2017594667108633171 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Tue, 19 Sep 2023 22:06:49 +0100 Subject: [PATCH 4/6] Reduce core timing mutex contention --- src/core/hid/emulated_controller.cpp | 11 ++++++----- src/core/hid/emulated_controller.h | 2 ++ src/core/hle/kernel/k_hardware_timer.cpp | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index 94bd656fe9..2af3f06fc2 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -542,6 +542,7 @@ void EmulatedController::UnloadInput() { } void EmulatedController::EnableConfiguration() { + std::scoped_lock lock{connect_mutex, npad_mutex}; is_configuring = true; tmp_is_connected = is_connected; tmp_npad_type = npad_type; @@ -1556,7 +1557,7 @@ void EmulatedController::Connect(bool use_temporary_value) { auto trigger_guard = SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Connected, !is_configuring); }); - std::scoped_lock lock{mutex}; + std::scoped_lock lock{connect_mutex, mutex}; if (is_configuring) { tmp_is_connected = true; return; @@ -1572,7 +1573,7 @@ void EmulatedController::Connect(bool use_temporary_value) { void EmulatedController::Disconnect() { auto trigger_guard = SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Disconnected, !is_configuring); }); - std::scoped_lock lock{mutex}; + std::scoped_lock lock{connect_mutex, mutex}; if (is_configuring) { tmp_is_connected = false; return; @@ -1586,7 +1587,7 @@ void EmulatedController::Disconnect() { } bool EmulatedController::IsConnected(bool get_temporary_value) const { - std::scoped_lock lock{mutex}; + std::scoped_lock lock{connect_mutex}; if (get_temporary_value && is_configuring) { return tmp_is_connected; } @@ -1599,7 +1600,7 @@ NpadIdType EmulatedController::GetNpadIdType() const { } NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) const { - std::scoped_lock lock{mutex}; + std::scoped_lock lock{npad_mutex}; if (get_temporary_value && is_configuring) { return tmp_npad_type; } @@ -1609,7 +1610,7 @@ NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) c void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) { auto trigger_guard = SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Type, !is_configuring); }); - std::scoped_lock lock{mutex}; + std::scoped_lock lock{mutex, npad_mutex}; if (is_configuring) { if (tmp_npad_type == npad_type_) { diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index 88d77db8de..d4500583ef 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -603,6 +603,8 @@ private: mutable std::mutex mutex; mutable std::mutex callback_mutex; + mutable std::mutex npad_mutex; + mutable std::mutex connect_mutex; std::unordered_map callback_list; int last_callback_key = 0; diff --git a/src/core/hle/kernel/k_hardware_timer.cpp b/src/core/hle/kernel/k_hardware_timer.cpp index 4dcd53821e..8e2e40307d 100644 --- a/src/core/hle/kernel/k_hardware_timer.cpp +++ b/src/core/hle/kernel/k_hardware_timer.cpp @@ -35,7 +35,9 @@ void KHardwareTimer::DoTask() { } // Disable the timer interrupt while we handle this. - this->DisableInterrupt(); + // Not necessary due to core timing already having popped this event to call it. + // this->DisableInterrupt(); + m_wakeup_time = std::numeric_limits::max(); if (const s64 next_time = this->DoInterruptTaskImpl(GetTick()); 0 < next_time && next_time <= m_wakeup_time) { From 7507a7f89fb6f7782c1a62a23a43e1bc8fe65f48 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 27 Sep 2023 19:11:47 -0400 Subject: [PATCH 5/6] renderer_vulkan: fix query cache for homebrew --- src/video_core/renderer_vulkan/vk_query_cache.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/video_core/renderer_vulkan/vk_query_cache.cpp b/src/video_core/renderer_vulkan/vk_query_cache.cpp index a32da3ba33..17b2587ad9 100644 --- a/src/video_core/renderer_vulkan/vk_query_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_query_cache.cpp @@ -1160,7 +1160,7 @@ struct QueryCacheRuntimeImpl { cpu_memory_), primitives_needed_minus_suceeded_streamer( static_cast(QueryType::StreamingPrimitivesNeededMinusSucceeded), runtime, 0u), - hcr_setup{}, hcr_is_set{}, is_hcr_running{} { + hcr_setup{}, hcr_is_set{}, is_hcr_running{}, maxwell3d{} { hcr_setup.sType = VK_STRUCTURE_TYPE_CONDITIONAL_RENDERING_BEGIN_INFO_EXT; hcr_setup.pNext = nullptr; @@ -1235,7 +1235,9 @@ void QueryCacheRuntime::Bind3DEngine(Maxwell3D* maxwell3d) { template void QueryCacheRuntime::View3DRegs(Func&& func) { - func(*impl->maxwell3d); + if (impl->maxwell3d) { + func(*impl->maxwell3d); + } } void QueryCacheRuntime::EndHostConditionalRendering() { From c62e089260d66900e8aaa4f38c6a794730a3e9f2 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Thu, 28 Sep 2023 23:47:10 +0100 Subject: [PATCH 6/6] Don't send a double focus change message --- src/core/hle/service/am/am.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index e9bd04842d..a83622f7cd 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -2094,9 +2094,6 @@ void IApplicationFunctions::PrepareForJit(HLERequestContext& ctx) { void LoopProcess(Nvnflinger::Nvnflinger& nvnflinger, Core::System& system) { auto message_queue = std::make_shared(system); - // Needed on game boot - message_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged); - auto server_manager = std::make_unique(system); server_manager->RegisterNamedService(