summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/fs/file.h5
-rw-r--r--src/common/settings.h1
-rw-r--r--src/common/wall_clock.cpp16
-rw-r--r--src/common/wall_clock.h8
-rw-r--r--src/common/x64/native_clock.cpp6
-rw-r--r--src/common/x64/xbyak_abi.h16
-rw-r--r--src/core/hid/emulated_controller.cpp1
-rw-r--r--src/core/hid/motion_input.cpp4
-rw-r--r--src/core/hle/kernel/k_affinity_mask.h2
-rw-r--r--src/core/hle/kernel/k_page_table.cpp204
-rw-r--r--src/core/hle/kernel/k_page_table.h11
-rw-r--r--src/core/hle/kernel/k_priority_queue.h2
-rw-r--r--src/core/hle/kernel/k_process.cpp26
-rw-r--r--src/core/hle/kernel/k_process.h8
-rw-r--r--src/core/hle/kernel/k_scheduler.cpp45
-rw-r--r--src/core/hle/kernel/k_thread.cpp11
-rw-r--r--src/core/hle/kernel/svc.cpp6
-rw-r--r--src/input_common/drivers/sdl_driver.cpp5
-rw-r--r--src/input_common/drivers/udp_client.cpp8
-rw-r--r--src/input_common/drivers/udp_client.h4
-rw-r--r--src/input_common/input_engine.h4
-rw-r--r--src/input_common/input_mapping.cpp11
-rw-r--r--src/input_common/input_mapping.h8
-rw-r--r--src/input_common/input_poller.cpp13
-rw-r--r--src/input_common/main.cpp6
-rw-r--r--src/input_common/main.h2
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv.cpp12
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_atomic.cpp4
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/video_minimum_maximum.cpp18
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate_program.h3
-rw-r--r--src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp2
-rw-r--r--src/video_core/buffer_cache/buffer_cache.h49
-rw-r--r--src/video_core/engines/engine_upload.cpp7
-rw-r--r--src/video_core/engines/engine_upload.h8
-rw-r--r--src/video_core/engines/kepler_compute.cpp1
-rw-r--r--src/video_core/engines/kepler_memory.cpp4
-rw-r--r--src/video_core/engines/kepler_memory.h7
-rw-r--r--src/video_core/engines/maxwell_3d.cpp3
-rw-r--r--src/video_core/engines/maxwell_3d.h3
-rw-r--r--src/video_core/gpu.cpp8
-rw-r--r--src/video_core/gpu.h16
-rw-r--r--src/video_core/macro/macro.cpp13
-rw-r--r--src/video_core/macro/macro.h2
-rw-r--r--src/video_core/macro/macro_hle.cpp34
-rw-r--r--src/video_core/macro/macro_hle.h21
-rw-r--r--src/video_core/macro/macro_interpreter.cpp92
-rw-r--r--src/video_core/macro/macro_interpreter.h78
-rw-r--r--src/video_core/macro/macro_jit_x64.cpp104
-rw-r--r--src/video_core/macro/macro_jit_x64.h71
-rw-r--r--src/video_core/memory_manager.h4
-rw-r--r--src/video_core/rasterizer_interface.h3
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp22
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h2
-rw-r--r--src/video_core/renderer_vulkan/vk_fsr.cpp2
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp22
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h2
-rw-r--r--src/video_core/shader_cache.cpp4
-rw-r--r--src/video_core/shader_cache.h3
-rw-r--r--src/yuzu/configuration/config.cpp3
-rw-r--r--src/yuzu/configuration/configure_graphics.ui2
-rw-r--r--src/yuzu/configuration/configure_input_advanced.cpp2
-rw-r--r--src/yuzu/configuration/configure_input_advanced.ui19
-rw-r--r--src/yuzu/configuration/configure_input_player.cpp94
-rw-r--r--src/yuzu/configuration/configure_motion_touch.cpp38
-rw-r--r--src/yuzu/game_list.cpp2
-rw-r--r--src/yuzu/hotkeys.cpp3
-rw-r--r--src/yuzu/util/controller_navigation.cpp3
67 files changed, 693 insertions, 530 deletions
diff --git a/src/common/fs/file.h b/src/common/fs/file.h
index 2c4ab4332..a4f7944cd 100644
--- a/src/common/fs/file.h
+++ b/src/common/fs/file.h
@@ -188,9 +188,8 @@ public:
188 188
189#ifdef _WIN32 189#ifdef _WIN32
190 template <typename Path> 190 template <typename Path>
191 [[nodiscard]] void Open(const Path& path, FileAccessMode mode, 191 void Open(const Path& path, FileAccessMode mode, FileType type = FileType::BinaryFile,
192 FileType type = FileType::BinaryFile, 192 FileShareFlag flag = FileShareFlag::ShareReadOnly) {
193 FileShareFlag flag = FileShareFlag::ShareReadOnly) {
194 using ValueType = typename Path::value_type; 193 using ValueType = typename Path::value_type;
195 if constexpr (IsChar<ValueType>) { 194 if constexpr (IsChar<ValueType>) {
196 Open(ToU8String(path), mode, type, flag); 195 Open(ToU8String(path), mode, type, flag);
diff --git a/src/common/settings.h b/src/common/settings.h
index d01c0448c..9bee6e10f 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -554,6 +554,7 @@ struct Values {
554 Setting<bool> use_docked_mode{true, "use_docked_mode"}; 554 Setting<bool> use_docked_mode{true, "use_docked_mode"};
555 555
556 BasicSetting<bool> enable_raw_input{false, "enable_raw_input"}; 556 BasicSetting<bool> enable_raw_input{false, "enable_raw_input"};
557 BasicSetting<bool> controller_navigation{true, "controller_navigation"};
557 558
558 Setting<bool> vibration_enabled{true, "vibration_enabled"}; 559 Setting<bool> vibration_enabled{true, "vibration_enabled"};
559 Setting<bool> enable_accurate_vibrations{false, "enable_accurate_vibrations"}; 560 Setting<bool> enable_accurate_vibrations{false, "enable_accurate_vibrations"};
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp
index 49830b8ab..9acf7551e 100644
--- a/src/common/wall_clock.cpp
+++ b/src/common/wall_clock.cpp
@@ -65,14 +65,20 @@ private:
65 65
66#ifdef ARCHITECTURE_x86_64 66#ifdef ARCHITECTURE_x86_64
67 67
68std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 68std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
69 u32 emulated_clock_frequency) { 69 u64 emulated_clock_frequency) {
70 const auto& caps = GetCPUCaps(); 70 const auto& caps = GetCPUCaps();
71 u64 rtsc_frequency = 0; 71 u64 rtsc_frequency = 0;
72 if (caps.invariant_tsc) { 72 if (caps.invariant_tsc) {
73 rtsc_frequency = EstimateRDTSCFrequency(); 73 rtsc_frequency = EstimateRDTSCFrequency();
74 } 74 }
75 if (rtsc_frequency == 0) { 75
76 // Fallback to StandardWallClock if the hardware TSC does not have the precision greater than:
77 // - A nanosecond
78 // - The emulated CPU frequency
79 // - The emulated clock counter frequency (CNTFRQ)
80 if (rtsc_frequency <= WallClock::NS_RATIO || rtsc_frequency <= emulated_cpu_frequency ||
81 rtsc_frequency <= emulated_clock_frequency) {
76 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, 82 return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
77 emulated_clock_frequency); 83 emulated_clock_frequency);
78 } else { 84 } else {
@@ -83,8 +89,8 @@ std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
83 89
84#else 90#else
85 91
86std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 92std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
87 u32 emulated_clock_frequency) { 93 u64 emulated_clock_frequency) {
88 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency); 94 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
89} 95}
90 96
diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h
index cef3e9499..874448c27 100644
--- a/src/common/wall_clock.h
+++ b/src/common/wall_clock.h
@@ -13,6 +13,10 @@ namespace Common {
13 13
14class WallClock { 14class WallClock {
15public: 15public:
16 static constexpr u64 NS_RATIO = 1'000'000'000;
17 static constexpr u64 US_RATIO = 1'000'000;
18 static constexpr u64 MS_RATIO = 1'000;
19
16 virtual ~WallClock() = default; 20 virtual ~WallClock() = default;
17 21
18 /// Returns current wall time in nanoseconds 22 /// Returns current wall time in nanoseconds
@@ -49,7 +53,7 @@ private:
49 bool is_native; 53 bool is_native;
50}; 54};
51 55
52[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 56[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
53 u32 emulated_clock_frequency); 57 u64 emulated_clock_frequency);
54 58
55} // namespace Common 59} // namespace Common
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp
index 82ee2c8a1..91b842829 100644
--- a/src/common/x64/native_clock.cpp
+++ b/src/common/x64/native_clock.cpp
@@ -47,9 +47,9 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen
47 _mm_mfence(); 47 _mm_mfence();
48 time_point.inner.last_measure = __rdtsc(); 48 time_point.inner.last_measure = __rdtsc();
49 time_point.inner.accumulated_ticks = 0U; 49 time_point.inner.accumulated_ticks = 0U;
50 ns_rtsc_factor = GetFixedPoint64Factor(1000000000, rtsc_frequency); 50 ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
51 us_rtsc_factor = GetFixedPoint64Factor(1000000, rtsc_frequency); 51 us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
52 ms_rtsc_factor = GetFixedPoint64Factor(1000, rtsc_frequency); 52 ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
53 clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency); 53 clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
54 cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency); 54 cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
55} 55}
diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h
index 0ddf9b83e..87b3d63a4 100644
--- a/src/common/x64/xbyak_abi.h
+++ b/src/common/x64/xbyak_abi.h
@@ -37,12 +37,12 @@ constexpr Xbyak::Reg IndexToReg(size_t reg_index) {
37 } 37 }
38} 38}
39 39
40inline std::bitset<32> BuildRegSet(std::initializer_list<Xbyak::Reg> regs) { 40constexpr std::bitset<32> BuildRegSet(std::initializer_list<Xbyak::Reg> regs) {
41 std::bitset<32> bits; 41 size_t bits = 0;
42 for (const Xbyak::Reg& reg : regs) { 42 for (const Xbyak::Reg& reg : regs) {
43 bits[RegToIndex(reg)] = true; 43 bits |= size_t{1} << RegToIndex(reg);
44 } 44 }
45 return bits; 45 return {bits};
46} 46}
47 47
48constexpr inline std::bitset<32> ABI_ALL_GPRS(0x0000FFFF); 48constexpr inline std::bitset<32> ABI_ALL_GPRS(0x0000FFFF);
@@ -57,7 +57,7 @@ constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rdx;
57constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::r8; 57constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::r8;
58constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::r9; 58constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::r9;
59 59
60const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({ 60constexpr inline std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
61 // GPRs 61 // GPRs
62 Xbyak::util::rcx, 62 Xbyak::util::rcx,
63 Xbyak::util::rdx, 63 Xbyak::util::rdx,
@@ -74,7 +74,7 @@ const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
74 Xbyak::util::xmm5, 74 Xbyak::util::xmm5,
75}); 75});
76 76
77const std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({ 77constexpr inline std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({
78 // GPRs 78 // GPRs
79 Xbyak::util::rbx, 79 Xbyak::util::rbx,
80 Xbyak::util::rsi, 80 Xbyak::util::rsi,
@@ -108,7 +108,7 @@ constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rsi;
108constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::rdx; 108constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::rdx;
109constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::rcx; 109constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::rcx;
110 110
111const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({ 111constexpr inline std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
112 // GPRs 112 // GPRs
113 Xbyak::util::rcx, 113 Xbyak::util::rcx,
114 Xbyak::util::rdx, 114 Xbyak::util::rdx,
@@ -137,7 +137,7 @@ const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({
137 Xbyak::util::xmm15, 137 Xbyak::util::xmm15,
138}); 138});
139 139
140const std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({ 140constexpr inline std::bitset<32> ABI_ALL_CALLEE_SAVED = BuildRegSet({
141 // GPRs 141 // GPRs
142 Xbyak::util::rbx, 142 Xbyak::util::rbx,
143 Xbyak::util::rbp, 143 Xbyak::util::rbp,
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp
index d12037b11..a7cdf45e6 100644
--- a/src/core/hid/emulated_controller.cpp
+++ b/src/core/hid/emulated_controller.cpp
@@ -749,6 +749,7 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback
749 raw_status.gyro.y.value, 749 raw_status.gyro.y.value,
750 raw_status.gyro.z.value, 750 raw_status.gyro.z.value,
751 }); 751 });
752 emulated.SetGyroThreshold(raw_status.gyro.x.properties.threshold);
752 emulated.UpdateRotation(raw_status.delta_timestamp); 753 emulated.UpdateRotation(raw_status.delta_timestamp);
753 emulated.UpdateOrientation(raw_status.delta_timestamp); 754 emulated.UpdateOrientation(raw_status.delta_timestamp);
754 force_update_motion = raw_status.force_update; 755 force_update_motion = raw_status.force_update;
diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp
index 6e126be19..05042fd99 100644
--- a/src/core/hid/motion_input.cpp
+++ b/src/core/hid/motion_input.cpp
@@ -10,7 +10,7 @@ namespace Core::HID {
10MotionInput::MotionInput() { 10MotionInput::MotionInput() {
11 // Initialize PID constants with default values 11 // Initialize PID constants with default values
12 SetPID(0.3f, 0.005f, 0.0f); 12 SetPID(0.3f, 0.005f, 0.0f);
13 SetGyroThreshold(0.00005f); 13 SetGyroThreshold(0.007f);
14} 14}
15 15
16void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { 16void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) {
@@ -31,7 +31,7 @@ void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
31 gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f); 31 gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f);
32 } 32 }
33 33
34 if (gyro.Length2() < gyro_threshold) { 34 if (gyro.Length() < gyro_threshold) {
35 gyro = {}; 35 gyro = {};
36 } else { 36 } else {
37 only_accelerometer = false; 37 only_accelerometer = false;
diff --git a/src/core/hle/kernel/k_affinity_mask.h b/src/core/hle/kernel/k_affinity_mask.h
index b906895fc..cf704ce87 100644
--- a/src/core/hle/kernel/k_affinity_mask.h
+++ b/src/core/hle/kernel/k_affinity_mask.h
@@ -31,8 +31,6 @@ public:
31 } 31 }
32 32
33 constexpr void SetAffinity(s32 core, bool set) { 33 constexpr void SetAffinity(s32 core, bool set) {
34 ASSERT(0 <= core && core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
35
36 if (set) { 34 if (set) {
37 this->mask |= GetCoreBit(core); 35 this->mask |= GetCoreBit(core);
38 } else { 36 } else {
diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp
index b650ea31d..2ebbc0819 100644
--- a/src/core/hle/kernel/k_page_table.cpp
+++ b/src/core/hle/kernel/k_page_table.cpp
@@ -276,22 +276,23 @@ ResultCode KPageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_
276 276
277ResultCode KPageTable::MapProcessCode(VAddr addr, std::size_t num_pages, KMemoryState state, 277ResultCode KPageTable::MapProcessCode(VAddr addr, std::size_t num_pages, KMemoryState state,
278 KMemoryPermission perm) { 278 KMemoryPermission perm) {
279 std::lock_guard lock{page_table_lock};
280
281 const u64 size{num_pages * PageSize}; 279 const u64 size{num_pages * PageSize};
282 280
283 if (!CanContain(addr, size, state)) { 281 // Validate the mapping request.
284 return ResultInvalidCurrentMemory; 282 R_UNLESS(this->CanContain(addr, size, state), ResultInvalidCurrentMemory);
285 }
286 283
287 if (IsRegionMapped(addr, size)) { 284 // Lock the table.
288 return ResultInvalidCurrentMemory; 285 std::lock_guard lock{page_table_lock};
289 } 286
287 // Verify that the destination memory is unmapped.
288 R_TRY(this->CheckMemoryState(addr, size, KMemoryState::All, KMemoryState::Free,
289 KMemoryPermission::None, KMemoryPermission::None,
290 KMemoryAttribute::None, KMemoryAttribute::None));
290 291
291 KPageLinkedList page_linked_list; 292 KPageLinkedList page_linked_list;
292 CASCADE_CODE(system.Kernel().MemoryManager().Allocate(page_linked_list, num_pages, memory_pool, 293 R_TRY(system.Kernel().MemoryManager().Allocate(page_linked_list, num_pages, memory_pool,
293 allocation_option)); 294 allocation_option));
294 CASCADE_CODE(Operate(addr, num_pages, page_linked_list, OperationType::MapGroup)); 295 R_TRY(Operate(addr, num_pages, page_linked_list, OperationType::MapGroup));
295 296
296 block_manager->Update(addr, num_pages, state, perm); 297 block_manager->Update(addr, num_pages, state, perm);
297 298
@@ -395,39 +396,12 @@ ResultCode KPageTable::UnmapProcessMemory(VAddr dst_addr, std::size_t size,
395 396
396 return ResultSuccess; 397 return ResultSuccess;
397} 398}
398void KPageTable::MapPhysicalMemory(KPageLinkedList& page_linked_list, VAddr start, VAddr end) {
399 auto node{page_linked_list.Nodes().begin()};
400 PAddr map_addr{node->GetAddress()};
401 std::size_t src_num_pages{node->GetNumPages()};
402
403 block_manager->IterateForRange(start, end, [&](const KMemoryInfo& info) {
404 if (info.state != KMemoryState::Free) {
405 return;
406 }
407
408 std::size_t dst_num_pages{GetSizeInRange(info, start, end) / PageSize};
409 VAddr dst_addr{GetAddressInRange(info, start)};
410
411 while (dst_num_pages) {
412 if (!src_num_pages) {
413 node = std::next(node);
414 map_addr = node->GetAddress();
415 src_num_pages = node->GetNumPages();
416 }
417
418 const std::size_t num_pages{std::min(src_num_pages, dst_num_pages)};
419 Operate(dst_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::Map,
420 map_addr);
421
422 dst_addr += num_pages * PageSize;
423 map_addr += num_pages * PageSize;
424 src_num_pages -= num_pages;
425 dst_num_pages -= num_pages;
426 }
427 });
428}
429 399
430ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { 400ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) {
401 // Lock the physical memory lock.
402 std::lock_guard phys_lk(map_physical_memory_lock);
403
404 // Lock the table.
431 std::lock_guard lock{page_table_lock}; 405 std::lock_guard lock{page_table_lock};
432 406
433 std::size_t mapped_size{}; 407 std::size_t mapped_size{};
@@ -463,7 +437,35 @@ ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) {
463 // We succeeded, so commit the memory reservation. 437 // We succeeded, so commit the memory reservation.
464 memory_reservation.Commit(); 438 memory_reservation.Commit();
465 439
466 MapPhysicalMemory(page_linked_list, addr, end_addr); 440 // Map the memory.
441 auto node{page_linked_list.Nodes().begin()};
442 PAddr map_addr{node->GetAddress()};
443 std::size_t src_num_pages{node->GetNumPages()};
444 block_manager->IterateForRange(addr, end_addr, [&](const KMemoryInfo& info) {
445 if (info.state != KMemoryState::Free) {
446 return;
447 }
448
449 std::size_t dst_num_pages{GetSizeInRange(info, addr, end_addr) / PageSize};
450 VAddr dst_addr{GetAddressInRange(info, addr)};
451
452 while (dst_num_pages) {
453 if (!src_num_pages) {
454 node = std::next(node);
455 map_addr = node->GetAddress();
456 src_num_pages = node->GetNumPages();
457 }
458
459 const std::size_t num_pages{std::min(src_num_pages, dst_num_pages)};
460 Operate(dst_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::Map,
461 map_addr);
462
463 dst_addr += num_pages * PageSize;
464 map_addr += num_pages * PageSize;
465 src_num_pages -= num_pages;
466 dst_num_pages -= num_pages;
467 }
468 });
467 469
468 mapped_physical_memory_size += remaining_size; 470 mapped_physical_memory_size += remaining_size;
469 471
@@ -503,23 +505,8 @@ ResultCode KPageTable::UnmapPhysicalMemory(VAddr addr, std::size_t size) {
503 return ResultSuccess; 505 return ResultSuccess;
504 } 506 }
505 507
506 CASCADE_CODE(UnmapMemory(addr, size));
507
508 auto process{system.Kernel().CurrentProcess()};
509 process->GetResourceLimit()->Release(LimitableResource::PhysicalMemory, mapped_size);
510 mapped_physical_memory_size -= mapped_size;
511
512 return ResultSuccess;
513}
514
515ResultCode KPageTable::UnmapMemory(VAddr addr, std::size_t size) {
516 std::lock_guard lock{page_table_lock};
517
518 const VAddr end_addr{addr + size};
519 ResultCode result{ResultSuccess};
520 KPageLinkedList page_linked_list;
521
522 // Unmap each region within the range 508 // Unmap each region within the range
509 KPageLinkedList page_linked_list;
523 block_manager->IterateForRange(addr, end_addr, [&](const KMemoryInfo& info) { 510 block_manager->IterateForRange(addr, end_addr, [&](const KMemoryInfo& info) {
524 if (info.state == KMemoryState::Normal) { 511 if (info.state == KMemoryState::Normal) {
525 const std::size_t block_size{GetSizeInRange(info, addr, end_addr)}; 512 const std::size_t block_size{GetSizeInRange(info, addr, end_addr)};
@@ -535,7 +522,6 @@ ResultCode KPageTable::UnmapMemory(VAddr addr, std::size_t size) {
535 } 522 }
536 } 523 }
537 }); 524 });
538
539 if (result.IsError()) { 525 if (result.IsError()) {
540 return result; 526 return result;
541 } 527 }
@@ -546,10 +532,14 @@ ResultCode KPageTable::UnmapMemory(VAddr addr, std::size_t size) {
546 532
547 block_manager->Update(addr, num_pages, KMemoryState::Free); 533 block_manager->Update(addr, num_pages, KMemoryState::Free);
548 534
535 auto process{system.Kernel().CurrentProcess()};
536 process->GetResourceLimit()->Release(LimitableResource::PhysicalMemory, mapped_size);
537 mapped_physical_memory_size -= mapped_size;
538
549 return ResultSuccess; 539 return ResultSuccess;
550} 540}
551 541
552ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { 542ResultCode KPageTable::MapMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) {
553 std::lock_guard lock{page_table_lock}; 543 std::lock_guard lock{page_table_lock};
554 544
555 KMemoryState src_state{}; 545 KMemoryState src_state{};
@@ -588,7 +578,7 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) {
588 return ResultSuccess; 578 return ResultSuccess;
589} 579}
590 580
591ResultCode KPageTable::Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size) { 581ResultCode KPageTable::UnmapMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) {
592 std::lock_guard lock{page_table_lock}; 582 std::lock_guard lock{page_table_lock};
593 583
594 KMemoryState src_state{}; 584 KMemoryState src_state{};
@@ -652,24 +642,26 @@ ResultCode KPageTable::MapPages(VAddr addr, const KPageLinkedList& page_linked_l
652 return ResultSuccess; 642 return ResultSuccess;
653} 643}
654 644
655ResultCode KPageTable::MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state, 645ResultCode KPageTable::MapPages(VAddr address, KPageLinkedList& page_linked_list,
656 KMemoryPermission perm) { 646 KMemoryState state, KMemoryPermission perm) {
657 std::lock_guard lock{page_table_lock}; 647 // Check that the map is in range.
658
659 const std::size_t num_pages{page_linked_list.GetNumPages()}; 648 const std::size_t num_pages{page_linked_list.GetNumPages()};
660 const std::size_t size{num_pages * PageSize}; 649 const std::size_t size{num_pages * PageSize};
650 R_UNLESS(this->CanContain(address, size, state), ResultInvalidCurrentMemory);
661 651
662 if (!CanContain(addr, size, state)) { 652 // Lock the table.
663 return ResultInvalidCurrentMemory; 653 std::lock_guard lock{page_table_lock};
664 }
665 654
666 if (IsRegionMapped(addr, num_pages * PageSize)) { 655 // Check the memory state.
667 return ResultInvalidCurrentMemory; 656 R_TRY(this->CheckMemoryState(address, size, KMemoryState::All, KMemoryState::Free,
668 } 657 KMemoryPermission::None, KMemoryPermission::None,
658 KMemoryAttribute::None, KMemoryAttribute::None));
669 659
670 CASCADE_CODE(MapPages(addr, page_linked_list, perm)); 660 // Map the pages.
661 R_TRY(MapPages(address, page_linked_list, perm));
671 662
672 block_manager->Update(addr, num_pages, state, perm); 663 // Update the blocks.
664 block_manager->Update(address, num_pages, state, perm);
673 665
674 return ResultSuccess; 666 return ResultSuccess;
675} 667}
@@ -693,21 +685,23 @@ ResultCode KPageTable::UnmapPages(VAddr addr, const KPageLinkedList& page_linked
693 685
694ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, 686ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list,
695 KMemoryState state) { 687 KMemoryState state) {
696 std::lock_guard lock{page_table_lock}; 688 // Check that the unmap is in range.
697
698 const std::size_t num_pages{page_linked_list.GetNumPages()}; 689 const std::size_t num_pages{page_linked_list.GetNumPages()};
699 const std::size_t size{num_pages * PageSize}; 690 const std::size_t size{num_pages * PageSize};
691 R_UNLESS(this->Contains(addr, size), ResultInvalidCurrentMemory);
700 692
701 if (!CanContain(addr, size, state)) { 693 // Lock the table.
702 return ResultInvalidCurrentMemory; 694 std::lock_guard lock{page_table_lock};
703 }
704 695
705 if (IsRegionMapped(addr, num_pages * PageSize)) { 696 // Check the memory state.
706 return ResultInvalidCurrentMemory; 697 R_TRY(this->CheckMemoryState(addr, size, KMemoryState::All, state, KMemoryPermission::None,
707 } 698 KMemoryPermission::None, KMemoryAttribute::All,
699 KMemoryAttribute::None));
708 700
709 CASCADE_CODE(UnmapPages(addr, page_linked_list)); 701 // Perform the unmap.
702 R_TRY(UnmapPages(addr, page_linked_list));
710 703
704 // Update the blocks.
711 block_manager->Update(addr, num_pages, state, KMemoryPermission::None); 705 block_manager->Update(addr, num_pages, state, KMemoryPermission::None);
712 706
713 return ResultSuccess; 707 return ResultSuccess;
@@ -765,7 +759,6 @@ ResultCode KPageTable::SetProcessMemoryPermission(VAddr addr, std::size_t size,
765 759
766 // Ensure cache coherency, if we're setting pages as executable. 760 // Ensure cache coherency, if we're setting pages as executable.
767 if (is_x) { 761 if (is_x) {
768 // Memory execution state is changing, invalidate CPU cache range
769 system.InvalidateCpuInstructionCacheRange(addr, size); 762 system.InvalidateCpuInstructionCacheRange(addr, size);
770 } 763 }
771 764
@@ -793,12 +786,12 @@ ResultCode KPageTable::ReserveTransferMemory(VAddr addr, std::size_t size, KMemo
793 KMemoryState state{}; 786 KMemoryState state{};
794 KMemoryAttribute attribute{}; 787 KMemoryAttribute attribute{};
795 788
796 CASCADE_CODE(CheckMemoryState( 789 R_TRY(CheckMemoryState(&state, nullptr, &attribute, nullptr, addr, size,
797 &state, nullptr, &attribute, nullptr, addr, size, 790 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted,
798 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, 791 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted,
799 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryPermission::All, 792 KMemoryPermission::All, KMemoryPermission::UserReadWrite,
800 KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, 793 KMemoryAttribute::Mask, KMemoryAttribute::None,
801 KMemoryAttribute::IpcAndDeviceMapped)); 794 KMemoryAttribute::IpcAndDeviceMapped));
802 795
803 block_manager->Update(addr, size / PageSize, state, perm, attribute | KMemoryAttribute::Locked); 796 block_manager->Update(addr, size / PageSize, state, perm, attribute | KMemoryAttribute::Locked);
804 797
@@ -810,12 +803,11 @@ ResultCode KPageTable::ResetTransferMemory(VAddr addr, std::size_t size) {
810 803
811 KMemoryState state{}; 804 KMemoryState state{};
812 805
813 CASCADE_CODE( 806 R_TRY(CheckMemoryState(&state, nullptr, nullptr, nullptr, addr, size,
814 CheckMemoryState(&state, nullptr, nullptr, nullptr, addr, size, 807 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted,
815 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, 808 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted,
816 KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, 809 KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask,
817 KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, 810 KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped));
818 KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped));
819 811
820 block_manager->Update(addr, size / PageSize, state, KMemoryPermission::UserReadWrite); 812 block_manager->Update(addr, size / PageSize, state, KMemoryPermission::UserReadWrite);
821 return ResultSuccess; 813 return ResultSuccess;
@@ -871,8 +863,9 @@ ResultCode KPageTable::SetMemoryAttribute(VAddr addr, std::size_t size, u32 mask
871 AttributeTestMask, KMemoryAttribute::None, ~AttributeTestMask)); 863 AttributeTestMask, KMemoryAttribute::None, ~AttributeTestMask));
872 864
873 // Determine the new attribute. 865 // Determine the new attribute.
874 const auto new_attr = ((old_attr & static_cast<KMemoryAttribute>(~mask)) | 866 const KMemoryAttribute new_attr =
875 static_cast<KMemoryAttribute>(attr & mask)); 867 static_cast<KMemoryAttribute>(((old_attr & static_cast<KMemoryAttribute>(~mask)) |
868 static_cast<KMemoryAttribute>(attr & mask)));
876 869
877 // Perform operation. 870 // Perform operation.
878 this->Operate(addr, num_pages, old_perm, OperationType::ChangePermissionsAndRefresh); 871 this->Operate(addr, num_pages, old_perm, OperationType::ChangePermissionsAndRefresh);
@@ -896,6 +889,9 @@ ResultCode KPageTable::SetMaxHeapSize(std::size_t size) {
896} 889}
897 890
898ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { 891ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) {
892 // Lock the physical memory lock.
893 std::lock_guard phys_lk(map_physical_memory_lock);
894
899 // Try to perform a reduction in heap, instead of an extension. 895 // Try to perform a reduction in heap, instead of an extension.
900 VAddr cur_address{}; 896 VAddr cur_address{};
901 std::size_t allocation_size{}; 897 std::size_t allocation_size{};
@@ -1025,12 +1021,12 @@ ResultVal<VAddr> KPageTable::AllocateAndMapMemory(std::size_t needed_num_pages,
1025 } 1021 }
1026 1022
1027 if (is_map_only) { 1023 if (is_map_only) {
1028 CASCADE_CODE(Operate(addr, needed_num_pages, perm, OperationType::Map, map_addr)); 1024 R_TRY(Operate(addr, needed_num_pages, perm, OperationType::Map, map_addr));
1029 } else { 1025 } else {
1030 KPageLinkedList page_group; 1026 KPageLinkedList page_group;
1031 CASCADE_CODE(system.Kernel().MemoryManager().Allocate(page_group, needed_num_pages, 1027 R_TRY(system.Kernel().MemoryManager().Allocate(page_group, needed_num_pages, memory_pool,
1032 memory_pool, allocation_option)); 1028 allocation_option));
1033 CASCADE_CODE(Operate(addr, needed_num_pages, page_group, OperationType::MapGroup)); 1029 R_TRY(Operate(addr, needed_num_pages, page_group, OperationType::MapGroup));
1034 } 1030 }
1035 1031
1036 block_manager->Update(addr, needed_num_pages, state, perm); 1032 block_manager->Update(addr, needed_num_pages, state, perm);
@@ -1186,7 +1182,7 @@ VAddr KPageTable::AllocateVirtualMemory(VAddr start, std::size_t region_num_page
1186 1182
1187ResultCode KPageTable::Operate(VAddr addr, std::size_t num_pages, const KPageLinkedList& page_group, 1183ResultCode KPageTable::Operate(VAddr addr, std::size_t num_pages, const KPageLinkedList& page_group,
1188 OperationType operation) { 1184 OperationType operation) {
1189 std::lock_guard lock{page_table_lock}; 1185 ASSERT(this->IsLockedByCurrentThread());
1190 1186
1191 ASSERT(Common::IsAligned(addr, PageSize)); 1187 ASSERT(Common::IsAligned(addr, PageSize));
1192 ASSERT(num_pages > 0); 1188 ASSERT(num_pages > 0);
@@ -1211,7 +1207,7 @@ ResultCode KPageTable::Operate(VAddr addr, std::size_t num_pages, const KPageLin
1211 1207
1212ResultCode KPageTable::Operate(VAddr addr, std::size_t num_pages, KMemoryPermission perm, 1208ResultCode KPageTable::Operate(VAddr addr, std::size_t num_pages, KMemoryPermission perm,
1213 OperationType operation, PAddr map_addr) { 1209 OperationType operation, PAddr map_addr) {
1214 std::lock_guard lock{page_table_lock}; 1210 ASSERT(this->IsLockedByCurrentThread());
1215 1211
1216 ASSERT(num_pages > 0); 1212 ASSERT(num_pages > 0);
1217 ASSERT(Common::IsAligned(addr, PageSize)); 1213 ASSERT(Common::IsAligned(addr, PageSize));
diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h
index f67986e91..60ae9b9e8 100644
--- a/src/core/hle/kernel/k_page_table.h
+++ b/src/core/hle/kernel/k_page_table.h
@@ -37,9 +37,8 @@ public:
37 VAddr src_addr); 37 VAddr src_addr);
38 ResultCode MapPhysicalMemory(VAddr addr, std::size_t size); 38 ResultCode MapPhysicalMemory(VAddr addr, std::size_t size);
39 ResultCode UnmapPhysicalMemory(VAddr addr, std::size_t size); 39 ResultCode UnmapPhysicalMemory(VAddr addr, std::size_t size);
40 ResultCode UnmapMemory(VAddr addr, std::size_t size); 40 ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, std::size_t size);
41 ResultCode Map(VAddr dst_addr, VAddr src_addr, std::size_t size); 41 ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, std::size_t size);
42 ResultCode Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size);
43 ResultCode MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state, 42 ResultCode MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state,
44 KMemoryPermission perm); 43 KMemoryPermission perm);
45 ResultCode UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state); 44 ResultCode UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state);
@@ -88,7 +87,6 @@ private:
88 ResultCode MapPages(VAddr addr, const KPageLinkedList& page_linked_list, 87 ResultCode MapPages(VAddr addr, const KPageLinkedList& page_linked_list,
89 KMemoryPermission perm); 88 KMemoryPermission perm);
90 ResultCode UnmapPages(VAddr addr, const KPageLinkedList& page_linked_list); 89 ResultCode UnmapPages(VAddr addr, const KPageLinkedList& page_linked_list);
91 void MapPhysicalMemory(KPageLinkedList& page_linked_list, VAddr start, VAddr end);
92 bool IsRegionMapped(VAddr address, u64 size); 90 bool IsRegionMapped(VAddr address, u64 size);
93 bool IsRegionContiguous(VAddr addr, u64 size) const; 91 bool IsRegionContiguous(VAddr addr, u64 size) const;
94 void AddRegionToPages(VAddr start, std::size_t num_pages, KPageLinkedList& page_linked_list); 92 void AddRegionToPages(VAddr start, std::size_t num_pages, KPageLinkedList& page_linked_list);
@@ -148,6 +146,7 @@ private:
148 } 146 }
149 147
150 std::recursive_mutex page_table_lock; 148 std::recursive_mutex page_table_lock;
149 std::mutex map_physical_memory_lock;
151 std::unique_ptr<KMemoryBlockManager> block_manager; 150 std::unique_ptr<KMemoryBlockManager> block_manager;
152 151
153public: 152public:
@@ -249,7 +248,9 @@ public:
249 return !IsOutsideASLRRegion(address, size); 248 return !IsOutsideASLRRegion(address, size);
250 } 249 }
251 constexpr PAddr GetPhysicalAddr(VAddr addr) { 250 constexpr PAddr GetPhysicalAddr(VAddr addr) {
252 return page_table_impl.backing_addr[addr >> PageBits] + addr; 251 const auto backing_addr = page_table_impl.backing_addr[addr >> PageBits];
252 ASSERT(backing_addr);
253 return backing_addr + addr;
253 } 254 }
254 constexpr bool Contains(VAddr addr) const { 255 constexpr bool Contains(VAddr addr) const {
255 return address_space_start <= addr && addr <= address_space_end - 1; 256 return address_space_start <= addr && addr <= address_space_end - 1;
diff --git a/src/core/hle/kernel/k_priority_queue.h b/src/core/hle/kernel/k_priority_queue.h
index 0b894c8cf..bd779739d 100644
--- a/src/core/hle/kernel/k_priority_queue.h
+++ b/src/core/hle/kernel/k_priority_queue.h
@@ -258,7 +258,7 @@ private:
258 258
259private: 259private:
260 constexpr void ClearAffinityBit(u64& affinity, s32 core) { 260 constexpr void ClearAffinityBit(u64& affinity, s32 core) {
261 affinity &= ~(u64(1) << core); 261 affinity &= ~(UINT64_C(1) << core);
262 } 262 }
263 263
264 constexpr s32 GetNextCore(u64& affinity) { 264 constexpr s32 GetNextCore(u64& affinity) {
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index 265ac6fa1..85c506979 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -146,6 +146,13 @@ ResultCode KProcess::Initialize(KProcess* process, Core::System& system, std::st
146 // Open a reference to the resource limit. 146 // Open a reference to the resource limit.
147 process->resource_limit->Open(); 147 process->resource_limit->Open();
148 148
149 // Clear remaining fields.
150 process->num_running_threads = 0;
151 process->is_signaled = false;
152 process->exception_thread = nullptr;
153 process->is_suspended = false;
154 process->schedule_count = 0;
155
149 return ResultSuccess; 156 return ResultSuccess;
150} 157}
151 158
@@ -157,20 +164,17 @@ KResourceLimit* KProcess::GetResourceLimit() const {
157 return resource_limit; 164 return resource_limit;
158} 165}
159 166
160void KProcess::IncrementThreadCount() { 167void KProcess::IncrementRunningThreadCount() {
161 ASSERT(num_threads >= 0); 168 ASSERT(num_running_threads.load() >= 0);
162 num_created_threads++; 169 ++num_running_threads;
163
164 if (const auto count = ++num_threads; count > peak_num_threads) {
165 peak_num_threads = count;
166 }
167} 170}
168 171
169void KProcess::DecrementThreadCount() { 172void KProcess::DecrementRunningThreadCount() {
170 ASSERT(num_threads > 0); 173 ASSERT(num_running_threads.load() > 0);
171 174
172 if (const auto count = --num_threads; count == 0) { 175 if (const auto prev = num_running_threads--; prev == 1) {
173 LOG_WARNING(Kernel, "Process termination is not fully implemented."); 176 // TODO(bunnei): Process termination to be implemented when multiprocess is supported.
177 UNIMPLEMENTED_MSG("KProcess termination is not implemennted!");
174 } 178 }
175} 179}
176 180
diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h
index c2a672021..38b446350 100644
--- a/src/core/hle/kernel/k_process.h
+++ b/src/core/hle/kernel/k_process.h
@@ -235,8 +235,8 @@ public:
235 ++schedule_count; 235 ++schedule_count;
236 } 236 }
237 237
238 void IncrementThreadCount(); 238 void IncrementRunningThreadCount();
239 void DecrementThreadCount(); 239 void DecrementRunningThreadCount();
240 240
241 void SetRunningThread(s32 core, KThread* thread, u64 idle_count) { 241 void SetRunningThread(s32 core, KThread* thread, u64 idle_count) {
242 running_threads[core] = thread; 242 running_threads[core] = thread;
@@ -473,9 +473,7 @@ private:
473 bool is_suspended{}; 473 bool is_suspended{};
474 bool is_initialized{}; 474 bool is_initialized{};
475 475
476 std::atomic<s32> num_created_threads{}; 476 std::atomic<u16> num_running_threads{};
477 std::atomic<u16> num_threads{};
478 u16 peak_num_threads{};
479 477
480 std::array<KThread*, Core::Hardware::NUM_CPU_CORES> running_threads{}; 478 std::array<KThread*, Core::Hardware::NUM_CPU_CORES> running_threads{};
481 std::array<u64, Core::Hardware::NUM_CPU_CORES> running_thread_idle_counts{}; 479 std::array<u64, Core::Hardware::NUM_CPU_CORES> running_thread_idle_counts{};
diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp
index b32d4f285..c96520828 100644
--- a/src/core/hle/kernel/k_scheduler.cpp
+++ b/src/core/hle/kernel/k_scheduler.cpp
@@ -710,23 +710,19 @@ void KScheduler::Unload(KThread* thread) {
710} 710}
711 711
712void KScheduler::Reload(KThread* thread) { 712void KScheduler::Reload(KThread* thread) {
713 LOG_TRACE(Kernel, "core {}, reload thread {}", core_id, thread ? thread->GetName() : "nullptr"); 713 LOG_TRACE(Kernel, "core {}, reload thread {}", core_id, thread->GetName());
714 714
715 if (thread) { 715 Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
716 ASSERT_MSG(thread->GetState() == ThreadState::Runnable, "Thread must be runnable."); 716 cpu_core.LoadContext(thread->GetContext32());
717 717 cpu_core.LoadContext(thread->GetContext64());
718 Core::ARM_Interface& cpu_core = system.ArmInterface(core_id); 718 cpu_core.SetTlsAddress(thread->GetTLSAddress());
719 cpu_core.LoadContext(thread->GetContext32()); 719 cpu_core.SetTPIDR_EL0(thread->GetTPIDR_EL0());
720 cpu_core.LoadContext(thread->GetContext64()); 720 cpu_core.ClearExclusiveState();
721 cpu_core.SetTlsAddress(thread->GetTLSAddress());
722 cpu_core.SetTPIDR_EL0(thread->GetTPIDR_EL0());
723 cpu_core.ClearExclusiveState();
724 }
725} 721}
726 722
727void KScheduler::SwitchContextStep2() { 723void KScheduler::SwitchContextStep2() {
728 // Load context of new thread 724 // Load context of new thread
729 Reload(current_thread.load()); 725 Reload(GetCurrentThread());
730 726
731 RescheduleCurrentCore(); 727 RescheduleCurrentCore();
732} 728}
@@ -735,13 +731,17 @@ void KScheduler::ScheduleImpl() {
735 KThread* previous_thread = GetCurrentThread(); 731 KThread* previous_thread = GetCurrentThread();
736 KThread* next_thread = state.highest_priority_thread; 732 KThread* next_thread = state.highest_priority_thread;
737 733
738 state.needs_scheduling = false; 734 state.needs_scheduling.store(false);
739 735
740 // We never want to schedule a null thread, so use the idle thread if we don't have a next. 736 // We never want to schedule a null thread, so use the idle thread if we don't have a next.
741 if (next_thread == nullptr) { 737 if (next_thread == nullptr) {
742 next_thread = idle_thread; 738 next_thread = idle_thread;
743 } 739 }
744 740
741 if (next_thread->GetCurrentCore() != core_id) {
742 next_thread->SetCurrentCore(core_id);
743 }
744
745 // We never want to schedule a dummy thread, as these are only used by host threads for locking. 745 // We never want to schedule a dummy thread, as these are only used by host threads for locking.
746 if (next_thread->GetThreadType() == ThreadType::Dummy) { 746 if (next_thread->GetThreadType() == ThreadType::Dummy) {
747 ASSERT_MSG(false, "Dummy threads should never be scheduled!"); 747 ASSERT_MSG(false, "Dummy threads should never be scheduled!");
@@ -755,14 +755,8 @@ void KScheduler::ScheduleImpl() {
755 return; 755 return;
756 } 756 }
757 757
758 if (next_thread->GetCurrentCore() != core_id) { 758 // Update the CPU time tracking variables.
759 next_thread->SetCurrentCore(core_id);
760 }
761
762 current_thread.store(next_thread);
763
764 KProcess* const previous_process = system.Kernel().CurrentProcess(); 759 KProcess* const previous_process = system.Kernel().CurrentProcess();
765
766 UpdateLastContextSwitchTime(previous_thread, previous_process); 760 UpdateLastContextSwitchTime(previous_thread, previous_process);
767 761
768 // Save context for previous thread 762 // Save context for previous thread
@@ -770,6 +764,10 @@ void KScheduler::ScheduleImpl() {
770 764
771 std::shared_ptr<Common::Fiber>* old_context; 765 std::shared_ptr<Common::Fiber>* old_context;
772 old_context = &previous_thread->GetHostContext(); 766 old_context = &previous_thread->GetHostContext();
767
768 // Set the new thread.
769 current_thread.store(next_thread);
770
773 guard.Unlock(); 771 guard.Unlock();
774 772
775 Common::Fiber::YieldTo(*old_context, *switch_fiber); 773 Common::Fiber::YieldTo(*old_context, *switch_fiber);
@@ -797,8 +795,8 @@ void KScheduler::SwitchToCurrent() {
797 do { 795 do {
798 auto next_thread = current_thread.load(); 796 auto next_thread = current_thread.load();
799 if (next_thread != nullptr) { 797 if (next_thread != nullptr) {
800 next_thread->context_guard.Lock(); 798 const auto locked = next_thread->context_guard.TryLock();
801 if (next_thread->GetRawState() != ThreadState::Runnable) { 799 if (state.needs_scheduling.load()) {
802 next_thread->context_guard.Unlock(); 800 next_thread->context_guard.Unlock();
803 break; 801 break;
804 } 802 }
@@ -806,6 +804,9 @@ void KScheduler::SwitchToCurrent() {
806 next_thread->context_guard.Unlock(); 804 next_thread->context_guard.Unlock();
807 break; 805 break;
808 } 806 }
807 if (!locked) {
808 continue;
809 }
809 } 810 }
810 auto thread = next_thread ? next_thread : idle_thread; 811 auto thread = next_thread ? next_thread : idle_thread;
811 Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext()); 812 Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext());
diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp
index f42abb8a1..de3ffe0c7 100644
--- a/src/core/hle/kernel/k_thread.cpp
+++ b/src/core/hle/kernel/k_thread.cpp
@@ -215,7 +215,6 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s
215 215
216 parent = owner; 216 parent = owner;
217 parent->Open(); 217 parent->Open();
218 parent->IncrementThreadCount();
219 } 218 }
220 219
221 // Initialize thread context. 220 // Initialize thread context.
@@ -327,11 +326,6 @@ void KThread::Finalize() {
327 } 326 }
328 } 327 }
329 328
330 // Decrement the parent process's thread count.
331 if (parent != nullptr) {
332 parent->DecrementThreadCount();
333 }
334
335 // Perform inherited finalization. 329 // Perform inherited finalization.
336 KSynchronizationObject::Finalize(); 330 KSynchronizationObject::Finalize();
337} 331}
@@ -1011,7 +1005,7 @@ ResultCode KThread::Run() {
1011 if (IsUserThread() && IsSuspended()) { 1005 if (IsUserThread() && IsSuspended()) {
1012 this->UpdateState(); 1006 this->UpdateState();
1013 } 1007 }
1014 owner->IncrementThreadCount(); 1008 owner->IncrementRunningThreadCount();
1015 } 1009 }
1016 1010
1017 // Set our state and finish. 1011 // Set our state and finish.
@@ -1026,10 +1020,11 @@ ResultCode KThread::Run() {
1026void KThread::Exit() { 1020void KThread::Exit() {
1027 ASSERT(this == GetCurrentThreadPointer(kernel)); 1021 ASSERT(this == GetCurrentThreadPointer(kernel));
1028 1022
1029 // Release the thread resource hint from parent. 1023 // Release the thread resource hint, running thread count from parent.
1030 if (parent != nullptr) { 1024 if (parent != nullptr) {
1031 parent->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 0, 1); 1025 parent->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 0, 1);
1032 resource_limit_release_hint = true; 1026 resource_limit_release_hint = true;
1027 parent->DecrementRunningThreadCount();
1033 } 1028 }
1034 1029
1035 // Perform termination. 1030 // Perform termination.
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index c7f5140f4..4f7aebf3f 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -230,7 +230,7 @@ static ResultCode MapMemory(Core::System& system, VAddr dst_addr, VAddr src_addr
230 return result; 230 return result;
231 } 231 }
232 232
233 return page_table.Map(dst_addr, src_addr, size); 233 return page_table.MapMemory(dst_addr, src_addr, size);
234} 234}
235 235
236static ResultCode MapMemory32(Core::System& system, u32 dst_addr, u32 src_addr, u32 size) { 236static ResultCode MapMemory32(Core::System& system, u32 dst_addr, u32 src_addr, u32 size) {
@@ -249,7 +249,7 @@ static ResultCode UnmapMemory(Core::System& system, VAddr dst_addr, VAddr src_ad
249 return result; 249 return result;
250 } 250 }
251 251
252 return page_table.Unmap(dst_addr, src_addr, size); 252 return page_table.UnmapMemory(dst_addr, src_addr, size);
253} 253}
254 254
255static ResultCode UnmapMemory32(Core::System& system, u32 dst_addr, u32 src_addr, u32 size) { 255static ResultCode UnmapMemory32(Core::System& system, u32 dst_addr, u32 src_addr, u32 size) {
@@ -2613,7 +2613,7 @@ static const FunctionDef SVC_Table_32[] = {
2613 {0x33, SvcWrap32<GetThreadContext32>, "GetThreadContext32"}, 2613 {0x33, SvcWrap32<GetThreadContext32>, "GetThreadContext32"},
2614 {0x34, SvcWrap32<WaitForAddress32>, "WaitForAddress32"}, 2614 {0x34, SvcWrap32<WaitForAddress32>, "WaitForAddress32"},
2615 {0x35, SvcWrap32<SignalToAddress32>, "SignalToAddress32"}, 2615 {0x35, SvcWrap32<SignalToAddress32>, "SignalToAddress32"},
2616 {0x36, nullptr, "Unknown"}, 2616 {0x36, SvcWrap32<SynchronizePreemptionState>, "SynchronizePreemptionState32"},
2617 {0x37, nullptr, "Unknown"}, 2617 {0x37, nullptr, "Unknown"},
2618 {0x38, nullptr, "Unknown"}, 2618 {0x38, nullptr, "Unknown"},
2619 {0x39, nullptr, "Unknown"}, 2619 {0x39, nullptr, "Unknown"},
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp
index ed6281772..577bf5c31 100644
--- a/src/input_common/drivers/sdl_driver.cpp
+++ b/src/input_common/drivers/sdl_driver.cpp
@@ -109,8 +109,9 @@ public:
109 109
110 bool HasHDRumble() const { 110 bool HasHDRumble() const {
111 if (sdl_controller) { 111 if (sdl_controller) {
112 return (SDL_GameControllerGetType(sdl_controller.get()) == 112 const auto type = SDL_GameControllerGetType(sdl_controller.get());
113 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO); 113 return (type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO) ||
114 (type == SDL_CONTROLLER_TYPE_PS5);
114 } 115 }
115 return false; 116 return false;
116 } 117 }
diff --git a/src/input_common/drivers/udp_client.cpp b/src/input_common/drivers/udp_client.cpp
index 9aaeb91be..d1cdb1ab2 100644
--- a/src/input_common/drivers/udp_client.cpp
+++ b/src/input_common/drivers/udp_client.cpp
@@ -339,7 +339,7 @@ void UDPClient::StartCommunication(std::size_t client, const std::string& host,
339 } 339 }
340} 340}
341 341
342const PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const { 342PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const {
343 const std::size_t client = pad_index / PADS_PER_CLIENT; 343 const std::size_t client = pad_index / PADS_PER_CLIENT;
344 return { 344 return {
345 .guid = clients[client].uuid, 345 .guid = clients[client].uuid,
@@ -348,9 +348,9 @@ const PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const {
348 }; 348 };
349} 349}
350 350
351const Common::UUID UDPClient::GetHostUUID(const std::string host) const { 351Common::UUID UDPClient::GetHostUUID(const std::string& host) const {
352 const auto ip = boost::asio::ip::address_v4::from_string(host); 352 const auto ip = boost::asio::ip::make_address_v4(host);
353 const auto hex_host = fmt::format("{:06x}", ip.to_ulong()); 353 const auto hex_host = fmt::format("{:06x}", ip.to_uint());
354 return Common::UUID{hex_host}; 354 return Common::UUID{hex_host};
355} 355}
356 356
diff --git a/src/input_common/drivers/udp_client.h b/src/input_common/drivers/udp_client.h
index 61a1fff37..30d7c2682 100644
--- a/src/input_common/drivers/udp_client.h
+++ b/src/input_common/drivers/udp_client.h
@@ -145,8 +145,8 @@ private:
145 void OnPortInfo(Response::PortInfo); 145 void OnPortInfo(Response::PortInfo);
146 void OnPadData(Response::PadData, std::size_t client); 146 void OnPadData(Response::PadData, std::size_t client);
147 void StartCommunication(std::size_t client, const std::string& host, u16 port); 147 void StartCommunication(std::size_t client, const std::string& host, u16 port);
148 const PadIdentifier GetPadIdentifier(std::size_t pad_index) const; 148 PadIdentifier GetPadIdentifier(std::size_t pad_index) const;
149 const Common::UUID GetHostUUID(const std::string host) const; 149 Common::UUID GetHostUUID(const std::string& host) const;
150 150
151 Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const; 151 Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const;
152 152
diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h
index 390581c94..fe2faee5a 100644
--- a/src/input_common/input_engine.h
+++ b/src/input_common/input_engine.h
@@ -16,7 +16,7 @@
16 16
17// Pad Identifier of data source 17// Pad Identifier of data source
18struct PadIdentifier { 18struct PadIdentifier {
19 Common::UUID guid{}; 19 Common::UUID guid{Common::INVALID_UUID};
20 std::size_t port{}; 20 std::size_t port{};
21 std::size_t pad{}; 21 std::size_t pad{};
22 22
@@ -89,7 +89,7 @@ struct UpdateCallback {
89 89
90// Triggered if data changed on the controller and the engine is on configuring mode 90// Triggered if data changed on the controller and the engine is on configuring mode
91struct MappingCallback { 91struct MappingCallback {
92 std::function<void(MappingData)> on_data; 92 std::function<void(const MappingData&)> on_data;
93}; 93};
94 94
95// Input Identifier of data source 95// Input Identifier of data source
diff --git a/src/input_common/input_mapping.cpp b/src/input_common/input_mapping.cpp
index 475257f42..a7a6ad8c2 100644
--- a/src/input_common/input_mapping.cpp
+++ b/src/input_common/input_mapping.cpp
@@ -2,14 +2,13 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included 3// Refer to the license.txt file included
4 4
5#include "common/common_types.h"
6#include "common/settings.h" 5#include "common/settings.h"
7#include "input_common/input_engine.h" 6#include "input_common/input_engine.h"
8#include "input_common/input_mapping.h" 7#include "input_common/input_mapping.h"
9 8
10namespace InputCommon { 9namespace InputCommon {
11 10
12MappingFactory::MappingFactory() {} 11MappingFactory::MappingFactory() = default;
13 12
14void MappingFactory::BeginMapping(Polling::InputType type) { 13void MappingFactory::BeginMapping(Polling::InputType type) {
15 is_enabled = true; 14 is_enabled = true;
@@ -19,7 +18,7 @@ void MappingFactory::BeginMapping(Polling::InputType type) {
19 second_axis = -1; 18 second_axis = -1;
20} 19}
21 20
22[[nodiscard]] const Common::ParamPackage MappingFactory::GetNextInput() { 21Common::ParamPackage MappingFactory::GetNextInput() {
23 Common::ParamPackage input; 22 Common::ParamPackage input;
24 input_queue.Pop(input); 23 input_queue.Pop(input);
25 return input; 24 return input;
@@ -57,7 +56,7 @@ void MappingFactory::StopMapping() {
57void MappingFactory::RegisterButton(const MappingData& data) { 56void MappingFactory::RegisterButton(const MappingData& data) {
58 Common::ParamPackage new_input; 57 Common::ParamPackage new_input;
59 new_input.Set("engine", data.engine); 58 new_input.Set("engine", data.engine);
60 if (data.pad.guid != Common::UUID{}) { 59 if (data.pad.guid.IsValid()) {
61 new_input.Set("guid", data.pad.guid.Format()); 60 new_input.Set("guid", data.pad.guid.Format());
62 } 61 }
63 new_input.Set("port", static_cast<int>(data.pad.port)); 62 new_input.Set("port", static_cast<int>(data.pad.port));
@@ -93,7 +92,7 @@ void MappingFactory::RegisterButton(const MappingData& data) {
93void MappingFactory::RegisterStick(const MappingData& data) { 92void MappingFactory::RegisterStick(const MappingData& data) {
94 Common::ParamPackage new_input; 93 Common::ParamPackage new_input;
95 new_input.Set("engine", data.engine); 94 new_input.Set("engine", data.engine);
96 if (data.pad.guid != Common::UUID{}) { 95 if (data.pad.guid.IsValid()) {
97 new_input.Set("guid", data.pad.guid.Format()); 96 new_input.Set("guid", data.pad.guid.Format());
98 } 97 }
99 new_input.Set("port", static_cast<int>(data.pad.port)); 98 new_input.Set("port", static_cast<int>(data.pad.port));
@@ -138,7 +137,7 @@ void MappingFactory::RegisterStick(const MappingData& data) {
138void MappingFactory::RegisterMotion(const MappingData& data) { 137void MappingFactory::RegisterMotion(const MappingData& data) {
139 Common::ParamPackage new_input; 138 Common::ParamPackage new_input;
140 new_input.Set("engine", data.engine); 139 new_input.Set("engine", data.engine);
141 if (data.pad.guid != Common::UUID{}) { 140 if (data.pad.guid.IsValid()) {
142 new_input.Set("guid", data.pad.guid.Format()); 141 new_input.Set("guid", data.pad.guid.Format());
143 } 142 }
144 new_input.Set("port", static_cast<int>(data.pad.port)); 143 new_input.Set("port", static_cast<int>(data.pad.port));
diff --git a/src/input_common/input_mapping.h b/src/input_common/input_mapping.h
index 93564b5f8..e0dfbc7ad 100644
--- a/src/input_common/input_mapping.h
+++ b/src/input_common/input_mapping.h
@@ -3,8 +3,14 @@
3// Refer to the license.txt file included 3// Refer to the license.txt file included
4 4
5#pragma once 5#pragma once
6
7#include "common/param_package.h"
6#include "common/threadsafe_queue.h" 8#include "common/threadsafe_queue.h"
7 9
10namespace InputCommon::Polling {
11enum class InputType;
12}
13
8namespace InputCommon { 14namespace InputCommon {
9class InputEngine; 15class InputEngine;
10struct MappingData; 16struct MappingData;
@@ -20,7 +26,7 @@ public:
20 void BeginMapping(Polling::InputType type); 26 void BeginMapping(Polling::InputType type);
21 27
22 /// Returns an input event with mapping information from the input_queue 28 /// Returns an input event with mapping information from the input_queue
23 [[nodiscard]] const Common::ParamPackage GetNextInput(); 29 [[nodiscard]] Common::ParamPackage GetNextInput();
24 30
25 /** 31 /**
26 * Registers mapping input data from the driver 32 * Registers mapping input data from the driver
diff --git a/src/input_common/input_poller.cpp b/src/input_common/input_poller.cpp
index 7b370335f..2f3c0735a 100644
--- a/src/input_common/input_poller.cpp
+++ b/src/input_common/input_poller.cpp
@@ -504,9 +504,10 @@ private:
504 504
505class InputFromMotion final : public Common::Input::InputDevice { 505class InputFromMotion final : public Common::Input::InputDevice {
506public: 506public:
507 explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, 507 explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, float gyro_threshold_,
508 InputEngine* input_engine_) 508 InputEngine* input_engine_)
509 : identifier(identifier_), motion_sensor(motion_sensor_), input_engine(input_engine_) { 509 : identifier(identifier_), motion_sensor(motion_sensor_), gyro_threshold(gyro_threshold_),
510 input_engine(input_engine_) {
510 UpdateCallback engine_callback{[this]() { OnChange(); }}; 511 UpdateCallback engine_callback{[this]() { OnChange(); }};
511 const InputIdentifier input_identifier{ 512 const InputIdentifier input_identifier{
512 .identifier = identifier, 513 .identifier = identifier,
@@ -525,8 +526,9 @@ public:
525 const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor); 526 const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor);
526 Common::Input::MotionStatus status{}; 527 Common::Input::MotionStatus status{};
527 const Common::Input::AnalogProperties properties = { 528 const Common::Input::AnalogProperties properties = {
528 .deadzone = 0.001f, 529 .deadzone = 0.0f,
529 .range = 1.0f, 530 .range = 1.0f,
531 .threshold = gyro_threshold,
530 .offset = 0.0f, 532 .offset = 0.0f,
531 }; 533 };
532 status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties}; 534 status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties};
@@ -551,6 +553,7 @@ public:
551private: 553private:
552 const PadIdentifier identifier; 554 const PadIdentifier identifier;
553 const int motion_sensor; 555 const int motion_sensor;
556 const float gyro_threshold;
554 int callback_key; 557 int callback_key;
555 InputEngine* input_engine; 558 InputEngine* input_engine;
556}; 559};
@@ -873,9 +876,11 @@ std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
873 876
874 if (params.Has("motion")) { 877 if (params.Has("motion")) {
875 const auto motion_sensor = params.Get("motion", 0); 878 const auto motion_sensor = params.Get("motion", 0);
879 const auto gyro_threshold = params.Get("threshold", 0.007f);
876 input_engine->PreSetController(identifier); 880 input_engine->PreSetController(identifier);
877 input_engine->PreSetMotion(identifier, motion_sensor); 881 input_engine->PreSetMotion(identifier, motion_sensor);
878 return std::make_unique<InputFromMotion>(identifier, motion_sensor, input_engine.get()); 882 return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
883 input_engine.get());
879 } 884 }
880 885
881 const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f); 886 const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index 940744c5f..a4d7ed645 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -27,7 +27,7 @@ namespace InputCommon {
27struct InputSubsystem::Impl { 27struct InputSubsystem::Impl {
28 void Initialize() { 28 void Initialize() {
29 mapping_factory = std::make_shared<MappingFactory>(); 29 mapping_factory = std::make_shared<MappingFactory>();
30 MappingCallback mapping_callback{[this](MappingData data) { RegisterInput(data); }}; 30 MappingCallback mapping_callback{[this](const MappingData& data) { RegisterInput(data); }};
31 31
32 keyboard = std::make_shared<Keyboard>("keyboard"); 32 keyboard = std::make_shared<Keyboard>("keyboard");
33 keyboard->SetMappingCallback(mapping_callback); 33 keyboard->SetMappingCallback(mapping_callback);
@@ -284,7 +284,7 @@ struct InputSubsystem::Impl {
284#endif 284#endif
285 } 285 }
286 286
287 void RegisterInput(MappingData data) { 287 void RegisterInput(const MappingData& data) {
288 mapping_factory->RegisterInput(data); 288 mapping_factory->RegisterInput(data);
289 } 289 }
290 290
@@ -394,7 +394,7 @@ void InputSubsystem::BeginMapping(Polling::InputType type) {
394 impl->mapping_factory->BeginMapping(type); 394 impl->mapping_factory->BeginMapping(type);
395} 395}
396 396
397const Common::ParamPackage InputSubsystem::GetNextInput() const { 397Common::ParamPackage InputSubsystem::GetNextInput() const {
398 return impl->mapping_factory->GetNextInput(); 398 return impl->mapping_factory->GetNextInput();
399} 399}
400 400
diff --git a/src/input_common/main.h b/src/input_common/main.h
index c6f97f691..baf107e0f 100644
--- a/src/input_common/main.h
+++ b/src/input_common/main.h
@@ -126,7 +126,7 @@ public:
126 void BeginMapping(Polling::InputType type); 126 void BeginMapping(Polling::InputType type);
127 127
128 /// Returns an input event with mapping information. 128 /// Returns an input event with mapping information.
129 [[nodiscard]] const Common::ParamPackage GetNextInput() const; 129 [[nodiscard]] Common::ParamPackage GetNextInput() const;
130 130
131 /// Stop polling from all backends. 131 /// Stop polling from all backends.
132 void StopMapping() const; 132 void StopMapping() const;
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.cpp b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
index 50918317f..08b3a81ce 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
@@ -387,6 +387,14 @@ void SetupSignedNanCapabilities(const Profile& profile, const IR::Program& progr
387 } 387 }
388} 388}
389 389
390void SetupTransformFeedbackCapabilities(EmitContext& ctx, Id main_func) {
391 if (ctx.runtime_info.xfb_varyings.empty()) {
392 return;
393 }
394 ctx.AddCapability(spv::Capability::TransformFeedback);
395 ctx.AddExecutionMode(main_func, spv::ExecutionMode::Xfb);
396}
397
390void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ctx) { 398void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ctx) {
391 if (info.uses_sampled_1d) { 399 if (info.uses_sampled_1d) {
392 ctx.AddCapability(spv::Capability::Sampled1D); 400 ctx.AddCapability(spv::Capability::Sampled1D);
@@ -442,9 +450,6 @@ void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ct
442 if (info.uses_sample_id) { 450 if (info.uses_sample_id) {
443 ctx.AddCapability(spv::Capability::SampleRateShading); 451 ctx.AddCapability(spv::Capability::SampleRateShading);
444 } 452 }
445 if (!ctx.runtime_info.xfb_varyings.empty()) {
446 ctx.AddCapability(spv::Capability::TransformFeedback);
447 }
448 if (info.uses_derivatives) { 453 if (info.uses_derivatives) {
449 ctx.AddCapability(spv::Capability::DerivativeControl); 454 ctx.AddCapability(spv::Capability::DerivativeControl);
450 } 455 }
@@ -484,6 +489,7 @@ std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_in
484 SetupSignedNanCapabilities(profile, program, ctx, main); 489 SetupSignedNanCapabilities(profile, program, ctx, main);
485 } 490 }
486 SetupCapabilities(profile, program.info, ctx); 491 SetupCapabilities(profile, program.info, ctx);
492 SetupTransformFeedbackCapabilities(ctx, main);
487 PatchPhiNodes(program, ctx); 493 PatchPhiNodes(program, ctx);
488 return ctx.Assemble(); 494 return ctx.Assemble();
489} 495}
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_atomic.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_atomic.cpp
index 0d37b405c..46ba52a25 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_atomic.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_atomic.cpp
@@ -74,7 +74,7 @@ Id StorageAtomicU64(EmitContext& ctx, const IR::Value& binding, const IR::Value&
74 const auto [scope, semantics]{AtomicArgs(ctx)}; 74 const auto [scope, semantics]{AtomicArgs(ctx)};
75 return (ctx.*atomic_func)(ctx.U64, pointer, scope, semantics, value); 75 return (ctx.*atomic_func)(ctx.U64, pointer, scope, semantics, value);
76 } 76 }
77 LOG_ERROR(Shader_SPIRV, "Int64 atomics not supported, fallback to non-atomic"); 77 LOG_WARNING(Shader_SPIRV, "Int64 atomics not supported, fallback to non-atomic");
78 const Id pointer{StoragePointer(ctx, ctx.storage_types.U32x2, &StorageDefinitions::U32x2, 78 const Id pointer{StoragePointer(ctx, ctx.storage_types.U32x2, &StorageDefinitions::U32x2,
79 binding, offset, sizeof(u32[2]))}; 79 binding, offset, sizeof(u32[2]))};
80 const Id original_value{ctx.OpBitcast(ctx.U64, ctx.OpLoad(ctx.U32[2], pointer))}; 80 const Id original_value{ctx.OpBitcast(ctx.U64, ctx.OpLoad(ctx.U32[2], pointer))};
@@ -267,7 +267,7 @@ Id EmitStorageAtomicExchange64(EmitContext& ctx, const IR::Value& binding, const
267 const auto [scope, semantics]{AtomicArgs(ctx)}; 267 const auto [scope, semantics]{AtomicArgs(ctx)};
268 return ctx.OpAtomicExchange(ctx.U64, pointer, scope, semantics, value); 268 return ctx.OpAtomicExchange(ctx.U64, pointer, scope, semantics, value);
269 } 269 }
270 LOG_ERROR(Shader_SPIRV, "Int64 atomics not supported, fallback to non-atomic"); 270 LOG_WARNING(Shader_SPIRV, "Int64 atomics not supported, fallback to non-atomic");
271 const Id pointer{StoragePointer(ctx, ctx.storage_types.U32x2, &StorageDefinitions::U32x2, 271 const Id pointer{StoragePointer(ctx, ctx.storage_types.U32x2, &StorageDefinitions::U32x2,
272 binding, offset, sizeof(u32[2]))}; 272 binding, offset, sizeof(u32[2]))};
273 const Id original{ctx.OpBitcast(ctx.U64, ctx.OpLoad(ctx.U32[2], pointer))}; 273 const Id original{ctx.OpBitcast(ctx.U64, ctx.OpLoad(ctx.U32[2], pointer))};
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/video_minimum_maximum.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/video_minimum_maximum.cpp
index 78869601f..4851b0b8d 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/video_minimum_maximum.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/video_minimum_maximum.cpp
@@ -57,16 +57,6 @@ void TranslatorVisitor::VMNMX(u64 insn) {
57 if (vmnmx.sat != 0) { 57 if (vmnmx.sat != 0) {
58 throw NotImplementedException("VMNMX SAT"); 58 throw NotImplementedException("VMNMX SAT");
59 } 59 }
60 // Selectors were shown to default to 2 in unit tests
61 if (vmnmx.src_a_selector != 2) {
62 throw NotImplementedException("VMNMX Selector {}", vmnmx.src_a_selector.Value());
63 }
64 if (vmnmx.src_b_selector != 2) {
65 throw NotImplementedException("VMNMX Selector {}", vmnmx.src_b_selector.Value());
66 }
67 if (vmnmx.src_a_width != VideoWidth::Word) {
68 throw NotImplementedException("VMNMX Source Width {}", vmnmx.src_a_width.Value());
69 }
70 60
71 const bool is_b_imm{vmnmx.is_src_b_reg == 0}; 61 const bool is_b_imm{vmnmx.is_src_b_reg == 0};
72 const IR::U32 src_a{GetReg8(insn)}; 62 const IR::U32 src_a{GetReg8(insn)};
@@ -76,10 +66,14 @@ void TranslatorVisitor::VMNMX(u64 insn) {
76 const VideoWidth a_width{vmnmx.src_a_width}; 66 const VideoWidth a_width{vmnmx.src_a_width};
77 const VideoWidth b_width{GetVideoSourceWidth(vmnmx.src_b_width, is_b_imm)}; 67 const VideoWidth b_width{GetVideoSourceWidth(vmnmx.src_b_width, is_b_imm)};
78 68
69 const u32 a_selector{static_cast<u32>(vmnmx.src_a_selector)};
70 // Immediate values can't have a selector
71 const u32 b_selector{is_b_imm ? 0U : static_cast<u32>(vmnmx.src_b_selector)};
72
79 const bool src_a_signed{vmnmx.src_a_sign != 0}; 73 const bool src_a_signed{vmnmx.src_a_sign != 0};
80 const bool src_b_signed{vmnmx.src_b_sign != 0}; 74 const bool src_b_signed{vmnmx.src_b_sign != 0};
81 const IR::U32 op_a{ExtractVideoOperandValue(ir, src_a, a_width, 0, src_a_signed)}; 75 const IR::U32 op_a{ExtractVideoOperandValue(ir, src_a, a_width, a_selector, src_a_signed)};
82 const IR::U32 op_b{ExtractVideoOperandValue(ir, src_b, b_width, 0, src_b_signed)}; 76 const IR::U32 op_b{ExtractVideoOperandValue(ir, src_b, b_width, b_selector, src_b_signed)};
83 77
84 // First operation's sign is only dependent on operand b's sign 78 // First operation's sign is only dependent on operand b's sign
85 const bool op_1_signed{src_b_signed}; 79 const bool op_1_signed{src_b_signed};
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.h b/src/shader_recompiler/frontend/maxwell/translate_program.h
index cd535f20d..eac83da9d 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.h
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.h
@@ -21,7 +21,6 @@ namespace Shader::Maxwell {
21[[nodiscard]] IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b, 21[[nodiscard]] IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
22 Environment& env_vertex_b); 22 Environment& env_vertex_b);
23 23
24[[nodiscard]] void ConvertLegacyToGeneric(IR::Program& program, 24void ConvertLegacyToGeneric(IR::Program& program, const RuntimeInfo& runtime_info);
25 const Shader::RuntimeInfo& runtime_info);
26 25
27} // namespace Shader::Maxwell 26} // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp
index a78c469be..b6a20f904 100644
--- a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp
+++ b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp
@@ -688,7 +688,7 @@ void VisitUsages(Info& info, IR::Inst& inst) {
688 case IR::Opcode::StorageAtomicAnd64: 688 case IR::Opcode::StorageAtomicAnd64:
689 case IR::Opcode::StorageAtomicOr64: 689 case IR::Opcode::StorageAtomicOr64:
690 case IR::Opcode::StorageAtomicXor64: 690 case IR::Opcode::StorageAtomicXor64:
691 info.used_storage_buffer_types |= IR::Type::U64; 691 info.used_storage_buffer_types |= IR::Type::U64 | IR::Type::U32x2;
692 info.uses_int64_bit_atomics = true; 692 info.uses_int64_bit_atomics = true;
693 break; 693 break;
694 case IR::Opcode::BindlessImageAtomicIAdd32: 694 case IR::Opcode::BindlessImageAtomicIAdd32:
diff --git a/src/video_core/buffer_cache/buffer_cache.h b/src/video_core/buffer_cache/buffer_cache.h
index 43bed63ac..fa26eb8b0 100644
--- a/src/video_core/buffer_cache/buffer_cache.h
+++ b/src/video_core/buffer_cache/buffer_cache.h
@@ -131,6 +131,8 @@ public:
131 131
132 void DownloadMemory(VAddr cpu_addr, u64 size); 132 void DownloadMemory(VAddr cpu_addr, u64 size);
133 133
134 bool InlineMemory(VAddr dest_address, size_t copy_size, std::span<u8> inlined_buffer);
135
134 void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size); 136 void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size);
135 137
136 void DisableGraphicsUniformBuffer(size_t stage, u32 index); 138 void DisableGraphicsUniformBuffer(size_t stage, u32 index);
@@ -808,6 +810,8 @@ void BufferCache<P>::CommitAsyncFlushesHigh() {
808 return; 810 return;
809 } 811 }
810 MICROPROFILE_SCOPE(GPU_DownloadMemory); 812 MICROPROFILE_SCOPE(GPU_DownloadMemory);
813 const bool is_accuracy_normal =
814 Settings::values.gpu_accuracy.GetValue() == Settings::GPUAccuracy::Normal;
811 815
812 boost::container::small_vector<std::pair<BufferCopy, BufferId>, 1> downloads; 816 boost::container::small_vector<std::pair<BufferCopy, BufferId>, 1> downloads;
813 u64 total_size_bytes = 0; 817 u64 total_size_bytes = 0;
@@ -819,6 +823,9 @@ void BufferCache<P>::CommitAsyncFlushesHigh() {
819 ForEachBufferInRange(cpu_addr, size, [&](BufferId buffer_id, Buffer& buffer) { 823 ForEachBufferInRange(cpu_addr, size, [&](BufferId buffer_id, Buffer& buffer) {
820 buffer.ForEachDownloadRangeAndClear( 824 buffer.ForEachDownloadRangeAndClear(
821 cpu_addr, size, [&](u64 range_offset, u64 range_size) { 825 cpu_addr, size, [&](u64 range_offset, u64 range_size) {
826 if (is_accuracy_normal) {
827 return;
828 }
822 const VAddr buffer_addr = buffer.CpuAddr(); 829 const VAddr buffer_addr = buffer.CpuAddr();
823 const auto add_download = [&](VAddr start, VAddr end) { 830 const auto add_download = [&](VAddr start, VAddr end) {
824 const u64 new_offset = start - buffer_addr; 831 const u64 new_offset = start - buffer_addr;
@@ -1417,10 +1424,8 @@ void BufferCache<P>::MarkWrittenBuffer(BufferId buffer_id, VAddr cpu_addr, u32 s
1417 const IntervalType base_interval{cpu_addr, cpu_addr + size}; 1424 const IntervalType base_interval{cpu_addr, cpu_addr + size};
1418 common_ranges.add(base_interval); 1425 common_ranges.add(base_interval);
1419 1426
1420 const bool is_accuracy_high =
1421 Settings::values.gpu_accuracy.GetValue() == Settings::GPUAccuracy::High;
1422 const bool is_async = Settings::values.use_asynchronous_gpu_emulation.GetValue(); 1427 const bool is_async = Settings::values.use_asynchronous_gpu_emulation.GetValue();
1423 if (!is_async && !is_accuracy_high) { 1428 if (!is_async) {
1424 return; 1429 return;
1425 } 1430 }
1426 uncommitted_ranges.add(base_interval); 1431 uncommitted_ranges.add(base_interval);
@@ -1474,6 +1479,8 @@ typename BufferCache<P>::OverlapResult BufferCache<P>::ResolveOverlaps(VAddr cpu
1474 // When this memory region has been joined a bunch of times, we assume it's being used 1479 // When this memory region has been joined a bunch of times, we assume it's being used
1475 // as a stream buffer. Increase the size to skip constantly recreating buffers. 1480 // as a stream buffer. Increase the size to skip constantly recreating buffers.
1476 has_stream_leap = true; 1481 has_stream_leap = true;
1482 begin -= PAGE_SIZE * 256;
1483 cpu_addr = begin;
1477 end += PAGE_SIZE * 256; 1484 end += PAGE_SIZE * 256;
1478 } 1485 }
1479 } 1486 }
@@ -1642,6 +1649,42 @@ void BufferCache<P>::MappedUploadMemory(Buffer& buffer, u64 total_size_bytes,
1642} 1649}
1643 1650
1644template <class P> 1651template <class P>
1652bool BufferCache<P>::InlineMemory(VAddr dest_address, size_t copy_size,
1653 std::span<u8> inlined_buffer) {
1654 const bool is_dirty = IsRegionRegistered(dest_address, copy_size);
1655 if (!is_dirty) {
1656 return false;
1657 }
1658 if (!IsRegionGpuModified(dest_address, copy_size)) {
1659 return false;
1660 }
1661
1662 const IntervalType subtract_interval{dest_address, dest_address + copy_size};
1663 ClearDownload(subtract_interval);
1664 common_ranges.subtract(subtract_interval);
1665
1666 BufferId buffer_id = FindBuffer(dest_address, static_cast<u32>(copy_size));
1667 auto& buffer = slot_buffers[buffer_id];
1668 SynchronizeBuffer(buffer, dest_address, static_cast<u32>(copy_size));
1669
1670 if constexpr (USE_MEMORY_MAPS) {
1671 std::array copies{BufferCopy{
1672 .src_offset = 0,
1673 .dst_offset = buffer.Offset(dest_address),
1674 .size = copy_size,
1675 }};
1676 auto upload_staging = runtime.UploadStagingBuffer(copy_size);
1677 u8* const src_pointer = upload_staging.mapped_span.data();
1678 std::memcpy(src_pointer, inlined_buffer.data(), copy_size);
1679 runtime.CopyBuffer(buffer, upload_staging.buffer, copies);
1680 } else {
1681 buffer.ImmediateUpload(buffer.Offset(dest_address), inlined_buffer.first(copy_size));
1682 }
1683
1684 return true;
1685}
1686
1687template <class P>
1645void BufferCache<P>::DownloadBufferMemory(Buffer& buffer) { 1688void BufferCache<P>::DownloadBufferMemory(Buffer& buffer) {
1646 DownloadBufferMemory(buffer, buffer.CpuAddr(), buffer.SizeBytes()); 1689 DownloadBufferMemory(buffer, buffer.CpuAddr(), buffer.SizeBytes());
1647} 1690}
diff --git a/src/video_core/engines/engine_upload.cpp b/src/video_core/engines/engine_upload.cpp
index 71d7e1473..351b110fe 100644
--- a/src/video_core/engines/engine_upload.cpp
+++ b/src/video_core/engines/engine_upload.cpp
@@ -7,6 +7,7 @@
7#include "common/assert.h" 7#include "common/assert.h"
8#include "video_core/engines/engine_upload.h" 8#include "video_core/engines/engine_upload.h"
9#include "video_core/memory_manager.h" 9#include "video_core/memory_manager.h"
10#include "video_core/rasterizer_interface.h"
10#include "video_core/textures/decoders.h" 11#include "video_core/textures/decoders.h"
11 12
12namespace Tegra::Engines::Upload { 13namespace Tegra::Engines::Upload {
@@ -16,6 +17,10 @@ State::State(MemoryManager& memory_manager_, Registers& regs_)
16 17
17State::~State() = default; 18State::~State() = default;
18 19
20void State::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
21 rasterizer = rasterizer_;
22}
23
19void State::ProcessExec(const bool is_linear_) { 24void State::ProcessExec(const bool is_linear_) {
20 write_offset = 0; 25 write_offset = 0;
21 copy_size = regs.line_length_in * regs.line_count; 26 copy_size = regs.line_length_in * regs.line_count;
@@ -32,7 +37,7 @@ void State::ProcessData(const u32 data, const bool is_last_call) {
32 } 37 }
33 const GPUVAddr address{regs.dest.Address()}; 38 const GPUVAddr address{regs.dest.Address()};
34 if (is_linear) { 39 if (is_linear) {
35 memory_manager.WriteBlock(address, inner_buffer.data(), copy_size); 40 rasterizer->AccelerateInlineToMemory(address, copy_size, inner_buffer);
36 } else { 41 } else {
37 UNIMPLEMENTED_IF(regs.dest.z != 0); 42 UNIMPLEMENTED_IF(regs.dest.z != 0);
38 UNIMPLEMENTED_IF(regs.dest.depth != 1); 43 UNIMPLEMENTED_IF(regs.dest.depth != 1);
diff --git a/src/video_core/engines/engine_upload.h b/src/video_core/engines/engine_upload.h
index 1c7f1effa..c9c5ec8c3 100644
--- a/src/video_core/engines/engine_upload.h
+++ b/src/video_core/engines/engine_upload.h
@@ -12,6 +12,10 @@ namespace Tegra {
12class MemoryManager; 12class MemoryManager;
13} 13}
14 14
15namespace VideoCore {
16class RasterizerInterface;
17}
18
15namespace Tegra::Engines::Upload { 19namespace Tegra::Engines::Upload {
16 20
17struct Registers { 21struct Registers {
@@ -60,6 +64,9 @@ public:
60 void ProcessExec(bool is_linear_); 64 void ProcessExec(bool is_linear_);
61 void ProcessData(u32 data, bool is_last_call); 65 void ProcessData(u32 data, bool is_last_call);
62 66
67 /// Binds a rasterizer to this engine.
68 void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
69
63private: 70private:
64 u32 write_offset = 0; 71 u32 write_offset = 0;
65 u32 copy_size = 0; 72 u32 copy_size = 0;
@@ -68,6 +75,7 @@ private:
68 bool is_linear = false; 75 bool is_linear = false;
69 Registers& regs; 76 Registers& regs;
70 MemoryManager& memory_manager; 77 MemoryManager& memory_manager;
78 VideoCore::RasterizerInterface* rasterizer = nullptr;
71}; 79};
72 80
73} // namespace Tegra::Engines::Upload 81} // namespace Tegra::Engines::Upload
diff --git a/src/video_core/engines/kepler_compute.cpp b/src/video_core/engines/kepler_compute.cpp
index 492b4c5a3..5a1c12076 100644
--- a/src/video_core/engines/kepler_compute.cpp
+++ b/src/video_core/engines/kepler_compute.cpp
@@ -22,6 +22,7 @@ KeplerCompute::~KeplerCompute() = default;
22 22
23void KeplerCompute::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) { 23void KeplerCompute::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
24 rasterizer = rasterizer_; 24 rasterizer = rasterizer_;
25 upload_state.BindRasterizer(rasterizer);
25} 26}
26 27
27void KeplerCompute::CallMethod(u32 method, u32 method_argument, bool is_last_call) { 28void KeplerCompute::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
diff --git a/src/video_core/engines/kepler_memory.cpp b/src/video_core/engines/kepler_memory.cpp
index 560551157..8aed16caa 100644
--- a/src/video_core/engines/kepler_memory.cpp
+++ b/src/video_core/engines/kepler_memory.cpp
@@ -19,6 +19,10 @@ KeplerMemory::KeplerMemory(Core::System& system_, MemoryManager& memory_manager)
19 19
20KeplerMemory::~KeplerMemory() = default; 20KeplerMemory::~KeplerMemory() = default;
21 21
22void KeplerMemory::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
23 upload_state.BindRasterizer(rasterizer_);
24}
25
22void KeplerMemory::CallMethod(u32 method, u32 method_argument, bool is_last_call) { 26void KeplerMemory::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
23 ASSERT_MSG(method < Regs::NUM_REGS, 27 ASSERT_MSG(method < Regs::NUM_REGS,
24 "Invalid KeplerMemory register, increase the size of the Regs structure"); 28 "Invalid KeplerMemory register, increase the size of the Regs structure");
diff --git a/src/video_core/engines/kepler_memory.h b/src/video_core/engines/kepler_memory.h
index 0d8ea09a9..949e2fae1 100644
--- a/src/video_core/engines/kepler_memory.h
+++ b/src/video_core/engines/kepler_memory.h
@@ -22,6 +22,10 @@ namespace Tegra {
22class MemoryManager; 22class MemoryManager;
23} 23}
24 24
25namespace VideoCore {
26class RasterizerInterface;
27}
28
25namespace Tegra::Engines { 29namespace Tegra::Engines {
26 30
27/** 31/**
@@ -38,6 +42,9 @@ public:
38 explicit KeplerMemory(Core::System& system_, MemoryManager& memory_manager); 42 explicit KeplerMemory(Core::System& system_, MemoryManager& memory_manager);
39 ~KeplerMemory() override; 43 ~KeplerMemory() override;
40 44
45 /// Binds a rasterizer to this engine.
46 void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
47
41 /// Write the value to the register identified by method. 48 /// Write the value to the register identified by method.
42 void CallMethod(u32 method, u32 method_argument, bool is_last_call) override; 49 void CallMethod(u32 method, u32 method_argument, bool is_last_call) override;
43 50
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index b18b8a02a..5d6d217bb 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -31,6 +31,7 @@ Maxwell3D::~Maxwell3D() = default;
31 31
32void Maxwell3D::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) { 32void Maxwell3D::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
33 rasterizer = rasterizer_; 33 rasterizer = rasterizer_;
34 upload_state.BindRasterizer(rasterizer_);
34} 35}
35 36
36void Maxwell3D::InitializeRegisterDefaults() { 37void Maxwell3D::InitializeRegisterDefaults() {
@@ -240,7 +241,7 @@ void Maxwell3D::CallMacroMethod(u32 method, const std::vector<u32>& parameters)
240 ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size()); 241 ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size());
241 242
242 // Execute the current macro. 243 // Execute the current macro.
243 macro_engine->Execute(*this, macro_positions[entry], parameters); 244 macro_engine->Execute(macro_positions[entry], parameters);
244 if (mme_draw.current_mode != MMEDrawMode::Undefined) { 245 if (mme_draw.current_mode != MMEDrawMode::Undefined) {
245 FlushMMEInlineDraw(); 246 FlushMMEInlineDraw();
246 } 247 }
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h
index f22342dfb..dc9df6c8b 100644
--- a/src/video_core/engines/maxwell_3d.h
+++ b/src/video_core/engines/maxwell_3d.h
@@ -1557,7 +1557,8 @@ private:
1557 1557
1558 static constexpr u32 null_cb_data = 0xFFFFFFFF; 1558 static constexpr u32 null_cb_data = 0xFFFFFFFF;
1559 struct CBDataState { 1559 struct CBDataState {
1560 std::array<std::array<u32, 0x4000>, 16> buffer; 1560 static constexpr size_t inline_size = 0x4000;
1561 std::array<std::array<u32, inline_size>, 16> buffer;
1561 u32 current{null_cb_data}; 1562 u32 current{null_cb_data};
1562 u32 id{null_cb_data}; 1563 u32 id{null_cb_data};
1563 u32 start_pos{}; 1564 u32 start_pos{};
diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp
index 705765c99..ba9ba082f 100644
--- a/src/video_core/gpu.cpp
+++ b/src/video_core/gpu.cpp
@@ -59,6 +59,7 @@ struct GPU::Impl {
59 maxwell_3d->BindRasterizer(rasterizer); 59 maxwell_3d->BindRasterizer(rasterizer);
60 fermi_2d->BindRasterizer(rasterizer); 60 fermi_2d->BindRasterizer(rasterizer);
61 kepler_compute->BindRasterizer(rasterizer); 61 kepler_compute->BindRasterizer(rasterizer);
62 kepler_memory->BindRasterizer(rasterizer);
62 maxwell_dma->BindRasterizer(rasterizer); 63 maxwell_dma->BindRasterizer(rasterizer);
63 } 64 }
64 65
@@ -502,8 +503,13 @@ struct GPU::Impl {
502 case BufferMethods::SemaphoreAddressHigh: 503 case BufferMethods::SemaphoreAddressHigh:
503 case BufferMethods::SemaphoreAddressLow: 504 case BufferMethods::SemaphoreAddressLow:
504 case BufferMethods::SemaphoreSequence: 505 case BufferMethods::SemaphoreSequence:
506 break;
505 case BufferMethods::UnkCacheFlush: 507 case BufferMethods::UnkCacheFlush:
508 rasterizer->SyncGuestHost();
509 break;
506 case BufferMethods::WrcacheFlush: 510 case BufferMethods::WrcacheFlush:
511 rasterizer->SignalReference();
512 break;
507 case BufferMethods::FenceValue: 513 case BufferMethods::FenceValue:
508 break; 514 break;
509 case BufferMethods::RefCnt: 515 case BufferMethods::RefCnt:
@@ -513,7 +519,7 @@ struct GPU::Impl {
513 ProcessFenceActionMethod(); 519 ProcessFenceActionMethod();
514 break; 520 break;
515 case BufferMethods::WaitForInterrupt: 521 case BufferMethods::WaitForInterrupt:
516 ProcessWaitForInterruptMethod(); 522 rasterizer->WaitForIdle();
517 break; 523 break;
518 case BufferMethods::SemaphoreTrigger: { 524 case BufferMethods::SemaphoreTrigger: {
519 ProcessSemaphoreTriggerMethod(); 525 ProcessSemaphoreTriggerMethod();
diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h
index 3188b83ed..26b8ea233 100644
--- a/src/video_core/gpu.h
+++ b/src/video_core/gpu.h
@@ -12,9 +12,6 @@
12#include "video_core/framebuffer_config.h" 12#include "video_core/framebuffer_config.h"
13 13
14namespace Core { 14namespace Core {
15namespace Frontend {
16class EmuWindow;
17}
18class System; 15class System;
19} // namespace Core 16} // namespace Core
20 17
@@ -25,7 +22,6 @@ class ShaderNotify;
25 22
26namespace Tegra { 23namespace Tegra {
27class DmaPusher; 24class DmaPusher;
28class CDmaPusher;
29struct CommandList; 25struct CommandList;
30 26
31enum class RenderTargetFormat : u32 { 27enum class RenderTargetFormat : u32 {
@@ -88,15 +84,9 @@ enum class DepthFormat : u32 {
88 D32_FLOAT_S8X24_UINT = 0x19, 84 D32_FLOAT_S8X24_UINT = 0x19,
89}; 85};
90 86
91struct CommandListHeader;
92class DebugContext;
93
94namespace Engines { 87namespace Engines {
95class Fermi2D;
96class Maxwell3D; 88class Maxwell3D;
97class MaxwellDMA;
98class KeplerCompute; 89class KeplerCompute;
99class KeplerMemory;
100} // namespace Engines 90} // namespace Engines
101 91
102enum class EngineID { 92enum class EngineID {
@@ -190,12 +180,6 @@ public:
190 /// Returns a const reference to the GPU DMA pusher. 180 /// Returns a const reference to the GPU DMA pusher.
191 [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const; 181 [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const;
192 182
193 /// Returns a reference to the GPU CDMA pusher.
194 [[nodiscard]] Tegra::CDmaPusher& CDmaPusher();
195
196 /// Returns a const reference to the GPU CDMA pusher.
197 [[nodiscard]] const Tegra::CDmaPusher& CDmaPusher() const;
198
199 /// Returns a reference to the underlying renderer. 183 /// Returns a reference to the underlying renderer.
200 [[nodiscard]] VideoCore::RendererBase& Renderer(); 184 [[nodiscard]] VideoCore::RendererBase& Renderer();
201 185
diff --git a/src/video_core/macro/macro.cpp b/src/video_core/macro/macro.cpp
index d7fabe605..0aeda4ce8 100644
--- a/src/video_core/macro/macro.cpp
+++ b/src/video_core/macro/macro.cpp
@@ -2,12 +2,13 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <cstring>
5#include <optional> 6#include <optional>
7
6#include <boost/container_hash/hash.hpp> 8#include <boost/container_hash/hash.hpp>
9
7#include "common/assert.h" 10#include "common/assert.h"
8#include "common/logging/log.h"
9#include "common/settings.h" 11#include "common/settings.h"
10#include "video_core/engines/maxwell_3d.h"
11#include "video_core/macro/macro.h" 12#include "video_core/macro/macro.h"
12#include "video_core/macro/macro_hle.h" 13#include "video_core/macro/macro_hle.h"
13#include "video_core/macro/macro_interpreter.h" 14#include "video_core/macro/macro_interpreter.h"
@@ -24,8 +25,7 @@ void MacroEngine::AddCode(u32 method, u32 data) {
24 uploaded_macro_code[method].push_back(data); 25 uploaded_macro_code[method].push_back(data);
25} 26}
26 27
27void MacroEngine::Execute(Engines::Maxwell3D& maxwell3d, u32 method, 28void MacroEngine::Execute(u32 method, const std::vector<u32>& parameters) {
28 const std::vector<u32>& parameters) {
29 auto compiled_macro = macro_cache.find(method); 29 auto compiled_macro = macro_cache.find(method);
30 if (compiled_macro != macro_cache.end()) { 30 if (compiled_macro != macro_cache.end()) {
31 const auto& cache_info = compiled_macro->second; 31 const auto& cache_info = compiled_macro->second;
@@ -66,10 +66,9 @@ void MacroEngine::Execute(Engines::Maxwell3D& maxwell3d, u32 method,
66 cache_info.lle_program = Compile(code); 66 cache_info.lle_program = Compile(code);
67 } 67 }
68 68
69 auto hle_program = hle_macros->GetHLEProgram(cache_info.hash); 69 if (auto hle_program = hle_macros->GetHLEProgram(cache_info.hash)) {
70 if (hle_program.has_value()) {
71 cache_info.has_hle_program = true; 70 cache_info.has_hle_program = true;
72 cache_info.hle_program = std::move(hle_program.value()); 71 cache_info.hle_program = std::move(hle_program);
73 cache_info.hle_program->Execute(parameters, method); 72 cache_info.hle_program->Execute(parameters, method);
74 } else { 73 } else {
75 cache_info.lle_program->Execute(parameters, method); 74 cache_info.lle_program->Execute(parameters, method);
diff --git a/src/video_core/macro/macro.h b/src/video_core/macro/macro.h
index 31ee3440a..7aaa49286 100644
--- a/src/video_core/macro/macro.h
+++ b/src/video_core/macro/macro.h
@@ -119,7 +119,7 @@ public:
119 void AddCode(u32 method, u32 data); 119 void AddCode(u32 method, u32 data);
120 120
121 // Compiles the macro if its not in the cache, and executes the compiled macro 121 // Compiles the macro if its not in the cache, and executes the compiled macro
122 void Execute(Engines::Maxwell3D& maxwell3d, u32 method, const std::vector<u32>& parameters); 122 void Execute(u32 method, const std::vector<u32>& parameters);
123 123
124protected: 124protected:
125 virtual std::unique_ptr<CachedMacro> Compile(const std::vector<u32>& code) = 0; 125 virtual std::unique_ptr<CachedMacro> Compile(const std::vector<u32>& code) = 0;
diff --git a/src/video_core/macro/macro_hle.cpp b/src/video_core/macro/macro_hle.cpp
index 70ac7c620..900ad23c9 100644
--- a/src/video_core/macro/macro_hle.cpp
+++ b/src/video_core/macro/macro_hle.cpp
@@ -5,12 +5,15 @@
5#include <array> 5#include <array>
6#include <vector> 6#include <vector>
7#include "video_core/engines/maxwell_3d.h" 7#include "video_core/engines/maxwell_3d.h"
8#include "video_core/macro/macro.h"
8#include "video_core/macro/macro_hle.h" 9#include "video_core/macro/macro_hle.h"
9#include "video_core/rasterizer_interface.h" 10#include "video_core/rasterizer_interface.h"
10 11
11namespace Tegra { 12namespace Tegra {
12
13namespace { 13namespace {
14
15using HLEFunction = void (*)(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters);
16
14// HLE'd functions 17// HLE'd functions
15void HLE_771BB18C62444DA0(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) { 18void HLE_771BB18C62444DA0(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
16 const u32 instance_count = parameters[2] & maxwell3d.GetRegisterValue(0xD1B); 19 const u32 instance_count = parameters[2] & maxwell3d.GetRegisterValue(0xD1B);
@@ -77,7 +80,6 @@ void HLE_0217920100488FF7(Engines::Maxwell3D& maxwell3d, const std::vector<u32>&
77 maxwell3d.CallMethodFromMME(0x8e5, 0x0); 80 maxwell3d.CallMethodFromMME(0x8e5, 0x0);
78 maxwell3d.mme_draw.current_mode = Engines::Maxwell3D::MMEDrawMode::Undefined; 81 maxwell3d.mme_draw.current_mode = Engines::Maxwell3D::MMEDrawMode::Undefined;
79} 82}
80} // Anonymous namespace
81 83
82constexpr std::array<std::pair<u64, HLEFunction>, 3> hle_funcs{{ 84constexpr std::array<std::pair<u64, HLEFunction>, 3> hle_funcs{{
83 {0x771BB18C62444DA0, &HLE_771BB18C62444DA0}, 85 {0x771BB18C62444DA0, &HLE_771BB18C62444DA0},
@@ -85,25 +87,31 @@ constexpr std::array<std::pair<u64, HLEFunction>, 3> hle_funcs{{
85 {0x0217920100488FF7, &HLE_0217920100488FF7}, 87 {0x0217920100488FF7, &HLE_0217920100488FF7},
86}}; 88}};
87 89
90class HLEMacroImpl final : public CachedMacro {
91public:
92 explicit HLEMacroImpl(Engines::Maxwell3D& maxwell3d_, HLEFunction func_)
93 : maxwell3d{maxwell3d_}, func{func_} {}
94
95 void Execute(const std::vector<u32>& parameters, u32 method) override {
96 func(maxwell3d, parameters);
97 }
98
99private:
100 Engines::Maxwell3D& maxwell3d;
101 HLEFunction func;
102};
103} // Anonymous namespace
104
88HLEMacro::HLEMacro(Engines::Maxwell3D& maxwell3d_) : maxwell3d{maxwell3d_} {} 105HLEMacro::HLEMacro(Engines::Maxwell3D& maxwell3d_) : maxwell3d{maxwell3d_} {}
89HLEMacro::~HLEMacro() = default; 106HLEMacro::~HLEMacro() = default;
90 107
91std::optional<std::unique_ptr<CachedMacro>> HLEMacro::GetHLEProgram(u64 hash) const { 108std::unique_ptr<CachedMacro> HLEMacro::GetHLEProgram(u64 hash) const {
92 const auto it = std::find_if(hle_funcs.cbegin(), hle_funcs.cend(), 109 const auto it = std::find_if(hle_funcs.cbegin(), hle_funcs.cend(),
93 [hash](const auto& pair) { return pair.first == hash; }); 110 [hash](const auto& pair) { return pair.first == hash; });
94 if (it == hle_funcs.end()) { 111 if (it == hle_funcs.end()) {
95 return std::nullopt; 112 return nullptr;
96 } 113 }
97 return std::make_unique<HLEMacroImpl>(maxwell3d, it->second); 114 return std::make_unique<HLEMacroImpl>(maxwell3d, it->second);
98} 115}
99 116
100HLEMacroImpl::~HLEMacroImpl() = default;
101
102HLEMacroImpl::HLEMacroImpl(Engines::Maxwell3D& maxwell3d_, HLEFunction func_)
103 : maxwell3d{maxwell3d_}, func{func_} {}
104
105void HLEMacroImpl::Execute(const std::vector<u32>& parameters, u32 method) {
106 func(maxwell3d, parameters);
107}
108
109} // namespace Tegra 117} // namespace Tegra
diff --git a/src/video_core/macro/macro_hle.h b/src/video_core/macro/macro_hle.h
index cb3bd1600..b86ba84a1 100644
--- a/src/video_core/macro/macro_hle.h
+++ b/src/video_core/macro/macro_hle.h
@@ -5,10 +5,7 @@
5#pragma once 5#pragma once
6 6
7#include <memory> 7#include <memory>
8#include <optional>
9#include <vector>
10#include "common/common_types.h" 8#include "common/common_types.h"
11#include "video_core/macro/macro.h"
12 9
13namespace Tegra { 10namespace Tegra {
14 11
@@ -16,29 +13,17 @@ namespace Engines {
16class Maxwell3D; 13class Maxwell3D;
17} 14}
18 15
19using HLEFunction = void (*)(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters);
20
21class HLEMacro { 16class HLEMacro {
22public: 17public:
23 explicit HLEMacro(Engines::Maxwell3D& maxwell3d_); 18 explicit HLEMacro(Engines::Maxwell3D& maxwell3d_);
24 ~HLEMacro(); 19 ~HLEMacro();
25 20
26 std::optional<std::unique_ptr<CachedMacro>> GetHLEProgram(u64 hash) const; 21 // Allocates and returns a cached macro if the hash matches a known function.
27 22 // Returns nullptr otherwise.
28private: 23 [[nodiscard]] std::unique_ptr<CachedMacro> GetHLEProgram(u64 hash) const;
29 Engines::Maxwell3D& maxwell3d;
30};
31
32class HLEMacroImpl : public CachedMacro {
33public:
34 explicit HLEMacroImpl(Engines::Maxwell3D& maxwell3d, HLEFunction func);
35 ~HLEMacroImpl();
36
37 void Execute(const std::vector<u32>& parameters, u32 method) override;
38 24
39private: 25private:
40 Engines::Maxwell3D& maxwell3d; 26 Engines::Maxwell3D& maxwell3d;
41 HLEFunction func;
42}; 27};
43 28
44} // namespace Tegra 29} // namespace Tegra
diff --git a/src/video_core/macro/macro_interpreter.cpp b/src/video_core/macro/macro_interpreter.cpp
index 8da26fd59..fba755448 100644
--- a/src/video_core/macro/macro_interpreter.cpp
+++ b/src/video_core/macro/macro_interpreter.cpp
@@ -2,6 +2,9 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <array>
6#include <optional>
7
5#include "common/assert.h" 8#include "common/assert.h"
6#include "common/logging/log.h" 9#include "common/logging/log.h"
7#include "common/microprofile.h" 10#include "common/microprofile.h"
@@ -11,16 +14,81 @@
11MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192)); 14MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192));
12 15
13namespace Tegra { 16namespace Tegra {
14MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d_) 17namespace {
15 : MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {} 18class MacroInterpreterImpl final : public CachedMacro {
19public:
20 explicit MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d_, const std::vector<u32>& code_)
21 : maxwell3d{maxwell3d_}, code{code_} {}
16 22
17std::unique_ptr<CachedMacro> MacroInterpreter::Compile(const std::vector<u32>& code) { 23 void Execute(const std::vector<u32>& params, u32 method) override;
18 return std::make_unique<MacroInterpreterImpl>(maxwell3d, code); 24
19} 25private:
26 /// Resets the execution engine state, zeroing registers, etc.
27 void Reset();
28
29 /**
30 * Executes a single macro instruction located at the current program counter. Returns whether
31 * the interpreter should keep running.
32 *
33 * @param is_delay_slot Whether the current step is being executed due to a delay slot in a
34 * previous instruction.
35 */
36 bool Step(bool is_delay_slot);
37
38 /// Calculates the result of an ALU operation. src_a OP src_b;
39 u32 GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b);
40
41 /// Performs the result operation on the input result and stores it in the specified register
42 /// (if necessary).
43 void ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result);
44
45 /// Evaluates the branch condition and returns whether the branch should be taken or not.
46 bool EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const;
47
48 /// Reads an opcode at the current program counter location.
49 Macro::Opcode GetOpcode() const;
50
51 /// Returns the specified register's value. Register 0 is hardcoded to always return 0.
52 u32 GetRegister(u32 register_id) const;
53
54 /// Sets the register to the input value.
55 void SetRegister(u32 register_id, u32 value);
56
57 /// Sets the method address to use for the next Send instruction.
58 void SetMethodAddress(u32 address);
20 59
21MacroInterpreterImpl::MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d_, 60 /// Calls a GPU Engine method with the input parameter.
22 const std::vector<u32>& code_) 61 void Send(u32 value);
23 : maxwell3d{maxwell3d_}, code{code_} {} 62
63 /// Reads a GPU register located at the method address.
64 u32 Read(u32 method) const;
65
66 /// Returns the next parameter in the parameter queue.
67 u32 FetchParameter();
68
69 Engines::Maxwell3D& maxwell3d;
70
71 /// Current program counter
72 u32 pc{};
73 /// Program counter to execute at after the delay slot is executed.
74 std::optional<u32> delayed_pc;
75
76 /// General purpose macro registers.
77 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers = {};
78
79 /// Method address to use for the next Send instruction.
80 Macro::MethodAddress method_address = {};
81
82 /// Input parameters of the current macro.
83 std::unique_ptr<u32[]> parameters;
84 std::size_t num_parameters = 0;
85 std::size_t parameters_capacity = 0;
86 /// Index of the next parameter that will be fetched by the 'parm' instruction.
87 u32 next_parameter_index = 0;
88
89 bool carry_flag = false;
90 const std::vector<u32>& code;
91};
24 92
25void MacroInterpreterImpl::Execute(const std::vector<u32>& params, u32 method) { 93void MacroInterpreterImpl::Execute(const std::vector<u32>& params, u32 method) {
26 MICROPROFILE_SCOPE(MacroInterp); 94 MICROPROFILE_SCOPE(MacroInterp);
@@ -283,5 +351,13 @@ u32 MacroInterpreterImpl::FetchParameter() {
283 ASSERT(next_parameter_index < num_parameters); 351 ASSERT(next_parameter_index < num_parameters);
284 return parameters[next_parameter_index++]; 352 return parameters[next_parameter_index++];
285} 353}
354} // Anonymous namespace
355
356MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d_)
357 : MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {}
358
359std::unique_ptr<CachedMacro> MacroInterpreter::Compile(const std::vector<u32>& code) {
360 return std::make_unique<MacroInterpreterImpl>(maxwell3d, code);
361}
286 362
287} // namespace Tegra 363} // namespace Tegra
diff --git a/src/video_core/macro/macro_interpreter.h b/src/video_core/macro/macro_interpreter.h
index d50c619ce..8a9648e46 100644
--- a/src/video_core/macro/macro_interpreter.h
+++ b/src/video_core/macro/macro_interpreter.h
@@ -3,10 +3,9 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#pragma once 5#pragma once
6#include <array> 6
7#include <optional>
8#include <vector> 7#include <vector>
9#include "common/bit_field.h" 8
10#include "common/common_types.h" 9#include "common/common_types.h"
11#include "video_core/macro/macro.h" 10#include "video_core/macro/macro.h"
12 11
@@ -26,77 +25,4 @@ private:
26 Engines::Maxwell3D& maxwell3d; 25 Engines::Maxwell3D& maxwell3d;
27}; 26};
28 27
29class MacroInterpreterImpl : public CachedMacro {
30public:
31 explicit MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d_, const std::vector<u32>& code_);
32 void Execute(const std::vector<u32>& params, u32 method) override;
33
34private:
35 /// Resets the execution engine state, zeroing registers, etc.
36 void Reset();
37
38 /**
39 * Executes a single macro instruction located at the current program counter. Returns whether
40 * the interpreter should keep running.
41 *
42 * @param is_delay_slot Whether the current step is being executed due to a delay slot in a
43 * previous instruction.
44 */
45 bool Step(bool is_delay_slot);
46
47 /// Calculates the result of an ALU operation. src_a OP src_b;
48 u32 GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b);
49
50 /// Performs the result operation on the input result and stores it in the specified register
51 /// (if necessary).
52 void ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result);
53
54 /// Evaluates the branch condition and returns whether the branch should be taken or not.
55 bool EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const;
56
57 /// Reads an opcode at the current program counter location.
58 Macro::Opcode GetOpcode() const;
59
60 /// Returns the specified register's value. Register 0 is hardcoded to always return 0.
61 u32 GetRegister(u32 register_id) const;
62
63 /// Sets the register to the input value.
64 void SetRegister(u32 register_id, u32 value);
65
66 /// Sets the method address to use for the next Send instruction.
67 void SetMethodAddress(u32 address);
68
69 /// Calls a GPU Engine method with the input parameter.
70 void Send(u32 value);
71
72 /// Reads a GPU register located at the method address.
73 u32 Read(u32 method) const;
74
75 /// Returns the next parameter in the parameter queue.
76 u32 FetchParameter();
77
78 Engines::Maxwell3D& maxwell3d;
79
80 /// Current program counter
81 u32 pc;
82 /// Program counter to execute at after the delay slot is executed.
83 std::optional<u32> delayed_pc;
84
85 /// General purpose macro registers.
86 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers = {};
87
88 /// Method address to use for the next Send instruction.
89 Macro::MethodAddress method_address = {};
90
91 /// Input parameters of the current macro.
92 std::unique_ptr<u32[]> parameters;
93 std::size_t num_parameters = 0;
94 std::size_t parameters_capacity = 0;
95 /// Index of the next parameter that will be fetched by the 'parm' instruction.
96 u32 next_parameter_index = 0;
97
98 bool carry_flag = false;
99 const std::vector<u32>& code;
100};
101
102} // namespace Tegra 28} // namespace Tegra
diff --git a/src/video_core/macro/macro_jit_x64.cpp b/src/video_core/macro/macro_jit_x64.cpp
index c6b2b2109..47b28ad16 100644
--- a/src/video_core/macro/macro_jit_x64.cpp
+++ b/src/video_core/macro/macro_jit_x64.cpp
@@ -2,9 +2,17 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <array>
6#include <bitset>
7#include <optional>
8
9#include <xbyak/xbyak.h>
10
5#include "common/assert.h" 11#include "common/assert.h"
12#include "common/bit_field.h"
6#include "common/logging/log.h" 13#include "common/logging/log.h"
7#include "common/microprofile.h" 14#include "common/microprofile.h"
15#include "common/x64/xbyak_abi.h"
8#include "common/x64/xbyak_util.h" 16#include "common/x64/xbyak_util.h"
9#include "video_core/engines/maxwell_3d.h" 17#include "video_core/engines/maxwell_3d.h"
10#include "video_core/macro/macro_interpreter.h" 18#include "video_core/macro/macro_interpreter.h"
@@ -14,13 +22,14 @@ MICROPROFILE_DEFINE(MacroJitCompile, "GPU", "Compile macro JIT", MP_RGB(173, 255
14MICROPROFILE_DEFINE(MacroJitExecute, "GPU", "Execute macro JIT", MP_RGB(255, 255, 0)); 22MICROPROFILE_DEFINE(MacroJitExecute, "GPU", "Execute macro JIT", MP_RGB(255, 255, 0));
15 23
16namespace Tegra { 24namespace Tegra {
25namespace {
17constexpr Xbyak::Reg64 STATE = Xbyak::util::rbx; 26constexpr Xbyak::Reg64 STATE = Xbyak::util::rbx;
18constexpr Xbyak::Reg32 RESULT = Xbyak::util::ebp; 27constexpr Xbyak::Reg32 RESULT = Xbyak::util::ebp;
19constexpr Xbyak::Reg64 PARAMETERS = Xbyak::util::r12; 28constexpr Xbyak::Reg64 PARAMETERS = Xbyak::util::r12;
20constexpr Xbyak::Reg32 METHOD_ADDRESS = Xbyak::util::r14d; 29constexpr Xbyak::Reg32 METHOD_ADDRESS = Xbyak::util::r14d;
21constexpr Xbyak::Reg64 BRANCH_HOLDER = Xbyak::util::r15; 30constexpr Xbyak::Reg64 BRANCH_HOLDER = Xbyak::util::r15;
22 31
23static const std::bitset<32> PERSISTENT_REGISTERS = Common::X64::BuildRegSet({ 32constexpr std::bitset<32> PERSISTENT_REGISTERS = Common::X64::BuildRegSet({
24 STATE, 33 STATE,
25 RESULT, 34 RESULT,
26 PARAMETERS, 35 PARAMETERS,
@@ -28,19 +37,75 @@ static const std::bitset<32> PERSISTENT_REGISTERS = Common::X64::BuildRegSet({
28 BRANCH_HOLDER, 37 BRANCH_HOLDER,
29}); 38});
30 39
31MacroJITx64::MacroJITx64(Engines::Maxwell3D& maxwell3d_) 40// Arbitrarily chosen based on current booting games.
32 : MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {} 41constexpr size_t MAX_CODE_SIZE = 0x10000;
33 42
34std::unique_ptr<CachedMacro> MacroJITx64::Compile(const std::vector<u32>& code) { 43std::bitset<32> PersistentCallerSavedRegs() {
35 return std::make_unique<MacroJITx64Impl>(maxwell3d, code); 44 return PERSISTENT_REGISTERS & Common::X64::ABI_ALL_CALLER_SAVED;
36} 45}
37 46
38MacroJITx64Impl::MacroJITx64Impl(Engines::Maxwell3D& maxwell3d_, const std::vector<u32>& code_) 47class MacroJITx64Impl final : public Xbyak::CodeGenerator, public CachedMacro {
39 : CodeGenerator{MAX_CODE_SIZE}, code{code_}, maxwell3d{maxwell3d_} { 48public:
40 Compile(); 49 explicit MacroJITx64Impl(Engines::Maxwell3D& maxwell3d_, const std::vector<u32>& code_)
41} 50 : CodeGenerator{MAX_CODE_SIZE}, code{code_}, maxwell3d{maxwell3d_} {
51 Compile();
52 }
53
54 void Execute(const std::vector<u32>& parameters, u32 method) override;
55
56 void Compile_ALU(Macro::Opcode opcode);
57 void Compile_AddImmediate(Macro::Opcode opcode);
58 void Compile_ExtractInsert(Macro::Opcode opcode);
59 void Compile_ExtractShiftLeftImmediate(Macro::Opcode opcode);
60 void Compile_ExtractShiftLeftRegister(Macro::Opcode opcode);
61 void Compile_Read(Macro::Opcode opcode);
62 void Compile_Branch(Macro::Opcode opcode);
63
64private:
65 void Optimizer_ScanFlags();
66
67 void Compile();
68 bool Compile_NextInstruction();
69
70 Xbyak::Reg32 Compile_FetchParameter();
71 Xbyak::Reg32 Compile_GetRegister(u32 index, Xbyak::Reg32 dst);
72
73 void Compile_ProcessResult(Macro::ResultOperation operation, u32 reg);
74 void Compile_Send(Xbyak::Reg32 value);
42 75
43MacroJITx64Impl::~MacroJITx64Impl() = default; 76 Macro::Opcode GetOpCode() const;
77
78 struct JITState {
79 Engines::Maxwell3D* maxwell3d{};
80 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers{};
81 u32 carry_flag{};
82 };
83 static_assert(offsetof(JITState, maxwell3d) == 0, "Maxwell3D is not at 0x0");
84 using ProgramType = void (*)(JITState*, const u32*);
85
86 struct OptimizerState {
87 bool can_skip_carry{};
88 bool has_delayed_pc{};
89 bool zero_reg_skip{};
90 bool skip_dummy_addimmediate{};
91 bool optimize_for_method_move{};
92 bool enable_asserts{};
93 };
94 OptimizerState optimizer{};
95
96 std::optional<Macro::Opcode> next_opcode{};
97 ProgramType program{nullptr};
98
99 std::array<Xbyak::Label, MAX_CODE_SIZE> labels;
100 std::array<Xbyak::Label, MAX_CODE_SIZE> delay_skip;
101 Xbyak::Label end_of_code{};
102
103 bool is_delay_slot{};
104 u32 pc{};
105
106 const std::vector<u32>& code;
107 Engines::Maxwell3D& maxwell3d;
108};
44 109
45void MacroJITx64Impl::Execute(const std::vector<u32>& parameters, u32 method) { 110void MacroJITx64Impl::Execute(const std::vector<u32>& parameters, u32 method) {
46 MICROPROFILE_SCOPE(MacroJitExecute); 111 MICROPROFILE_SCOPE(MacroJitExecute);
@@ -307,11 +372,11 @@ void MacroJITx64Impl::Compile_Read(Macro::Opcode opcode) {
307 Compile_ProcessResult(opcode.result_operation, opcode.dst); 372 Compile_ProcessResult(opcode.result_operation, opcode.dst);
308} 373}
309 374
310static void Send(Engines::Maxwell3D* maxwell3d, Macro::MethodAddress method_address, u32 value) { 375void Send(Engines::Maxwell3D* maxwell3d, Macro::MethodAddress method_address, u32 value) {
311 maxwell3d->CallMethodFromMME(method_address.address, value); 376 maxwell3d->CallMethodFromMME(method_address.address, value);
312} 377}
313 378
314void Tegra::MacroJITx64Impl::Compile_Send(Xbyak::Reg32 value) { 379void MacroJITx64Impl::Compile_Send(Xbyak::Reg32 value) {
315 Common::X64::ABI_PushRegistersAndAdjustStack(*this, PersistentCallerSavedRegs(), 0); 380 Common::X64::ABI_PushRegistersAndAdjustStack(*this, PersistentCallerSavedRegs(), 0);
316 mov(Common::X64::ABI_PARAM1, qword[STATE]); 381 mov(Common::X64::ABI_PARAM1, qword[STATE]);
317 mov(Common::X64::ABI_PARAM2, METHOD_ADDRESS); 382 mov(Common::X64::ABI_PARAM2, METHOD_ADDRESS);
@@ -338,7 +403,7 @@ void Tegra::MacroJITx64Impl::Compile_Send(Xbyak::Reg32 value) {
338 L(dont_process); 403 L(dont_process);
339} 404}
340 405
341void Tegra::MacroJITx64Impl::Compile_Branch(Macro::Opcode opcode) { 406void MacroJITx64Impl::Compile_Branch(Macro::Opcode opcode) {
342 ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid"); 407 ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
343 const s32 jump_address = 408 const s32 jump_address =
344 static_cast<s32>(pc) + static_cast<s32>(opcode.GetBranchTarget() / sizeof(s32)); 409 static_cast<s32>(pc) + static_cast<s32>(opcode.GetBranchTarget() / sizeof(s32));
@@ -392,7 +457,7 @@ void Tegra::MacroJITx64Impl::Compile_Branch(Macro::Opcode opcode) {
392 L(end); 457 L(end);
393} 458}
394 459
395void Tegra::MacroJITx64Impl::Optimizer_ScanFlags() { 460void MacroJITx64Impl::Optimizer_ScanFlags() {
396 optimizer.can_skip_carry = true; 461 optimizer.can_skip_carry = true;
397 optimizer.has_delayed_pc = false; 462 optimizer.has_delayed_pc = false;
398 for (auto raw_op : code) { 463 for (auto raw_op : code) {
@@ -534,7 +599,7 @@ bool MacroJITx64Impl::Compile_NextInstruction() {
534 return true; 599 return true;
535} 600}
536 601
537Xbyak::Reg32 Tegra::MacroJITx64Impl::Compile_FetchParameter() { 602Xbyak::Reg32 MacroJITx64Impl::Compile_FetchParameter() {
538 mov(eax, dword[PARAMETERS]); 603 mov(eax, dword[PARAMETERS]);
539 add(PARAMETERS, sizeof(u32)); 604 add(PARAMETERS, sizeof(u32));
540 return eax; 605 return eax;
@@ -611,9 +676,12 @@ Macro::Opcode MacroJITx64Impl::GetOpCode() const {
611 ASSERT(pc < code.size()); 676 ASSERT(pc < code.size());
612 return {code[pc]}; 677 return {code[pc]};
613} 678}
679} // Anonymous namespace
614 680
615std::bitset<32> MacroJITx64Impl::PersistentCallerSavedRegs() const { 681MacroJITx64::MacroJITx64(Engines::Maxwell3D& maxwell3d_)
616 return PERSISTENT_REGISTERS & Common::X64::ABI_ALL_CALLER_SAVED; 682 : MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {}
617}
618 683
684std::unique_ptr<CachedMacro> MacroJITx64::Compile(const std::vector<u32>& code) {
685 return std::make_unique<MacroJITx64Impl>(maxwell3d, code);
686}
619} // namespace Tegra 687} // namespace Tegra
diff --git a/src/video_core/macro/macro_jit_x64.h b/src/video_core/macro/macro_jit_x64.h
index d03d480b4..773b037ae 100644
--- a/src/video_core/macro/macro_jit_x64.h
+++ b/src/video_core/macro/macro_jit_x64.h
@@ -4,12 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
8#include <bitset>
9#include <xbyak/xbyak.h>
10#include "common/bit_field.h"
11#include "common/common_types.h" 7#include "common/common_types.h"
12#include "common/x64/xbyak_abi.h"
13#include "video_core/macro/macro.h" 8#include "video_core/macro/macro.h"
14 9
15namespace Tegra { 10namespace Tegra {
@@ -18,9 +13,6 @@ namespace Engines {
18class Maxwell3D; 13class Maxwell3D;
19} 14}
20 15
21/// MAX_CODE_SIZE is arbitrarily chosen based on current booting games
22constexpr size_t MAX_CODE_SIZE = 0x10000;
23
24class MacroJITx64 final : public MacroEngine { 16class MacroJITx64 final : public MacroEngine {
25public: 17public:
26 explicit MacroJITx64(Engines::Maxwell3D& maxwell3d_); 18 explicit MacroJITx64(Engines::Maxwell3D& maxwell3d_);
@@ -32,67 +24,4 @@ private:
32 Engines::Maxwell3D& maxwell3d; 24 Engines::Maxwell3D& maxwell3d;
33}; 25};
34 26
35class MacroJITx64Impl : public Xbyak::CodeGenerator, public CachedMacro {
36public:
37 explicit MacroJITx64Impl(Engines::Maxwell3D& maxwell3d_, const std::vector<u32>& code_);
38 ~MacroJITx64Impl();
39
40 void Execute(const std::vector<u32>& parameters, u32 method) override;
41
42 void Compile_ALU(Macro::Opcode opcode);
43 void Compile_AddImmediate(Macro::Opcode opcode);
44 void Compile_ExtractInsert(Macro::Opcode opcode);
45 void Compile_ExtractShiftLeftImmediate(Macro::Opcode opcode);
46 void Compile_ExtractShiftLeftRegister(Macro::Opcode opcode);
47 void Compile_Read(Macro::Opcode opcode);
48 void Compile_Branch(Macro::Opcode opcode);
49
50private:
51 void Optimizer_ScanFlags();
52
53 void Compile();
54 bool Compile_NextInstruction();
55
56 Xbyak::Reg32 Compile_FetchParameter();
57 Xbyak::Reg32 Compile_GetRegister(u32 index, Xbyak::Reg32 dst);
58
59 void Compile_ProcessResult(Macro::ResultOperation operation, u32 reg);
60 void Compile_Send(Xbyak::Reg32 value);
61
62 Macro::Opcode GetOpCode() const;
63 std::bitset<32> PersistentCallerSavedRegs() const;
64
65 struct JITState {
66 Engines::Maxwell3D* maxwell3d{};
67 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers{};
68 u32 carry_flag{};
69 };
70 static_assert(offsetof(JITState, maxwell3d) == 0, "Maxwell3D is not at 0x0");
71 using ProgramType = void (*)(JITState*, const u32*);
72
73 struct OptimizerState {
74 bool can_skip_carry{};
75 bool has_delayed_pc{};
76 bool zero_reg_skip{};
77 bool skip_dummy_addimmediate{};
78 bool optimize_for_method_move{};
79 bool enable_asserts{};
80 };
81 OptimizerState optimizer{};
82
83 std::optional<Macro::Opcode> next_opcode{};
84 ProgramType program{nullptr};
85
86 std::array<Xbyak::Label, MAX_CODE_SIZE> labels;
87 std::array<Xbyak::Label, MAX_CODE_SIZE> delay_skip;
88 Xbyak::Label end_of_code{};
89
90 bool is_delay_slot{};
91 u32 pc{};
92 std::optional<u32> delayed_pc;
93
94 const std::vector<u32>& code;
95 Engines::Maxwell3D& maxwell3d;
96};
97
98} // namespace Tegra 27} // namespace Tegra
diff --git a/src/video_core/memory_manager.h b/src/video_core/memory_manager.h
index 38d8d9d74..61bfe47c7 100644
--- a/src/video_core/memory_manager.h
+++ b/src/video_core/memory_manager.h
@@ -143,6 +143,8 @@ public:
143 [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align); 143 [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align);
144 void Unmap(GPUVAddr gpu_addr, std::size_t size); 144 void Unmap(GPUVAddr gpu_addr, std::size_t size);
145 145
146 void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
147
146private: 148private:
147 [[nodiscard]] PageEntry GetPageEntry(GPUVAddr gpu_addr) const; 149 [[nodiscard]] PageEntry GetPageEntry(GPUVAddr gpu_addr) const;
148 void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size); 150 void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size);
@@ -153,8 +155,6 @@ private:
153 void TryLockPage(PageEntry page_entry, std::size_t size); 155 void TryLockPage(PageEntry page_entry, std::size_t size);
154 void TryUnlockPage(PageEntry page_entry, std::size_t size); 156 void TryUnlockPage(PageEntry page_entry, std::size_t size);
155 157
156 void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
157
158 void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size, 158 void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
159 bool is_safe) const; 159 bool is_safe) const;
160 void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size, 160 void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h
index b094fc064..1f1f12291 100644
--- a/src/video_core/rasterizer_interface.h
+++ b/src/video_core/rasterizer_interface.h
@@ -123,6 +123,9 @@ public:
123 123
124 [[nodiscard]] virtual Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() = 0; 124 [[nodiscard]] virtual Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() = 0;
125 125
126 virtual void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
127 std::span<u8> memory) = 0;
128
126 /// Attempt to use a faster method to display the framebuffer to screen 129 /// Attempt to use a faster method to display the framebuffer to screen
127 [[nodiscard]] virtual bool AccelerateDisplay(const Tegra::FramebufferConfig& config, 130 [[nodiscard]] virtual bool AccelerateDisplay(const Tegra::FramebufferConfig& config,
128 VAddr framebuffer_addr, u32 pixel_stride) { 131 VAddr framebuffer_addr, u32 pixel_stride) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 9b516c64f..142412a8e 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -484,6 +484,28 @@ Tegra::Engines::AccelerateDMAInterface& RasterizerOpenGL::AccessAccelerateDMA()
484 return accelerate_dma; 484 return accelerate_dma;
485} 485}
486 486
487void RasterizerOpenGL::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
488 std::span<u8> memory) {
489 auto cpu_addr = gpu_memory.GpuToCpuAddress(address);
490 if (!cpu_addr) [[unlikely]] {
491 gpu_memory.WriteBlock(address, memory.data(), copy_size);
492 return;
493 }
494 gpu_memory.WriteBlockUnsafe(address, memory.data(), copy_size);
495 {
496 std::unique_lock<std::mutex> lock{buffer_cache.mutex};
497 if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
498 buffer_cache.WriteMemory(*cpu_addr, copy_size);
499 }
500 }
501 {
502 std::scoped_lock lock_texture{texture_cache.mutex};
503 texture_cache.WriteMemory(*cpu_addr, copy_size);
504 }
505 shader_cache.InvalidateRegion(*cpu_addr, copy_size);
506 query_cache.InvalidateRegion(*cpu_addr, copy_size);
507}
508
487bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config, 509bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config,
488 VAddr framebuffer_addr, u32 pixel_stride) { 510 VAddr framebuffer_addr, u32 pixel_stride) {
489 if (framebuffer_addr == 0) { 511 if (framebuffer_addr == 0) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index d0397b745..98f6fd342 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -106,6 +106,8 @@ public:
106 const Tegra::Engines::Fermi2D::Surface& dst, 106 const Tegra::Engines::Fermi2D::Surface& dst,
107 const Tegra::Engines::Fermi2D::Config& copy_config) override; 107 const Tegra::Engines::Fermi2D::Config& copy_config) override;
108 Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override; 108 Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
109 void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
110 std::span<u8> memory) override;
109 bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr, 111 bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
110 u32 pixel_stride) override; 112 u32 pixel_stride) override;
111 void LoadDiskResources(u64 title_id, std::stop_token stop_loading, 113 void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
diff --git a/src/video_core/renderer_vulkan/vk_fsr.cpp b/src/video_core/renderer_vulkan/vk_fsr.cpp
index 73629d229..b630090e8 100644
--- a/src/video_core/renderer_vulkan/vk_fsr.cpp
+++ b/src/video_core/renderer_vulkan/vk_fsr.cpp
@@ -214,7 +214,7 @@ VkImageView FSR::Draw(VKScheduler& scheduler, size_t image_index, VkImageView im
214 214
215 { 215 {
216 VkImageMemoryBarrier fsr_write_barrier = base_barrier; 216 VkImageMemoryBarrier fsr_write_barrier = base_barrier;
217 fsr_write_barrier.image = *images[image_index], 217 fsr_write_barrier.image = *images[image_index];
218 fsr_write_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; 218 fsr_write_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
219 219
220 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 220 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index fd334a146..2227d9197 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -548,6 +548,28 @@ Tegra::Engines::AccelerateDMAInterface& RasterizerVulkan::AccessAccelerateDMA()
548 return accelerate_dma; 548 return accelerate_dma;
549} 549}
550 550
551void RasterizerVulkan::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
552 std::span<u8> memory) {
553 auto cpu_addr = gpu_memory.GpuToCpuAddress(address);
554 if (!cpu_addr) [[unlikely]] {
555 gpu_memory.WriteBlock(address, memory.data(), copy_size);
556 return;
557 }
558 gpu_memory.WriteBlockUnsafe(address, memory.data(), copy_size);
559 {
560 std::unique_lock<std::mutex> lock{buffer_cache.mutex};
561 if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
562 buffer_cache.WriteMemory(*cpu_addr, copy_size);
563 }
564 }
565 {
566 std::scoped_lock lock_texture{texture_cache.mutex};
567 texture_cache.WriteMemory(*cpu_addr, copy_size);
568 }
569 pipeline_cache.InvalidateRegion(*cpu_addr, copy_size);
570 query_cache.InvalidateRegion(*cpu_addr, copy_size);
571}
572
551bool RasterizerVulkan::AccelerateDisplay(const Tegra::FramebufferConfig& config, 573bool RasterizerVulkan::AccelerateDisplay(const Tegra::FramebufferConfig& config,
552 VAddr framebuffer_addr, u32 pixel_stride) { 574 VAddr framebuffer_addr, u32 pixel_stride) {
553 if (!framebuffer_addr) { 575 if (!framebuffer_addr) {
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index 866827247..5af2e275b 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -99,6 +99,8 @@ public:
99 const Tegra::Engines::Fermi2D::Surface& dst, 99 const Tegra::Engines::Fermi2D::Surface& dst,
100 const Tegra::Engines::Fermi2D::Config& copy_config) override; 100 const Tegra::Engines::Fermi2D::Config& copy_config) override;
101 Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override; 101 Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
102 void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
103 std::span<u8> memory) override;
102 bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr, 104 bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
103 u32 pixel_stride) override; 105 u32 pixel_stride) override;
104 void LoadDiskResources(u64 title_id, std::stop_token stop_loading, 106 void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
diff --git a/src/video_core/shader_cache.cpp b/src/video_core/shader_cache.cpp
index 78bf90c48..87636857d 100644
--- a/src/video_core/shader_cache.cpp
+++ b/src/video_core/shader_cache.cpp
@@ -170,7 +170,7 @@ void ShaderCache::RemovePendingShaders() {
170 marked_for_removal.clear(); 170 marked_for_removal.clear();
171 171
172 if (!removed_shaders.empty()) { 172 if (!removed_shaders.empty()) {
173 RemoveShadersFromStorage(std::move(removed_shaders)); 173 RemoveShadersFromStorage(removed_shaders);
174 } 174 }
175} 175}
176 176
@@ -213,7 +213,7 @@ void ShaderCache::UnmarkMemory(Entry* entry) {
213 rasterizer.UpdatePagesCachedCount(addr, size, -1); 213 rasterizer.UpdatePagesCachedCount(addr, size, -1);
214} 214}
215 215
216void ShaderCache::RemoveShadersFromStorage(std::vector<ShaderInfo*> removed_shaders) { 216void ShaderCache::RemoveShadersFromStorage(std::span<ShaderInfo*> removed_shaders) {
217 // Remove them from the cache 217 // Remove them from the cache
218 std::erase_if(storage, [&removed_shaders](const std::unique_ptr<ShaderInfo>& shader) { 218 std::erase_if(storage, [&removed_shaders](const std::unique_ptr<ShaderInfo>& shader) {
219 return std::ranges::find(removed_shaders, shader.get()) != removed_shaders.end(); 219 return std::ranges::find(removed_shaders, shader.get()) != removed_shaders.end();
diff --git a/src/video_core/shader_cache.h b/src/video_core/shader_cache.h
index 136fe294c..8836bc8c6 100644
--- a/src/video_core/shader_cache.h
+++ b/src/video_core/shader_cache.h
@@ -4,7 +4,6 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <algorithm>
8#include <array> 7#include <array>
9#include <memory> 8#include <memory>
10#include <mutex> 9#include <mutex>
@@ -138,7 +137,7 @@ private:
138 /// @param removed_shaders Shaders to be removed from the storage 137 /// @param removed_shaders Shaders to be removed from the storage
139 /// @pre invalidation_mutex is locked 138 /// @pre invalidation_mutex is locked
140 /// @pre lookup_mutex is locked 139 /// @pre lookup_mutex is locked
141 void RemoveShadersFromStorage(std::vector<ShaderInfo*> removed_shaders); 140 void RemoveShadersFromStorage(std::span<ShaderInfo*> removed_shaders);
142 141
143 /// @brief Creates a new entry in the lookup cache and returns its pointer 142 /// @brief Creates a new entry in the lookup cache and returns its pointer
144 /// @pre lookup_mutex is locked 143 /// @pre lookup_mutex is locked
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index 7e8f92840..2c8c10c50 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -393,6 +393,8 @@ void Config::ReadControlValues() {
393 ReadGlobalSetting(Settings::values.enable_accurate_vibrations); 393 ReadGlobalSetting(Settings::values.enable_accurate_vibrations);
394 ReadGlobalSetting(Settings::values.motion_enabled); 394 ReadGlobalSetting(Settings::values.motion_enabled);
395 395
396 ReadBasicSetting(Settings::values.controller_navigation);
397
396 qt_config->endGroup(); 398 qt_config->endGroup();
397} 399}
398 400
@@ -1001,6 +1003,7 @@ void Config::SaveControlValues() {
1001 WriteBasicSetting(Settings::values.keyboard_enabled); 1003 WriteBasicSetting(Settings::values.keyboard_enabled);
1002 WriteBasicSetting(Settings::values.emulate_analog_keyboard); 1004 WriteBasicSetting(Settings::values.emulate_analog_keyboard);
1003 WriteBasicSetting(Settings::values.mouse_panning_sensitivity); 1005 WriteBasicSetting(Settings::values.mouse_panning_sensitivity);
1006 WriteBasicSetting(Settings::values.controller_navigation);
1004 1007
1005 WriteBasicSetting(Settings::values.tas_enable); 1008 WriteBasicSetting(Settings::values.tas_enable);
1006 WriteBasicSetting(Settings::values.tas_loop); 1009 WriteBasicSetting(Settings::values.tas_loop);
diff --git a/src/yuzu/configuration/configure_graphics.ui b/src/yuzu/configuration/configure_graphics.ui
index 9241678e4..74f0e0b79 100644
--- a/src/yuzu/configuration/configure_graphics.ui
+++ b/src/yuzu/configuration/configure_graphics.ui
@@ -429,7 +429,7 @@
429 </item> 429 </item>
430 <item> 430 <item>
431 <property name="text"> 431 <property name="text">
432 <string>AMD FidelityFX™️ Super Resolution [Vulkan Only]</string> 432 <string>AMD FidelityFX™️ Super Resolution (Vulkan Only)</string>
433 </property> 433 </property>
434 </item> 434 </item>
435 </widget> 435 </widget>
diff --git a/src/yuzu/configuration/configure_input_advanced.cpp b/src/yuzu/configuration/configure_input_advanced.cpp
index 65c8e59ac..20fc2599d 100644
--- a/src/yuzu/configuration/configure_input_advanced.cpp
+++ b/src/yuzu/configuration/configure_input_advanced.cpp
@@ -131,6 +131,7 @@ void ConfigureInputAdvanced::ApplyConfiguration() {
131 Settings::values.touchscreen.enabled = ui->touchscreen_enabled->isChecked(); 131 Settings::values.touchscreen.enabled = ui->touchscreen_enabled->isChecked();
132 Settings::values.enable_raw_input = ui->enable_raw_input->isChecked(); 132 Settings::values.enable_raw_input = ui->enable_raw_input->isChecked();
133 Settings::values.enable_udp_controller = ui->enable_udp_controller->isChecked(); 133 Settings::values.enable_udp_controller = ui->enable_udp_controller->isChecked();
134 Settings::values.controller_navigation = ui->controller_navigation->isChecked();
134} 135}
135 136
136void ConfigureInputAdvanced::LoadConfiguration() { 137void ConfigureInputAdvanced::LoadConfiguration() {
@@ -162,6 +163,7 @@ void ConfigureInputAdvanced::LoadConfiguration() {
162 ui->touchscreen_enabled->setChecked(Settings::values.touchscreen.enabled); 163 ui->touchscreen_enabled->setChecked(Settings::values.touchscreen.enabled);
163 ui->enable_raw_input->setChecked(Settings::values.enable_raw_input.GetValue()); 164 ui->enable_raw_input->setChecked(Settings::values.enable_raw_input.GetValue());
164 ui->enable_udp_controller->setChecked(Settings::values.enable_udp_controller.GetValue()); 165 ui->enable_udp_controller->setChecked(Settings::values.enable_udp_controller.GetValue());
166 ui->controller_navigation->setChecked(Settings::values.controller_navigation.GetValue());
165 167
166 UpdateUIEnabled(); 168 UpdateUIEnabled();
167} 169}
diff --git a/src/yuzu/configuration/configure_input_advanced.ui b/src/yuzu/configuration/configure_input_advanced.ui
index df0e4d602..66f2075f2 100644
--- a/src/yuzu/configuration/configure_input_advanced.ui
+++ b/src/yuzu/configuration/configure_input_advanced.ui
@@ -2655,6 +2655,19 @@
2655 </widget> 2655 </widget>
2656 </item> 2656 </item>
2657 <item row="4" column="0"> 2657 <item row="4" column="0">
2658 <widget class="QCheckBox" name="controller_navigation">
2659 <property name="minimumSize">
2660 <size>
2661 <width>0</width>
2662 <height>23</height>
2663 </size>
2664 </property>
2665 <property name="text">
2666 <string>Controller navigation</string>
2667 </property>
2668 </widget>
2669 </item>
2670 <item row="5" column="0">
2658 <widget class="QCheckBox" name="mouse_panning"> 2671 <widget class="QCheckBox" name="mouse_panning">
2659 <property name="minimumSize"> 2672 <property name="minimumSize">
2660 <size> 2673 <size>
@@ -2667,7 +2680,7 @@
2667 </property> 2680 </property>
2668 </widget> 2681 </widget>
2669 </item> 2682 </item>
2670 <item row="4" column="2"> 2683 <item row="5" column="2">
2671 <widget class="QSpinBox" name="mouse_panning_sensitivity"> 2684 <widget class="QSpinBox" name="mouse_panning_sensitivity">
2672 <property name="toolTip"> 2685 <property name="toolTip">
2673 <string>Mouse sensitivity</string> 2686 <string>Mouse sensitivity</string>
@@ -2689,14 +2702,14 @@
2689 </property> 2702 </property>
2690 </widget> 2703 </widget>
2691 </item> 2704 </item>
2692 <item row="5" column="0"> 2705 <item row="6" column="0">
2693 <widget class="QLabel" name="motion_touch"> 2706 <widget class="QLabel" name="motion_touch">
2694 <property name="text"> 2707 <property name="text">
2695 <string>Motion / Touch</string> 2708 <string>Motion / Touch</string>
2696 </property> 2709 </property>
2697 </widget> 2710 </widget>
2698 </item> 2711 </item>
2699 <item row="5" column="2"> 2712 <item row="6" column="2">
2700 <widget class="QPushButton" name="buttonMotionTouch"> 2713 <widget class="QPushButton" name="buttonMotionTouch">
2701 <property name="text"> 2714 <property name="text">
2702 <string>Configure</string> 2715 <string>Configure</string>
diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp
index d2132b408..ba563b382 100644
--- a/src/yuzu/configuration/configure_input_player.cpp
+++ b/src/yuzu/configuration/configure_input_player.cpp
@@ -147,7 +147,7 @@ QString ConfigureInputPlayer::ButtonToText(const Common::ParamPackage& param) {
147 // Retrieve the names from Qt 147 // Retrieve the names from Qt
148 if (param.Get("engine", "") == "keyboard") { 148 if (param.Get("engine", "") == "keyboard") {
149 const QString button_str = GetKeyName(param.Get("code", 0)); 149 const QString button_str = GetKeyName(param.Get("code", 0));
150 return QObject::tr("%1%2").arg(toggle, button_str); 150 return QObject::tr("%1%2%3").arg(toggle, inverted, button_str);
151 } 151 }
152 152
153 if (common_button_name == Common::Input::ButtonNames::Invalid) { 153 if (common_button_name == Common::Input::ButtonNames::Invalid) {
@@ -326,7 +326,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
326 connect(button, &QPushButton::clicked, [=, this] { 326 connect(button, &QPushButton::clicked, [=, this] {
327 HandleClick( 327 HandleClick(
328 button, button_id, 328 button, button_id,
329 [=, this](Common::ParamPackage params) { 329 [=, this](const Common::ParamPackage& params) {
330 emulated_controller->SetButtonParam(button_id, params); 330 emulated_controller->SetButtonParam(button_id, params);
331 }, 331 },
332 InputCommon::Polling::InputType::Button); 332 InputCommon::Polling::InputType::Button);
@@ -341,7 +341,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
341 emulated_controller->SetButtonParam(button_id, {}); 341 emulated_controller->SetButtonParam(button_id, {});
342 button_map[button_id]->setText(tr("[not set]")); 342 button_map[button_id]->setText(tr("[not set]"));
343 }); 343 });
344 if (param.Has("button") || param.Has("hat")) { 344 if (param.Has("code") || param.Has("button") || param.Has("hat")) {
345 context_menu.addAction(tr("Toggle button"), [&] { 345 context_menu.addAction(tr("Toggle button"), [&] {
346 const bool toggle_value = !param.Get("toggle", false); 346 const bool toggle_value = !param.Get("toggle", false);
347 param.Set("toggle", toggle_value); 347 param.Set("toggle", toggle_value);
@@ -349,8 +349,8 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
349 emulated_controller->SetButtonParam(button_id, param); 349 emulated_controller->SetButtonParam(button_id, param);
350 }); 350 });
351 context_menu.addAction(tr("Invert button"), [&] { 351 context_menu.addAction(tr("Invert button"), [&] {
352 const bool toggle_value = !param.Get("inverted", false); 352 const bool invert_value = !param.Get("inverted", false);
353 param.Set("inverted", toggle_value); 353 param.Set("inverted", invert_value);
354 button_map[button_id]->setText(ButtonToText(param)); 354 button_map[button_id]->setText(ButtonToText(param));
355 emulated_controller->SetButtonParam(button_id, param); 355 emulated_controller->SetButtonParam(button_id, param);
356 }); 356 });
@@ -392,7 +392,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
392 connect(button, &QPushButton::clicked, [=, this] { 392 connect(button, &QPushButton::clicked, [=, this] {
393 HandleClick( 393 HandleClick(
394 button, motion_id, 394 button, motion_id,
395 [=, this](Common::ParamPackage params) { 395 [=, this](const Common::ParamPackage& params) {
396 emulated_controller->SetMotionParam(motion_id, params); 396 emulated_controller->SetMotionParam(motion_id, params);
397 }, 397 },
398 InputCommon::Polling::InputType::Motion); 398 InputCommon::Polling::InputType::Motion);
@@ -403,10 +403,22 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
403 connect(button, &QPushButton::customContextMenuRequested, 403 connect(button, &QPushButton::customContextMenuRequested,
404 [=, this](const QPoint& menu_location) { 404 [=, this](const QPoint& menu_location) {
405 QMenu context_menu; 405 QMenu context_menu;
406 Common::ParamPackage param = emulated_controller->GetMotionParam(motion_id);
406 context_menu.addAction(tr("Clear"), [&] { 407 context_menu.addAction(tr("Clear"), [&] {
407 emulated_controller->SetMotionParam(motion_id, {}); 408 emulated_controller->SetMotionParam(motion_id, {});
408 motion_map[motion_id]->setText(tr("[not set]")); 409 motion_map[motion_id]->setText(tr("[not set]"));
409 }); 410 });
411 if (param.Has("motion")) {
412 context_menu.addAction(tr("Set gyro threshold"), [&] {
413 const int gyro_threshold =
414 static_cast<int>(param.Get("threshold", 0.007f) * 1000.0f);
415 const int new_threshold = QInputDialog::getInt(
416 this, tr("Set threshold"), tr("Choose a value between 0% and 100%"),
417 gyro_threshold, 0, 100);
418 param.Set("threshold", new_threshold / 1000.0f);
419 emulated_controller->SetMotionParam(motion_id, param);
420 });
421 }
410 context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location)); 422 context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location));
411 }); 423 });
412 } 424 }
@@ -485,10 +497,11 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
485 param.Set("invert_y", invert_str); 497 param.Set("invert_y", invert_str);
486 emulated_controller->SetStickParam(analog_id, param); 498 emulated_controller->SetStickParam(analog_id, param);
487 } 499 }
488 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; 500 for (int analog_sub_button_id = 0;
489 ++sub_button_id) { 501 analog_sub_button_id < ANALOG_SUB_BUTTONS_NUM;
490 analog_map_buttons[analog_id][sub_button_id]->setText( 502 ++analog_sub_button_id) {
491 AnalogToText(param, analog_sub_buttons[sub_button_id])); 503 analog_map_buttons[analog_id][analog_sub_button_id]->setText(
504 AnalogToText(param, analog_sub_buttons[analog_sub_button_id]));
492 } 505 }
493 }); 506 });
494 context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal( 507 context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal(
@@ -510,28 +523,37 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
510 523
511 analog_map_modifier_button[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu); 524 analog_map_modifier_button[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu);
512 525
513 connect(analog_map_modifier_button[analog_id], &QPushButton::customContextMenuRequested, 526 connect(
514 [=, this](const QPoint& menu_location) { 527 analog_map_modifier_button[analog_id], &QPushButton::customContextMenuRequested,
515 QMenu context_menu; 528 [=, this](const QPoint& menu_location) {
516 Common::ParamPackage param = emulated_controller->GetStickParam(analog_id); 529 QMenu context_menu;
517 context_menu.addAction(tr("Clear"), [&] { 530 Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
518 param.Set("modifier", ""); 531 context_menu.addAction(tr("Clear"), [&] {
519 analog_map_modifier_button[analog_id]->setText(tr("[not set]")); 532 param.Set("modifier", "");
520 emulated_controller->SetStickParam(analog_id, param); 533 analog_map_modifier_button[analog_id]->setText(tr("[not set]"));
521 }); 534 emulated_controller->SetStickParam(analog_id, param);
522 context_menu.addAction(tr("Toggle button"), [&] {
523 Common::ParamPackage modifier_param =
524 Common::ParamPackage{param.Get("modifier", "")};
525 const bool toggle_value = !modifier_param.Get("toggle", false);
526 modifier_param.Set("toggle", toggle_value);
527 param.Set("modifier", modifier_param.Serialize());
528 analog_map_modifier_button[analog_id]->setText(
529 ButtonToText(modifier_param));
530 emulated_controller->SetStickParam(analog_id, param);
531 });
532 context_menu.exec(
533 analog_map_modifier_button[analog_id]->mapToGlobal(menu_location));
534 }); 535 });
536 context_menu.addAction(tr("Toggle button"), [&] {
537 Common::ParamPackage modifier_param =
538 Common::ParamPackage{param.Get("modifier", "")};
539 const bool toggle_value = !modifier_param.Get("toggle", false);
540 modifier_param.Set("toggle", toggle_value);
541 param.Set("modifier", modifier_param.Serialize());
542 analog_map_modifier_button[analog_id]->setText(ButtonToText(modifier_param));
543 emulated_controller->SetStickParam(analog_id, param);
544 });
545 context_menu.addAction(tr("Invert button"), [&] {
546 Common::ParamPackage modifier_param =
547 Common::ParamPackage{param.Get("modifier", "")};
548 const bool invert_value = !modifier_param.Get("inverted", false);
549 modifier_param.Set("inverted", invert_value);
550 param.Set("modifier", modifier_param.Serialize());
551 analog_map_modifier_button[analog_id]->setText(ButtonToText(modifier_param));
552 emulated_controller->SetStickParam(analog_id, param);
553 });
554 context_menu.exec(
555 analog_map_modifier_button[analog_id]->mapToGlobal(menu_location));
556 });
535 557
536 connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged), 558 connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged),
537 [=, this] { 559 [=, this] {
@@ -762,7 +784,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
762 if (devices.size() == 1) { 784 if (devices.size() == 1) {
763 const auto devices_it = std::find_if( 785 const auto devices_it = std::find_if(
764 input_devices.begin(), input_devices.end(), 786 input_devices.begin(), input_devices.end(),
765 [first_engine, first_guid, first_port, first_pad](const Common::ParamPackage param) { 787 [first_engine, first_guid, first_port, first_pad](const Common::ParamPackage& param) {
766 return param.Get("engine", "") == first_engine && 788 return param.Get("engine", "") == first_engine &&
767 param.Get("guid", "") == first_guid && param.Get("port", 0) == first_port && 789 param.Get("guid", "") == first_guid && param.Get("port", 0) == first_port &&
768 param.Get("pad", 0) == first_pad; 790 param.Get("pad", 0) == first_pad;
@@ -793,7 +815,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
793 if (is_engine_equal && is_port_equal) { 815 if (is_engine_equal && is_port_equal) {
794 const auto devices_it = std::find_if( 816 const auto devices_it = std::find_if(
795 input_devices.begin(), input_devices.end(), 817 input_devices.begin(), input_devices.end(),
796 [first_engine, first_guid, second_guid, first_port](const Common::ParamPackage param) { 818 [first_engine, first_guid, second_guid, first_port](const Common::ParamPackage& param) {
797 const bool is_guid_valid = 819 const bool is_guid_valid =
798 (param.Get("guid", "") == first_guid && 820 (param.Get("guid", "") == first_guid &&
799 param.Get("guid2", "") == second_guid) || 821 param.Get("guid2", "") == second_guid) ||
@@ -1005,7 +1027,7 @@ int ConfigureInputPlayer::GetIndexFromControllerType(Core::HID::NpadStyleIndex t
1005void ConfigureInputPlayer::UpdateInputDevices() { 1027void ConfigureInputPlayer::UpdateInputDevices() {
1006 input_devices = input_subsystem->GetInputDevices(); 1028 input_devices = input_subsystem->GetInputDevices();
1007 ui->comboDevices->clear(); 1029 ui->comboDevices->clear();
1008 for (auto device : input_devices) { 1030 for (const auto& device : input_devices) {
1009 ui->comboDevices->addItem(QString::fromStdString(device.Get("display", "Unknown")), {}); 1031 ui->comboDevices->addItem(QString::fromStdString(device.Get("display", "Unknown")), {});
1010 } 1032 }
1011} 1033}
@@ -1287,7 +1309,7 @@ void ConfigureInputPlayer::HandleClick(
1287 } 1309 }
1288 button->setFocus(); 1310 button->setFocus();
1289 1311
1290 input_setter = new_input_setter; 1312 input_setter = std::move(new_input_setter);
1291 1313
1292 input_subsystem->BeginMapping(type); 1314 input_subsystem->BeginMapping(type);
1293 1315
@@ -1337,7 +1359,7 @@ bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params)
1337 return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse"; 1359 return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse";
1338 } 1360 }
1339 1361
1340 const auto current_input_device = input_devices[ui->comboDevices->currentIndex()]; 1362 const auto& current_input_device = input_devices[ui->comboDevices->currentIndex()];
1341 return params.Get("engine", "") == current_input_device.Get("engine", "") && 1363 return params.Get("engine", "") == current_input_device.Get("engine", "") &&
1342 (params.Get("guid", "") == current_input_device.Get("guid", "") || 1364 (params.Get("guid", "") == current_input_device.Get("guid", "") ||
1343 params.Get("guid", "") == current_input_device.Get("guid2", "")) && 1365 params.Get("guid", "") == current_input_device.Get("guid2", "")) &&
diff --git a/src/yuzu/configuration/configure_motion_touch.cpp b/src/yuzu/configuration/configure_motion_touch.cpp
index 8539a5c8b..4340de304 100644
--- a/src/yuzu/configuration/configure_motion_touch.cpp
+++ b/src/yuzu/configuration/configure_motion_touch.cpp
@@ -42,23 +42,25 @@ CalibrationConfigurationDialog::CalibrationConfigurationDialog(QWidget* parent,
42 job = std::make_unique<CalibrationConfigurationJob>( 42 job = std::make_unique<CalibrationConfigurationJob>(
43 host, port, 43 host, port,
44 [this](CalibrationConfigurationJob::Status status) { 44 [this](CalibrationConfigurationJob::Status status) {
45 QString text; 45 QMetaObject::invokeMethod(this, [status, this] {
46 switch (status) { 46 QString text;
47 case CalibrationConfigurationJob::Status::Ready: 47 switch (status) {
48 text = tr("Touch the top left corner <br>of your touchpad."); 48 case CalibrationConfigurationJob::Status::Ready:
49 break; 49 text = tr("Touch the top left corner <br>of your touchpad.");
50 case CalibrationConfigurationJob::Status::Stage1Completed: 50 break;
51 text = tr("Now touch the bottom right corner <br>of your touchpad."); 51 case CalibrationConfigurationJob::Status::Stage1Completed:
52 break; 52 text = tr("Now touch the bottom right corner <br>of your touchpad.");
53 case CalibrationConfigurationJob::Status::Completed: 53 break;
54 text = tr("Configuration completed!"); 54 case CalibrationConfigurationJob::Status::Completed:
55 break; 55 text = tr("Configuration completed!");
56 default: 56 break;
57 break; 57 default:
58 } 58 break;
59 QMetaObject::invokeMethod(this, "UpdateLabelText", Q_ARG(QString, text)); 59 }
60 UpdateLabelText(text);
61 });
60 if (status == CalibrationConfigurationJob::Status::Completed) { 62 if (status == CalibrationConfigurationJob::Status::Completed) {
61 QMetaObject::invokeMethod(this, "UpdateButtonText", Q_ARG(QString, tr("OK"))); 63 QMetaObject::invokeMethod(this, [this] { UpdateButtonText(tr("OK")); });
62 } 64 }
63 }, 65 },
64 [this](u16 min_x_, u16 min_y_, u16 max_x_, u16 max_y_) { 66 [this](u16 min_x_, u16 min_y_, u16 max_x_, u16 max_y_) {
@@ -215,11 +217,11 @@ void ConfigureMotionTouch::OnCemuhookUDPTest() {
215 ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()), 217 ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()),
216 [this] { 218 [this] {
217 LOG_INFO(Frontend, "UDP input test success"); 219 LOG_INFO(Frontend, "UDP input test success");
218 QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true)); 220 QMetaObject::invokeMethod(this, [this] { ShowUDPTestResult(true); });
219 }, 221 },
220 [this] { 222 [this] {
221 LOG_ERROR(Frontend, "UDP input test failed"); 223 LOG_ERROR(Frontend, "UDP input test failed");
222 QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, false)); 224 QMetaObject::invokeMethod(this, [this] { ShowUDPTestResult(false); });
223 }); 225 });
224} 226}
225 227
diff --git a/src/yuzu/game_list.cpp b/src/yuzu/game_list.cpp
index 8b5c4a10a..e3661b390 100644
--- a/src/yuzu/game_list.cpp
+++ b/src/yuzu/game_list.cpp
@@ -161,7 +161,7 @@ GameListSearchField::GameListSearchField(GameList* parent) : QWidget{parent} {
161 * @return true if the haystack contains all words of userinput 161 * @return true if the haystack contains all words of userinput
162 */ 162 */
163static bool ContainsAllWords(const QString& haystack, const QString& userinput) { 163static bool ContainsAllWords(const QString& haystack, const QString& userinput) {
164 const QStringList userinput_split = userinput.split(QLatin1Char{' '}, QString::SkipEmptyParts); 164 const QStringList userinput_split = userinput.split(QLatin1Char{' '}, Qt::SkipEmptyParts);
165 165
166 return std::all_of(userinput_split.begin(), userinput_split.end(), 166 return std::all_of(userinput_split.begin(), userinput_split.end(),
167 [&haystack](const QString& s) { return haystack.contains(s); }); 167 [&haystack](const QString& s) { return haystack.contains(s); });
diff --git a/src/yuzu/hotkeys.cpp b/src/yuzu/hotkeys.cpp
index d96497c4e..6ed9611c7 100644
--- a/src/yuzu/hotkeys.cpp
+++ b/src/yuzu/hotkeys.cpp
@@ -190,6 +190,9 @@ void ControllerShortcut::ControllerUpdateEvent(Core::HID::ControllerTriggerType
190 if (type != Core::HID::ControllerTriggerType::Button) { 190 if (type != Core::HID::ControllerTriggerType::Button) {
191 return; 191 return;
192 } 192 }
193 if (!Settings::values.controller_navigation) {
194 return;
195 }
193 if (button_sequence.npad.raw == Core::HID::NpadButton::None && 196 if (button_sequence.npad.raw == Core::HID::NpadButton::None &&
194 button_sequence.capture.raw == 0 && button_sequence.home.raw == 0) { 197 button_sequence.capture.raw == 0 && button_sequence.home.raw == 0) {
195 return; 198 return;
diff --git a/src/yuzu/util/controller_navigation.cpp b/src/yuzu/util/controller_navigation.cpp
index 86fb28b9f..c2b13123d 100644
--- a/src/yuzu/util/controller_navigation.cpp
+++ b/src/yuzu/util/controller_navigation.cpp
@@ -40,6 +40,9 @@ void ControllerNavigation::TriggerButton(Settings::NativeButton::Values native_b
40 40
41void ControllerNavigation::ControllerUpdateEvent(Core::HID::ControllerTriggerType type) { 41void ControllerNavigation::ControllerUpdateEvent(Core::HID::ControllerTriggerType type) {
42 std::lock_guard lock{mutex}; 42 std::lock_guard lock{mutex};
43 if (!Settings::values.controller_navigation) {
44 return;
45 }
43 if (type == Core::HID::ControllerTriggerType::Button) { 46 if (type == Core::HID::ControllerTriggerType::Button) {
44 ControllerUpdateButton(); 47 ControllerUpdateButton();
45 return; 48 return;