From bab2c1488830972d7c47dff0b23e269cae1f6d30 Mon Sep 17 00:00:00 2001 From: pineappleEA Date: Thu, 4 May 2023 16:44:44 +0200 Subject: [PATCH] early-access version 3552 --- README.md | 2 +- src/common/settings.cpp | 5 +- src/common/settings.h | 11 +- src/core/core.cpp | 4 + src/core/core.h | 2 + src/core/memory.cpp | 25 ++- src/core/telemetry_session.cpp | 17 +- src/tests/video_core/memory_tracker.cpp | 4 +- src/video_core/buffer_cache/buffer_base.h | 9 + src/video_core/buffer_cache/buffer_cache.h | 152 +++++++++------- .../buffer_cache/buffer_cache_base.h | 7 +- .../buffer_cache/memory_tracker_base.h | 26 +++ src/video_core/buffer_cache/word_manager.h | 27 ++- src/video_core/engines/maxwell_dma.cpp | 8 +- src/video_core/fence_manager.h | 5 + src/video_core/gpu.cpp | 19 ++ src/video_core/gpu.h | 4 + src/video_core/query_cache.h | 2 - src/video_core/rasterizer_download_area.h | 16 ++ src/video_core/rasterizer_interface.h | 3 + .../renderer_null/null_rasterizer.cpp | 10 ++ .../renderer_null/null_rasterizer.h | 1 + .../renderer_opengl/gl_rasterizer.cpp | 23 +++ .../renderer_opengl/gl_rasterizer.h | 1 + .../renderer_vulkan/renderer_vulkan.cpp | 2 +- .../renderer_vulkan/vk_rasterizer.cpp | 27 ++- .../renderer_vulkan/vk_rasterizer.h | 1 + .../renderer_vulkan/vk_swapchain.cpp | 75 +++----- src/video_core/renderer_vulkan/vk_swapchain.h | 6 +- src/video_core/texture_cache/image_info.h | 2 + .../texture_cache/image_view_base.cpp | 5 +- src/video_core/texture_cache/texture_cache.h | 60 ++++++- .../texture_cache/texture_cache_base.h | 2 + .../vulkan_common/vulkan_surface.cpp | 6 +- src/video_core/vulkan_common/vulkan_surface.h | 9 +- src/yuzu/CMakeLists.txt | 2 - src/yuzu/bootmanager.cpp | 90 ++++++---- src/yuzu/bootmanager.h | 26 +-- src/yuzu/configuration/config.cpp | 12 +- src/yuzu/configuration/configure_graphics.cpp | 168 +----------------- src/yuzu/configuration/configure_graphics.h | 23 +-- src/yuzu/configuration/configure_graphics.ui | 40 +---- .../configure_graphics_advanced.cpp | 12 ++ .../configure_graphics_advanced.h | 1 + .../configure_graphics_advanced.ui | 20 +++ .../configuration/configure_input_player.cpp | 1 - src/yuzu_cmd/config.cpp | 3 +- src/yuzu_cmd/default_ini.h | 14 +- 48 files changed, 508 insertions(+), 482 deletions(-) create mode 100755 src/video_core/rasterizer_download_area.h diff --git a/README.md b/README.md index 16665ea86..a16b0c8d4 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ yuzu emulator early access ============= -This is the source code for early-access 3549. +This is the source code for early-access 3552. ## Legal Notice diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 1537510be..860dec694 100755 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -61,7 +61,8 @@ void LogSettings() { log_setting("Renderer_NvdecEmulation", values.nvdec_emulation.GetValue()); log_setting("Renderer_AccelerateASTC", values.accelerate_astc.GetValue()); log_setting("Renderer_AsyncASTC", values.async_astc.GetValue()); - log_setting("Renderer_UseVsync", values.vsync_mode.GetValue()); + log_setting("Renderer_UseVsync", values.use_vsync.GetValue()); + log_setting("Renderer_UseReactiveFlushing", values.use_reactive_flushing.GetValue()); log_setting("Renderer_ShaderBackend", values.shader_backend.GetValue()); log_setting("Renderer_UseAsynchronousShaders", values.use_asynchronous_shaders.GetValue()); log_setting("Renderer_AnisotropicFilteringLevel", values.max_anisotropy.GetValue()); @@ -223,6 +224,8 @@ void RestoreGlobalState(bool is_powered_on) { values.nvdec_emulation.SetGlobal(true); values.accelerate_astc.SetGlobal(true); values.async_astc.SetGlobal(true); + values.use_vsync.SetGlobal(true); + values.use_reactive_flushing.SetGlobal(true); values.shader_backend.SetGlobal(true); values.use_asynchronous_shaders.SetGlobal(true); values.use_fast_gpu_time.SetGlobal(true); diff --git a/src/common/settings.h b/src/common/settings.h index 263185eff..58f6e1177 100755 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -16,13 +16,6 @@ namespace Settings { -enum class VSyncMode : u32 { - Immediate = 0, - Mailbox = 1, - FIFO = 2, - FIFORelaxed = 3, -}; - enum class RendererBackend : u32 { OpenGL = 0, Vulkan = 1, @@ -463,8 +456,8 @@ struct Values { SwitchableSetting nvdec_emulation{NvdecEmulation::GPU, "nvdec_emulation"}; SwitchableSetting accelerate_astc{true, "accelerate_astc"}; SwitchableSetting async_astc{false, "async_astc"}; - Setting vsync_mode{VSyncMode::FIFO, VSyncMode::Immediate, - VSyncMode::FIFORelaxed, "use_vsync"}; + SwitchableSetting use_vsync{true, "use_vsync"}; + SwitchableSetting use_reactive_flushing{true, "use_reactive_flushing"}; SwitchableSetting shader_backend{ShaderBackend::GLSL, ShaderBackend::GLSL, ShaderBackend::SPIRV, "shader_backend"}; SwitchableSetting use_asynchronous_shaders{false, "use_asynchronous_shaders"}; diff --git a/src/core/core.cpp b/src/core/core.cpp index 043238437..ccf1256ff 100755 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -612,6 +612,10 @@ void System::PrepareReschedule(const u32 core_index) { impl->kernel.PrepareReschedule(core_index); } +size_t System::GetCurrentHostThreadID() const { + return impl->kernel.GetCurrentHostThreadID(); +} + PerfStatsResults System::GetAndResetPerfStats() { return impl->GetAndResetPerfStats(); } diff --git a/src/core/core.h b/src/core/core.h index dbea14591..d4b1166f5 100755 --- a/src/core/core.h +++ b/src/core/core.h @@ -222,6 +222,8 @@ public: /// Prepare the core emulation for a reschedule void PrepareReschedule(u32 core_index); + [[nodiscard]] size_t GetCurrentHostThreadID() const; + /// Gets and resets core performance statistics [[nodiscard]] PerfStatsResults GetAndResetPerfStats(); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 13ff86623..d3e991221 100755 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -13,10 +13,12 @@ #include "common/swap.h" #include "core/core.h" #include "core/device_memory.h" +#include "core/hardware_properties.h" #include "core/hle/kernel/k_page_table.h" #include "core/hle/kernel/k_process.h" #include "core/memory.h" #include "video_core/gpu.h" +#include "video_core/rasterizer_download_area.h" namespace Core::Memory { @@ -243,7 +245,7 @@ struct Memory::Impl { [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount, const u8* const host_ptr) { if constexpr (!UNSAFE) { - system.GPU().FlushRegion(GetInteger(current_vaddr), copy_amount); + HandleRasterizerDownload(GetInteger(current_vaddr), copy_amount); } std::memcpy(dest_buffer, host_ptr, copy_amount); }, @@ -334,7 +336,7 @@ struct Memory::Impl { }, [&](const Common::ProcessAddress current_vaddr, const std::size_t copy_amount, u8* const host_ptr) { - system.GPU().FlushRegion(GetInteger(current_vaddr), copy_amount); + HandleRasterizerDownload(GetInteger(current_vaddr), copy_amount); WriteBlockImpl(process, dest_addr, host_ptr, copy_amount); }, [&](const std::size_t copy_amount) { @@ -373,7 +375,7 @@ struct Memory::Impl { const std::size_t block_size) { // dc ivac: Invalidate to point of coherency // GPU flush -> CPU invalidate - system.GPU().FlushRegion(GetInteger(current_vaddr), block_size); + HandleRasterizerDownload(GetInteger(current_vaddr), block_size); }; return PerformCacheOperation(process, dest_addr, size, on_rasterizer); } @@ -462,7 +464,8 @@ struct Memory::Impl { } if (Settings::IsFastmemEnabled()) { - const bool is_read_enable = !Settings::IsGPULevelExtreme() || !cached; + const bool is_read_enable = + !Settings::values.use_reactive_flushing.GetValue() || !cached; system.DeviceMemory().buffer.Protect(vaddr, size, is_read_enable, !cached); } @@ -651,7 +654,7 @@ struct Memory::Impl { LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:016X}", sizeof(T) * 8, GetInteger(vaddr)); }, - [&]() { system.GPU().FlushRegion(GetInteger(vaddr), sizeof(T)); }); + [&]() { HandleRasterizerDownload(GetInteger(vaddr), sizeof(T)); }); if (ptr) { std::memcpy(&result, ptr, sizeof(T)); } @@ -712,7 +715,19 @@ struct Memory::Impl { return true; } + void HandleRasterizerDownload(VAddr address, size_t size) { + const size_t core = system.GetCurrentHostThreadID(); + auto& current_area = rasterizer_areas[core]; + const VAddr end_address = address + size; + if (current_area.start_address <= address && end_address <= current_area.end_address) + [[likely]] { + return; + } + current_area = system.GPU().OnCPURead(address, size); + } + Common::PageTable* current_page_table = nullptr; + std::array rasterizer_areas{}; Core::System& system; }; diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index 40e5ef789..12e08e894 100755 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -85,20 +85,6 @@ static const char* TranslateNvdecEmulation(Settings::NvdecEmulation backend) { return "Unknown"; } -static constexpr const char* TranslateVSyncMode(Settings::VSyncMode mode) { - switch (mode) { - case Settings::VSyncMode::Immediate: - return "Immediate"; - case Settings::VSyncMode::Mailbox: - return "Mailbox"; - case Settings::VSyncMode::FIFO: - return "FIFO"; - case Settings::VSyncMode::FIFORelaxed: - return "FIFO Relaxed"; - } - return "Unknown"; -} - u64 GetTelemetryId() { u64 telemetry_id{}; const auto filename = Common::FS::GetYuzuPath(Common::FS::YuzuPath::ConfigDir) / "telemetry_id"; @@ -255,8 +241,7 @@ void TelemetrySession::AddInitialInfo(Loader::AppLoader& app_loader, AddField(field_type, "Renderer_NvdecEmulation", TranslateNvdecEmulation(Settings::values.nvdec_emulation.GetValue())); AddField(field_type, "Renderer_AccelerateASTC", Settings::values.accelerate_astc.GetValue()); - AddField(field_type, "Renderer_UseVsync", - TranslateVSyncMode(Settings::values.vsync_mode.GetValue())); + AddField(field_type, "Renderer_UseVsync", Settings::values.use_vsync.GetValue()); AddField(field_type, "Renderer_ShaderBackend", static_cast(Settings::values.shader_backend.GetValue())); AddField(field_type, "Renderer_UseAsynchronousShaders", diff --git a/src/tests/video_core/memory_tracker.cpp b/src/tests/video_core/memory_tracker.cpp index 3981907a2..618793668 100755 --- 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") { memory_track->MarkRegionAsGpuModified(c + PAGE, PAGE); int num = 0; memory_track->ForEachDownloadRangeAndClear(c, WORD, [&](u64 offset, u64 size) { ++num; }); - REQUIRE(num == 1); + REQUIRE(num == 0); num = 0; memory_track->ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) { ++num; }); REQUIRE(num == 0); REQUIRE(!memory_track->IsRegionCpuModified(c + PAGE, PAGE)); - REQUIRE(!memory_track->IsRegionGpuModified(c + PAGE, PAGE)); + REQUIRE(memory_track->IsRegionGpuModified(c + PAGE, PAGE)); memory_track->FlushCachedWrites(); REQUIRE(memory_track->IsRegionCpuModified(c + PAGE, PAGE)); 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 800fd285d..caa7062e0 100755 --- a/src/video_core/buffer_cache/buffer_base.h +++ b/src/video_core/buffer_cache/buffer_base.h @@ -18,6 +18,7 @@ namespace VideoCommon { enum class BufferFlagBits { Picked = 1 << 0, CachedWrites = 1 << 1, + PreemtiveDownload = 1 << 2, }; DECLARE_ENUM_FLAG_OPERATORS(BufferFlagBits) @@ -54,6 +55,10 @@ public: flags |= BufferFlagBits::Picked; } + void MarkPreemtiveDownload() noexcept { + flags |= BufferFlagBits::PreemtiveDownload; + } + /// Unmark buffer as picked void Unpick() noexcept { flags &= ~BufferFlagBits::Picked; @@ -84,6 +89,10 @@ public: return True(flags & BufferFlagBits::CachedWrites); } + bool IsPreemtiveDownload() const noexcept { + return True(flags & BufferFlagBits::PreemtiveDownload); + } + /// Returns the base CPU address of the buffer [[nodiscard]] VAddr CpuAddr() const noexcept { return cpu_addr; diff --git a/src/video_core/buffer_cache/buffer_cache.h b/src/video_core/buffer_cache/buffer_cache.h index 85d565cd1..9d979c923 100755 --- a/src/video_core/buffer_cache/buffer_cache.h +++ b/src/video_core/buffer_cache/buffer_cache.h @@ -23,8 +23,6 @@ BufferCache

::BufferCache(VideoCore::RasterizerInterface& rasterizer_, common_ranges.clear(); inline_buffer_id = NULL_BUFFER_ID; - active_async_buffers = !Settings::IsGPULevelHigh(); - if (!runtime.CanReportMemoryUsage()) { minimum_memory = DEFAULT_EXPECTED_MEMORY; critical_memory = DEFAULT_CRITICAL_MEMORY; @@ -75,8 +73,6 @@ void BufferCache

::TickFrame() { uniform_cache_hits[0] = 0; uniform_cache_shots[0] = 0; - active_async_buffers = !Settings::IsGPULevelHigh(); - const bool skip_preferred = hits * 256 < shots * 251; uniform_buffer_skip_cache_size = skip_preferred ? DEFAULT_SKIP_CACHE_SIZE : 0; @@ -111,9 +107,25 @@ void BufferCache

::WriteMemory(VAddr cpu_addr, u64 size) { template void BufferCache

::CachedWriteMemory(VAddr cpu_addr, u64 size) { memory_tracker.CachedCpuWrite(cpu_addr, size); - const IntervalType add_interval{Common::AlignDown(cpu_addr, YUZU_PAGESIZE), - Common::AlignUp(cpu_addr + size, YUZU_PAGESIZE)}; - cached_ranges.add(add_interval); +} + +template +std::optional BufferCache

::GetFlushArea(VAddr cpu_addr, + u64 size) { + std::optional area{}; + area.emplace(); + VAddr cpu_addr_start_aligned = Common::AlignDown(cpu_addr, Core::Memory::YUZU_PAGESIZE); + VAddr cpu_addr_end_aligned = Common::AlignUp(cpu_addr + size, Core::Memory::YUZU_PAGESIZE); + area->start_address = cpu_addr_start_aligned; + area->end_address = cpu_addr_end_aligned; + if (memory_tracker.IsRegionPreflushable(cpu_addr, size)) { + area->preemtive = true; + return area; + }; + memory_tracker.MarkRegionAsPreflushable(cpu_addr_start_aligned, + cpu_addr_end_aligned - cpu_addr_start_aligned); + area->preemtive = !IsRegionGpuModified(cpu_addr, size); + return area; } template @@ -191,8 +203,11 @@ bool BufferCache

::DMACopy(GPUVAddr src_address, GPUVAddr dest_address, u64 am const VAddr new_base_address = *cpu_dest_address + diff; const IntervalType add_interval{new_base_address, new_base_address + size}; tmp_intervals.push_back(add_interval); - uncommitted_ranges.add(add_interval); - pending_ranges.add(add_interval); + if (!Settings::values.use_reactive_flushing.GetValue() || + memory_tracker.IsRegionPreflushable(new_base_address, new_base_address + size)) { + uncommitted_ranges.add(add_interval); + pending_ranges.add(add_interval); + } }; ForEachInRangeSet(common_ranges, *cpu_src_address, amount, mirror); // This subtraction in this order is important for overlapping copies. @@ -205,7 +220,7 @@ bool BufferCache

::DMACopy(GPUVAddr src_address, GPUVAddr dest_address, u64 am if (has_new_downloads) { memory_tracker.MarkRegionAsGpuModified(*cpu_dest_address, amount); } - std::vector tmp_buffer(amount); + tmp_buffer.resize(amount); cpu_memory.ReadBlockUnsafe(*cpu_src_address, tmp_buffer.data(), amount); cpu_memory.WriteBlockUnsafe(*cpu_dest_address, tmp_buffer.data(), amount); return true; @@ -441,9 +456,7 @@ void BufferCache

::BindComputeTextureBuffer(size_t tbo_index, GPUVAddr gpu_add template void BufferCache

::FlushCachedWrites() { - cached_write_buffer_ids.clear(); memory_tracker.FlushCachedWrites(); - cached_ranges.clear(); } template @@ -474,9 +487,8 @@ void BufferCache

::CommitAsyncFlushesHigh() { if (committed_ranges.empty()) { if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { - if (active_async_buffers) { - async_buffers.emplace_back(std::optional{}); - } + + async_buffers.emplace_back(std::optional{}); } return; } @@ -537,64 +549,65 @@ void BufferCache

::CommitAsyncFlushesHigh() { committed_ranges.clear(); if (downloads.empty()) { if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { - if (active_async_buffers) { - async_buffers.emplace_back(std::optional{}); - } + + async_buffers.emplace_back(std::optional{}); } return; } - if (active_async_buffers) { - if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { - auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes, true); - boost::container::small_vector normalized_copies; - IntervalSet new_async_range{}; - runtime.PreCopyBarrier(); - for (auto& [copy, buffer_id] : downloads) { - copy.dst_offset += download_staging.offset; - const std::array copies{copy}; - BufferCopy second_copy{copy}; - Buffer& buffer = slot_buffers[buffer_id]; - second_copy.src_offset = static_cast(buffer.CpuAddr()) + copy.src_offset; - VAddr orig_cpu_addr = static_cast(second_copy.src_offset); - const IntervalType base_interval{orig_cpu_addr, orig_cpu_addr + copy.size}; - async_downloads += std::make_pair(base_interval, 1); - runtime.CopyBuffer(download_staging.buffer, buffer, copies, false); - normalized_copies.push_back(second_copy); - } - runtime.PostCopyBarrier(); - pending_downloads.emplace_back(std::move(normalized_copies)); - async_buffers.emplace_back(download_staging); - } else { + if constexpr (IMPLEMENTS_ASYNC_DOWNLOADS) { + auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes, true); + boost::container::small_vector normalized_copies; + IntervalSet new_async_range{}; + runtime.PreCopyBarrier(); + for (auto& [copy, buffer_id] : downloads) { + copy.dst_offset += download_staging.offset; + const std::array copies{copy}; + BufferCopy second_copy{copy}; + Buffer& buffer = slot_buffers[buffer_id]; + second_copy.src_offset = static_cast(buffer.CpuAddr()) + copy.src_offset; + VAddr orig_cpu_addr = static_cast(second_copy.src_offset); + const IntervalType base_interval{orig_cpu_addr, orig_cpu_addr + copy.size}; + async_downloads += std::make_pair(base_interval, 1); + runtime.CopyBuffer(download_staging.buffer, buffer, copies, false); + normalized_copies.push_back(second_copy); + } + runtime.PostCopyBarrier(); + pending_downloads.emplace_back(std::move(normalized_copies)); + async_buffers.emplace_back(download_staging); + } else { + if (!Settings::IsGPULevelHigh()) { committed_ranges.clear(); uncommitted_ranges.clear(); - } - } else { - if constexpr (USE_MEMORY_MAPS) { - auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes); - runtime.PreCopyBarrier(); - for (auto& [copy, buffer_id] : downloads) { - // Have in mind the staging buffer offset for the copy - copy.dst_offset += download_staging.offset; - const std::array copies{copy}; - runtime.CopyBuffer(download_staging.buffer, slot_buffers[buffer_id], copies, false); - } - runtime.PostCopyBarrier(); - runtime.Finish(); - for (const auto& [copy, buffer_id] : downloads) { - const Buffer& buffer = slot_buffers[buffer_id]; - const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset; - // Undo the modified offset - const u64 dst_offset = copy.dst_offset - download_staging.offset; - const u8* read_mapped_memory = download_staging.mapped_span.data() + dst_offset; - cpu_memory.WriteBlockUnsafe(cpu_addr, read_mapped_memory, copy.size); - } } else { - const std::span immediate_buffer = ImmediateBuffer(largest_copy); - for (const auto& [copy, buffer_id] : downloads) { - Buffer& buffer = slot_buffers[buffer_id]; - buffer.ImmediateDownload(copy.src_offset, immediate_buffer.subspan(0, copy.size)); - const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset; - cpu_memory.WriteBlockUnsafe(cpu_addr, immediate_buffer.data(), copy.size); + if constexpr (USE_MEMORY_MAPS) { + auto download_staging = runtime.DownloadStagingBuffer(total_size_bytes); + runtime.PreCopyBarrier(); + for (auto& [copy, buffer_id] : downloads) { + // Have in mind the staging buffer offset for the copy + copy.dst_offset += download_staging.offset; + const std::array copies{copy}; + runtime.CopyBuffer(download_staging.buffer, slot_buffers[buffer_id], copies, + false); + } + runtime.PostCopyBarrier(); + runtime.Finish(); + for (const auto& [copy, buffer_id] : downloads) { + const Buffer& buffer = slot_buffers[buffer_id]; + const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset; + // Undo the modified offset + const u64 dst_offset = copy.dst_offset - download_staging.offset; + const u8* read_mapped_memory = download_staging.mapped_span.data() + dst_offset; + cpu_memory.WriteBlockUnsafe(cpu_addr, read_mapped_memory, copy.size); + } + } else { + const std::span immediate_buffer = ImmediateBuffer(largest_copy); + for (const auto& [copy, buffer_id] : downloads) { + Buffer& buffer = slot_buffers[buffer_id]; + buffer.ImmediateDownload(copy.src_offset, + immediate_buffer.subspan(0, copy.size)); + const VAddr cpu_addr = buffer.CpuAddr() + copy.src_offset; + cpu_memory.WriteBlockUnsafe(cpu_addr, immediate_buffer.data(), copy.size); + } } } } @@ -1221,6 +1234,10 @@ void BufferCache

::MarkWrittenBuffer(BufferId buffer_id, VAddr cpu_addr, u32 s const IntervalType base_interval{cpu_addr, cpu_addr + size}; common_ranges.add(base_interval); + if (Settings::values.use_reactive_flushing.GetValue() && + !memory_tracker.IsRegionPreflushable(cpu_addr, cpu_addr + size)) { + return; + } uncommitted_ranges.add(base_interval); pending_ranges.add(base_interval); } @@ -1629,7 +1646,6 @@ void BufferCache

::DeleteBuffer(BufferId buffer_id, bool do_not_mark) { replace(transform_feedback_buffers); replace(compute_uniform_buffers); replace(compute_storage_buffers); - std::erase(cached_write_buffer_ids, buffer_id); // Mark the whole buffer as CPU written to stop tracking CPU writes 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 100755 --- 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: void DownloadMemory(VAddr cpu_addr, u64 size); + std::optional GetFlushArea(VAddr cpu_addr, u64 size); + bool InlineMemory(VAddr dest_address, size_t copy_size, std::span inlined_buffer); void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size); @@ -541,8 +543,6 @@ private: std::array, NUM_STAGES>, Empty> uniform_buffer_binding_sizes{}; - std::vector cached_write_buffer_ids; - MemoryTracker memory_tracker; IntervalSet uncommitted_ranges; IntervalSet common_ranges; @@ -572,9 +572,8 @@ private: u64 critical_memory = 0; BufferId inline_buffer_id; - bool active_async_buffers = false; - std::array> CACHING_PAGEBITS)> page_table; + std::vector tmp_buffer; }; } // 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 100755 --- 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: }); } + /// Returns true if a region has been marked as Preflushable + [[nodiscard]] bool IsRegionPreflushable(VAddr query_cpu_addr, u64 query_size) noexcept { + return IteratePages( + query_cpu_addr, query_size, [](Manager* manager, u64 offset, size_t size) { + return manager->template IsRegionModified(offset, size); + }); + } + /// Mark region as CPU modified, notifying the rasterizer about this change void MarkRegionAsCpuModified(VAddr dirty_cpu_addr, u64 query_size) { IteratePages(dirty_cpu_addr, query_size, @@ -93,6 +101,15 @@ public: }); } + /// Mark region as modified from the host GPU + void MarkRegionAsPreflushable(VAddr dirty_cpu_addr, u64 query_size) noexcept { + IteratePages(dirty_cpu_addr, query_size, + [](Manager* manager, u64 offset, size_t size) { + manager->template ChangeRegionState( + manager->GetCpuAddr() + offset, size); + }); + } + /// Unmark region as modified from the host GPU void UnmarkRegionAsGpuModified(VAddr dirty_cpu_addr, u64 query_size) noexcept { IteratePages(dirty_cpu_addr, query_size, @@ -102,6 +119,15 @@ public: }); } + /// Unmark region as modified from the host GPU + void UnmarkRegionAsPreflushable(VAddr dirty_cpu_addr, u64 query_size) noexcept { + IteratePages(dirty_cpu_addr, query_size, + [](Manager* manager, u64 offset, size_t size) { + manager->template ChangeRegionState( + manager->GetCpuAddr() + offset, size); + }); + } + /// Mark region as modified from the CPU /// but don't mark it as modified until FlusHCachedWrites is called. 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 100755 --- 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 { GPU, CachedCPU, Untracked, + Preflushable, }; /// Vector tracking modified pages tightly packed with small vector optimization @@ -55,17 +56,20 @@ struct Words { gpu.stack.fill(0); cached_cpu.stack.fill(0); untracked.stack.fill(~u64{0}); + preflushable.stack.fill(0); } else { // Share allocation between CPU and GPU pages and set their default values - u64* const alloc = new u64[num_words * 4]; + u64* const alloc = new u64[num_words * 5]; cpu.heap = alloc; gpu.heap = alloc + num_words; cached_cpu.heap = alloc + num_words * 2; untracked.heap = alloc + num_words * 3; + preflushable.heap = alloc + num_words * 4; std::fill_n(cpu.heap, num_words, ~u64{0}); std::fill_n(gpu.heap, num_words, 0); std::fill_n(cached_cpu.heap, num_words, 0); std::fill_n(untracked.heap, num_words, ~u64{0}); + std::fill_n(preflushable.heap, num_words, 0); } // Clean up tailing bits const u64 last_word_size = size_bytes % BYTES_PER_WORD; @@ -88,13 +92,14 @@ struct Words { gpu = rhs.gpu; cached_cpu = rhs.cached_cpu; untracked = rhs.untracked; + preflushable = rhs.preflushable; rhs.cpu.heap = nullptr; return *this; } Words(Words&& rhs) noexcept : size_bytes{rhs.size_bytes}, num_words{rhs.num_words}, cpu{rhs.cpu}, gpu{rhs.gpu}, - cached_cpu{rhs.cached_cpu}, untracked{rhs.untracked} { + cached_cpu{rhs.cached_cpu}, untracked{rhs.untracked}, preflushable{rhs.preflushable} { rhs.cpu.heap = nullptr; } @@ -129,6 +134,8 @@ struct Words { return std::span(cached_cpu.Pointer(IsShort()), num_words); } else if constexpr (type == Type::Untracked) { return std::span(untracked.Pointer(IsShort()), num_words); + } else if constexpr (type == Type::Preflushable) { + return std::span(preflushable.Pointer(IsShort()), num_words); } } @@ -142,6 +149,8 @@ struct Words { return std::span(cached_cpu.Pointer(IsShort()), num_words); } else if constexpr (type == Type::Untracked) { return std::span(untracked.Pointer(IsShort()), num_words); + } else if constexpr (type == Type::Preflushable) { + return std::span(preflushable.Pointer(IsShort()), num_words); } } @@ -151,6 +160,7 @@ struct Words { WordsArray gpu; WordsArray cached_cpu; WordsArray untracked; + WordsArray preflushable; }; template @@ -292,6 +302,9 @@ public: (pending_pointer - pending_offset) * BYTES_PER_PAGE); }; IterateWords(offset, size, [&](size_t index, u64 mask) { + if constexpr (type == Type::GPU) { + mask &= ~untracked_words[index]; + } const u64 word = state_words[index] & mask; if constexpr (clear) { if constexpr (type == Type::CPU || type == Type::CachedCPU) { @@ -340,8 +353,13 @@ public: static_assert(type != Type::Untracked); const std::span state_words = words.template Span(); + [[maybe_unused]] const std::span untracked_words = + words.template Span(); bool result = false; IterateWords(offset, size, [&](size_t index, u64 mask) { + if constexpr (type == Type::GPU) { + mask &= ~untracked_words[index]; + } const u64 word = state_words[index] & mask; if (word != 0) { result = true; @@ -362,9 +380,14 @@ public: [[nodiscard]] std::pair ModifiedRegion(u64 offset, u64 size) const noexcept { static_assert(type != Type::Untracked); const std::span state_words = words.template Span(); + [[maybe_unused]] const std::span untracked_words = + words.template Span(); u64 begin = std::numeric_limits::max(); u64 end = 0; IterateWords(offset, size, [&](size_t index, u64 mask) { + if constexpr (type == Type::GPU) { + mask &= ~untracked_words[index]; + } const u64 word = state_words[index] & mask; if (word == 0) { return; diff --git a/src/video_core/engines/maxwell_dma.cpp b/src/video_core/engines/maxwell_dma.cpp index ab068f519..1c698fbb1 100755 --- a/src/video_core/engines/maxwell_dma.cpp +++ b/src/video_core/engines/maxwell_dma.cpp @@ -223,7 +223,7 @@ void MaxwellDMA::CopyBlockLinearToPitch() { write_buffer.resize_destructive(dst_size); memory_manager.ReadBlock(src_operand.address, read_buffer.data(), src_size); - memory_manager.ReadBlockUnsafe(dst_operand.address, write_buffer.data(), dst_size); + memory_manager.ReadBlock(dst_operand.address, write_buffer.data(), dst_size); UnswizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset, src_params.origin.y, x_elements, regs.line_count, block_height, block_depth, @@ -288,11 +288,7 @@ void MaxwellDMA::CopyPitchToBlockLinear() { write_buffer.resize_destructive(dst_size); memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size); - if (Settings::IsGPULevelExtreme()) { - memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size); - } else { - memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size); - } + memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size); // If the input is linear and the output is tiled, swizzle the input and copy it over. 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 3c088786f..e44b3a0fd 100755 --- a/src/video_core/fence_manager.h +++ b/src/video_core/fence_manager.h @@ -59,6 +59,11 @@ public: buffer_cache.AccumulateFlushes(); } + void SignalReference() { + std::function do_nothing([] {}); + SignalFence(std::move(do_nothing)); + } + void SyncOperation(std::function&& func) { uncommitted_operations.emplace_back(std::move(func)); } diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp index ae268a870..3ab20e4d2 100755 --- a/src/video_core/gpu.cpp +++ b/src/video_core/gpu.cpp @@ -283,6 +283,21 @@ struct GPU::Impl { gpu_thread.FlushRegion(addr, size); } + VideoCore::RasterizerDownloadArea OnCPURead(VAddr addr, u64 size) { + auto raster_area = rasterizer->GetFlushArea(addr, size); + if (raster_area.preemtive) { + return raster_area; + } + raster_area.preemtive = true; + const u64 fence = RequestSyncOperation([this, &raster_area]() { + rasterizer->FlushRegion(raster_area.start_address, + raster_area.end_address - raster_area.start_address); + }); + gpu_thread.TickGPU(); + WaitForSyncOperation(fence); + return raster_area; + } + /// Notify rasterizer that any caches of the specified region should be invalidated void InvalidateRegion(VAddr addr, u64 size) { gpu_thread.InvalidateRegion(addr, size); @@ -538,6 +553,10 @@ void GPU::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { impl->SwapBuffers(framebuffer); } +VideoCore::RasterizerDownloadArea GPU::OnCPURead(VAddr addr, u64 size) { + return impl->OnCPURead(addr, size); +} + void GPU::FlushRegion(VAddr addr, u64 size) { impl->FlushRegion(addr, size); } diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h index b9e70b9c1..cae765a6c 100755 --- a/src/video_core/gpu.h +++ b/src/video_core/gpu.h @@ -10,6 +10,7 @@ #include "core/hle/service/nvdrv/nvdata.h" #include "video_core/cdma_pusher.h" #include "video_core/framebuffer_config.h" +#include "video_core/rasterizer_download_area.h" namespace Core { class System; @@ -240,6 +241,9 @@ public: /// Swap buffers (render frame) void SwapBuffers(const Tegra::FramebufferConfig* framebuffer); + /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory + [[nodiscard]] VideoCore::RasterizerDownloadArea OnCPURead(VAddr addr, u64 size); + /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory void FlushRegion(VAddr addr, u64 size); diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h index bede0c32c..2c6b53e1e 100755 --- a/src/video_core/query_cache.h +++ b/src/video_core/query_cache.h @@ -255,7 +255,6 @@ private: if (!in_range(query)) { continue; } - rasterizer.UpdatePagesCachedCount(query.GetCpuAddr(), query.SizeInBytes(), -1); AsyncJobId async_job_id = query.GetAsyncJob(); auto flush_result = query.Flush(async); if (async_job_id == NULL_ASYNC_JOB_ID) { @@ -273,7 +272,6 @@ private: /// Registers the passed parameters as cached and returns a pointer to the stored cached query. CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) { - rasterizer.UpdatePagesCachedCount(cpu_addr, CachedQuery::SizeInBytes(timestamp), 1); const u64 page = static_cast(cpu_addr) >> YUZU_PAGEBITS; return &cached_queries[page].emplace_back(static_cast(*this), type, cpu_addr, host_ptr); diff --git a/src/video_core/rasterizer_download_area.h b/src/video_core/rasterizer_download_area.h new file mode 100755 index 000000000..2d7425c79 --- /dev/null +++ b/src/video_core/rasterizer_download_area.h @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include "common/common_types.h" + +namespace VideoCore { + +struct RasterizerDownloadArea { + VAddr start_address; + VAddr end_address; + bool preemtive; +}; + +} // 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 8bf280523..9e26d2a32 100755 --- a/src/video_core/rasterizer_interface.h +++ b/src/video_core/rasterizer_interface.h @@ -12,6 +12,7 @@ #include "video_core/cache_types.h" #include "video_core/engines/fermi_2d.h" #include "video_core/gpu.h" +#include "video_core/rasterizer_download_area.h" namespace Tegra { class MemoryManager; @@ -95,6 +96,8 @@ public: virtual bool MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) = 0; + virtual RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) = 0; + /// Notify rasterizer that any caches of the specified region should be invalidated virtual void InvalidateRegion(VAddr addr, u64 size, 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 100755 --- a/src/video_core/renderer_null/null_rasterizer.cpp +++ b/src/video_core/renderer_null/null_rasterizer.cpp @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "common/alignment.h" +#include "core/memory.h" #include "video_core/host1x/host1x.h" #include "video_core/memory_manager.h" #include "video_core/renderer_null/null_rasterizer.h" @@ -46,6 +48,14 @@ bool RasterizerNull::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheTyp } void RasterizerNull::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType) {} void RasterizerNull::OnCPUWrite(VAddr addr, u64 size) {} +VideoCore::RasterizerDownloadArea RasterizerNull::GetFlushArea(VAddr addr, u64 size) { + VideoCore::RasterizerDownloadArea new_area{ + .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE), + .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE), + .preemtive = true, + }; + return new_area; +} void RasterizerNull::InvalidateGPUCache() {} void RasterizerNull::UnmapMemory(VAddr addr, u64 size) {} void 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 100755 --- a/src/video_core/renderer_null/null_rasterizer.h +++ b/src/video_core/renderer_null/null_rasterizer.h @@ -54,6 +54,7 @@ public: void InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override; void OnCPUWrite(VAddr addr, u64 size) override; + VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override; void InvalidateGPUCache() override; void UnmapMemory(VAddr addr, u64 size) override; 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 be32767a7..0c830b5b3 100755 --- 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 return false; } +VideoCore::RasterizerDownloadArea RasterizerOpenGL::GetFlushArea(VAddr addr, u64 size) { + { + std::scoped_lock lock{texture_cache.mutex}; + auto area = texture_cache.GetFlushArea(addr, size); + if (area) { + return *area; + } + } + { + std::scoped_lock lock{buffer_cache.mutex}; + auto area = buffer_cache.GetFlushArea(addr, size); + if (area) { + return *area; + } + } + VideoCore::RasterizerDownloadArea new_area{ + .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE), + .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE), + .preemtive = true, + }; + return new_area; +} + void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) { MICROPROFILE_SCOPE(OpenGL_CacheManagement); if (addr == 0 || size == 0) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 18f971c2b..6fa755e62 100755 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -95,6 +95,7 @@ public: VideoCommon::CacheType which = VideoCommon::CacheType::All) override; bool MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override; + VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override; void InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override; void OnCPUWrite(VAddr addr, u64 size) override; diff --git a/src/video_core/renderer_vulkan/renderer_vulkan.cpp b/src/video_core/renderer_vulkan/renderer_vulkan.cpp index 0d11073c6..b7e145eae 100755 --- a/src/video_core/renderer_vulkan/renderer_vulkan.cpp +++ b/src/video_core/renderer_vulkan/renderer_vulkan.cpp @@ -88,7 +88,7 @@ RendererVulkan::RendererVulkan(Core::TelemetrySession& telemetry_session_, instance(CreateInstance(library, dld, VK_API_VERSION_1_1, render_window.GetWindowInfo().type, Settings::values.renderer_debug.GetValue())), debug_callback(Settings::values.renderer_debug ? CreateDebugCallback(instance) : nullptr), - surface(CreateSurface(instance, render_window.GetWindowInfo())), + surface(CreateSurface(instance, render_window)), device(CreateDevice(instance, dld, *surface)), memory_allocator(device, false), state_tracker(), scheduler(device, state_tracker), swapchain(*surface, device, scheduler, render_window.GetFramebufferLayout().width, diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index 7a8afb122..1430364f6 100755 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp @@ -502,6 +502,29 @@ bool RasterizerVulkan::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheT return false; } +VideoCore::RasterizerDownloadArea RasterizerVulkan::GetFlushArea(VAddr addr, u64 size) { + { + std::scoped_lock lock{texture_cache.mutex}; + auto area = texture_cache.GetFlushArea(addr, size); + if (area) { + return *area; + } + } + { + std::scoped_lock lock{buffer_cache.mutex}; + auto area = buffer_cache.GetFlushArea(addr, size); + if (area) { + return *area; + } + } + VideoCore::RasterizerDownloadArea new_area{ + .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE), + .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE), + .preemtive = true, + }; + return new_area; +} + void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) { if (addr == 0 || size == 0) { return; @@ -598,7 +621,7 @@ void RasterizerVulkan::SignalSyncPoint(u32 value) { } void RasterizerVulkan::SignalReference() { - fence_manager.SignalOrdering(); + fence_manager.SignalReference(); } void RasterizerVulkan::ReleaseFences() { @@ -631,7 +654,7 @@ void RasterizerVulkan::WaitForIdle() { cmdbuf.SetEvent(event, flags); cmdbuf.WaitEvents(event, flags, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, {}, {}, {}); }); - SignalReference(); + fence_manager.SignalOrdering(); } void RasterizerVulkan::FragmentBarrier() { diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h index c5be4fbf1..249c4952d 100755 --- a/src/video_core/renderer_vulkan/vk_rasterizer.h +++ b/src/video_core/renderer_vulkan/vk_rasterizer.h @@ -92,6 +92,7 @@ public: VideoCommon::CacheType which = VideoCommon::CacheType::All) override; bool MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override; + VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override; void InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override; void InnerInvalidation(std::span> sequences) override; diff --git a/src/video_core/renderer_vulkan/vk_swapchain.cpp b/src/video_core/renderer_vulkan/vk_swapchain.cpp index 0df787917..04a00ce0c 100755 --- a/src/video_core/renderer_vulkan/vk_swapchain.cpp +++ b/src/video_core/renderer_vulkan/vk_swapchain.cpp @@ -14,7 +14,6 @@ #include "video_core/renderer_vulkan/vk_swapchain.h" #include "video_core/vulkan_common/vulkan_device.h" #include "video_core/vulkan_common/vulkan_wrapper.h" -#include "vulkan/vulkan_core.h" namespace Vulkan { @@ -34,47 +33,23 @@ VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span formats) return found != formats.end() ? *found : formats[0]; } -static constexpr VkPresentModeKHR ChooseSwapPresentMode(bool has_imm, bool has_mailbox, - bool has_fifo_relaxed) { - // Mailbox doesn't lock the application like FIFO (vsync) - // FIFO present mode locks the framerate to the monitor's refresh rate - Settings::VSyncMode setting = [has_imm, has_mailbox]() { - // Choose Mailbox or Immediate if unlocked and those modes are supported - const auto mode = Settings::values.vsync_mode.GetValue(); - if (Settings::values.use_speed_limit.GetValue()) { - return mode; - } - switch (mode) { - case Settings::VSyncMode::FIFO: - case Settings::VSyncMode::FIFORelaxed: - if (has_mailbox) { - return Settings::VSyncMode::Mailbox; - } else if (has_imm) { - return Settings::VSyncMode::Immediate; - } - [[fallthrough]]; - default: - return mode; - } - }(); - if ((setting == Settings::VSyncMode::Mailbox && !has_mailbox) || - (setting == Settings::VSyncMode::Immediate && !has_imm) || - (setting == Settings::VSyncMode::FIFORelaxed && !has_fifo_relaxed)) { - setting = Settings::VSyncMode::FIFO; - } - - switch (setting) { - case Settings::VSyncMode::Immediate: - return VK_PRESENT_MODE_IMMEDIATE_KHR; - case Settings::VSyncMode::Mailbox: +VkPresentModeKHR ChooseSwapPresentMode(vk::Span modes) { + // Mailbox (triple buffering) doesn't lock the application like fifo (vsync), + // prefer it if vsync option is not selected + const auto found_mailbox = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_MAILBOX_KHR); + if (Settings::values.fullscreen_mode.GetValue() == Settings::FullscreenMode::Borderless && + found_mailbox != modes.end() && !Settings::values.use_vsync.GetValue()) { return VK_PRESENT_MODE_MAILBOX_KHR; - case Settings::VSyncMode::FIFO: - return VK_PRESENT_MODE_FIFO_KHR; - case Settings::VSyncMode::FIFORelaxed: - return VK_PRESENT_MODE_FIFO_RELAXED_KHR; - default: - return VK_PRESENT_MODE_FIFO_KHR; } + if (!Settings::values.use_speed_limit.GetValue()) { + // FIFO present mode locks the framerate to the monitor's refresh rate, + // Find an alternative to surpass this limitation if FPS is unlocked. + const auto found_imm = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_IMMEDIATE_KHR); + if (found_imm != modes.end()) { + return VK_PRESENT_MODE_IMMEDIATE_KHR; + } + } + return VK_PRESENT_MODE_FIFO_KHR; } VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, u32 width, u32 height) { @@ -192,17 +167,11 @@ void Swapchain::Present(VkSemaphore render_semaphore) { void Swapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, bool srgb) { const auto physical_device{device.GetPhysical()}; const auto formats{physical_device.GetSurfaceFormatsKHR(surface)}; - const auto present_modes = physical_device.GetSurfacePresentModesKHR(surface); - has_mailbox = std::find(present_modes.begin(), present_modes.end(), - VK_PRESENT_MODE_MAILBOX_KHR) != present_modes.end(); - has_imm = std::find(present_modes.begin(), present_modes.end(), - VK_PRESENT_MODE_IMMEDIATE_KHR) != present_modes.end(); - has_fifo_relaxed = std::find(present_modes.begin(), present_modes.end(), - VK_PRESENT_MODE_FIFO_RELAXED_KHR) != present_modes.end(); + const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)}; const VkCompositeAlphaFlagBitsKHR alpha_flags{ChooseAlphaFlags(capabilities)}; surface_format = ChooseSwapSurfaceFormat(formats); - present_mode = ChooseSwapPresentMode(has_imm, has_mailbox, has_fifo_relaxed); + present_mode = ChooseSwapPresentMode(present_modes); u32 requested_image_count{capabilities.minImageCount + 1}; // Ensure Triple buffering if possible. @@ -263,6 +232,7 @@ void Swapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, bo extent = swapchain_ci.imageExtent; current_srgb = srgb; + current_fps_unlocked = !Settings::values.use_speed_limit.GetValue(); images = swapchain.GetImages(); image_count = static_cast(images.size()); @@ -284,9 +254,14 @@ void Swapchain::Destroy() { swapchain.reset(); } +bool Swapchain::HasFpsUnlockChanged() const { + return current_fps_unlocked != !Settings::values.use_speed_limit.GetValue(); +} + bool Swapchain::NeedsPresentModeUpdate() const { - const auto requested_mode = ChooseSwapPresentMode(has_imm, has_mailbox, has_fifo_relaxed); - return present_mode != requested_mode; + // Mailbox present mode is the ideal for all scenarios. If it is not available, + // A different present mode is needed to support unlocked FPS above the monitor's refresh rate. + return present_mode != VK_PRESENT_MODE_MAILBOX_KHR && HasFpsUnlockChanged(); } } // namespace Vulkan diff --git a/src/video_core/renderer_vulkan/vk_swapchain.h b/src/video_core/renderer_vulkan/vk_swapchain.h index 081f91396..de052244a 100755 --- a/src/video_core/renderer_vulkan/vk_swapchain.h +++ b/src/video_core/renderer_vulkan/vk_swapchain.h @@ -116,6 +116,8 @@ private: void Destroy(); + bool HasFpsUnlockChanged() const; + bool NeedsPresentModeUpdate() const; const VkSurfaceKHR surface; @@ -140,11 +142,9 @@ private: VkExtent2D extent{}; VkPresentModeKHR present_mode{}; VkSurfaceFormatKHR surface_format{}; - bool has_imm{false}; - bool has_mailbox{false}; - bool has_fifo_relaxed{false}; bool current_srgb{}; + bool current_fps_unlocked{}; bool is_outdated{}; bool is_suboptimal{}; }; diff --git a/src/video_core/texture_cache/image_info.h b/src/video_core/texture_cache/image_info.h index eca3b313c..0a50795b3 100755 --- a/src/video_core/texture_cache/image_info.h +++ b/src/video_core/texture_cache/image_info.h @@ -39,6 +39,8 @@ struct ImageInfo { u32 tile_width_spacing = 0; bool rescaleable = false; bool downscaleable = false; + bool forced_flushed = false; + bool dma_downloaded = false; }; } // 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 860cfca20..46a46ba91 100755 --- a/src/video_core/texture_cache/image_view_base.cpp +++ b/src/video_core/texture_cache/image_view_base.cpp @@ -26,8 +26,9 @@ ImageViewBase::ImageViewBase(const ImageViewInfo& info, const ImageInfo& image_i ASSERT_MSG(VideoCore::Surface::IsViewCompatible(image_info.format, info.format, false, true), "Image view format {} is incompatible with image format {}", info.format, image_info.format); - const bool is_async = Settings::values.use_asynchronous_gpu_emulation.GetValue(); - if (image_info.type == ImageType::Linear && is_async) { + const bool preemptive = + !Settings::values.use_reactive_flushing.GetValue() && image_info.type == ImageType::Linear; + if (image_info.forced_flushed || preemptive) { flags |= ImageViewFlagBits::PreemtiveDownload; } 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 025012b47..7c82b9f3f 100755 --- a/src/video_core/texture_cache/texture_cache.h +++ b/src/video_core/texture_cache/texture_cache.h @@ -490,6 +490,32 @@ void TextureCache

::DownloadMemory(VAddr cpu_addr, size_t size) { } } +template +std::optional TextureCache

::GetFlushArea(VAddr cpu_addr, + u64 size) { + std::optional area{}; + ForEachImageInRegion(cpu_addr, size, [&](ImageId, ImageBase& image) { + if (False(image.flags & ImageFlagBits::GpuModified)) { + return; + } + if (!area) { + area.emplace(); + area->start_address = cpu_addr; + area->end_address = cpu_addr + size; + area->preemtive = true; + } + area->start_address = std::min(area->start_address, image.cpu_addr); + area->end_address = std::max(area->end_address, image.cpu_addr_end); + for (auto image_view_id : image.image_view_ids) { + auto& image_view = slot_image_views[image_view_id]; + image_view.flags |= ImageViewFlagBits::PreemtiveDownload; + } + area->preemtive &= image.info.forced_flushed; + image.info.forced_flushed = true; + }); + return area; +} + template void TextureCache

::UnmapMemory(VAddr cpu_addr, size_t size) { std::vector deleted_images; @@ -683,18 +709,43 @@ void TextureCache

::CommitAsyncFlushes() { download_info.async_buffer_id = last_async_buffer_id; } } + if (any_none_dma) { + bool all_pre_sync = true; auto download_map = runtime.DownloadStagingBuffer(total_size_bytes, true); for (const PendingDownload& download_info : download_ids) { if (download_info.is_swizzle) { Image& image = slot_images[download_info.object_id]; + all_pre_sync &= image.info.dma_downloaded; + image.info.dma_downloaded = true; const auto copies = FullDownloadCopies(image.info); image.DownloadMemory(download_map, copies); download_map.offset += Common::AlignUp(image.unswizzled_size_bytes, 64); } } - uncommitted_async_buffers.emplace_back(download_map); + if (!all_pre_sync) { + runtime.Finish(); + auto it = download_ids.begin(); + while (it != download_ids.end()) { + const PendingDownload& download_info = *it; + if (download_info.is_swizzle) { + const ImageBase& image = slot_images[download_info.object_id]; + const auto copies = FullDownloadCopies(image.info); + download_map.offset -= Common::AlignUp(image.unswizzled_size_bytes, 64); + std::span download_span = + download_map.mapped_span.subspan(download_map.offset); + SwizzleImage(*gpu_memory, image.gpu_addr, image.info, copies, download_span, + swizzle_data_buffer); + it = download_ids.erase(it); + } else { + it++; + } + } + } else { + uncommitted_async_buffers.emplace_back(download_map); + } } + async_buffers.emplace_back(std::move(uncommitted_async_buffers)); uncommitted_async_buffers.clear(); } @@ -789,11 +840,16 @@ ImageId TextureCache

::DmaImageId(const Tegra::DMA::ImageOperand& operand) { if (!dst_id) { return NULL_IMAGE_ID; } - const auto& image = slot_images[dst_id]; + auto& image = slot_images[dst_id]; if (False(image.flags & ImageFlagBits::GpuModified)) { // No need to waste time on an image that's synced with guest return NULL_IMAGE_ID; } + if (!image.info.dma_downloaded) { + // Force a full sync. + image.info.dma_downloaded = true; + return NULL_IMAGE_ID; + } const auto base = image.TryFindBase(operand.address); if (!base) { return NULL_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 ccf1c8021..c1f36d86b 100755 --- 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: /// Download contents of host images to guest memory in a region void DownloadMemory(VAddr cpu_addr, size_t size); + std::optional GetFlushArea(VAddr cpu_addr, u64 size); + /// Remove images in a region void UnmapMemory(VAddr cpu_addr, size_t size); diff --git a/src/video_core/vulkan_common/vulkan_surface.cpp b/src/video_core/vulkan_common/vulkan_surface.cpp index af736e628..bb8208b97 100755 --- a/src/video_core/vulkan_common/vulkan_surface.cpp +++ b/src/video_core/vulkan_common/vulkan_surface.cpp @@ -23,10 +23,10 @@ namespace Vulkan { -vk::SurfaceKHR CreateSurface( - const vk::Instance& instance, - [[maybe_unused]] const Core::Frontend::EmuWindow::WindowSystemInfo& window_info) { +vk::SurfaceKHR CreateSurface(const vk::Instance& instance, + const Core::Frontend::EmuWindow& emu_window) { [[maybe_unused]] const vk::InstanceDispatch& dld = instance.Dispatch(); + [[maybe_unused]] const auto& window_info = emu_window.GetWindowInfo(); VkSurfaceKHR unsafe_surface = nullptr; #ifdef _WIN32 diff --git a/src/video_core/vulkan_common/vulkan_surface.h b/src/video_core/vulkan_common/vulkan_surface.h index 152c40614..b290a1618 100755 --- a/src/video_core/vulkan_common/vulkan_surface.h +++ b/src/video_core/vulkan_common/vulkan_surface.h @@ -3,12 +3,15 @@ #pragma once -#include "core/frontend/emu_window.h" #include "video_core/vulkan_common/vulkan_wrapper.h" +namespace Core::Frontend { +class EmuWindow; +} + namespace Vulkan { -[[nodiscard]] vk::SurfaceKHR CreateSurface( - const vk::Instance& instance, const Core::Frontend::EmuWindow::WindowSystemInfo& window_info); +[[nodiscard]] vk::SurfaceKHR CreateSurface(const vk::Instance& instance, + const Core::Frontend::EmuWindow& emu_window); } // namespace Vulkan diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index d9f07ea41..7fa835de9 100755 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt @@ -189,8 +189,6 @@ add_executable(yuzu multiplayer/state.h multiplayer/validation.h precompiled_headers.h - qt_common.cpp - qt_common.h startup_checks.cpp startup_checks.h uisettings.cpp diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index 20a0ea36e..2e08e3bd4 100755 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -1,48 +1,36 @@ // SPDX-FileCopyrightText: 2014 Citra Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include -#include -#include -#include -#include -#include -#include #include -#include +#include #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA -#include #include #include #endif -#include -#include -#include #include -#include -#include -#include #include +#include #include -#include -#include -#include -#include +#include +#include #include -#include #ifdef HAS_OPENGL #include #include #endif +#if !defined(WIN32) +#include +#endif + +#include + +#include "common/assert.h" #include "common/microprofile.h" -#include "common/polyfill_thread.h" #include "common/scm_rev.h" #include "common/settings.h" -#include "common/settings_input.h" -#include "common/thread.h" #include "core/core.h" #include "core/cpu_manager.h" #include "core/frontend/framebuffer_layout.h" @@ -52,16 +40,11 @@ #include "input_common/drivers/tas_input.h" #include "input_common/drivers/touch_screen.h" #include "input_common/main.h" -#include "video_core/gpu.h" -#include "video_core/rasterizer_interface.h" #include "video_core/renderer_base.h" #include "yuzu/bootmanager.h" #include "yuzu/main.h" -#include "yuzu/qt_common.h" -class QObject; -class QPaintEngine; -class QSurface; +static Core::Frontend::WindowSystemType GetWindowSystemType(); EmuThread::EmuThread(Core::System& system) : m_system{system} {} @@ -171,10 +154,7 @@ public: // disable vsync for any shared contexts auto format = share_context->format(); - const int swap_interval = - Settings::values.vsync_mode.GetValue() == Settings::VSyncMode::Immediate ? 0 : 1; - - format.setSwapInterval(main_surface ? swap_interval : 0); + format.setSwapInterval(main_surface ? Settings::values.use_vsync.GetValue() : 0); context = std::make_unique(); context->setShareContext(share_context); @@ -241,7 +221,7 @@ public: explicit RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) { setAttribute(Qt::WA_NativeWindow); setAttribute(Qt::WA_PaintOnScreen); - if (QtCommon::GetWindowSystemType() == Core::Frontend::WindowSystemType::Wayland) { + if (GetWindowSystemType() == Core::Frontend::WindowSystemType::Wayland) { setAttribute(Qt::WA_DontCreateNativeAncestors); } } @@ -279,6 +259,46 @@ struct NullRenderWidget : public RenderWidget { explicit NullRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {} }; +static Core::Frontend::WindowSystemType GetWindowSystemType() { + // Determine WSI type based on Qt platform. + QString platform_name = QGuiApplication::platformName(); + if (platform_name == QStringLiteral("windows")) + return Core::Frontend::WindowSystemType::Windows; + else if (platform_name == QStringLiteral("xcb")) + return Core::Frontend::WindowSystemType::X11; + else if (platform_name == QStringLiteral("wayland")) + return Core::Frontend::WindowSystemType::Wayland; + else if (platform_name == QStringLiteral("wayland-egl")) + return Core::Frontend::WindowSystemType::Wayland; + else if (platform_name == QStringLiteral("cocoa")) + return Core::Frontend::WindowSystemType::Cocoa; + else if (platform_name == QStringLiteral("android")) + return Core::Frontend::WindowSystemType::Android; + + LOG_CRITICAL(Frontend, "Unknown Qt platform {}!", platform_name.toStdString()); + return Core::Frontend::WindowSystemType::Windows; +} + +static Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) { + Core::Frontend::EmuWindow::WindowSystemInfo wsi; + wsi.type = GetWindowSystemType(); + + // Our Win32 Qt external doesn't have the private API. +#if defined(WIN32) || defined(__APPLE__) + wsi.render_surface = window ? reinterpret_cast(window->winId()) : nullptr; +#else + QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface(); + wsi.display_connection = pni->nativeResourceForWindow("display", window); + if (wsi.type == Core::Frontend::WindowSystemType::Wayland) + wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr; + else + wsi.render_surface = window ? reinterpret_cast(window->winId()) : nullptr; +#endif + wsi.render_surface_scale = window ? static_cast(window->devicePixelRatio()) : 1.0f; + + return wsi; +} + GRenderWindow::GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_, std::shared_ptr input_subsystem_, Core::System& system_) @@ -884,7 +904,7 @@ bool GRenderWindow::InitRenderTarget() { } // Update the Window System information with the new render target - window_info = QtCommon::GetWindowSystemInfo(child_widget->windowHandle()); + window_info = GetWindowSystemInfo(child_widget->windowHandle()); child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height); layout()->addWidget(child_widget); diff --git a/src/yuzu/bootmanager.h b/src/yuzu/bootmanager.h index 59fa291a9..d91f94ccc 100755 --- a/src/yuzu/bootmanager.h +++ b/src/yuzu/bootmanager.h @@ -5,46 +5,27 @@ #include #include -#include #include #include -#include -#include -#include -#include #include -#include -#include -#include #include #include +#include #include -#include -#include -#include -#include "common/common_types.h" -#include "common/logging/log.h" #include "common/polyfill_thread.h" #include "common/thread.h" #include "core/frontend/emu_window.h" +class GRenderWindow; class GMainWindow; class QCamera; class QCameraImageCapture; -class QCloseEvent; -class QFocusEvent; class QKeyEvent; -class QMouseEvent; -class QObject; -class QResizeEvent; -class QShowEvent; -class QTimer; -class QTouchEvent; -class QWheelEvent; namespace Core { +enum class SystemResultStatus : u32; class System; } // namespace Core @@ -59,6 +40,7 @@ enum class TasState; namespace VideoCore { enum class LoadCallbackStage; +class RendererBase; } // namespace VideoCore class EmuThread final : public QThread { diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 8020c4359..8bc9abd29 100755 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -6,7 +6,6 @@ #include #include "common/fs/fs.h" #include "common/fs/path_util.h" -#include "common/settings.h" #include "core/core.h" #include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/hid/controllers/npad.h" @@ -710,6 +709,8 @@ void Config::ReadRendererValues() { ReadGlobalSetting(Settings::values.nvdec_emulation); ReadGlobalSetting(Settings::values.accelerate_astc); ReadGlobalSetting(Settings::values.async_astc); + ReadGlobalSetting(Settings::values.use_vsync); + ReadGlobalSetting(Settings::values.use_reactive_flushing); ReadGlobalSetting(Settings::values.shader_backend); ReadGlobalSetting(Settings::values.use_asynchronous_shaders); ReadGlobalSetting(Settings::values.use_fast_gpu_time); @@ -719,10 +720,6 @@ void Config::ReadRendererValues() { ReadGlobalSetting(Settings::values.bg_blue); if (global) { - Settings::values.vsync_mode.SetValue(static_cast( - ReadSetting(QString::fromStdString(Settings::values.vsync_mode.GetLabel()), - static_cast(Settings::values.vsync_mode.GetDefault())) - .value())); ReadBasicSetting(Settings::values.renderer_debug); ReadBasicSetting(Settings::values.renderer_shader_feedback); ReadBasicSetting(Settings::values.enable_nsight_aftermath); @@ -1355,6 +1352,8 @@ void Config::SaveRendererValues() { Settings::values.nvdec_emulation.UsingGlobal()); WriteGlobalSetting(Settings::values.accelerate_astc); WriteGlobalSetting(Settings::values.async_astc); + WriteGlobalSetting(Settings::values.use_vsync); + WriteGlobalSetting(Settings::values.use_reactive_flushing); WriteSetting(QString::fromStdString(Settings::values.shader_backend.GetLabel()), static_cast(Settings::values.shader_backend.GetValue(global)), static_cast(Settings::values.shader_backend.GetDefault()), @@ -1367,9 +1366,6 @@ void Config::SaveRendererValues() { WriteGlobalSetting(Settings::values.bg_blue); if (global) { - WriteSetting(QString::fromStdString(Settings::values.vsync_mode.GetLabel()), - static_cast(Settings::values.vsync_mode.GetValue()), - static_cast(Settings::values.vsync_mode.GetDefault())); WriteBasicSetting(Settings::values.renderer_debug); WriteBasicSetting(Settings::values.renderer_shader_feedback); WriteBasicSetting(Settings::values.enable_nsight_aftermath); diff --git a/src/yuzu/configuration/configure_graphics.cpp b/src/yuzu/configuration/configure_graphics.cpp index ea249e9bb..a4042d029 100755 --- a/src/yuzu/configuration/configure_graphics.cpp +++ b/src/yuzu/configuration/configure_graphics.cpp @@ -4,76 +4,20 @@ // Include this early to include Vulkan headers how we want to #include "video_core/vulkan_common/vulkan_wrapper.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include "common/common_types.h" -#include "common/dynamic_library.h" #include "common/logging/log.h" #include "common/settings.h" #include "core/core.h" #include "ui_configure_graphics.h" #include "video_core/vulkan_common/vulkan_instance.h" #include "video_core/vulkan_common/vulkan_library.h" -#include "video_core/vulkan_common/vulkan_surface.h" #include "yuzu/configuration/configuration_shared.h" #include "yuzu/configuration/configure_graphics.h" -#include "yuzu/qt_common.h" #include "yuzu/uisettings.h" -static const std::vector default_present_modes{VK_PRESENT_MODE_IMMEDIATE_KHR, - VK_PRESENT_MODE_FIFO_KHR}; - -// Converts a setting to a present mode (or vice versa) -static constexpr VkPresentModeKHR VSyncSettingToMode(Settings::VSyncMode mode) { - switch (mode) { - case Settings::VSyncMode::Immediate: - return VK_PRESENT_MODE_IMMEDIATE_KHR; - case Settings::VSyncMode::Mailbox: - return VK_PRESENT_MODE_MAILBOX_KHR; - case Settings::VSyncMode::FIFO: - return VK_PRESENT_MODE_FIFO_KHR; - case Settings::VSyncMode::FIFORelaxed: - return VK_PRESENT_MODE_FIFO_RELAXED_KHR; - default: - return VK_PRESENT_MODE_FIFO_KHR; - } -} - -static constexpr Settings::VSyncMode PresentModeToSetting(VkPresentModeKHR mode) { - switch (mode) { - case VK_PRESENT_MODE_IMMEDIATE_KHR: - return Settings::VSyncMode::Immediate; - case VK_PRESENT_MODE_MAILBOX_KHR: - return Settings::VSyncMode::Mailbox; - case VK_PRESENT_MODE_FIFO_KHR: - return Settings::VSyncMode::FIFO; - case VK_PRESENT_MODE_FIFO_RELAXED_KHR: - return Settings::VSyncMode::FIFORelaxed; - default: - return Settings::VSyncMode::FIFO; - } -} - ConfigureGraphics::ConfigureGraphics(const Core::System& system_, QWidget* parent) : QWidget(parent), ui{std::make_unique()}, system{system_} { vulkan_device = Settings::values.vulkan_device.GetValue(); @@ -95,16 +39,13 @@ ConfigureGraphics::ConfigureGraphics(const Core::System& system_, QWidget* paren connect(ui->api, qOverload(&QComboBox::currentIndexChanged), this, [this] { UpdateAPILayout(); - PopulateVSyncModeSelection(); if (!Settings::IsConfiguringGlobal()) { ConfigurationShared::SetHighlight( ui->api_widget, ui->api->currentIndex() != ConfigurationShared::USE_GLOBAL_INDEX); } }); - connect(ui->device, qOverload(&QComboBox::activated), this, [this](int device) { - UpdateDeviceSelection(device); - PopulateVSyncModeSelection(); - }); + connect(ui->device, qOverload(&QComboBox::activated), this, + [this](int device) { UpdateDeviceSelection(device); }); connect(ui->backend, qOverload(&QComboBox::activated), this, [this](int backend) { UpdateShaderBackendSelection(backend); }); @@ -129,43 +70,6 @@ ConfigureGraphics::ConfigureGraphics(const Core::System& system_, QWidget* paren ui->fsr_sharpening_label->setVisible(Settings::IsConfiguringGlobal()); } -void ConfigureGraphics::PopulateVSyncModeSelection() { - const Settings::RendererBackend backend{GetCurrentGraphicsBackend()}; - if (backend == Settings::RendererBackend::Null) { - ui->vsync_mode_combobox->setEnabled(false); - return; - } - ui->vsync_mode_combobox->setEnabled(true); - - const int current_index = //< current selected vsync mode from combobox - ui->vsync_mode_combobox->currentIndex(); - const auto current_mode = //< current selected vsync mode as a VkPresentModeKHR - current_index == -1 ? VSyncSettingToMode(Settings::values.vsync_mode.GetValue()) - : vsync_mode_combobox_enum_map[current_index]; - int index{}; - const int device{ui->device->currentIndex()}; //< current selected Vulkan device - const auto& present_modes = //< relevant vector of present modes for the selected device or API - backend == Settings::RendererBackend::Vulkan ? device_present_modes[device] - : default_present_modes; - - ui->vsync_mode_combobox->clear(); - vsync_mode_combobox_enum_map.clear(); - vsync_mode_combobox_enum_map.reserve(present_modes.size()); - for (const auto present_mode : present_modes) { - const auto mode_name = TranslateVSyncMode(present_mode, backend); - if (mode_name.isEmpty()) { - continue; - } - - ui->vsync_mode_combobox->insertItem(index, mode_name); - vsync_mode_combobox_enum_map.push_back(present_mode); - if (present_mode == current_mode) { - ui->vsync_mode_combobox->setCurrentIndex(index); - } - index++; - } -} - void ConfigureGraphics::UpdateDeviceSelection(int device) { if (device == -1) { return; @@ -195,9 +99,6 @@ void ConfigureGraphics::SetConfiguration() { ui->nvdec_emulation_widget->setEnabled(runtime_lock); ui->resolution_combobox->setEnabled(runtime_lock); ui->accelerate_astc->setEnabled(runtime_lock); - ui->vsync_mode_layout->setEnabled(runtime_lock || - Settings::values.renderer_backend.GetValue() == - Settings::RendererBackend::Vulkan); ui->use_disk_shader_cache->setChecked(Settings::values.use_disk_shader_cache.GetValue()); ui->use_asynchronous_gpu_emulation->setChecked( Settings::values.use_asynchronous_gpu_emulation.GetValue()); @@ -269,24 +170,7 @@ void ConfigureGraphics::SetConfiguration() { Settings::values.bg_green.GetValue(), Settings::values.bg_blue.GetValue())); UpdateAPILayout(); - PopulateVSyncModeSelection(); //< must happen after UpdateAPILayout SetFSRIndicatorText(ui->fsr_sharpening_slider->sliderPosition()); - - // VSync setting needs to be determined after populating the VSync combobox - if (Settings::IsConfiguringGlobal()) { - const auto vsync_mode_setting = Settings::values.vsync_mode.GetValue(); - const auto vsync_mode = VSyncSettingToMode(vsync_mode_setting); - int index{}; - for (const auto mode : vsync_mode_combobox_enum_map) { - if (mode == vsync_mode) { - break; - } - index++; - } - if (static_cast(index) < vsync_mode_combobox_enum_map.size()) { - ui->vsync_mode_combobox->setCurrentIndex(index); - } - } } void ConfigureGraphics::SetFSRIndicatorText(int percentage) { @@ -294,27 +178,6 @@ void ConfigureGraphics::SetFSRIndicatorText(int percentage) { tr("%1%", "FSR sharpening percentage (e.g. 50%)").arg(100 - (percentage / 2))); } -const QString ConfigureGraphics::TranslateVSyncMode(VkPresentModeKHR mode, - Settings::RendererBackend backend) const { - switch (mode) { - case VK_PRESENT_MODE_IMMEDIATE_KHR: - return backend == Settings::RendererBackend::OpenGL - ? tr("Off") - : QStringLiteral("Immediate (%1)").arg(tr("VSync Off")); - case VK_PRESENT_MODE_MAILBOX_KHR: - return QStringLiteral("Mailbox (%1)").arg(tr("Recommended")); - case VK_PRESENT_MODE_FIFO_KHR: - return backend == Settings::RendererBackend::OpenGL - ? tr("On") - : QStringLiteral("FIFO (%1)").arg(tr("VSync On")); - case VK_PRESENT_MODE_FIFO_RELAXED_KHR: - return QStringLiteral("FIFO Relaxed"); - default: - return {}; - break; - } -} - void ConfigureGraphics::ApplyConfiguration() { const auto resolution_setup = static_cast( ui->resolution_combobox->currentIndex() - @@ -369,10 +232,6 @@ void ConfigureGraphics::ApplyConfiguration() { Settings::values.anti_aliasing.SetValue(anti_aliasing); } Settings::values.fsr_sharpening_slider.SetValue(ui->fsr_sharpening_slider->value()); - - const auto mode = vsync_mode_combobox_enum_map[ui->vsync_mode_combobox->currentIndex()]; - const auto vsync_mode = PresentModeToSetting(mode); - Settings::values.vsync_mode.SetValue(vsync_mode); } else { if (ui->resolution_combobox->currentIndex() == ConfigurationShared::USE_GLOBAL_INDEX) { Settings::values.resolution_setup.SetGlobal(true); @@ -486,9 +345,7 @@ void ConfigureGraphics::UpdateAPILayout() { ui->backend_widget->setVisible(true); break; case Settings::RendererBackend::Vulkan: - if (static_cast(vulkan_device) < ui->device->count()) { - ui->device->setCurrentIndex(vulkan_device); - } + ui->device->setCurrentIndex(vulkan_device); ui->device_widget->setVisible(true); ui->backend_widget->setVisible(false); break; @@ -506,27 +363,16 @@ void ConfigureGraphics::RetrieveVulkanDevices() try { using namespace Vulkan; - auto* window = this->window()->windowHandle(); - auto wsi = QtCommon::GetWindowSystemInfo(window); - vk::InstanceDispatch dld; const Common::DynamicLibrary library = OpenLibrary(); - const vk::Instance instance = CreateInstance(library, dld, VK_API_VERSION_1_1, wsi.type); + const vk::Instance instance = CreateInstance(library, dld, VK_API_VERSION_1_1); const std::vector physical_devices = instance.EnumeratePhysicalDevices(); - vk::SurfaceKHR surface = //< needed to view present modes for a device - CreateSurface(instance, wsi); vulkan_devices.clear(); vulkan_devices.reserve(physical_devices.size()); - device_present_modes.clear(); - device_present_modes.reserve(physical_devices.size()); for (const VkPhysicalDevice device : physical_devices) { - const auto physical_device = vk::PhysicalDevice(device, dld); - const std::string name = physical_device.GetProperties().deviceName; - const std::vector present_modes = - physical_device.GetSurfacePresentModesKHR(*surface); + const std::string name = vk::PhysicalDevice(device, dld).GetProperties().deviceName; vulkan_devices.push_back(QString::fromStdString(name)); - device_present_modes.push_back(present_modes); } } catch (const Vulkan::vk::Exception& exception) { LOG_ERROR(Frontend, "Failed to enumerate devices with error: {}", exception.what()); @@ -619,6 +465,4 @@ void ConfigureGraphics::SetupPerGameUI() { ui->api, static_cast(Settings::values.renderer_backend.GetValue(true))); ConfigurationShared::InsertGlobalItem( ui->nvdec_emulation, static_cast(Settings::values.nvdec_emulation.GetValue(true))); - - ui->vsync_mode_layout->setVisible(false); } diff --git a/src/yuzu/configuration/configure_graphics.h b/src/yuzu/configuration/configure_graphics.h index 01237228f..4e0f7128a 100755 --- a/src/yuzu/configuration/configure_graphics.h +++ b/src/yuzu/configuration/configure_graphics.h @@ -5,21 +5,9 @@ #include #include -#include #include #include -#include -#include -#include "common/common_types.h" - -class QEvent; -class QObject; - -namespace Settings { -enum class NvdecEmulation : u32; -enum class RendererBackend : u32; -enum class ShaderBackend : u32; -} // namespace Settings +#include "common/settings.h" namespace Core { class System; @@ -47,7 +35,6 @@ private: void changeEvent(QEvent* event) override; void RetranslateUI(); - void PopulateVSyncModeSelection(); void UpdateBackgroundColorButton(QColor color); void UpdateAPILayout(); void UpdateDeviceSelection(int device); @@ -56,10 +43,6 @@ private: void RetrieveVulkanDevices(); void SetFSRIndicatorText(int percentage); - /* Turns a Vulkan present mode into a textual string for a UI - * (and eventually for a human to read) */ - const QString TranslateVSyncMode(VkPresentModeKHR mode, - Settings::RendererBackend backend) const; void SetupPerGameUI(); @@ -75,10 +58,6 @@ private: ConfigurationShared::CheckState use_asynchronous_gpu_emulation; std::vector vulkan_devices; - std::vector> device_present_modes; - std::vector - vsync_mode_combobox_enum_map; //< Keeps track of which present mode corresponds to which - // selection in the combobox u32 vulkan_device{}; Settings::ShaderBackend shader_backend{}; diff --git a/src/yuzu/configuration/configure_graphics.ui b/src/yuzu/configuration/configure_graphics.ui index 3f3f82085..d29a2faa2 100755 --- a/src/yuzu/configuration/configure_graphics.ui +++ b/src/yuzu/configuration/configure_graphics.ui @@ -188,44 +188,6 @@ - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - VSync Mode: - - - - - - - FIFO (VSync) does not drop frames or exhibit tearing but is limited by the screen refresh rate. -FIFO Relaxed is similar to FIFO but allows tearing as it recovers from a slow down. -Mailbox can have lower latency than FIFO and does not tear but may drop frames. -Immediate (no synchronization) just presents whatever is available and can exhibit tearing. - - - - - - - - - @@ -404,7 +366,7 @@ Immediate (no synchronization) just presents whatever is available and can exhib - 1.5X (1080p/1620p) [EXPERIMENTAL] + 1.5X (1080p/1620p) [EXPERIMENTAL] diff --git a/src/yuzu/configuration/configure_graphics_advanced.cpp b/src/yuzu/configuration/configure_graphics_advanced.cpp index 5464e9570..6e669de41 100755 --- a/src/yuzu/configuration/configure_graphics_advanced.cpp +++ b/src/yuzu/configuration/configure_graphics_advanced.cpp @@ -21,6 +21,8 @@ ConfigureGraphicsAdvanced::~ConfigureGraphicsAdvanced() = default; void ConfigureGraphicsAdvanced::SetConfiguration() { const bool runtime_lock = !system.IsPoweredOn(); + ui->use_vsync->setEnabled(runtime_lock); + ui->use_reactive_flushing->setEnabled(runtime_lock); ui->async_present->setEnabled(runtime_lock); ui->renderer_force_max_clock->setEnabled(runtime_lock); ui->async_astc->setEnabled(runtime_lock); @@ -29,6 +31,8 @@ void ConfigureGraphicsAdvanced::SetConfiguration() { ui->async_present->setChecked(Settings::values.async_presentation.GetValue()); ui->renderer_force_max_clock->setChecked(Settings::values.renderer_force_max_clock.GetValue()); + ui->use_vsync->setChecked(Settings::values.use_vsync.GetValue()); + ui->use_reactive_flushing->setChecked(Settings::values.use_reactive_flushing.GetValue()); ui->async_astc->setChecked(Settings::values.async_astc.GetValue()); ui->use_asynchronous_shaders->setChecked(Settings::values.use_asynchronous_shaders.GetValue()); ui->use_fast_gpu_time->setChecked(Settings::values.use_fast_gpu_time.GetValue()); @@ -60,6 +64,9 @@ void ConfigureGraphicsAdvanced::ApplyConfiguration() { renderer_force_max_clock); ConfigurationShared::ApplyPerGameSetting(&Settings::values.max_anisotropy, ui->anisotropic_filtering_combobox); + ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_vsync, ui->use_vsync, use_vsync); + ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_reactive_flushing, + ui->use_reactive_flushing, use_reactive_flushing); ConfigurationShared::ApplyPerGameSetting(&Settings::values.async_astc, ui->async_astc, async_astc); ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_asynchronous_shaders, @@ -91,6 +98,8 @@ void ConfigureGraphicsAdvanced::SetupPerGameUI() { ui->async_present->setEnabled(Settings::values.async_presentation.UsingGlobal()); ui->renderer_force_max_clock->setEnabled( Settings::values.renderer_force_max_clock.UsingGlobal()); + ui->use_vsync->setEnabled(Settings::values.use_vsync.UsingGlobal()); + ui->use_vsync->setEnabled(Settings::values.use_reactive_flushing.UsingGlobal()); ui->async_astc->setEnabled(Settings::values.async_astc.UsingGlobal()); ui->use_asynchronous_shaders->setEnabled( Settings::values.use_asynchronous_shaders.UsingGlobal()); @@ -108,6 +117,9 @@ void ConfigureGraphicsAdvanced::SetupPerGameUI() { ConfigurationShared::SetColoredTristate(ui->renderer_force_max_clock, Settings::values.renderer_force_max_clock, renderer_force_max_clock); + ConfigurationShared::SetColoredTristate(ui->use_vsync, Settings::values.use_vsync, use_vsync); + ConfigurationShared::SetColoredTristate( + ui->use_reactive_flushing, Settings::values.use_reactive_flushing, use_reactive_flushing); ConfigurationShared::SetColoredTristate(ui->async_astc, Settings::values.async_astc, async_astc); 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 fb35b3dce..4356b07c2 100755 --- a/src/yuzu/configuration/configure_graphics_advanced.h +++ b/src/yuzu/configuration/configure_graphics_advanced.h @@ -40,6 +40,7 @@ private: ConfigurationShared::CheckState renderer_force_max_clock; ConfigurationShared::CheckState use_vsync; ConfigurationShared::CheckState async_astc; + ConfigurationShared::CheckState use_reactive_flushing; ConfigurationShared::CheckState use_asynchronous_shaders; ConfigurationShared::CheckState use_fast_gpu_time; 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 ef2864584..97fc54388 100755 --- a/src/yuzu/configuration/configure_graphics_advanced.ui +++ b/src/yuzu/configuration/configure_graphics_advanced.ui @@ -86,6 +86,16 @@ + + + + VSync prevents the screen from tearing, but some graphics cards have lower performance with VSync enabled. Keep it enabled if you don't notice a performance difference. + + + Use VSync + + + @@ -96,6 +106,16 @@ + + + + Uses reactive flushing instead of predictive flushing. Allowing a more accurate syncing of memory. + + + Enable Reactive Flushing + + + diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index 3342a6514..2fff66ef2 100755 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include "common/assert.h" #include "common/param_package.h" diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 4662af808..ee38fdcb6 100755 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -320,8 +320,9 @@ void Config::ReadValues() { ReadSetting("Renderer", Settings::values.use_disk_shader_cache); ReadSetting("Renderer", Settings::values.gpu_accuracy); ReadSetting("Renderer", Settings::values.use_asynchronous_gpu_emulation); - ReadSetting("Renderer", Settings::values.vsync_mode); + ReadSetting("Renderer", Settings::values.use_vsync); ReadSetting("Renderer", Settings::values.shader_backend); + ReadSetting("Renderer", Settings::values.use_reactive_flushing); ReadSetting("Renderer", Settings::values.use_asynchronous_shaders); ReadSetting("Renderer", Settings::values.nvdec_emulation); ReadSetting("Renderer", Settings::values.accelerate_astc); diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h index 0923b77d1..cf9029ba0 100755 --- a/src/yuzu_cmd/default_ini.h +++ b/src/yuzu_cmd/default_ini.h @@ -325,14 +325,8 @@ aspect_ratio = # 0: Default, 1: 2x, 2: 4x, 3: 8x, 4: 16x max_anisotropy = -# Whether to enable VSync or not. -# OpenGL: Values other than 0 enable VSync -# Vulkan: FIFO is selected if the requested mode is not supported by the driver. -# FIFO (VSync) does not drop frames or exhibit tearing but is limited by the screen refresh rate. -# FIFO Relaxed is similar to FIFO but allows tearing as it recovers from a slow down. -# Mailbox can have lower latency than FIFO and does not tear but may drop frames. -# Immediate (no synchronization) just presents whatever is available and can exhibit tearing. -# 0: Immediate (Off), 1: Mailbox, 2 (Default): FIFO (On), 3: FIFO Relaxed +# Whether to enable V-Sync (caps the framerate at 60FPS) or not. +# 0 (default): Off, 1: On use_vsync = # Selects the OpenGL shader backend. NV_gpu_program5 is required for GLASM. If NV_gpu_program5 is @@ -340,6 +334,10 @@ use_vsync = # 0: GLSL, 1 (default): GLASM, 2: SPIR-V shader_backend = +# Uses reactive flushing instead of predictive flushing. Allowing a more accurate syncing of memory. +# 0: Off, 1 (default): On +use_reactive_flushing = + # Whether to allow asynchronous shader building. # 0 (default): Off, 1: On use_asynchronous_shaders =