2022-11-05 13:58:44 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
2023-04-24 01:59:22 +02:00
|
|
|
#include <condition_variable>
|
2022-11-05 13:58:44 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <deque>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2023-04-24 01:59:22 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2022-11-05 13:58:44 +01:00
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2023-04-24 01:59:22 +02:00
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/scope_exit.h"
|
|
|
|
#include "common/settings.h"
|
|
|
|
#include "common/thread.h"
|
2022-11-05 13:58:44 +01:00
|
|
|
#include "video_core/delayed_destruction_ring.h"
|
|
|
|
#include "video_core/gpu.h"
|
|
|
|
#include "video_core/host1x/host1x.h"
|
|
|
|
#include "video_core/host1x/syncpoint_manager.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
|
|
|
|
namespace VideoCommon {
|
|
|
|
|
|
|
|
class FenceBase {
|
|
|
|
public:
|
|
|
|
explicit FenceBase(bool is_stubbed_) : is_stubbed{is_stubbed_} {}
|
|
|
|
|
2023-04-24 01:59:22 +02:00
|
|
|
bool IsStubbed() const {
|
|
|
|
return is_stubbed;
|
|
|
|
}
|
|
|
|
|
2022-11-05 13:58:44 +01:00
|
|
|
protected:
|
|
|
|
bool is_stubbed;
|
|
|
|
};
|
|
|
|
|
2023-04-24 01:59:22 +02:00
|
|
|
template <typename Traits>
|
2022-11-05 13:58:44 +01:00
|
|
|
class FenceManager {
|
2023-04-24 01:59:22 +02:00
|
|
|
using TFence = typename Traits::FenceType;
|
|
|
|
using TTextureCache = typename Traits::TextureCacheType;
|
|
|
|
using TBufferCache = typename Traits::BufferCacheType;
|
|
|
|
using TQueryCache = typename Traits::QueryCacheType;
|
|
|
|
static constexpr bool can_async_check = Traits::HAS_ASYNC_CHECK;
|
|
|
|
|
2022-11-05 13:58:44 +01:00
|
|
|
public:
|
|
|
|
/// Notify the fence manager about a new frame
|
|
|
|
void TickFrame() {
|
2023-04-24 01:59:22 +02:00
|
|
|
std::unique_lock lock(ring_guard);
|
2022-11-05 13:58:44 +01:00
|
|
|
delayed_destruction_ring.Tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlike other fences, this one doesn't
|
|
|
|
void SignalOrdering() {
|
|
|
|
std::scoped_lock lock{buffer_cache.mutex};
|
|
|
|
buffer_cache.AccumulateFlushes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncOperation(std::function<void()>&& func) {
|
|
|
|
uncommitted_operations.emplace_back(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalFence(std::function<void()>&& func) {
|
2023-04-29 03:18:52 +02:00
|
|
|
rasterizer.InvalidateGPUCache();
|
2023-04-24 01:59:22 +02:00
|
|
|
bool delay_fence = Settings::IsGPULevelHigh();
|
|
|
|
if constexpr (!can_async_check) {
|
|
|
|
TryReleasePendingFences<false>();
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
const bool should_flush = ShouldFlush();
|
|
|
|
CommitAsyncFlushes();
|
|
|
|
TFence new_fence = CreateFence(!should_flush);
|
2023-04-24 01:59:22 +02:00
|
|
|
if constexpr (can_async_check) {
|
|
|
|
guard.lock();
|
|
|
|
}
|
|
|
|
if (delay_fence) {
|
|
|
|
uncommitted_operations.emplace_back(std::move(func));
|
|
|
|
}
|
|
|
|
pending_operations.emplace_back(std::move(uncommitted_operations));
|
2022-11-05 13:58:44 +01:00
|
|
|
QueueFence(new_fence);
|
2023-04-24 01:59:22 +02:00
|
|
|
if (!delay_fence) {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
fences.push(std::move(new_fence));
|
2022-11-05 13:58:44 +01:00
|
|
|
if (should_flush) {
|
|
|
|
rasterizer.FlushCommands();
|
|
|
|
}
|
2023-04-24 01:59:22 +02:00
|
|
|
if constexpr (can_async_check) {
|
|
|
|
guard.unlock();
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignalSyncPoint(u32 value) {
|
|
|
|
syncpoint_manager.IncrementGuest(value);
|
|
|
|
std::function<void()> func([this, value] { syncpoint_manager.IncrementHost(value); });
|
|
|
|
SignalFence(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitPendingFences() {
|
2023-04-24 01:59:22 +02:00
|
|
|
if constexpr (!can_async_check) {
|
|
|
|
TryReleasePendingFences<true>();
|
2022-11-05 13:58:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit FenceManager(VideoCore::RasterizerInterface& rasterizer_, Tegra::GPU& gpu_,
|
2023-04-24 01:59:22 +02:00
|
|
|
TTextureCache& texture_cache_, TBufferCache& buffer_cache_,
|
2022-11-05 13:58:44 +01:00
|
|
|
TQueryCache& query_cache_)
|
|
|
|
: rasterizer{rasterizer_}, gpu{gpu_}, syncpoint_manager{gpu.Host1x().GetSyncpointManager()},
|
2023-04-24 01:59:22 +02:00
|
|
|
texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, query_cache{query_cache_} {
|
|
|
|
if constexpr (can_async_check) {
|
|
|
|
fence_thread =
|
|
|
|
std::jthread([this](std::stop_token token) { ReleaseThreadFunc(token); });
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
|
2023-04-24 01:59:22 +02:00
|
|
|
virtual ~FenceManager() {
|
|
|
|
if constexpr (can_async_check) {
|
|
|
|
fence_thread.request_stop();
|
|
|
|
cv.notify_all();
|
|
|
|
fence_thread.join();
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
|
|
|
|
/// Creates a Fence Interface, does not create a backend fence if 'is_stubbed' is
|
|
|
|
/// true
|
|
|
|
virtual TFence CreateFence(bool is_stubbed) = 0;
|
|
|
|
/// Queues a fence into the backend if the fence isn't stubbed.
|
|
|
|
virtual void QueueFence(TFence& fence) = 0;
|
|
|
|
/// Notifies that the backend fence has been signaled/reached in host GPU.
|
|
|
|
virtual bool IsFenceSignaled(TFence& fence) const = 0;
|
|
|
|
/// Waits until a fence has been signalled by the host GPU.
|
|
|
|
virtual void WaitFence(TFence& fence) = 0;
|
|
|
|
|
|
|
|
VideoCore::RasterizerInterface& rasterizer;
|
|
|
|
Tegra::GPU& gpu;
|
|
|
|
Tegra::Host1x::SyncpointManager& syncpoint_manager;
|
|
|
|
TTextureCache& texture_cache;
|
2023-04-24 01:59:22 +02:00
|
|
|
TBufferCache& buffer_cache;
|
2022-11-05 13:58:44 +01:00
|
|
|
TQueryCache& query_cache;
|
|
|
|
|
|
|
|
private:
|
2023-04-24 01:59:22 +02:00
|
|
|
template <bool force_wait>
|
2022-11-05 13:58:44 +01:00
|
|
|
void TryReleasePendingFences() {
|
|
|
|
while (!fences.empty()) {
|
|
|
|
TFence& current_fence = fences.front();
|
|
|
|
if (ShouldWait() && !IsFenceSignaled(current_fence)) {
|
2023-04-24 01:59:22 +02:00
|
|
|
if constexpr (force_wait) {
|
|
|
|
WaitFence(current_fence);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
}
|
|
|
|
PopAsyncFlushes();
|
|
|
|
auto operations = std::move(pending_operations.front());
|
|
|
|
pending_operations.pop_front();
|
|
|
|
for (auto& operation : operations) {
|
|
|
|
operation();
|
|
|
|
}
|
2023-04-24 01:59:22 +02:00
|
|
|
{
|
|
|
|
std::unique_lock lock(ring_guard);
|
|
|
|
delayed_destruction_ring.Push(std::move(current_fence));
|
|
|
|
}
|
|
|
|
fences.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseThreadFunc(std::stop_token stop_token) {
|
|
|
|
std::string name = "GPUFencingThread";
|
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
SCOPE_EXIT({ MicroProfileOnThreadExit(); });
|
|
|
|
|
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
|
|
|
|
TFence current_fence;
|
|
|
|
std::deque<std::function<void()>> current_operations;
|
|
|
|
while (!stop_token.stop_requested()) {
|
|
|
|
{
|
|
|
|
std::unique_lock lock(guard);
|
|
|
|
cv.wait(lock, [&] { return stop_token.stop_requested() || !fences.empty(); });
|
|
|
|
if (stop_token.stop_requested()) [[unlikely]] {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_fence = std::move(fences.front());
|
|
|
|
current_operations = std::move(pending_operations.front());
|
|
|
|
fences.pop();
|
|
|
|
pending_operations.pop_front();
|
|
|
|
}
|
|
|
|
if (!current_fence->IsStubbed()) {
|
|
|
|
WaitFence(current_fence);
|
|
|
|
}
|
|
|
|
PopAsyncFlushes();
|
|
|
|
for (auto& operation : current_operations) {
|
|
|
|
operation();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock lock(ring_guard);
|
|
|
|
delayed_destruction_ring.Push(std::move(current_fence));
|
|
|
|
}
|
2022-11-05 13:58:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldWait() const {
|
|
|
|
std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
|
|
|
|
return texture_cache.ShouldWaitAsyncFlushes() || buffer_cache.ShouldWaitAsyncFlushes() ||
|
|
|
|
query_cache.ShouldWaitAsyncFlushes();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldFlush() const {
|
|
|
|
std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
|
|
|
|
return texture_cache.HasUncommittedFlushes() || buffer_cache.HasUncommittedFlushes() ||
|
|
|
|
query_cache.HasUncommittedFlushes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopAsyncFlushes() {
|
|
|
|
{
|
|
|
|
std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
|
|
|
|
texture_cache.PopAsyncFlushes();
|
|
|
|
buffer_cache.PopAsyncFlushes();
|
|
|
|
}
|
|
|
|
query_cache.PopAsyncFlushes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommitAsyncFlushes() {
|
|
|
|
{
|
|
|
|
std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
|
|
|
|
texture_cache.CommitAsyncFlushes();
|
|
|
|
buffer_cache.CommitAsyncFlushes();
|
|
|
|
}
|
|
|
|
query_cache.CommitAsyncFlushes();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::queue<TFence> fences;
|
|
|
|
std::deque<std::function<void()>> uncommitted_operations;
|
|
|
|
std::deque<std::deque<std::function<void()>>> pending_operations;
|
|
|
|
|
2023-04-24 01:59:22 +02:00
|
|
|
std::mutex guard;
|
|
|
|
std::mutex ring_guard;
|
|
|
|
std::condition_variable cv;
|
|
|
|
|
|
|
|
std::jthread fence_thread;
|
|
|
|
|
2022-11-05 13:58:44 +01:00
|
|
|
DelayedDestructionRing<TFence, 6> delayed_destruction_ring;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|