2020-02-17 16:19:26 -08:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
2020-02-18 13:20:39 -08:00
|
|
|
#include <queue>
|
2020-02-17 16:19:26 -08:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/memory.h"
|
|
|
|
#include "core/settings.h"
|
|
|
|
#include "video_core/gpu.h"
|
|
|
|
#include "video_core/memory_manager.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
|
|
|
|
namespace VideoCommon {
|
|
|
|
|
|
|
|
class FenceBase {
|
|
|
|
public:
|
|
|
|
FenceBase(GPUVAddr address, u32 payload) : address{address}, payload{payload} {}
|
|
|
|
|
|
|
|
constexpr GPUVAddr GetAddress() const {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u32 GetPayload() const {
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GPUVAddr address;
|
|
|
|
u32 payload;
|
|
|
|
};
|
|
|
|
|
2020-02-18 13:20:39 -08:00
|
|
|
template <typename TFence, typename TTextureCache, typename TTBufferCache>
|
2020-02-17 16:19:26 -08:00
|
|
|
class FenceManager {
|
|
|
|
public:
|
|
|
|
void SignalFence(GPUVAddr addr, u32 value) {
|
|
|
|
TryReleasePendingFences();
|
|
|
|
texture_cache.CommitAsyncFlushes();
|
2020-02-18 13:20:39 -08:00
|
|
|
buffer_cache.CommitAsyncFlushes();
|
2020-02-19 06:49:07 -08:00
|
|
|
TFence new_fence = CreateFence(addr, value);
|
|
|
|
fences.push(new_fence);
|
|
|
|
QueueFence(new_fence);
|
2020-02-17 16:19:26 -08:00
|
|
|
rasterizer.FlushCommands();
|
|
|
|
rasterizer.SyncGuestHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitPendingFences() {
|
|
|
|
while (!fences.empty()) {
|
|
|
|
TFence& current_fence = fences.front();
|
2020-02-17 18:15:43 -08:00
|
|
|
bool should_wait = texture_cache.ShouldWaitAsyncFlushes();
|
2020-02-18 13:20:39 -08:00
|
|
|
should_wait |= buffer_cache.ShouldWaitAsyncFlushes();
|
2020-02-17 18:15:43 -08:00
|
|
|
if (should_wait) {
|
|
|
|
WaitFence(current_fence);
|
|
|
|
}
|
2020-02-17 16:19:26 -08:00
|
|
|
texture_cache.PopAsyncFlushes();
|
2020-02-18 13:20:39 -08:00
|
|
|
buffer_cache.PopAsyncFlushes();
|
2020-02-17 16:19:26 -08:00
|
|
|
auto& gpu{system.GPU()};
|
|
|
|
auto& memory_manager{gpu.MemoryManager()};
|
|
|
|
memory_manager.Write<u32>(current_fence->GetAddress(), current_fence->GetPayload());
|
|
|
|
fences.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
FenceManager(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
|
2020-02-18 13:20:39 -08:00
|
|
|
TTextureCache& texture_cache, TTBufferCache& buffer_cache)
|
|
|
|
: system{system}, rasterizer{rasterizer}, texture_cache{texture_cache}, buffer_cache{
|
|
|
|
buffer_cache} {}
|
2020-02-17 16:19:26 -08:00
|
|
|
|
|
|
|
virtual TFence CreateFence(GPUVAddr addr, u32 value) = 0;
|
|
|
|
virtual void QueueFence(TFence& fence) = 0;
|
|
|
|
virtual bool IsFenceSignaled(TFence& fence) = 0;
|
|
|
|
virtual void WaitFence(TFence& fence) = 0;
|
|
|
|
|
|
|
|
Core::System& system;
|
|
|
|
VideoCore::RasterizerInterface& rasterizer;
|
|
|
|
TTextureCache& texture_cache;
|
2020-02-18 13:20:39 -08:00
|
|
|
TTBufferCache& buffer_cache;
|
2020-02-17 16:19:26 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void TryReleasePendingFences() {
|
|
|
|
while (!fences.empty()) {
|
|
|
|
TFence& current_fence = fences.front();
|
2020-02-17 18:15:43 -08:00
|
|
|
bool should_wait = texture_cache.ShouldWaitAsyncFlushes();
|
2020-02-18 13:20:39 -08:00
|
|
|
should_wait |= buffer_cache.ShouldWaitAsyncFlushes();
|
2020-02-17 18:15:43 -08:00
|
|
|
if (should_wait && !IsFenceSignaled(current_fence)) {
|
2020-02-17 16:19:26 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
texture_cache.PopAsyncFlushes();
|
2020-02-18 13:20:39 -08:00
|
|
|
buffer_cache.PopAsyncFlushes();
|
2020-02-17 16:19:26 -08:00
|
|
|
auto& gpu{system.GPU()};
|
|
|
|
auto& memory_manager{gpu.MemoryManager()};
|
|
|
|
memory_manager.Write<u32>(current_fence->GetAddress(), current_fence->GetPayload());
|
|
|
|
fences.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::queue<TFence> fences;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|