summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/threadsafe_queue.h27
-rw-r--r--src/core/core.cpp6
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp8
-rw-r--r--src/video_core/gpu.cpp8
-rw-r--r--src/video_core/gpu.h3
-rw-r--r--src/video_core/gpu_thread.cpp57
-rw-r--r--src/video_core/gpu_thread.h13
-rw-r--r--src/video_core/renderer_vulkan/renderer_vulkan.cpp2
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.cpp19
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.h7
-rw-r--r--src/video_core/renderer_vulkan/vk_swapchain.cpp29
-rw-r--r--src/video_core/renderer_vulkan/vk_swapchain.h11
12 files changed, 94 insertions, 96 deletions
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h
index 8430b9778..2c8c2b90e 100644
--- a/src/common/threadsafe_queue.h
+++ b/src/common/threadsafe_queue.h
@@ -14,7 +14,7 @@
#include <utility>
namespace Common {
-template <typename T>
+template <typename T, bool with_stop_token = false>
class SPSCQueue {
public:
SPSCQueue() {
@@ -84,7 +84,7 @@ public:
void Wait() {
if (Empty()) {
std::unique_lock lock{cv_mutex};
- cv.wait(lock, [this]() { return !Empty(); });
+ cv.wait(lock, [this] { return !Empty(); });
}
}
@@ -95,6 +95,19 @@ public:
return t;
}
+ T PopWait(std::stop_token stop_token) {
+ if (Empty()) {
+ std::unique_lock lock{cv_mutex};
+ cv.wait(lock, stop_token, [this] { return !Empty(); });
+ }
+ if (stop_token.stop_requested()) {
+ return T{};
+ }
+ T t;
+ Pop(t);
+ return t;
+ }
+
// not thread-safe
void Clear() {
size.store(0);
@@ -123,13 +136,13 @@ private:
ElementPtr* read_ptr;
std::atomic_size_t size{0};
std::mutex cv_mutex;
- std::condition_variable cv;
+ std::conditional_t<with_stop_token, std::condition_variable_any, std::condition_variable> cv;
};
// a simple thread-safe,
// single reader, multiple writer queue
-template <typename T>
+template <typename T, bool with_stop_token = false>
class MPSCQueue {
public:
[[nodiscard]] std::size_t Size() const {
@@ -166,13 +179,17 @@ public:
return spsc_queue.PopWait();
}
+ T PopWait(std::stop_token stop_token) {
+ return spsc_queue.PopWait(stop_token);
+ }
+
// not thread-safe
void Clear() {
spsc_queue.Clear();
}
private:
- SPSCQueue<T> spsc_queue;
+ SPSCQueue<T, with_stop_token> spsc_queue;
std::mutex write_lock;
};
} // namespace Common
diff --git a/src/core/core.cpp b/src/core/core.cpp
index b13350f6e..54ebed2c1 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -305,10 +305,7 @@ struct System::Impl {
is_powered_on = false;
exit_lock = false;
- if (gpu_core) {
- gpu_core->ShutDown();
- }
-
+ gpu_core.reset();
services.reset();
service_manager.reset();
cheat_engine.reset();
@@ -317,7 +314,6 @@ struct System::Impl {
time_manager.Shutdown();
core_timing.Shutdown();
app_loader.reset();
- gpu_core.reset();
perf_stats.reset();
kernel.Shutdown();
memory.Reset();
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
index 68f360b3c..6f60c6574 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
@@ -477,7 +477,13 @@ void EmitSetSampleMask(EmitContext& ctx, Id value) {
}
void EmitSetFragDepth(EmitContext& ctx, Id value) {
- ctx.OpStore(ctx.frag_depth, value);
+ if (!ctx.runtime_info.convert_depth_mode) {
+ ctx.OpStore(ctx.frag_depth, value);
+ return;
+ }
+ const Id unit{ctx.Const(0.5f)};
+ const Id new_depth{ctx.OpFma(ctx.F32[1], value, unit, unit)};
+ ctx.OpStore(ctx.frag_depth, new_depth);
}
void EmitGetZFlag(EmitContext&) {
diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp
index ff024f530..2ae3639b5 100644
--- a/src/video_core/gpu.cpp
+++ b/src/video_core/gpu.cpp
@@ -531,14 +531,6 @@ void GPU::TriggerCpuInterrupt(const u32 syncpoint_id, const u32 value) const {
interrupt_manager.GPUInterruptSyncpt(syncpoint_id, value);
}
-void GPU::ShutDown() {
- // Signal that threads should no longer block on syncpoint fences
- shutting_down.store(true, std::memory_order_relaxed);
- sync_cv.notify_all();
-
- gpu_thread.ShutDown();
-}
-
void GPU::OnCommandListEnd() {
if (is_async) {
// This command only applies to asynchronous GPU mode
diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h
index a8e98e51b..e6a02a71b 100644
--- a/src/video_core/gpu.h
+++ b/src/video_core/gpu.h
@@ -219,9 +219,6 @@ public:
return *shader_notify;
}
- // Stops the GPU execution and waits for the GPU to finish working
- void ShutDown();
-
/// Allows the CPU/NvFlinger to wait on the GPU before presenting a frame.
void WaitFence(u32 syncpoint_id, u32 value);
diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp
index 46f642b19..9547f277a 100644
--- a/src/video_core/gpu_thread.cpp
+++ b/src/video_core/gpu_thread.cpp
@@ -17,9 +17,9 @@
namespace VideoCommon::GPUThread {
/// Runs the GPU thread
-static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
- Core::Frontend::GraphicsContext& context, Tegra::DmaPusher& dma_pusher,
- SynchState& state) {
+static void RunThread(std::stop_token stop_token, Core::System& system,
+ VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context,
+ Tegra::DmaPusher& dma_pusher, SynchState& state) {
std::string name = "yuzu:GPU";
MicroProfileOnThreadCreate(name.c_str());
SCOPE_EXIT({ MicroProfileOnThreadExit(); });
@@ -28,20 +28,14 @@ static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
system.RegisterHostThread();
- // Wait for first GPU command before acquiring the window context
- state.queue.Wait();
-
- // If emulation was stopped during disk shader loading, abort before trying to acquire context
- if (!state.is_running) {
- return;
- }
-
auto current_context = context.Acquire();
VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer();
- CommandDataContainer next;
- while (state.is_running) {
- next = state.queue.PopWait();
+ while (!stop_token.stop_requested()) {
+ CommandDataContainer next = state.queue.PopWait(stop_token);
+ if (stop_token.stop_requested()) {
+ break;
+ }
if (auto* submit_list = std::get_if<SubmitListCommand>(&next.data)) {
dma_pusher.Push(std::move(submit_list->entries));
dma_pusher.DispatchCalls();
@@ -55,8 +49,6 @@ static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
rasterizer->FlushRegion(flush->addr, flush->size);
} else if (const auto* invalidate = std::get_if<InvalidateRegionCommand>(&next.data)) {
rasterizer->OnCPUWrite(invalidate->addr, invalidate->size);
- } else if (std::holds_alternative<EndProcessingCommand>(next.data)) {
- ASSERT(state.is_running == false);
} else {
UNREACHABLE();
}
@@ -73,16 +65,14 @@ static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
ThreadManager::ThreadManager(Core::System& system_, bool is_async_)
: system{system_}, is_async{is_async_} {}
-ThreadManager::~ThreadManager() {
- ShutDown();
-}
+ThreadManager::~ThreadManager() = default;
void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
Core::Frontend::GraphicsContext& context,
Tegra::DmaPusher& dma_pusher) {
rasterizer = renderer.ReadRasterizer();
- thread = std::thread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
- std::ref(dma_pusher), std::ref(state));
+ thread = std::jthread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
+ std::ref(dma_pusher), std::ref(state));
}
void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
@@ -117,26 +107,6 @@ void ThreadManager::FlushAndInvalidateRegion(VAddr addr, u64 size) {
rasterizer->OnCPUWrite(addr, size);
}
-void ThreadManager::ShutDown() {
- if (!state.is_running) {
- return;
- }
-
- {
- std::lock_guard lk(state.write_lock);
- state.is_running = false;
- state.cv.notify_all();
- }
-
- if (!thread.joinable()) {
- return;
- }
-
- // Notify GPU thread that a shutdown is pending
- PushCommand(EndProcessingCommand());
- thread.join();
-}
-
void ThreadManager::OnCommandListEnd() {
PushCommand(OnCommandListEndCommand());
}
@@ -152,9 +122,8 @@ u64 ThreadManager::PushCommand(CommandData&& command_data, bool block) {
state.queue.Push(CommandDataContainer(std::move(command_data), fence, block));
if (block) {
- state.cv.wait(lk, [this, fence] {
- return fence <= state.signaled_fence.load(std::memory_order_relaxed) ||
- !state.is_running;
+ state.cv.wait(lk, thread.get_stop_token(), [this, fence] {
+ return fence <= state.signaled_fence.load(std::memory_order_relaxed);
});
}
diff --git a/src/video_core/gpu_thread.h b/src/video_core/gpu_thread.h
index 11a648f38..91bada925 100644
--- a/src/video_core/gpu_thread.h
+++ b/src/video_core/gpu_thread.h
@@ -33,9 +33,6 @@ class RendererBase;
namespace VideoCommon::GPUThread {
-/// Command to signal to the GPU thread that processing has ended
-struct EndProcessingCommand final {};
-
/// Command to signal to the GPU thread that a command list is ready for processing
struct SubmitListCommand final {
explicit SubmitListCommand(Tegra::CommandList&& entries_) : entries{std::move(entries_)} {}
@@ -83,7 +80,7 @@ struct OnCommandListEndCommand final {};
struct GPUTickCommand final {};
using CommandData =
- std::variant<EndProcessingCommand, SubmitListCommand, SwapBuffersCommand, FlushRegionCommand,
+ std::variant<std::monostate, SubmitListCommand, SwapBuffersCommand, FlushRegionCommand,
InvalidateRegionCommand, FlushAndInvalidateRegionCommand, OnCommandListEndCommand,
GPUTickCommand>;
@@ -100,14 +97,12 @@ struct CommandDataContainer {
/// Struct used to synchronize the GPU thread
struct SynchState final {
- std::atomic_bool is_running{true};
-
- using CommandQueue = Common::SPSCQueue<CommandDataContainer>;
+ using CommandQueue = Common::SPSCQueue<CommandDataContainer, true>;
std::mutex write_lock;
CommandQueue queue;
u64 last_fence{};
std::atomic<u64> signaled_fence{};
- std::condition_variable cv;
+ std::condition_variable_any cv;
};
/// Class used to manage the GPU thread
@@ -149,7 +144,7 @@ private:
VideoCore::RasterizerInterface* rasterizer = nullptr;
SynchState state;
- std::thread thread;
+ std::jthread thread;
};
} // namespace VideoCommon::GPUThread
diff --git a/src/video_core/renderer_vulkan/renderer_vulkan.cpp b/src/video_core/renderer_vulkan/renderer_vulkan.cpp
index 9ff0a28cd..adb6b7a3b 100644
--- a/src/video_core/renderer_vulkan/renderer_vulkan.cpp
+++ b/src/video_core/renderer_vulkan/renderer_vulkan.cpp
@@ -149,7 +149,7 @@ void RendererVulkan::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
const Layout::FramebufferLayout layout = render_window.GetFramebufferLayout();
swapchain.Create(layout.width, layout.height, is_srgb);
};
- if (swapchain.IsSubOptimal() || swapchain.HasColorSpaceChanged(is_srgb)) {
+ if (swapchain.NeedsRecreation(is_srgb)) {
recreate_swapchain();
}
bool is_outdated;
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.cpp b/src/video_core/renderer_vulkan/vk_scheduler.cpp
index 1d438787a..0c11c814f 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.cpp
+++ b/src/video_core/renderer_vulkan/vk_scheduler.cpp
@@ -43,17 +43,10 @@ VKScheduler::VKScheduler(const Device& device_, StateTracker& state_tracker_)
command_pool{std::make_unique<CommandPool>(*master_semaphore, device)} {
AcquireNewChunk();
AllocateWorkerCommandBuffer();
- worker_thread = std::thread(&VKScheduler::WorkerThread, this);
+ worker_thread = std::jthread([this](std::stop_token token) { WorkerThread(token); });
}
-VKScheduler::~VKScheduler() {
- {
- std::lock_guard lock{work_mutex};
- quit = true;
- }
- work_cv.notify_all();
- worker_thread.join();
-}
+VKScheduler::~VKScheduler() = default;
void VKScheduler::Flush(VkSemaphore signal_semaphore, VkSemaphore wait_semaphore) {
SubmitExecution(signal_semaphore, wait_semaphore);
@@ -135,7 +128,7 @@ bool VKScheduler::UpdateGraphicsPipeline(GraphicsPipeline* pipeline) {
return true;
}
-void VKScheduler::WorkerThread() {
+void VKScheduler::WorkerThread(std::stop_token stop_token) {
Common::SetCurrentThreadName("yuzu:VulkanWorker");
do {
if (work_queue.empty()) {
@@ -144,8 +137,8 @@ void VKScheduler::WorkerThread() {
std::unique_ptr<CommandChunk> work;
{
std::unique_lock lock{work_mutex};
- work_cv.wait(lock, [this] { return !work_queue.empty() || quit; });
- if (quit) {
+ work_cv.wait(lock, stop_token, [this] { return !work_queue.empty(); });
+ if (stop_token.stop_requested()) {
continue;
}
work = std::move(work_queue.front());
@@ -158,7 +151,7 @@ void VKScheduler::WorkerThread() {
}
std::lock_guard reserve_lock{reserve_mutex};
chunk_reserve.push_back(std::move(work));
- } while (!quit);
+ } while (!stop_token.stop_requested());
}
void VKScheduler::AllocateWorkerCommandBuffer() {
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.h b/src/video_core/renderer_vulkan/vk_scheduler.h
index 759ed5a48..bd22e4e83 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.h
+++ b/src/video_core/renderer_vulkan/vk_scheduler.h
@@ -187,7 +187,7 @@ private:
GraphicsPipeline* graphics_pipeline = nullptr;
};
- void WorkerThread();
+ void WorkerThread(std::stop_token stop_token);
void AllocateWorkerCommandBuffer();
@@ -212,7 +212,7 @@ private:
vk::CommandBuffer current_cmdbuf;
std::unique_ptr<CommandChunk> chunk;
- std::thread worker_thread;
+ std::jthread worker_thread;
State state;
@@ -224,9 +224,8 @@ private:
std::vector<std::unique_ptr<CommandChunk>> chunk_reserve;
std::mutex reserve_mutex;
std::mutex work_mutex;
- std::condition_variable work_cv;
+ std::condition_variable_any work_cv;
std::condition_variable wait_cv;
- std::atomic_bool quit{};
};
} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_swapchain.cpp b/src/video_core/renderer_vulkan/vk_swapchain.cpp
index aadf03cb0..8972a6921 100644
--- a/src/video_core/renderer_vulkan/vk_swapchain.cpp
+++ b/src/video_core/renderer_vulkan/vk_swapchain.cpp
@@ -9,6 +9,7 @@
#include "common/assert.h"
#include "common/logging/log.h"
+#include "common/settings.h"
#include "core/core.h"
#include "core/frontend/framebuffer_layout.h"
#include "video_core/renderer_vulkan/vk_scheduler.h"
@@ -36,8 +37,19 @@ VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span<VkSurfaceFormatKHR> formats)
VkPresentModeKHR ChooseSwapPresentMode(vk::Span<VkPresentModeKHR> modes) {
// Mailbox doesn't lock the application like fifo (vsync), prefer it
- const auto found = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_MAILBOX_KHR);
- return found != modes.end() ? *found : VK_PRESENT_MODE_FIFO_KHR;
+ const auto found_mailbox = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_MAILBOX_KHR);
+ if (found_mailbox != modes.end()) {
+ return VK_PRESENT_MODE_MAILBOX_KHR;
+ }
+ if (Settings::values.disable_fps_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) {
@@ -143,7 +155,7 @@ void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities,
const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)};
const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats)};
- const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)};
+ present_mode = ChooseSwapPresentMode(present_modes);
u32 requested_image_count{capabilities.minImageCount + 1};
if (capabilities.maxImageCount > 0 && requested_image_count > capabilities.maxImageCount) {
@@ -196,6 +208,7 @@ void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities,
extent = swapchain_ci.imageExtent;
current_srgb = srgb;
+ current_fps_unlocked = Settings::values.disable_fps_limit.GetValue();
images = swapchain.GetImages();
image_count = static_cast<u32>(images.size());
@@ -248,4 +261,14 @@ void VKSwapchain::Destroy() {
swapchain.reset();
}
+bool VKSwapchain::HasFpsUnlockChanged() const {
+ return current_fps_unlocked != Settings::values.disable_fps_limit.GetValue();
+}
+
+bool VKSwapchain::NeedsPresentModeUpdate() const {
+ // 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 5bce41e21..61a6d959e 100644
--- a/src/video_core/renderer_vulkan/vk_swapchain.h
+++ b/src/video_core/renderer_vulkan/vk_swapchain.h
@@ -33,6 +33,11 @@ public:
/// Presents the rendered image to the swapchain.
void Present(VkSemaphore render_semaphore);
+ /// Returns true when the swapchain needs to be recreated.
+ bool NeedsRecreation(bool is_srgb) const {
+ return HasColorSpaceChanged(is_srgb) || IsSubOptimal() || NeedsPresentModeUpdate();
+ }
+
/// Returns true when the color space has changed.
bool HasColorSpaceChanged(bool is_srgb) const {
return current_srgb != is_srgb;
@@ -84,6 +89,10 @@ private:
void Destroy();
+ bool HasFpsUnlockChanged() const;
+
+ bool NeedsPresentModeUpdate() const;
+
const VkSurfaceKHR surface;
const Device& device;
VKScheduler& scheduler;
@@ -102,8 +111,10 @@ private:
VkFormat image_view_format{};
VkExtent2D extent{};
+ VkPresentModeKHR present_mode{};
bool current_srgb{};
+ bool current_fps_unlocked{};
bool is_outdated{};
bool is_suboptimal{};
};