summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar liamwhite2023-05-07 19:05:56 -0400
committerGravatar GitHub2023-05-07 19:05:56 -0400
commit2688fb1aa2d80fc4edac87ff93365f3570cd3af8 (patch)
treeb874061d30aa6a03fd3c92116df92ed6d3b91a19 /src
parentMerge pull request #10097 from german77/nfp_full (diff)
parentTexture cache: Only force flush the dma downloads (diff)
downloadyuzu-2688fb1aa2d80fc4edac87ff93365f3570cd3af8.tar.gz
yuzu-2688fb1aa2d80fc4edac87ff93365f3570cd3af8.tar.xz
yuzu-2688fb1aa2d80fc4edac87ff93365f3570cd3af8.zip
Merge pull request #10155 from FernandoS27/reactive-flushing-new
Y.F.C. bring back Reactive Flushing
Diffstat (limited to 'src')
-rw-r--r--src/common/settings.cpp2
-rw-r--r--src/common/settings.h1
-rw-r--r--src/core/core.cpp4
-rw-r--r--src/core/core.h2
-rw-r--r--src/core/memory.cpp25
-rw-r--r--src/tests/video_core/memory_tracker.cpp4
-rw-r--r--src/video_core/buffer_cache/buffer_base.h9
-rw-r--r--src/video_core/buffer_cache/buffer_cache.h141
-rw-r--r--src/video_core/buffer_cache/buffer_cache_base.h7
-rw-r--r--src/video_core/buffer_cache/memory_tracker_base.h26
-rw-r--r--src/video_core/buffer_cache/word_manager.h27
-rw-r--r--src/video_core/engines/maxwell_dma.cpp8
-rw-r--r--src/video_core/fence_manager.h5
-rw-r--r--src/video_core/gpu.cpp19
-rw-r--r--src/video_core/gpu.h4
-rw-r--r--src/video_core/query_cache.h2
-rw-r--r--src/video_core/rasterizer_download_area.h16
-rw-r--r--src/video_core/rasterizer_interface.h3
-rw-r--r--src/video_core/renderer_null/null_rasterizer.cpp10
-rw-r--r--src/video_core/renderer_null/null_rasterizer.h1
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp25
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h1
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp22
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h1
-rw-r--r--src/video_core/texture_cache/image_info.cpp12
-rw-r--r--src/video_core/texture_cache/image_info.h2
-rw-r--r--src/video_core/texture_cache/image_view_base.cpp4
-rw-r--r--src/video_core/texture_cache/texture_cache.h45
-rw-r--r--src/video_core/texture_cache/texture_cache_base.h4
-rw-r--r--src/yuzu/configuration/config.cpp2
-rw-r--r--src/yuzu/configuration/configure_graphics_advanced.cpp7
-rw-r--r--src/yuzu/configuration/configure_graphics_advanced.h1
-rw-r--r--src/yuzu/configuration/configure_graphics_advanced.ui10
-rw-r--r--src/yuzu_cmd/config.cpp1
-rw-r--r--src/yuzu_cmd/default_ini.h4
35 files changed, 358 insertions, 99 deletions
diff --git a/src/common/settings.cpp b/src/common/settings.cpp
index f1ee42ab2..db1774c71 100644
--- a/src/common/settings.cpp
+++ b/src/common/settings.cpp
@@ -62,6 +62,7 @@ void LogSettings() {
62 log_setting("Renderer_AccelerateASTC", values.accelerate_astc.GetValue()); 62 log_setting("Renderer_AccelerateASTC", values.accelerate_astc.GetValue());
63 log_setting("Renderer_AsyncASTC", values.async_astc.GetValue()); 63 log_setting("Renderer_AsyncASTC", values.async_astc.GetValue());
64 log_setting("Renderer_UseVsync", values.vsync_mode.GetValue()); 64 log_setting("Renderer_UseVsync", values.vsync_mode.GetValue());
65 log_setting("Renderer_UseReactiveFlushing", values.use_reactive_flushing.GetValue());
65 log_setting("Renderer_ShaderBackend", values.shader_backend.GetValue()); 66 log_setting("Renderer_ShaderBackend", values.shader_backend.GetValue());
66 log_setting("Renderer_UseAsynchronousShaders", values.use_asynchronous_shaders.GetValue()); 67 log_setting("Renderer_UseAsynchronousShaders", values.use_asynchronous_shaders.GetValue());
67 log_setting("Renderer_AnisotropicFilteringLevel", values.max_anisotropy.GetValue()); 68 log_setting("Renderer_AnisotropicFilteringLevel", values.max_anisotropy.GetValue());
@@ -223,6 +224,7 @@ void RestoreGlobalState(bool is_powered_on) {
223 values.nvdec_emulation.SetGlobal(true); 224 values.nvdec_emulation.SetGlobal(true);
224 values.accelerate_astc.SetGlobal(true); 225 values.accelerate_astc.SetGlobal(true);
225 values.async_astc.SetGlobal(true); 226 values.async_astc.SetGlobal(true);
227 values.use_reactive_flushing.SetGlobal(true);
226 values.shader_backend.SetGlobal(true); 228 values.shader_backend.SetGlobal(true);
227 values.use_asynchronous_shaders.SetGlobal(true); 229 values.use_asynchronous_shaders.SetGlobal(true);
228 values.use_fast_gpu_time.SetGlobal(true); 230 values.use_fast_gpu_time.SetGlobal(true);
diff --git a/src/common/settings.h b/src/common/settings.h
index 2bf191cef..f4eb4e3cd 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -465,6 +465,7 @@ struct Values {
465 SwitchableSetting<bool> async_astc{false, "async_astc"}; 465 SwitchableSetting<bool> async_astc{false, "async_astc"};
466 Setting<VSyncMode, true> vsync_mode{VSyncMode::FIFO, VSyncMode::Immediate, 466 Setting<VSyncMode, true> vsync_mode{VSyncMode::FIFO, VSyncMode::Immediate,
467 VSyncMode::FIFORelaxed, "use_vsync"}; 467 VSyncMode::FIFORelaxed, "use_vsync"};
468 SwitchableSetting<bool> use_reactive_flushing{true, "use_reactive_flushing"};
468 SwitchableSetting<ShaderBackend, true> shader_backend{ShaderBackend::GLSL, ShaderBackend::GLSL, 469 SwitchableSetting<ShaderBackend, true> shader_backend{ShaderBackend::GLSL, ShaderBackend::GLSL,
469 ShaderBackend::SPIRV, "shader_backend"}; 470 ShaderBackend::SPIRV, "shader_backend"};
470 SwitchableSetting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"}; 471 SwitchableSetting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"};
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 06fba4ce5..b5f62690e 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -612,6 +612,10 @@ void System::PrepareReschedule(const u32 core_index) {
612 impl->kernel.PrepareReschedule(core_index); 612 impl->kernel.PrepareReschedule(core_index);
613} 613}
614 614
615size_t System::GetCurrentHostThreadID() const {
616 return impl->kernel.GetCurrentHostThreadID();
617}
618
615PerfStatsResults System::GetAndResetPerfStats() { 619PerfStatsResults System::GetAndResetPerfStats() {
616 return impl->GetAndResetPerfStats(); 620 return impl->GetAndResetPerfStats();
617} 621}
diff --git a/src/core/core.h b/src/core/core.h
index 4a5aba032..4f153154f 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -222,6 +222,8 @@ public:
222 /// Prepare the core emulation for a reschedule 222 /// Prepare the core emulation for a reschedule
223 void PrepareReschedule(u32 core_index); 223 void PrepareReschedule(u32 core_index);
224 224
225 [[nodiscard]] size_t GetCurrentHostThreadID() const;
226
225 /// Gets and resets core performance statistics 227 /// Gets and resets core performance statistics
226 [[nodiscard]] PerfStatsResults GetAndResetPerfStats(); 228 [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
227 229
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index a9667463f..514ba0d66 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -13,10 +13,12 @@
13#include "common/swap.h" 13#include "common/swap.h"
14#include "core/core.h" 14#include "core/core.h"
15#include "core/device_memory.h" 15#include "core/device_memory.h"
16#include "core/hardware_properties.h"
16#include "core/hle/kernel/k_page_table.h" 17#include "core/hle/kernel/k_page_table.h"
17#include "core/hle/kernel/k_process.h" 18#include "core/hle/kernel/k_process.h"
18#include "core/memory.h" 19#include "core/memory.h"
19#include "video_core/gpu.h" 20#include "video_core/gpu.h"
21#include "video_core/rasterizer_download_area.h"
20 22
21namespace Core::Memory { 23namespace Core::Memory {
22 24
@@ -243,7 +245,7 @@ struct Memory::Impl {
243 [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount, 245 [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount,
244 const u8* const host_ptr) { 246 const u8* const host_ptr) {
245 if constexpr (!UNSAFE) { 247 if constexpr (!UNSAFE) {
246 system.GPU().FlushRegion(GetInteger(current_vaddr), copy_amount); 248 HandleRasterizerDownload(GetInteger(current_vaddr), copy_amount);
247 } 249 }
248 std::memcpy(dest_buffer, host_ptr, copy_amount); 250 std::memcpy(dest_buffer, host_ptr, copy_amount);
249 }, 251 },
@@ -334,7 +336,7 @@ struct Memory::Impl {
334 }, 336 },
335 [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount, 337 [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount,
336 u8* const host_ptr) { 338 u8* const host_ptr) {
337 system.GPU().FlushRegion(GetInteger(current_vaddr), copy_amount); 339 HandleRasterizerDownload(GetInteger(current_vaddr), copy_amount);
338 WriteBlockImpl<false>(process, dest_addr, host_ptr, copy_amount); 340 WriteBlockImpl<false>(process, dest_addr, host_ptr, copy_amount);
339 }, 341 },
340 [&](const std::size_t copy_amount) { 342 [&](const std::size_t copy_amount) {
@@ -373,7 +375,7 @@ struct Memory::Impl {
373 const std::size_t block_size) { 375 const std::size_t block_size) {
374 // dc ivac: Invalidate to point of coherency 376 // dc ivac: Invalidate to point of coherency
375 // GPU flush -> CPU invalidate 377 // GPU flush -> CPU invalidate
376 system.GPU().FlushRegion(GetInteger(current_vaddr), block_size); 378 HandleRasterizerDownload(GetInteger(current_vaddr), block_size);
377 }; 379 };
378 return PerformCacheOperation(process, dest_addr, size, on_rasterizer); 380 return PerformCacheOperation(process, dest_addr, size, on_rasterizer);
379 } 381 }
@@ -462,7 +464,8 @@ struct Memory::Impl {
462 } 464 }
463 465
464 if (Settings::IsFastmemEnabled()) { 466 if (Settings::IsFastmemEnabled()) {
465 const bool is_read_enable = !Settings::IsGPULevelExtreme() || !cached; 467 const bool is_read_enable =
468 !Settings::values.use_reactive_flushing.GetValue() || !cached;
466 system.DeviceMemory().buffer.Protect(vaddr, size, is_read_enable, !cached); 469 system.DeviceMemory().buffer.Protect(vaddr, size, is_read_enable, !cached);
467 } 470 }
468 471
@@ -651,7 +654,7 @@ struct Memory::Impl {
651 LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:016X}", sizeof(T) * 8, 654 LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:016X}", sizeof(T) * 8,
652 GetInteger(vaddr)); 655 GetInteger(vaddr));
653 }, 656 },
654 [&]() { system.GPU().FlushRegion(GetInteger(vaddr), sizeof(T)); }); 657 [&]() { HandleRasterizerDownload(GetInteger(vaddr), sizeof(T)); });
655 if (ptr) { 658 if (ptr) {
656 std::memcpy(&result, ptr, sizeof(T)); 659 std::memcpy(&result, ptr, sizeof(T));
657 } 660 }
@@ -712,7 +715,19 @@ struct Memory::Impl {
712 return true; 715 return true;
713 } 716 }
714 717
718 void HandleRasterizerDownload(VAddr address, size_t size) {
719 const size_t core = system.GetCurrentHostThreadID();
720 auto& current_area = rasterizer_areas[core];
721 const VAddr end_address = address + size;
722 if (current_area.start_address <= address && end_address <= current_area.end_address)
723 [[likely]] {
724 return;
725 }
726 current_area = system.GPU().OnCPURead(address, size);
727 }
728
715 Common::PageTable* current_page_table = nullptr; 729 Common::PageTable* current_page_table = nullptr;
730 std::array<VideoCore::RasterizerDownloadArea, Core::Hardware::NUM_CPU_CORES> rasterizer_areas{};
716 Core::System& system; 731 Core::System& system;
717}; 732};
718 733
diff --git a/src/tests/video_core/memory_tracker.cpp b/src/tests/video_core/memory_tracker.cpp
index 3981907a2..618793668 100644
--- a/src/tests/video_core/memory_tracker.cpp
+++ b/src/tests/video_core/memory_tracker.cpp
@@ -535,12 +535,12 @@ TEST_CASE("MemoryTracker: Cached write downloads") {
535 memory_track->MarkRegionAsGpuModified(c + PAGE, PAGE); 535 memory_track->MarkRegionAsGpuModified(c + PAGE, PAGE);
536 int num = 0; 536 int num = 0;
537 memory_track->ForEachDownloadRangeAndClear(c, WORD, [&](u64 offset, u64 size) { ++num; }); 537 memory_track->ForEachDownloadRangeAndClear(c, WORD, [&](u64 offset, u64 size) { ++num; });
538 REQUIRE(num == 1); 538 REQUIRE(num == 0);
539 num = 0; 539 num = 0;
540 memory_track->ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) { ++num; }); 540 memory_track->ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) { ++num; });
541 REQUIRE(num == 0); 541 REQUIRE(num == 0);
542 REQUIRE(!memory_track->IsRegionCpuModified(c + PAGE, PAGE)); 542 REQUIRE(!memory_track->IsRegionCpuModified(c + PAGE, PAGE));
543 REQUIRE(!memory_track->IsRegionGpuModified(c + PAGE, PAGE)); 543 REQUIRE(memory_track->IsRegionGpuModified(c + PAGE, PAGE));
544 memory_track->FlushCachedWrites(); 544 memory_track->FlushCachedWrites();
545 REQUIRE(memory_track->IsRegionCpuModified(c + PAGE, PAGE)); 545 REQUIRE(memory_track->IsRegionCpuModified(c + PAGE, PAGE));
546 REQUIRE(!memory_track->IsRegionGpuModified(c + PAGE, PAGE)); 546 REQUIRE(!memory_track->IsRegionGpuModified(c + PAGE, PAGE));
diff --git a/src/video_core/buffer_cache/buffer_base.h b/src/video_core/buffer_cache/buffer_base.h
index 9cbd95c4b..0bb3bf8ae 100644
--- a/src/video_core/buffer_cache/buffer_base.h
+++ b/src/video_core/buffer_cache/buffer_base.h
@@ -18,6 +18,7 @@ namespace VideoCommon {
18enum class BufferFlagBits { 18enum class BufferFlagBits {
19 Picked = 1 << 0, 19 Picked = 1 << 0,
20 CachedWrites = 1 << 1, 20 CachedWrites = 1 << 1,
21 PreemtiveDownload = 1 << 2,
21}; 22};
22DECLARE_ENUM_FLAG_OPERATORS(BufferFlagBits) 23DECLARE_ENUM_FLAG_OPERATORS(BufferFlagBits)
23 24
@@ -54,6 +55,10 @@ public:
54 flags |= BufferFlagBits::Picked; 55 flags |= BufferFlagBits::Picked;
55 } 56 }
56 57
58 void MarkPreemtiveDownload() noexcept {
59 flags |= BufferFlagBits::PreemtiveDownload;
60 }
61
57 /// Unmark buffer as picked 62 /// Unmark buffer as picked
58 void Unpick() noexcept { 63 void Unpick() noexcept {
59 flags &= ~BufferFlagBits::Picked; 64 flags &= ~BufferFlagBits::Picked;
@@ -84,6 +89,10 @@ public:
84 return True(flags & BufferFlagBits::CachedWrites); 89 return True(flags & BufferFlagBits::CachedWrites);
85 } 90 }
86 91
92 bool IsPreemtiveDownload() const noexcept {
93 return True(flags & BufferFlagBits::PreemtiveDownload);
94 }
95
87 /// Returns the base CPU address of the buffer 96 /// Returns the base CPU address of the buffer
88 [[nodiscard]] VAddr CpuAddr() const noexcept { 97 [[nodiscard]] VAddr CpuAddr() const noexcept {
89 return cpu_addr; 98 return cpu_addr;
diff --git a/src/video_core/buffer_cache/buffer_cache.h b/src/video_core/buffer_cache/buffer_cache.h
index e534e1e9c..6624919a4 100644
--- a/src/video_core/buffer_cache/buffer_cache.h
+++ b/src/video_core/buffer_cache/buffer_cache.h
@@ -23,8 +23,6 @@ BufferCache<P>::BufferCache(VideoCore::RasterizerInterface& rasterizer_,
23 common_ranges.clear(); 23 common_ranges.clear();
24 inline_buffer_id = NULL_BUFFER_ID; 24 inline_buffer_id = NULL_BUFFER_ID;
25 25
26 active_async_buffers = !Settings::IsGPULevelHigh();
27
28 if (!runtime.CanReportMemoryUsage()) { 26 if (!runtime.CanReportMemoryUsage()) {
29 minimum_memory = DEFAULT_EXPECTED_MEMORY; 27 minimum_memory = DEFAULT_EXPECTED_MEMORY;
30 critical_memory = DEFAULT_CRITICAL_MEMORY; 28 critical_memory = DEFAULT_CRITICAL_MEMORY;
@@ -75,8 +73,6 @@ void BufferCache<P>::TickFrame() {
75 uniform_cache_hits[0] = 0; 73 uniform_cache_hits[0] = 0;
76 uniform_cache_shots[0] = 0; 74 uniform_cache_shots[0] = 0;
77 75
78 active_async_buffers = !Settings::IsGPULevelHigh();
79
80 const bool skip_preferred = hits * 256 < shots * 251; 76 const bool skip_preferred = hits * 256 < shots * 251;
81 uniform_buffer_skip_cache_size = skip_preferred ? DEFAULT_SKIP_CACHE_SIZE : 0; 77 uniform_buffer_skip_cache_size = skip_preferred ? DEFAULT_SKIP_CACHE_SIZE : 0;
82 78
@@ -111,9 +107,25 @@ void BufferCache<P>::WriteMemory(VAddr cpu_addr, u64 size) {
111template <class P> 107template <class P>
112void BufferCache<P>::CachedWriteMemory(VAddr cpu_addr, u64 size) { 108void BufferCache<P>::CachedWriteMemory(VAddr cpu_addr, u64 size) {
113 memory_tracker.CachedCpuWrite(cpu_addr, size); 109 memory_tracker.CachedCpuWrite(cpu_addr, size);
114 const IntervalType add_interval{Common::AlignDown(cpu_addr, YUZU_PAGESIZE), 110}
115 Common::AlignUp(cpu_addr + size, YUZU_PAGESIZE)}; 111
116 cached_ranges.add(add_interval); 112template <class P>
113std::optional<VideoCore::RasterizerDownloadArea> BufferCache<P>::GetFlushArea(VAddr cpu_addr,
114 u64 size) {
115 std::optional<VideoCore::RasterizerDownloadArea> area{};
116 area.emplace();
117 VAddr cpu_addr_start_aligned = Common::AlignDown(cpu_addr, Core::Memory::YUZU_PAGESIZE);
118 VAddr cpu_addr_end_aligned = Common::AlignUp(cpu_addr + size, Core::Memory::YUZU_PAGESIZE);
119 area->start_address = cpu_addr_start_aligned;
120 area->end_address = cpu_addr_end_aligned;
121 if (memory_tracker.IsRegionPreflushable(cpu_addr, size)) {
122 area->preemtive = true;
123 return area;
124 };
125 memory_tracker.MarkRegionAsPreflushable(cpu_addr_start_aligned,
126 cpu_addr_end_aligned - cpu_addr_start_aligned);
127 area->preemtive = !IsRegionGpuModified(cpu_addr, size);
128 return area;
117} 129}
118 130
119template <class P> 131template <class P>
@@ -205,7 +217,7 @@ bool BufferCache<P>::DMACopy(GPUVAddr src_address, GPUVAddr dest_address, u64 am
205 if (has_new_downloads) { 217 if (has_new_downloads) {
206 memory_tracker.MarkRegionAsGpuModified(*cpu_dest_address, amount); 218 memory_tracker.MarkRegionAsGpuModified(*cpu_dest_address, amount);
207 } 219 }
208 std::vector<u8> tmp_buffer(amount); 220 tmp_buffer.resize(amount);
209 cpu_memory.ReadBlockUnsafe(*cpu_src_address, tmp_buffer.data(), amount); 221 cpu_memory.ReadBlockUnsafe(*cpu_src_address, tmp_buffer.data(), amount);
210 cpu_memory.WriteBlockUnsafe(*cpu_dest_address, tmp_buffer.data(), amount); 222 cpu_memory.WriteBlockUnsafe(*cpu_dest_address, tmp_buffer.data(), amount);
211 return true; 223 return true;
@@ -441,9 +453,7 @@ void BufferCache<P>::BindComputeTextureBuffer(size_t tbo_index, GPUVAddr gpu_add
441 453
442template <class P> 454template <class P>
443void BufferCache<P>::FlushCachedWrites() { 455void BufferCache<P>::FlushCachedWrites() {
444 cached_write_buffer_ids.clear();
445 memory_tracker.FlushCachedWrites(); 456 memory_tracker.FlushCachedWrites();
446 cached_ranges.clear();
447} 457}
448 458
449template <class P> 459template <class P>
@@ -474,9 +484,8 @@ void BufferCache<P>::CommitAsyncFlushesHigh() {
474 484
475 if (committed_ranges.empty()) { 485 if (committed_ranges.empty()) {
476 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { 486 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) {
477 if (active_async_buffers) { 487
478 async_buffers.emplace_back(std::optional<Async_Buffer>{}); 488 async_buffers.emplace_back(std::optional<Async_Buffer>{});
479 }
480 } 489 }
481 return; 490 return;
482 } 491 }
@@ -537,64 +546,65 @@ void BufferCache<P>::CommitAsyncFlushesHigh() {
537 committed_ranges.clear(); 546 committed_ranges.clear();
538 if (downloads.empty()) { 547 if (downloads.empty()) {
539 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { 548 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) {
540 if (active_async_buffers) { 549
541 async_buffers.emplace_back(std::optional<Async_Buffer>{}); 550 async_buffers.emplace_back(std::optional<Async_Buffer>{});
542 }
543 } 551 }
544 return; 552 return;
545 } 553 }
546 if (active_async_buffers) { 554 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) {
547 if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { 555 auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes, true);
548 auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes, true); 556 boost::container::small_vector<BufferCopy, 4> normalized_copies;
549 boost::container::small_vector<BufferCopy, 4> normalized_copies; 557 IntervalSet new_async_range{};
550 IntervalSet new_async_range{}; 558 runtime.PreCopyBarrier();
551 runtime.PreCopyBarrier(); 559 for (auto& [copy, buffer_id] : downloads) {
552 for (auto& [copy, buffer_id] : downloads) { 560 copy.dst_offset += download_staging.offset;
553 copy.dst_offset += download_staging.offset; 561 const std::array copies{copy};
554 const std::array copies{copy}; 562 BufferCopy second_copy{copy};
555 BufferCopy second_copy{copy}; 563 Buffer& buffer = slot_buffers[buffer_id];
556 Buffer& buffer = slot_buffers[buffer_id]; 564 second_copy.src_offset = static_cast<size_t>(buffer.CpuAddr()) + copy.src_offset;
557 second_copy.src_offset = static_cast<size_t>(buffer.CpuAddr()) + copy.src_offset; 565 VAddr orig_cpu_addr = static_cast<VAddr>(second_copy.src_offset);
558 VAddr orig_cpu_addr = static_cast<VAddr>(second_copy.src_offset); 566 const IntervalType base_interval{orig_cpu_addr, orig_cpu_addr + copy.size};
559 const IntervalType base_interval{orig_cpu_addr, orig_cpu_addr + copy.size}; 567 async_downloads += std::make_pair(base_interval, 1);
560 async_downloads += std::make_pair(base_interval, 1); 568 runtime.CopyBuffer(download_staging.buffer, buffer, copies, false);
561 runtime.CopyBuffer(download_staging.buffer, buffer, copies, false); 569 normalized_copies.push_back(second_copy);
562 normalized_copies.push_back(second_copy);
563 }
564 runtime.PostCopyBarrier();
565 pending_downloads.emplace_back(std::move(normalized_copies));
566 async_buffers.emplace_back(download_staging);
567 } else {
568 committed_ranges.clear();
569 uncommitted_ranges.clear();
570 } 570 }
571 runtime.PostCopyBarrier();
572 pending_downloads.emplace_back(std::move(normalized_copies));
573 async_buffers.emplace_back(download_staging);
571 } else { 574 } else {
572 if constexpr (USE_MEMORY_MAPS) { 575 if (!Settings::IsGPULevelHigh()) {
573 auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes); 576 committed_ranges.clear();
574 runtime.PreCopyBarrier(); 577 uncommitted_ranges.clear();
575 for (auto& [copy, buffer_id] : downloads) {
576 // Have in mind the staging buffer offset for the copy
577 copy.dst_offset += download_staging.offset;
578 const std::array copies{copy};
579 runtime.CopyBuffer(download_staging.buffer, slot_buffers[buffer_id], copies, false);
580 }
581 runtime.PostCopyBarrier();
582 runtime.Finish();
583 for (const auto& [copy, buffer_id] : downloads) {
584 const Buffer& buffer = slot_buffers[buffer_id];
585 const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset;
586 // Undo the modified offset
587 const u64 dst_offset = copy.dst_offset - download_staging.offset;
588 const u8* read_mapped_memory = download_staging.mapped_span.data() + dst_offset;
589 cpu_memory.WriteBlockUnsafe(cpu_addr, read_mapped_memory, copy.size);
590 }
591 } else { 578 } else {
592 const std::span<u8> immediate_buffer = ImmediateBuffer(largest_copy); 579 if constexpr (USE_MEMORY_MAPS) {
593 for (const auto& [copy, buffer_id] : downloads) { 580 auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes);
594 Buffer& buffer = slot_buffers[buffer_id]; 581 runtime.PreCopyBarrier();
595 buffer.ImmediateDownload(copy.src_offset, immediate_buffer.subspan(0, copy.size)); 582 for (auto& [copy, buffer_id] : downloads) {
596 const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset; 583 // Have in mind the staging buffer offset for the copy
597 cpu_memory.WriteBlockUnsafe(cpu_addr, immediate_buffer.data(), copy.size); 584 copy.dst_offset += download_staging.offset;
585 const std::array copies{copy};
586 runtime.CopyBuffer(download_staging.buffer, slot_buffers[buffer_id], copies,
587 false);
588 }
589 runtime.PostCopyBarrier();
590 runtime.Finish();
591 for (const auto& [copy, buffer_id] : downloads) {
592 const Buffer& buffer = slot_buffers[buffer_id];
593 const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset;
594 // Undo the modified offset
595 const u64 dst_offset = copy.dst_offset - download_staging.offset;
596 const u8* read_mapped_memory = download_staging.mapped_span.data() + dst_offset;
597 cpu_memory.WriteBlockUnsafe(cpu_addr, read_mapped_memory, copy.size);
598 }
599 } else {
600 const std::span<u8> immediate_buffer = ImmediateBuffer(largest_copy);
601 for (const auto& [copy, buffer_id] : downloads) {
602 Buffer& buffer = slot_buffers[buffer_id];
603 buffer.ImmediateDownload(copy.src_offset,
604 immediate_buffer.subspan(0, copy.size));
605 const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset;
606 cpu_memory.WriteBlockUnsafe(cpu_addr, immediate_buffer.data(), copy.size);
607 }
598 } 608 }
599 } 609 }
600 } 610 }
@@ -1629,7 +1639,6 @@ void BufferCache<P>::DeleteBuffer(BufferId buffer_id, bool do_not_mark) {
1629 replace(transform_feedback_buffers); 1639 replace(transform_feedback_buffers);
1630 replace(compute_uniform_buffers); 1640 replace(compute_uniform_buffers);
1631 replace(compute_storage_buffers); 1641 replace(compute_storage_buffers);
1632 std::erase(cached_write_buffer_ids, buffer_id);
1633 1642
1634 // Mark the whole buffer as CPU written to stop tracking CPU writes 1643 // Mark the whole buffer as CPU written to stop tracking CPU writes
1635 if (!do_not_mark) { 1644 if (!do_not_mark) {
diff --git a/src/video_core/buffer_cache/buffer_cache_base.h b/src/video_core/buffer_cache/buffer_cache_base.h
index 656baa550..0445ec47f 100644
--- a/src/video_core/buffer_cache/buffer_cache_base.h
+++ b/src/video_core/buffer_cache/buffer_cache_base.h
@@ -188,6 +188,8 @@ public:
188 188
189 void DownloadMemory(VAddr cpu_addr, u64 size); 189 void DownloadMemory(VAddr cpu_addr, u64 size);
190 190
191 std::optional<VideoCore::RasterizerDownloadArea> GetFlushArea(VAddr cpu_addr, u64 size);
192
191 bool InlineMemory(VAddr dest_address, size_t copy_size, std::span<const u8> inlined_buffer); 193 bool InlineMemory(VAddr dest_address, size_t copy_size, std::span<const u8> inlined_buffer);
192 194
193 void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size); 195 void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size);
@@ -541,8 +543,6 @@ private:
541 std::array<std::array<u32, NUM_GRAPHICS_UNIFORM_BUFFERS>, NUM_STAGES>, Empty> 543 std::array<std::array<u32, NUM_GRAPHICS_UNIFORM_BUFFERS>, NUM_STAGES>, Empty>
542 uniform_buffer_binding_sizes{}; 544 uniform_buffer_binding_sizes{};
543 545
544 std::vector<BufferId> cached_write_buffer_ids;
545
546 MemoryTracker memory_tracker; 546 MemoryTracker memory_tracker;
547 IntervalSet uncommitted_ranges; 547 IntervalSet uncommitted_ranges;
548 IntervalSet common_ranges; 548 IntervalSet common_ranges;
@@ -572,9 +572,8 @@ private:
572 u64 critical_memory = 0; 572 u64 critical_memory = 0;
573 BufferId inline_buffer_id; 573 BufferId inline_buffer_id;
574 574
575 bool active_async_buffers = false;
576
577 std::array<BufferId, ((1ULL << 39) >> CACHING_PAGEBITS)> page_table; 575 std::array<BufferId, ((1ULL << 39) >> CACHING_PAGEBITS)> page_table;
576 std::vector<u8> tmp_buffer;
578}; 577};
579 578
580} // namespace VideoCommon 579} // namespace VideoCommon
diff --git a/src/video_core/buffer_cache/memory_tracker_base.h b/src/video_core/buffer_cache/memory_tracker_base.h
index dc4ebfcaa..6036b21c9 100644
--- a/src/video_core/buffer_cache/memory_tracker_base.h
+++ b/src/video_core/buffer_cache/memory_tracker_base.h
@@ -66,6 +66,14 @@ public:
66 }); 66 });
67 } 67 }
68 68
69 /// Returns true if a region has been marked as Preflushable
70 [[nodiscard]] bool IsRegionPreflushable(VAddr query_cpu_addr, u64 query_size) noexcept {
71 return IteratePages<false>(
72 query_cpu_addr, query_size, [](Manager* manager, u64 offset, size_t size) {
73 return manager->template IsRegionModified<Type::Preflushable>(offset, size);
74 });
75 }
76
69 /// Mark region as CPU modified, notifying the rasterizer about this change 77 /// Mark region as CPU modified, notifying the rasterizer about this change
70 void MarkRegionAsCpuModified(VAddr dirty_cpu_addr, u64 query_size) { 78 void MarkRegionAsCpuModified(VAddr dirty_cpu_addr, u64 query_size) {
71 IteratePages<true>(dirty_cpu_addr, query_size, 79 IteratePages<true>(dirty_cpu_addr, query_size,
@@ -93,6 +101,15 @@ public:
93 }); 101 });
94 } 102 }
95 103
104 /// Mark region as modified from the host GPU
105 void MarkRegionAsPreflushable(VAddr dirty_cpu_addr, u64 query_size) noexcept {
106 IteratePages<true>(dirty_cpu_addr, query_size,
107 [](Manager* manager, u64 offset, size_t size) {
108 manager->template ChangeRegionState<Type::Preflushable, true>(
109 manager->GetCpuAddr() + offset, size);
110 });
111 }
112
96 /// Unmark region as modified from the host GPU 113 /// Unmark region as modified from the host GPU
97 void UnmarkRegionAsGpuModified(VAddr dirty_cpu_addr, u64 query_size) noexcept { 114 void UnmarkRegionAsGpuModified(VAddr dirty_cpu_addr, u64 query_size) noexcept {
98 IteratePages<true>(dirty_cpu_addr, query_size, 115 IteratePages<true>(dirty_cpu_addr, query_size,
@@ -102,6 +119,15 @@ public:
102 }); 119 });
103 } 120 }
104 121
122 /// Unmark region as modified from the host GPU
123 void UnmarkRegionAsPreflushable(VAddr dirty_cpu_addr, u64 query_size) noexcept {
124 IteratePages<true>(dirty_cpu_addr, query_size,
125 [](Manager* manager, u64 offset, size_t size) {
126 manager->template ChangeRegionState<Type::Preflushable, false>(
127 manager->GetCpuAddr() + offset, size);
128 });
129 }
130
105 /// Mark region as modified from the CPU 131 /// Mark region as modified from the CPU
106 /// but don't mark it as modified until FlusHCachedWrites is called. 132 /// but don't mark it as modified until FlusHCachedWrites is called.
107 void CachedCpuWrite(VAddr dirty_cpu_addr, u64 query_size) { 133 void CachedCpuWrite(VAddr dirty_cpu_addr, u64 query_size) {
diff --git a/src/video_core/buffer_cache/word_manager.h b/src/video_core/buffer_cache/word_manager.h
index a42455045..a336bde41 100644
--- a/src/video_core/buffer_cache/word_manager.h
+++ b/src/video_core/buffer_cache/word_manager.h
@@ -26,6 +26,7 @@ enum class Type {
26 GPU, 26 GPU,
27 CachedCPU, 27 CachedCPU,
28 Untracked, 28 Untracked,
29 Preflushable,
29}; 30};
30 31
31/// Vector tracking modified pages tightly packed with small vector optimization 32/// Vector tracking modified pages tightly packed with small vector optimization
@@ -55,17 +56,20 @@ struct Words {
55 gpu.stack.fill(0); 56 gpu.stack.fill(0);
56 cached_cpu.stack.fill(0); 57 cached_cpu.stack.fill(0);
57 untracked.stack.fill(~u64{0}); 58 untracked.stack.fill(~u64{0});
59 preflushable.stack.fill(0);
58 } else { 60 } else {
59 // Share allocation between CPU and GPU pages and set their default values 61 // Share allocation between CPU and GPU pages and set their default values
60 u64* const alloc = new u64[num_words * 4]; 62 u64* const alloc = new u64[num_words * 5];
61 cpu.heap = alloc; 63 cpu.heap = alloc;
62 gpu.heap = alloc + num_words; 64 gpu.heap = alloc + num_words;
63 cached_cpu.heap = alloc + num_words * 2; 65 cached_cpu.heap = alloc + num_words * 2;
64 untracked.heap = alloc + num_words * 3; 66 untracked.heap = alloc + num_words * 3;
67 preflushable.heap = alloc + num_words * 4;
65 std::fill_n(cpu.heap, num_words, ~u64{0}); 68 std::fill_n(cpu.heap, num_words, ~u64{0});
66 std::fill_n(gpu.heap, num_words, 0); 69 std::fill_n(gpu.heap, num_words, 0);
67 std::fill_n(cached_cpu.heap, num_words, 0); 70 std::fill_n(cached_cpu.heap, num_words, 0);
68 std::fill_n(untracked.heap, num_words, ~u64{0}); 71 std::fill_n(untracked.heap, num_words, ~u64{0});
72 std::fill_n(preflushable.heap, num_words, 0);
69 } 73 }
70 // Clean up tailing bits 74 // Clean up tailing bits
71 const u64 last_word_size = size_bytes % BYTES_PER_WORD; 75 const u64 last_word_size = size_bytes % BYTES_PER_WORD;
@@ -88,13 +92,14 @@ struct Words {
88 gpu = rhs.gpu; 92 gpu = rhs.gpu;
89 cached_cpu = rhs.cached_cpu; 93 cached_cpu = rhs.cached_cpu;
90 untracked = rhs.untracked; 94 untracked = rhs.untracked;
95 preflushable = rhs.preflushable;
91 rhs.cpu.heap = nullptr; 96 rhs.cpu.heap = nullptr;
92 return *this; 97 return *this;
93 } 98 }
94 99
95 Words(Words&& rhs) noexcept 100 Words(Words&& rhs) noexcept
96 : size_bytes{rhs.size_bytes}, num_words{rhs.num_words}, cpu{rhs.cpu}, gpu{rhs.gpu}, 101 : size_bytes{rhs.size_bytes}, num_words{rhs.num_words}, cpu{rhs.cpu}, gpu{rhs.gpu},
97 cached_cpu{rhs.cached_cpu}, untracked{rhs.untracked} { 102 cached_cpu{rhs.cached_cpu}, untracked{rhs.untracked}, preflushable{rhs.preflushable} {
98 rhs.cpu.heap = nullptr; 103 rhs.cpu.heap = nullptr;
99 } 104 }
100 105
@@ -129,6 +134,8 @@ struct Words {
129 return std::span<u64>(cached_cpu.Pointer(IsShort()), num_words); 134 return std::span<u64>(cached_cpu.Pointer(IsShort()), num_words);
130 } else if constexpr (type == Type::Untracked) { 135 } else if constexpr (type == Type::Untracked) {
131 return std::span<u64>(untracked.Pointer(IsShort()), num_words); 136 return std::span<u64>(untracked.Pointer(IsShort()), num_words);
137 } else if constexpr (type == Type::Preflushable) {
138 return std::span<u64>(preflushable.Pointer(IsShort()), num_words);
132 } 139 }
133 } 140 }
134 141
@@ -142,6 +149,8 @@ struct Words {
142 return std::span<const u64>(cached_cpu.Pointer(IsShort()), num_words); 149 return std::span<const u64>(cached_cpu.Pointer(IsShort()), num_words);
143 } else if constexpr (type == Type::Untracked) { 150 } else if constexpr (type == Type::Untracked) {
144 return std::span<const u64>(untracked.Pointer(IsShort()), num_words); 151 return std::span<const u64>(untracked.Pointer(IsShort()), num_words);
152 } else if constexpr (type == Type::Preflushable) {
153 return std::span<const u64>(preflushable.Pointer(IsShort()), num_words);
145 } 154 }
146 } 155 }
147 156
@@ -151,6 +160,7 @@ struct Words {
151 WordsArray<stack_words> gpu; 160 WordsArray<stack_words> gpu;
152 WordsArray<stack_words> cached_cpu; 161 WordsArray<stack_words> cached_cpu;
153 WordsArray<stack_words> untracked; 162 WordsArray<stack_words> untracked;
163 WordsArray<stack_words> preflushable;
154}; 164};
155 165
156template <class RasterizerInterface, size_t stack_words = 1> 166template <class RasterizerInterface, size_t stack_words = 1>
@@ -292,6 +302,9 @@ public:
292 (pending_pointer - pending_offset) * BYTES_PER_PAGE); 302 (pending_pointer - pending_offset) * BYTES_PER_PAGE);
293 }; 303 };
294 IterateWords(offset, size, [&](size_t index, u64 mask) { 304 IterateWords(offset, size, [&](size_t index, u64 mask) {
305 if constexpr (type == Type::GPU) {
306 mask &= ~untracked_words[index];
307 }
295 const u64 word = state_words[index] & mask; 308 const u64 word = state_words[index] & mask;
296 if constexpr (clear) { 309 if constexpr (clear) {
297 if constexpr (type == Type::CPU || type == Type::CachedCPU) { 310 if constexpr (type == Type::CPU || type == Type::CachedCPU) {
@@ -340,8 +353,13 @@ public:
340 static_assert(type != Type::Untracked); 353 static_assert(type != Type::Untracked);
341 354
342 const std::span<const u64> state_words = words.template Span<type>(); 355 const std::span<const u64> state_words = words.template Span<type>();
356 [[maybe_unused]] const std::span<const u64> untracked_words =
357 words.template Span<Type::Untracked>();
343 bool result = false; 358 bool result = false;
344 IterateWords(offset, size, [&](size_t index, u64 mask) { 359 IterateWords(offset, size, [&](size_t index, u64 mask) {
360 if constexpr (type == Type::GPU) {
361 mask &= ~untracked_words[index];
362 }
345 const u64 word = state_words[index] & mask; 363 const u64 word = state_words[index] & mask;
346 if (word != 0) { 364 if (word != 0) {
347 result = true; 365 result = true;
@@ -362,9 +380,14 @@ public:
362 [[nodiscard]] std::pair<u64, u64> ModifiedRegion(u64 offset, u64 size) const noexcept { 380 [[nodiscard]] std::pair<u64, u64> ModifiedRegion(u64 offset, u64 size) const noexcept {
363 static_assert(type != Type::Untracked); 381 static_assert(type != Type::Untracked);
364 const std::span<const u64> state_words = words.template Span<type>(); 382 const std::span<const u64> state_words = words.template Span<type>();
383 [[maybe_unused]] const std::span<const u64> untracked_words =
384 words.template Span<Type::Untracked>();
365 u64 begin = std::numeric_limits<u64>::max(); 385 u64 begin = std::numeric_limits<u64>::max();
366 u64 end = 0; 386 u64 end = 0;
367 IterateWords(offset, size, [&](size_t index, u64 mask) { 387 IterateWords(offset, size, [&](size_t index, u64 mask) {
388 if constexpr (type == Type::GPU) {
389 mask &= ~untracked_words[index];
390 }
368 const u64 word = state_words[index] & mask; 391 const u64 word = state_words[index] & mask;
369 if (word == 0) { 392 if (word == 0) {
370 return; 393 return;
diff --git a/src/video_core/engines/maxwell_dma.cpp b/src/video_core/engines/maxwell_dma.cpp
index e68850dc5..ebe5536de 100644
--- a/src/video_core/engines/maxwell_dma.cpp
+++ b/src/video_core/engines/maxwell_dma.cpp
@@ -223,7 +223,7 @@ void MaxwellDMA::CopyBlockLinearToPitch() {
223 write_buffer.resize_destructive(dst_size); 223 write_buffer.resize_destructive(dst_size);
224 224
225 memory_manager.ReadBlock(src_operand.address, read_buffer.data(), src_size); 225 memory_manager.ReadBlock(src_operand.address, read_buffer.data(), src_size);
226 memory_manager.ReadBlockUnsafe(dst_operand.address, write_buffer.data(), dst_size); 226 memory_manager.ReadBlock(dst_operand.address, write_buffer.data(), dst_size);
227 227
228 UnswizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset, 228 UnswizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset,
229 src_params.origin.y, x_elements, regs.line_count, block_height, block_depth, 229 src_params.origin.y, x_elements, regs.line_count, block_height, block_depth,
@@ -288,11 +288,7 @@ void MaxwellDMA::CopyPitchToBlockLinear() {
288 write_buffer.resize_destructive(dst_size); 288 write_buffer.resize_destructive(dst_size);
289 289
290 memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size); 290 memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
291 if (Settings::IsGPULevelExtreme()) { 291 memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
292 memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
293 } else {
294 memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
295 }
296 292
297 // If the input is linear and the output is tiled, swizzle the input and copy it over. 293 // If the input is linear and the output is tiled, swizzle the input and copy it over.
298 SwizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset, 294 SwizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset,
diff --git a/src/video_core/fence_manager.h b/src/video_core/fence_manager.h
index 3b2f6aab6..35d699bbf 100644
--- a/src/video_core/fence_manager.h
+++ b/src/video_core/fence_manager.h
@@ -59,6 +59,11 @@ public:
59 buffer_cache.AccumulateFlushes(); 59 buffer_cache.AccumulateFlushes();
60 } 60 }
61 61
62 void SignalReference() {
63 std::function<void()> do_nothing([] {});
64 SignalFence(std::move(do_nothing));
65 }
66
62 void SyncOperation(std::function<void()>&& func) { 67 void SyncOperation(std::function<void()>&& func) {
63 uncommitted_operations.emplace_back(std::move(func)); 68 uncommitted_operations.emplace_back(std::move(func));
64 } 69 }
diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp
index 2e7f9c5ed..295a416a8 100644
--- a/src/video_core/gpu.cpp
+++ b/src/video_core/gpu.cpp
@@ -283,6 +283,21 @@ struct GPU::Impl {
283 gpu_thread.FlushRegion(addr, size); 283 gpu_thread.FlushRegion(addr, size);
284 } 284 }
285 285
286 VideoCore::RasterizerDownloadArea OnCPURead(VAddr addr, u64 size) {
287 auto raster_area = rasterizer->GetFlushArea(addr, size);
288 if (raster_area.preemtive) {
289 return raster_area;
290 }
291 raster_area.preemtive = true;
292 const u64 fence = RequestSyncOperation([this, &raster_area]() {
293 rasterizer->FlushRegion(raster_area.start_address,
294 raster_area.end_address - raster_area.start_address);
295 });
296 gpu_thread.TickGPU();
297 WaitForSyncOperation(fence);
298 return raster_area;
299 }
300
286 /// Notify rasterizer that any caches of the specified region should be invalidated 301 /// Notify rasterizer that any caches of the specified region should be invalidated
287 void InvalidateRegion(VAddr addr, u64 size) { 302 void InvalidateRegion(VAddr addr, u64 size) {
288 gpu_thread.InvalidateRegion(addr, size); 303 gpu_thread.InvalidateRegion(addr, size);
@@ -538,6 +553,10 @@ void GPU::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
538 impl->SwapBuffers(framebuffer); 553 impl->SwapBuffers(framebuffer);
539} 554}
540 555
556VideoCore::RasterizerDownloadArea GPU::OnCPURead(VAddr addr, u64 size) {
557 return impl->OnCPURead(addr, size);
558}
559
541void GPU::FlushRegion(VAddr addr, u64 size) { 560void GPU::FlushRegion(VAddr addr, u64 size) {
542 impl->FlushRegion(addr, size); 561 impl->FlushRegion(addr, size);
543} 562}
diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h
index 8a871593a..e49c40cf2 100644
--- a/src/video_core/gpu.h
+++ b/src/video_core/gpu.h
@@ -10,6 +10,7 @@
10#include "core/hle/service/nvdrv/nvdata.h" 10#include "core/hle/service/nvdrv/nvdata.h"
11#include "video_core/cdma_pusher.h" 11#include "video_core/cdma_pusher.h"
12#include "video_core/framebuffer_config.h" 12#include "video_core/framebuffer_config.h"
13#include "video_core/rasterizer_download_area.h"
13 14
14namespace Core { 15namespace Core {
15class System; 16class System;
@@ -241,6 +242,9 @@ public:
241 void SwapBuffers(const Tegra::FramebufferConfig* framebuffer); 242 void SwapBuffers(const Tegra::FramebufferConfig* framebuffer);
242 243
243 /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory 244 /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
245 [[nodiscard]] VideoCore::RasterizerDownloadArea OnCPURead(VAddr addr, u64 size);
246
247 /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
244 void FlushRegion(VAddr addr, u64 size); 248 void FlushRegion(VAddr addr, u64 size);
245 249
246 /// Notify rasterizer that any caches of the specified region should be invalidated 250 /// Notify rasterizer that any caches of the specified region should be invalidated
diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h
index 941de95c1..1528cc1dd 100644
--- a/src/video_core/query_cache.h
+++ b/src/video_core/query_cache.h
@@ -255,7 +255,6 @@ private:
255 if (!in_range(query)) { 255 if (!in_range(query)) {
256 continue; 256 continue;
257 } 257 }
258 rasterizer.UpdatePagesCachedCount(query.GetCpuAddr(), query.SizeInBytes(), -1);
259 AsyncJobId async_job_id = query.GetAsyncJob(); 258 AsyncJobId async_job_id = query.GetAsyncJob();
260 auto flush_result = query.Flush(async); 259 auto flush_result = query.Flush(async);
261 if (async_job_id == NULL_ASYNC_JOB_ID) { 260 if (async_job_id == NULL_ASYNC_JOB_ID) {
@@ -273,7 +272,6 @@ private:
273 272
274 /// Registers the passed parameters as cached and returns a pointer to the stored cached query. 273 /// Registers the passed parameters as cached and returns a pointer to the stored cached query.
275 CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) { 274 CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) {
276 rasterizer.UpdatePagesCachedCount(cpu_addr, CachedQuery::SizeInBytes(timestamp), 1);
277 const u64 page = static_cast<u64>(cpu_addr) >> YUZU_PAGEBITS; 275 const u64 page = static_cast<u64>(cpu_addr) >> YUZU_PAGEBITS;
278 return &cached_queries[page].emplace_back(static_cast<QueryCache&>(*this), type, cpu_addr, 276 return &cached_queries[page].emplace_back(static_cast<QueryCache&>(*this), type, cpu_addr,
279 host_ptr); 277 host_ptr);
diff --git a/src/video_core/rasterizer_download_area.h b/src/video_core/rasterizer_download_area.h
new file mode 100644
index 000000000..2d7425c79
--- /dev/null
+++ b/src/video_core/rasterizer_download_area.h
@@ -0,0 +1,16 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include "common/common_types.h"
7
8namespace VideoCore {
9
10struct RasterizerDownloadArea {
11 VAddr start_address;
12 VAddr end_address;
13 bool preemtive;
14};
15
16} // namespace VideoCore \ No newline at end of file
diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h
index 33e2610bc..7566a8c4e 100644
--- a/src/video_core/rasterizer_interface.h
+++ b/src/video_core/rasterizer_interface.h
@@ -12,6 +12,7 @@
12#include "video_core/cache_types.h" 12#include "video_core/cache_types.h"
13#include "video_core/engines/fermi_2d.h" 13#include "video_core/engines/fermi_2d.h"
14#include "video_core/gpu.h" 14#include "video_core/gpu.h"
15#include "video_core/rasterizer_download_area.h"
15 16
16namespace Tegra { 17namespace Tegra {
17class MemoryManager; 18class MemoryManager;
@@ -95,6 +96,8 @@ public:
95 virtual bool MustFlushRegion(VAddr addr, u64 size, 96 virtual bool MustFlushRegion(VAddr addr, u64 size,
96 VideoCommon::CacheType which = VideoCommon::CacheType::All) = 0; 97 VideoCommon::CacheType which = VideoCommon::CacheType::All) = 0;
97 98
99 virtual RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) = 0;
100
98 /// Notify rasterizer that any caches of the specified region should be invalidated 101 /// Notify rasterizer that any caches of the specified region should be invalidated
99 virtual void InvalidateRegion(VAddr addr, u64 size, 102 virtual void InvalidateRegion(VAddr addr, u64 size,
100 VideoCommon::CacheType which = VideoCommon::CacheType::All) = 0; 103 VideoCommon::CacheType which = VideoCommon::CacheType::All) = 0;
diff --git a/src/video_core/renderer_null/null_rasterizer.cpp b/src/video_core/renderer_null/null_rasterizer.cpp
index 2b5c7defa..bf2ce4c49 100644
--- a/src/video_core/renderer_null/null_rasterizer.cpp
+++ b/src/video_core/renderer_null/null_rasterizer.cpp
@@ -1,6 +1,8 @@
1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include "common/alignment.h"
5#include "core/memory.h"
4#include "video_core/host1x/host1x.h" 6#include "video_core/host1x/host1x.h"
5#include "video_core/memory_manager.h" 7#include "video_core/memory_manager.h"
6#include "video_core/renderer_null/null_rasterizer.h" 8#include "video_core/renderer_null/null_rasterizer.h"
@@ -46,6 +48,14 @@ bool RasterizerNull::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheTyp
46} 48}
47void RasterizerNull::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType) {} 49void RasterizerNull::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType) {}
48void RasterizerNull::OnCPUWrite(VAddr addr, u64 size) {} 50void RasterizerNull::OnCPUWrite(VAddr addr, u64 size) {}
51VideoCore::RasterizerDownloadArea RasterizerNull::GetFlushArea(VAddr addr, u64 size) {
52 VideoCore::RasterizerDownloadArea new_area{
53 .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE),
54 .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE),
55 .preemtive = true,
56 };
57 return new_area;
58}
49void RasterizerNull::InvalidateGPUCache() {} 59void RasterizerNull::InvalidateGPUCache() {}
50void RasterizerNull::UnmapMemory(VAddr addr, u64 size) {} 60void RasterizerNull::UnmapMemory(VAddr addr, u64 size) {}
51void RasterizerNull::ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) {} 61void RasterizerNull::ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) {}
diff --git a/src/video_core/renderer_null/null_rasterizer.h b/src/video_core/renderer_null/null_rasterizer.h
index 0c59e6a1f..a8d35d2c1 100644
--- a/src/video_core/renderer_null/null_rasterizer.h
+++ b/src/video_core/renderer_null/null_rasterizer.h
@@ -54,6 +54,7 @@ public:
54 void InvalidateRegion(VAddr addr, u64 size, 54 void InvalidateRegion(VAddr addr, u64 size,
55 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 55 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
56 void OnCPUWrite(VAddr addr, u64 size) override; 56 void OnCPUWrite(VAddr addr, u64 size) override;
57 VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override;
57 void InvalidateGPUCache() override; 58 void InvalidateGPUCache() override;
58 void UnmapMemory(VAddr addr, u64 size) override; 59 void UnmapMemory(VAddr addr, u64 size) override;
59 void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override; 60 void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 0089b4b27..f5baa0f3c 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -433,6 +433,29 @@ bool RasterizerOpenGL::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheT
433 return false; 433 return false;
434} 434}
435 435
436VideoCore::RasterizerDownloadArea RasterizerOpenGL::GetFlushArea(VAddr addr, u64 size) {
437 {
438 std::scoped_lock lock{texture_cache.mutex};
439 auto area = texture_cache.GetFlushArea(addr, size);
440 if (area) {
441 return *area;
442 }
443 }
444 {
445 std::scoped_lock lock{buffer_cache.mutex};
446 auto area = buffer_cache.GetFlushArea(addr, size);
447 if (area) {
448 return *area;
449 }
450 }
451 VideoCore::RasterizerDownloadArea new_area{
452 .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE),
453 .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE),
454 .preemtive = true,
455 };
456 return new_area;
457}
458
436void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) { 459void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
437 MICROPROFILE_SCOPE(OpenGL_CacheManagement); 460 MICROPROFILE_SCOPE(OpenGL_CacheManagement);
438 if (addr == 0 || size == 0) { 461 if (addr == 0 || size == 0) {
@@ -1281,7 +1304,7 @@ bool AccelerateDMA::DmaBufferImageCopy(const Tegra::DMA::ImageCopy& copy_info,
1281 const Tegra::DMA::BufferOperand& buffer_operand, 1304 const Tegra::DMA::BufferOperand& buffer_operand,
1282 const Tegra::DMA::ImageOperand& image_operand) { 1305 const Tegra::DMA::ImageOperand& image_operand) {
1283 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 1306 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
1284 const auto image_id = texture_cache.DmaImageId(image_operand); 1307 const auto image_id = texture_cache.DmaImageId(image_operand, IS_IMAGE_UPLOAD);
1285 if (image_id == VideoCommon::NULL_IMAGE_ID) { 1308 if (image_id == VideoCommon::NULL_IMAGE_ID) {
1286 return false; 1309 return false;
1287 } 1310 }
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index ad6978bd0..410d8ffc5 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -95,6 +95,7 @@ public:
95 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 95 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
96 bool MustFlushRegion(VAddr addr, u64 size, 96 bool MustFlushRegion(VAddr addr, u64 size,
97 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 97 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
98 VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override;
98 void InvalidateRegion(VAddr addr, u64 size, 99 void InvalidateRegion(VAddr addr, u64 size,
99 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 100 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
100 void OnCPUWrite(VAddr addr, u64 size) override; 101 void OnCPUWrite(VAddr addr, u64 size) override;
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index d1489fc95..628e1376f 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -502,6 +502,22 @@ bool RasterizerVulkan::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheT
502 return false; 502 return false;
503} 503}
504 504
505VideoCore::RasterizerDownloadArea RasterizerVulkan::GetFlushArea(VAddr addr, u64 size) {
506 {
507 std::scoped_lock lock{texture_cache.mutex};
508 auto area = texture_cache.GetFlushArea(addr, size);
509 if (area) {
510 return *area;
511 }
512 }
513 VideoCore::RasterizerDownloadArea new_area{
514 .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE),
515 .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE),
516 .preemtive = true,
517 };
518 return new_area;
519}
520
505void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) { 521void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
506 if (addr == 0 || size == 0) { 522 if (addr == 0 || size == 0) {
507 return; 523 return;
@@ -598,7 +614,7 @@ void RasterizerVulkan::SignalSyncPoint(u32 value) {
598} 614}
599 615
600void RasterizerVulkan::SignalReference() { 616void RasterizerVulkan::SignalReference() {
601 fence_manager.SignalOrdering(); 617 fence_manager.SignalReference();
602} 618}
603 619
604void RasterizerVulkan::ReleaseFences() { 620void RasterizerVulkan::ReleaseFences() {
@@ -631,7 +647,7 @@ void RasterizerVulkan::WaitForIdle() {
631 cmdbuf.SetEvent(event, flags); 647 cmdbuf.SetEvent(event, flags);
632 cmdbuf.WaitEvents(event, flags, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, {}, {}, {}); 648 cmdbuf.WaitEvents(event, flags, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, {}, {}, {});
633 }); 649 });
634 SignalReference(); 650 fence_manager.SignalOrdering();
635} 651}
636 652
637void RasterizerVulkan::FragmentBarrier() { 653void RasterizerVulkan::FragmentBarrier() {
@@ -777,7 +793,7 @@ bool AccelerateDMA::DmaBufferImageCopy(const Tegra::DMA::ImageCopy& copy_info,
777 const Tegra::DMA::BufferOperand& buffer_operand, 793 const Tegra::DMA::BufferOperand& buffer_operand,
778 const Tegra::DMA::ImageOperand& image_operand) { 794 const Tegra::DMA::ImageOperand& image_operand) {
779 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 795 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
780 const auto image_id = texture_cache.DmaImageId(image_operand); 796 const auto image_id = texture_cache.DmaImageId(image_operand, IS_IMAGE_UPLOAD);
781 if (image_id == VideoCommon::NULL_IMAGE_ID) { 797 if (image_id == VideoCommon::NULL_IMAGE_ID) {
782 return false; 798 return false;
783 } 799 }
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index 1659fbc13..9bd422850 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -92,6 +92,7 @@ public:
92 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 92 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
93 bool MustFlushRegion(VAddr addr, u64 size, 93 bool MustFlushRegion(VAddr addr, u64 size,
94 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 94 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
95 VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override;
95 void InvalidateRegion(VAddr addr, u64 size, 96 void InvalidateRegion(VAddr addr, u64 size,
96 VideoCommon::CacheType which = VideoCommon::CacheType::All) override; 97 VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
97 void InnerInvalidation(std::span<const std::pair<VAddr, std::size_t>> sequences) override; 98 void InnerInvalidation(std::span<const std::pair<VAddr, std::size_t>> sequences) override;
diff --git a/src/video_core/texture_cache/image_info.cpp b/src/video_core/texture_cache/image_info.cpp
index 11f3f78a1..e8ddde691 100644
--- a/src/video_core/texture_cache/image_info.cpp
+++ b/src/video_core/texture_cache/image_info.cpp
@@ -4,6 +4,7 @@
4#include <fmt/format.h> 4#include <fmt/format.h>
5 5
6#include "common/assert.h" 6#include "common/assert.h"
7#include "common/settings.h"
7#include "video_core/surface.h" 8#include "video_core/surface.h"
8#include "video_core/texture_cache/format_lookup_table.h" 9#include "video_core/texture_cache/format_lookup_table.h"
9#include "video_core/texture_cache/image_info.h" 10#include "video_core/texture_cache/image_info.h"
@@ -22,6 +23,8 @@ using VideoCore::Surface::PixelFormat;
22using VideoCore::Surface::SurfaceType; 23using VideoCore::Surface::SurfaceType;
23 24
24ImageInfo::ImageInfo(const TICEntry& config) noexcept { 25ImageInfo::ImageInfo(const TICEntry& config) noexcept {
26 forced_flushed = config.IsPitchLinear() && !Settings::values.use_reactive_flushing.GetValue();
27 dma_downloaded = forced_flushed;
25 format = PixelFormatFromTextureInfo(config.format, config.r_type, config.g_type, config.b_type, 28 format = PixelFormatFromTextureInfo(config.format, config.r_type, config.g_type, config.b_type,
26 config.a_type, config.srgb_conversion); 29 config.a_type, config.srgb_conversion);
27 num_samples = NumSamples(config.msaa_mode); 30 num_samples = NumSamples(config.msaa_mode);
@@ -117,6 +120,9 @@ ImageInfo::ImageInfo(const TICEntry& config) noexcept {
117 120
118ImageInfo::ImageInfo(const Maxwell3D::Regs::RenderTargetConfig& ct, 121ImageInfo::ImageInfo(const Maxwell3D::Regs::RenderTargetConfig& ct,
119 Tegra::Texture::MsaaMode msaa_mode) noexcept { 122 Tegra::Texture::MsaaMode msaa_mode) noexcept {
123 forced_flushed =
124 ct.tile_mode.is_pitch_linear && !Settings::values.use_reactive_flushing.GetValue();
125 dma_downloaded = forced_flushed;
120 format = VideoCore::Surface::PixelFormatFromRenderTargetFormat(ct.format); 126 format = VideoCore::Surface::PixelFormatFromRenderTargetFormat(ct.format);
121 rescaleable = false; 127 rescaleable = false;
122 if (ct.tile_mode.is_pitch_linear) { 128 if (ct.tile_mode.is_pitch_linear) {
@@ -155,6 +161,9 @@ ImageInfo::ImageInfo(const Maxwell3D::Regs::RenderTargetConfig& ct,
155 161
156ImageInfo::ImageInfo(const Maxwell3D::Regs::Zeta& zt, const Maxwell3D::Regs::ZetaSize& zt_size, 162ImageInfo::ImageInfo(const Maxwell3D::Regs::Zeta& zt, const Maxwell3D::Regs::ZetaSize& zt_size,
157 Tegra::Texture::MsaaMode msaa_mode) noexcept { 163 Tegra::Texture::MsaaMode msaa_mode) noexcept {
164 forced_flushed =
165 zt.tile_mode.is_pitch_linear && !Settings::values.use_reactive_flushing.GetValue();
166 dma_downloaded = forced_flushed;
158 format = VideoCore::Surface::PixelFormatFromDepthFormat(zt.format); 167 format = VideoCore::Surface::PixelFormatFromDepthFormat(zt.format);
159 size.width = zt_size.width; 168 size.width = zt_size.width;
160 size.height = zt_size.height; 169 size.height = zt_size.height;
@@ -195,6 +204,9 @@ ImageInfo::ImageInfo(const Maxwell3D::Regs::Zeta& zt, const Maxwell3D::Regs::Zet
195 204
196ImageInfo::ImageInfo(const Fermi2D::Surface& config) noexcept { 205ImageInfo::ImageInfo(const Fermi2D::Surface& config) noexcept {
197 UNIMPLEMENTED_IF_MSG(config.layer != 0, "Surface layer is not zero"); 206 UNIMPLEMENTED_IF_MSG(config.layer != 0, "Surface layer is not zero");
207 forced_flushed = config.linear == Fermi2D::MemoryLayout::Pitch &&
208 !Settings::values.use_reactive_flushing.GetValue();
209 dma_downloaded = forced_flushed;
198 format = VideoCore::Surface::PixelFormatFromRenderTargetFormat(config.format); 210 format = VideoCore::Surface::PixelFormatFromRenderTargetFormat(config.format);
199 rescaleable = false; 211 rescaleable = false;
200 if (config.linear == Fermi2D::MemoryLayout::Pitch) { 212 if (config.linear == Fermi2D::MemoryLayout::Pitch) {
diff --git a/src/video_core/texture_cache/image_info.h b/src/video_core/texture_cache/image_info.h
index 4b7dfa315..8a4cb0cbd 100644
--- a/src/video_core/texture_cache/image_info.h
+++ b/src/video_core/texture_cache/image_info.h
@@ -39,6 +39,8 @@ struct ImageInfo {
39 u32 tile_width_spacing = 0; 39 u32 tile_width_spacing = 0;
40 bool rescaleable = false; 40 bool rescaleable = false;
41 bool downscaleable = false; 41 bool downscaleable = false;
42 bool forced_flushed = false;
43 bool dma_downloaded = false;
42}; 44};
43 45
44} // namespace VideoCommon 46} // namespace VideoCommon
diff --git a/src/video_core/texture_cache/image_view_base.cpp b/src/video_core/texture_cache/image_view_base.cpp
index bcad40353..d134b6738 100644
--- a/src/video_core/texture_cache/image_view_base.cpp
+++ b/src/video_core/texture_cache/image_view_base.cpp
@@ -4,7 +4,6 @@
4#include <algorithm> 4#include <algorithm>
5 5
6#include "common/assert.h" 6#include "common/assert.h"
7#include "common/settings.h"
8#include "video_core/compatible_formats.h" 7#include "video_core/compatible_formats.h"
9#include "video_core/surface.h" 8#include "video_core/surface.h"
10#include "video_core/texture_cache/formatter.h" 9#include "video_core/texture_cache/formatter.h"
@@ -26,8 +25,7 @@ ImageViewBase::ImageViewBase(const ImageViewInfo& info, const ImageInfo& image_i
26 ASSERT_MSG(VideoCore::Surface::IsViewCompatible(image_info.format, info.format, false, true), 25 ASSERT_MSG(VideoCore::Surface::IsViewCompatible(image_info.format, info.format, false, true),
27 "Image view format {} is incompatible with image format {}", info.format, 26 "Image view format {} is incompatible with image format {}", info.format,
28 image_info.format); 27 image_info.format);
29 const bool is_async = Settings::values.use_asynchronous_gpu_emulation.GetValue(); 28 if (image_info.forced_flushed) {
30 if (image_info.type == ImageType::Linear && is_async) {
31 flags |= ImageViewFlagBits::PreemtiveDownload; 29 flags |= ImageViewFlagBits::PreemtiveDownload;
32 } 30 }
33 if (image_info.type == ImageType::e3D && info.type != ImageViewType::e3D) { 31 if (image_info.type == ImageType::e3D && info.type != ImageViewType::e3D) {
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index b5297e76b..e1198dcf8 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -491,6 +491,32 @@ void TextureCache<P>::DownloadMemory(VAddr cpu_addr, size_t size) {
491} 491}
492 492
493template <class P> 493template <class P>
494std::optional<VideoCore::RasterizerDownloadArea> TextureCache<P>::GetFlushArea(VAddr cpu_addr,
495 u64 size) {
496 std::optional<VideoCore::RasterizerDownloadArea> area{};
497 ForEachImageInRegion(cpu_addr, size, [&](ImageId, ImageBase& image) {
498 if (False(image.flags & ImageFlagBits::GpuModified)) {
499 return;
500 }
501 if (!area) {
502 area.emplace();
503 area->start_address = cpu_addr;
504 area->end_address = cpu_addr + size;
505 area->preemtive = true;
506 }
507 area->start_address = std::min(area->start_address, image.cpu_addr);
508 area->end_address = std::max(area->end_address, image.cpu_addr_end);
509 for (auto image_view_id : image.image_view_ids) {
510 auto& image_view = slot_image_views[image_view_id];
511 image_view.flags |= ImageViewFlagBits::PreemtiveDownload;
512 }
513 area->preemtive &= image.info.forced_flushed;
514 image.info.forced_flushed = true;
515 });
516 return area;
517}
518
519template <class P>
494void TextureCache<P>::UnmapMemory(VAddr cpu_addr, size_t size) { 520void TextureCache<P>::UnmapMemory(VAddr cpu_addr, size_t size) {
495 std::vector<ImageId> deleted_images; 521 std::vector<ImageId> deleted_images;
496 ForEachImageInRegion(cpu_addr, size, [&](ImageId id, Image&) { deleted_images.push_back(id); }); 522 ForEachImageInRegion(cpu_addr, size, [&](ImageId id, Image&) { deleted_images.push_back(id); });
@@ -683,6 +709,7 @@ void TextureCache<P>::CommitAsyncFlushes() {
683 download_info.async_buffer_id = last_async_buffer_id; 709 download_info.async_buffer_id = last_async_buffer_id;
684 } 710 }
685 } 711 }
712
686 if (any_none_dma) { 713 if (any_none_dma) {
687 auto download_map = runtime.DownloadStagingBuffer(total_size_bytes, true); 714 auto download_map = runtime.DownloadStagingBuffer(total_size_bytes, true);
688 for (const PendingDownload& download_info : download_ids) { 715 for (const PendingDownload& download_info : download_ids) {
@@ -695,6 +722,7 @@ void TextureCache<P>::CommitAsyncFlushes() {
695 } 722 }
696 uncommitted_async_buffers.emplace_back(download_map); 723 uncommitted_async_buffers.emplace_back(download_map);
697 } 724 }
725
698 async_buffers.emplace_back(std::move(uncommitted_async_buffers)); 726 async_buffers.emplace_back(std::move(uncommitted_async_buffers));
699 uncommitted_async_buffers.clear(); 727 uncommitted_async_buffers.clear();
700 } 728 }
@@ -783,17 +811,22 @@ void TextureCache<P>::PopAsyncFlushes() {
783} 811}
784 812
785template <class P> 813template <class P>
786ImageId TextureCache<P>::DmaImageId(const Tegra::DMA::ImageOperand& operand) { 814ImageId TextureCache<P>::DmaImageId(const Tegra::DMA::ImageOperand& operand, bool is_upload) {
787 const ImageInfo dst_info(operand); 815 const ImageInfo dst_info(operand);
788 const ImageId dst_id = FindDMAImage(dst_info, operand.address); 816 const ImageId dst_id = FindDMAImage(dst_info, operand.address);
789 if (!dst_id) { 817 if (!dst_id) {
790 return NULL_IMAGE_ID; 818 return NULL_IMAGE_ID;
791 } 819 }
792 const auto& image = slot_images[dst_id]; 820 auto& image = slot_images[dst_id];
793 if (False(image.flags & ImageFlagBits::GpuModified)) { 821 if (False(image.flags & ImageFlagBits::GpuModified)) {
794 // No need to waste time on an image that's synced with guest 822 // No need to waste time on an image that's synced with guest
795 return NULL_IMAGE_ID; 823 return NULL_IMAGE_ID;
796 } 824 }
825 if (!is_upload && !image.info.dma_downloaded) {
826 // Force a full sync.
827 image.info.dma_downloaded = true;
828 return NULL_IMAGE_ID;
829 }
797 const auto base = image.TryFindBase(operand.address); 830 const auto base = image.TryFindBase(operand.address);
798 if (!base) { 831 if (!base) {
799 return NULL_IMAGE_ID; 832 return NULL_IMAGE_ID;
@@ -1290,7 +1323,6 @@ ImageId TextureCache<P>::JoinImages(const ImageInfo& info, GPUVAddr gpu_addr, VA
1290 all_siblings.push_back(overlap_id); 1323 all_siblings.push_back(overlap_id);
1291 } else { 1324 } else {
1292 bad_overlap_ids.push_back(overlap_id); 1325 bad_overlap_ids.push_back(overlap_id);
1293 overlap.flags |= ImageFlagBits::BadOverlap;
1294 } 1326 }
1295 }; 1327 };
1296 ForEachImageInRegion(cpu_addr, size_bytes, region_check); 1328 ForEachImageInRegion(cpu_addr, size_bytes, region_check);
@@ -1401,7 +1433,12 @@ ImageId TextureCache<P>::JoinImages(const ImageInfo& info, GPUVAddr gpu_addr, VA
1401 ImageBase& aliased = slot_images[aliased_id]; 1433 ImageBase& aliased = slot_images[aliased_id];
1402 aliased.overlapping_images.push_back(new_image_id); 1434 aliased.overlapping_images.push_back(new_image_id);
1403 new_image.overlapping_images.push_back(aliased_id); 1435 new_image.overlapping_images.push_back(aliased_id);
1404 new_image.flags |= ImageFlagBits::BadOverlap; 1436 if (aliased.info.resources.levels == 1 && aliased.overlapping_images.size() > 1) {
1437 aliased.flags |= ImageFlagBits::BadOverlap;
1438 }
1439 if (new_image.info.resources.levels == 1 && new_image.overlapping_images.size() > 1) {
1440 new_image.flags |= ImageFlagBits::BadOverlap;
1441 }
1405 } 1442 }
1406 RegisterImage(new_image_id); 1443 RegisterImage(new_image_id);
1407 return new_image_id; 1444 return new_image_id;
diff --git a/src/video_core/texture_cache/texture_cache_base.h b/src/video_core/texture_cache/texture_cache_base.h
index 758b7e212..0720494e5 100644
--- a/src/video_core/texture_cache/texture_cache_base.h
+++ b/src/video_core/texture_cache/texture_cache_base.h
@@ -179,6 +179,8 @@ public:
179 /// Download contents of host images to guest memory in a region 179 /// Download contents of host images to guest memory in a region
180 void DownloadMemory(VAddr cpu_addr, size_t size); 180 void DownloadMemory(VAddr cpu_addr, size_t size);
181 181
182 std::optional<VideoCore::RasterizerDownloadArea> GetFlushArea(VAddr cpu_addr, u64 size);
183
182 /// Remove images in a region 184 /// Remove images in a region
183 void UnmapMemory(VAddr cpu_addr, size_t size); 185 void UnmapMemory(VAddr cpu_addr, size_t size);
184 186
@@ -205,7 +207,7 @@ public:
205 /// Pop asynchronous downloads 207 /// Pop asynchronous downloads
206 void PopAsyncFlushes(); 208 void PopAsyncFlushes();
207 209
208 [[nodiscard]] ImageId DmaImageId(const Tegra::DMA::ImageOperand& operand); 210 [[nodiscard]] ImageId DmaImageId(const Tegra::DMA::ImageOperand& operand, bool is_upload);
209 211
210 [[nodiscard]] std::pair<Image*, BufferImageCopy> DmaBufferImageCopy( 212 [[nodiscard]] std::pair<Image*, BufferImageCopy> DmaBufferImageCopy(
211 const Tegra::DMA::ImageCopy& copy_info, const Tegra::DMA::BufferOperand& buffer_operand, 213 const Tegra::DMA::ImageCopy& copy_info, const Tegra::DMA::BufferOperand& buffer_operand,
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index a85eb4687..a49d12266 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -710,6 +710,7 @@ void Config::ReadRendererValues() {
710 ReadGlobalSetting(Settings::values.nvdec_emulation); 710 ReadGlobalSetting(Settings::values.nvdec_emulation);
711 ReadGlobalSetting(Settings::values.accelerate_astc); 711 ReadGlobalSetting(Settings::values.accelerate_astc);
712 ReadGlobalSetting(Settings::values.async_astc); 712 ReadGlobalSetting(Settings::values.async_astc);
713 ReadGlobalSetting(Settings::values.use_reactive_flushing);
713 ReadGlobalSetting(Settings::values.shader_backend); 714 ReadGlobalSetting(Settings::values.shader_backend);
714 ReadGlobalSetting(Settings::values.use_asynchronous_shaders); 715 ReadGlobalSetting(Settings::values.use_asynchronous_shaders);
715 ReadGlobalSetting(Settings::values.use_fast_gpu_time); 716 ReadGlobalSetting(Settings::values.use_fast_gpu_time);
@@ -1355,6 +1356,7 @@ void Config::SaveRendererValues() {
1355 Settings::values.nvdec_emulation.UsingGlobal()); 1356 Settings::values.nvdec_emulation.UsingGlobal());
1356 WriteGlobalSetting(Settings::values.accelerate_astc); 1357 WriteGlobalSetting(Settings::values.accelerate_astc);
1357 WriteGlobalSetting(Settings::values.async_astc); 1358 WriteGlobalSetting(Settings::values.async_astc);
1359 WriteGlobalSetting(Settings::values.use_reactive_flushing);
1358 WriteSetting(QString::fromStdString(Settings::values.shader_backend.GetLabel()), 1360 WriteSetting(QString::fromStdString(Settings::values.shader_backend.GetLabel()),
1359 static_cast<u32>(Settings::values.shader_backend.GetValue(global)), 1361 static_cast<u32>(Settings::values.shader_backend.GetValue(global)),
1360 static_cast<u32>(Settings::values.shader_backend.GetDefault()), 1362 static_cast<u32>(Settings::values.shader_backend.GetDefault()),
diff --git a/src/yuzu/configuration/configure_graphics_advanced.cpp b/src/yuzu/configuration/configure_graphics_advanced.cpp
index 005b022ca..627ed8b17 100644
--- a/src/yuzu/configuration/configure_graphics_advanced.cpp
+++ b/src/yuzu/configuration/configure_graphics_advanced.cpp
@@ -21,6 +21,7 @@ ConfigureGraphicsAdvanced::~ConfigureGraphicsAdvanced() = default;
21 21
22void ConfigureGraphicsAdvanced::SetConfiguration() { 22void ConfigureGraphicsAdvanced::SetConfiguration() {
23 const bool runtime_lock = !system.IsPoweredOn(); 23 const bool runtime_lock = !system.IsPoweredOn();
24 ui->use_reactive_flushing->setEnabled(runtime_lock);
24 ui->async_present->setEnabled(runtime_lock); 25 ui->async_present->setEnabled(runtime_lock);
25 ui->renderer_force_max_clock->setEnabled(runtime_lock); 26 ui->renderer_force_max_clock->setEnabled(runtime_lock);
26 ui->async_astc->setEnabled(runtime_lock); 27 ui->async_astc->setEnabled(runtime_lock);
@@ -29,6 +30,7 @@ void ConfigureGraphicsAdvanced::SetConfiguration() {
29 30
30 ui->async_present->setChecked(Settings::values.async_presentation.GetValue()); 31 ui->async_present->setChecked(Settings::values.async_presentation.GetValue());
31 ui->renderer_force_max_clock->setChecked(Settings::values.renderer_force_max_clock.GetValue()); 32 ui->renderer_force_max_clock->setChecked(Settings::values.renderer_force_max_clock.GetValue());
33 ui->use_reactive_flushing->setChecked(Settings::values.use_reactive_flushing.GetValue());
32 ui->async_astc->setChecked(Settings::values.async_astc.GetValue()); 34 ui->async_astc->setChecked(Settings::values.async_astc.GetValue());
33 ui->use_asynchronous_shaders->setChecked(Settings::values.use_asynchronous_shaders.GetValue()); 35 ui->use_asynchronous_shaders->setChecked(Settings::values.use_asynchronous_shaders.GetValue());
34 ui->use_fast_gpu_time->setChecked(Settings::values.use_fast_gpu_time.GetValue()); 36 ui->use_fast_gpu_time->setChecked(Settings::values.use_fast_gpu_time.GetValue());
@@ -60,6 +62,8 @@ void ConfigureGraphicsAdvanced::ApplyConfiguration() {
60 renderer_force_max_clock); 62 renderer_force_max_clock);
61 ConfigurationShared::ApplyPerGameSetting(&Settings::values.max_anisotropy, 63 ConfigurationShared::ApplyPerGameSetting(&Settings::values.max_anisotropy,
62 ui->anisotropic_filtering_combobox); 64 ui->anisotropic_filtering_combobox);
65 ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_reactive_flushing,
66 ui->use_reactive_flushing, use_reactive_flushing);
63 ConfigurationShared::ApplyPerGameSetting(&Settings::values.async_astc, ui->async_astc, 67 ConfigurationShared::ApplyPerGameSetting(&Settings::values.async_astc, ui->async_astc,
64 async_astc); 68 async_astc);
65 ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_asynchronous_shaders, 69 ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_asynchronous_shaders,
@@ -91,6 +95,7 @@ void ConfigureGraphicsAdvanced::SetupPerGameUI() {
91 ui->async_present->setEnabled(Settings::values.async_presentation.UsingGlobal()); 95 ui->async_present->setEnabled(Settings::values.async_presentation.UsingGlobal());
92 ui->renderer_force_max_clock->setEnabled( 96 ui->renderer_force_max_clock->setEnabled(
93 Settings::values.renderer_force_max_clock.UsingGlobal()); 97 Settings::values.renderer_force_max_clock.UsingGlobal());
98 ui->use_reactive_flushing->setEnabled(Settings::values.use_reactive_flushing.UsingGlobal());
94 ui->async_astc->setEnabled(Settings::values.async_astc.UsingGlobal()); 99 ui->async_astc->setEnabled(Settings::values.async_astc.UsingGlobal());
95 ui->use_asynchronous_shaders->setEnabled( 100 ui->use_asynchronous_shaders->setEnabled(
96 Settings::values.use_asynchronous_shaders.UsingGlobal()); 101 Settings::values.use_asynchronous_shaders.UsingGlobal());
@@ -108,6 +113,8 @@ void ConfigureGraphicsAdvanced::SetupPerGameUI() {
108 ConfigurationShared::SetColoredTristate(ui->renderer_force_max_clock, 113 ConfigurationShared::SetColoredTristate(ui->renderer_force_max_clock,
109 Settings::values.renderer_force_max_clock, 114 Settings::values.renderer_force_max_clock,
110 renderer_force_max_clock); 115 renderer_force_max_clock);
116 ConfigurationShared::SetColoredTristate(
117 ui->use_reactive_flushing, Settings::values.use_reactive_flushing, use_reactive_flushing);
111 ConfigurationShared::SetColoredTristate(ui->async_astc, Settings::values.async_astc, 118 ConfigurationShared::SetColoredTristate(ui->async_astc, Settings::values.async_astc,
112 async_astc); 119 async_astc);
113 ConfigurationShared::SetColoredTristate(ui->use_asynchronous_shaders, 120 ConfigurationShared::SetColoredTristate(ui->use_asynchronous_shaders,
diff --git a/src/yuzu/configuration/configure_graphics_advanced.h b/src/yuzu/configuration/configure_graphics_advanced.h
index ff5060957..ae3c10946 100644
--- a/src/yuzu/configuration/configure_graphics_advanced.h
+++ b/src/yuzu/configuration/configure_graphics_advanced.h
@@ -40,6 +40,7 @@ private:
40 ConfigurationShared::CheckState renderer_force_max_clock; 40 ConfigurationShared::CheckState renderer_force_max_clock;
41 ConfigurationShared::CheckState use_vsync; 41 ConfigurationShared::CheckState use_vsync;
42 ConfigurationShared::CheckState async_astc; 42 ConfigurationShared::CheckState async_astc;
43 ConfigurationShared::CheckState use_reactive_flushing;
43 ConfigurationShared::CheckState use_asynchronous_shaders; 44 ConfigurationShared::CheckState use_asynchronous_shaders;
44 ConfigurationShared::CheckState use_fast_gpu_time; 45 ConfigurationShared::CheckState use_fast_gpu_time;
45 ConfigurationShared::CheckState use_vulkan_driver_pipeline_cache; 46 ConfigurationShared::CheckState use_vulkan_driver_pipeline_cache;
diff --git a/src/yuzu/configuration/configure_graphics_advanced.ui b/src/yuzu/configuration/configure_graphics_advanced.ui
index d073fe9b1..9d8cbea09 100644
--- a/src/yuzu/configuration/configure_graphics_advanced.ui
+++ b/src/yuzu/configuration/configure_graphics_advanced.ui
@@ -97,6 +97,16 @@
97 </widget> 97 </widget>
98 </item> 98 </item>
99 <item> 99 <item>
100 <widget class="QCheckBox" name="use_reactive_flushing">
101 <property name="toolTip">
102 <string>Uses reactive flushing instead of predictive flushing. Allowing a more accurate syncing of memory.</string>
103 </property>
104 <property name="text">
105 <string>Enable Reactive Flushing</string>
106 </property>
107 </widget>
108 </item>
109 <item>
100 <widget class="QCheckBox" name="use_asynchronous_shaders"> 110 <widget class="QCheckBox" name="use_asynchronous_shaders">
101 <property name="toolTip"> 111 <property name="toolTip">
102 <string>Enables asynchronous shader compilation, which may reduce shader stutter. This feature is experimental.</string> 112 <string>Enables asynchronous shader compilation, which may reduce shader stutter. This feature is experimental.</string>
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index a6418e693..abe7092fc 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -312,6 +312,7 @@ void Config::ReadValues() {
312 ReadSetting("Renderer", Settings::values.use_asynchronous_gpu_emulation); 312 ReadSetting("Renderer", Settings::values.use_asynchronous_gpu_emulation);
313 ReadSetting("Renderer", Settings::values.vsync_mode); 313 ReadSetting("Renderer", Settings::values.vsync_mode);
314 ReadSetting("Renderer", Settings::values.shader_backend); 314 ReadSetting("Renderer", Settings::values.shader_backend);
315 ReadSetting("Renderer", Settings::values.use_reactive_flushing);
315 ReadSetting("Renderer", Settings::values.use_asynchronous_shaders); 316 ReadSetting("Renderer", Settings::values.use_asynchronous_shaders);
316 ReadSetting("Renderer", Settings::values.nvdec_emulation); 317 ReadSetting("Renderer", Settings::values.nvdec_emulation);
317 ReadSetting("Renderer", Settings::values.accelerate_astc); 318 ReadSetting("Renderer", Settings::values.accelerate_astc);
diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h
index 086ed4cfa..5e7c3ac04 100644
--- a/src/yuzu_cmd/default_ini.h
+++ b/src/yuzu_cmd/default_ini.h
@@ -340,6 +340,10 @@ use_vsync =
340# 0: GLSL, 1 (default): GLASM, 2: SPIR-V 340# 0: GLSL, 1 (default): GLASM, 2: SPIR-V
341shader_backend = 341shader_backend =
342 342
343# Uses reactive flushing instead of predictive flushing. Allowing a more accurate syncing of memory.
344# 0: Off, 1 (default): On
345use_reactive_flushing =
346
343# Whether to allow asynchronous shader building. 347# Whether to allow asynchronous shader building.
344# 0 (default): Off, 1: On 348# 0 (default): Off, 1: On
345use_asynchronous_shaders = 349use_asynchronous_shaders =