2018-08-23 20:38:57 +01:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-08-29 01:23:44 +01:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include <boost/icl/interval_map.hpp>
|
2018-08-31 17:21:34 +01:00
|
|
|
#include <boost/range/iterator_range_core.hpp>
|
2018-08-23 20:38:57 +01:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2018-10-14 21:09:01 +01:00
|
|
|
#include "core/settings.h"
|
2018-08-28 23:43:08 +01:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2018-08-23 20:38:57 +01:00
|
|
|
|
2018-10-16 21:51:53 +01:00
|
|
|
class RasterizerCacheObject {
|
|
|
|
public:
|
2018-11-08 05:31:35 +00:00
|
|
|
virtual ~RasterizerCacheObject();
|
|
|
|
|
2018-10-16 21:51:53 +01:00
|
|
|
/// Gets the address of the shader in guest memory, required for cache management
|
|
|
|
virtual VAddr GetAddr() const = 0;
|
|
|
|
|
|
|
|
/// Gets the size of the shader in guest memory, required for cache management
|
|
|
|
virtual std::size_t GetSizeInBytes() const = 0;
|
|
|
|
|
|
|
|
/// Wriets any cached resources back to memory
|
|
|
|
virtual void Flush() = 0;
|
|
|
|
|
|
|
|
/// Sets whether the cached object should be considered registered
|
|
|
|
void SetIsRegistered(bool registered) {
|
|
|
|
is_registered = registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the cached object is registered
|
|
|
|
bool IsRegistered() const {
|
|
|
|
return is_registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the cached object is dirty
|
|
|
|
bool IsDirty() const {
|
|
|
|
return is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns ticks from when this cached object was last modified
|
|
|
|
u64 GetLastModifiedTicks() const {
|
|
|
|
return last_modified_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks an object as recently modified, used to specify whether it is clean or dirty
|
|
|
|
template <class T>
|
|
|
|
void MarkAsModified(bool dirty, T& cache) {
|
|
|
|
is_dirty = dirty;
|
|
|
|
last_modified_ticks = cache.GetModifiedTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_registered{}; ///< Whether the object is currently registered with the cache
|
|
|
|
bool is_dirty{}; ///< Whether the object is dirty (out of sync with guest memory)
|
|
|
|
u64 last_modified_ticks{}; ///< When the object was last modified, used for in-order flushing
|
|
|
|
};
|
|
|
|
|
2018-08-23 20:38:57 +01:00
|
|
|
template <class T>
|
|
|
|
class RasterizerCache : NonCopyable {
|
2018-10-16 21:51:53 +01:00
|
|
|
friend class RasterizerCacheObject;
|
|
|
|
|
2018-08-23 20:38:57 +01:00
|
|
|
public:
|
2018-11-08 11:08:00 +00:00
|
|
|
explicit RasterizerCache(VideoCore::RasterizerInterface& rasterizer) : rasterizer{rasterizer} {}
|
|
|
|
|
2018-10-16 21:51:53 +01:00
|
|
|
/// Write any cached resources overlapping the specified region back to memory
|
2018-10-10 00:28:58 +01:00
|
|
|
void FlushRegion(Tegra::GPUVAddr addr, size_t size) {
|
2018-10-16 21:51:53 +01:00
|
|
|
const auto& objects{GetSortedObjectsFromRegion(addr, size)};
|
|
|
|
for (auto& object : objects) {
|
|
|
|
FlushObject(object);
|
2018-10-10 00:28:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:38:57 +01:00
|
|
|
/// Mark the specified region as being invalidated
|
2018-08-29 01:23:44 +01:00
|
|
|
void InvalidateRegion(VAddr addr, u64 size) {
|
2018-10-16 21:51:53 +01:00
|
|
|
const auto& objects{GetSortedObjectsFromRegion(addr, size)};
|
|
|
|
for (auto& object : objects) {
|
|
|
|
if (!object->IsRegistered()) {
|
|
|
|
// Skip duplicates
|
|
|
|
continue;
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
2018-10-16 21:51:53 +01:00
|
|
|
Unregister(object);
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
2018-08-29 01:23:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidates everything in the cache
|
|
|
|
void InvalidateAll() {
|
|
|
|
while (object_cache.begin() != object_cache.end()) {
|
|
|
|
Unregister(*object_cache.begin()->second.begin());
|
|
|
|
}
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Tries to get an object from the cache with the specified address
|
2018-08-28 02:35:15 +01:00
|
|
|
T TryGet(VAddr addr) const {
|
2018-08-29 01:23:44 +01:00
|
|
|
const ObjectInterval interval{addr};
|
|
|
|
for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) {
|
|
|
|
for (auto& cached_object : pair.second) {
|
|
|
|
if (cached_object->GetAddr() == addr) {
|
|
|
|
return cached_object;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register an object into the cache
|
|
|
|
void Register(const T& object) {
|
2018-10-16 21:51:53 +01:00
|
|
|
object->SetIsRegistered(true);
|
2018-08-29 01:23:44 +01:00
|
|
|
object_cache.add({GetInterval(object), ObjectSet{object}});
|
2018-08-28 23:43:08 +01:00
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1);
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregisters an object from the cache
|
|
|
|
void Unregister(const T& object) {
|
2018-10-16 21:51:53 +01:00
|
|
|
object->SetIsRegistered(false);
|
2018-08-28 23:43:08 +01:00
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1);
|
2018-10-14 21:09:01 +01:00
|
|
|
|
2018-10-16 22:02:29 +01:00
|
|
|
// Only flush if use_accurate_gpu_emulation is enabled, as it incurs a performance hit
|
|
|
|
if (Settings::values.use_accurate_gpu_emulation) {
|
2018-10-16 21:51:53 +01:00
|
|
|
FlushObject(object);
|
2018-10-14 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 01:23:44 +01:00
|
|
|
object_cache.subtract({GetInterval(object), ObjectSet{object}});
|
2018-08-23 20:38:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 21:51:53 +01:00
|
|
|
/// Returns a ticks counter used for tracking when cached objects were last modified
|
|
|
|
u64 GetModifiedTicks() {
|
|
|
|
return ++modified_ticks;
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:38:57 +01:00
|
|
|
private:
|
2018-10-16 21:51:53 +01:00
|
|
|
/// Returns a list of cached objects from the specified memory region, ordered by access time
|
|
|
|
std::vector<T> GetSortedObjectsFromRegion(VAddr addr, u64 size) {
|
|
|
|
if (size == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<T> objects;
|
|
|
|
const ObjectInterval interval{addr, addr + size};
|
|
|
|
for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) {
|
|
|
|
for (auto& cached_object : pair.second) {
|
|
|
|
if (!cached_object) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
objects.push_back(cached_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(objects.begin(), objects.end(), [](const T& a, const T& b) -> bool {
|
|
|
|
return a->GetLastModifiedTicks() < b->GetLastModifiedTicks();
|
|
|
|
});
|
|
|
|
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flushes the specified object, updating appropriate cache state as needed
|
|
|
|
void FlushObject(const T& object) {
|
|
|
|
if (!object->IsDirty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
object->Flush();
|
|
|
|
object->MarkAsModified(false, *this);
|
|
|
|
}
|
|
|
|
|
2018-08-29 01:23:44 +01:00
|
|
|
using ObjectSet = std::set<T>;
|
|
|
|
using ObjectCache = boost::icl::interval_map<VAddr, ObjectSet>;
|
|
|
|
using ObjectInterval = typename ObjectCache::interval_type;
|
|
|
|
|
|
|
|
static auto GetInterval(const T& object) {
|
|
|
|
return ObjectInterval::right_open(object->GetAddr(),
|
|
|
|
object->GetAddr() + object->GetSizeInBytes());
|
|
|
|
}
|
|
|
|
|
2018-10-16 21:51:53 +01:00
|
|
|
ObjectCache object_cache; ///< Cache of objects
|
|
|
|
u64 modified_ticks{}; ///< Counter of cache state ticks, used for in-order flushing
|
2018-11-08 11:08:00 +00:00
|
|
|
VideoCore::RasterizerInterface& rasterizer;
|
2018-08-23 20:38:57 +01:00
|
|
|
};
|