2018-08-23 19:38:57 +00: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 00:23:44 +00:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include <boost/icl/interval_map.hpp>
|
2018-08-31 16:21:34 +00:00
|
|
|
#include <boost/range/iterator_range_core.hpp>
|
2018-08-23 19:38:57 +00:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2018-08-28 22:43:08 +00:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
#include "video_core/renderer_base.h"
|
2018-08-23 19:38:57 +00:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class RasterizerCache : NonCopyable {
|
|
|
|
public:
|
2018-10-09 23:28:58 +00:00
|
|
|
/// Write any cached resources overlapping the region back to memory (if dirty)
|
|
|
|
void FlushRegion(Tegra::GPUVAddr addr, size_t size) {
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
cached_object->Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:38:57 +00:00
|
|
|
/// Mark the specified region as being invalidated
|
2018-08-29 00:23:44 +00:00
|
|
|
void InvalidateRegion(VAddr addr, u64 size) {
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
2018-08-23 19:38:57 +00:00
|
|
|
|
2018-08-29 00:23:44 +00:00
|
|
|
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;
|
2018-08-23 19:38:57 +00:00
|
|
|
|
2018-08-29 00:23:44 +00:00
|
|
|
remove_objects.emplace(cached_object);
|
2018-08-23 19:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 00:23:44 +00:00
|
|
|
|
|
|
|
for (auto& remove_object : remove_objects) {
|
|
|
|
Unregister(remove_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_objects.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidates everything in the cache
|
|
|
|
void InvalidateAll() {
|
|
|
|
while (object_cache.begin() != object_cache.end()) {
|
|
|
|
Unregister(*object_cache.begin()->second.begin());
|
|
|
|
}
|
2018-08-23 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Tries to get an object from the cache with the specified address
|
2018-08-28 01:35:15 +00:00
|
|
|
T TryGet(VAddr addr) const {
|
2018-08-29 00:23:44 +00: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 19:38:57 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register an object into the cache
|
|
|
|
void Register(const T& object) {
|
2018-08-29 00:23:44 +00:00
|
|
|
object_cache.add({GetInterval(object), ObjectSet{object}});
|
2018-08-28 22:43:08 +00:00
|
|
|
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
|
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1);
|
2018-08-23 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregisters an object from the cache
|
|
|
|
void Unregister(const T& object) {
|
2018-08-28 22:43:08 +00:00
|
|
|
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
|
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1);
|
2018-10-09 23:28:58 +00:00
|
|
|
object->Flush();
|
2018-08-29 00:23:44 +00:00
|
|
|
object_cache.subtract({GetInterval(object), ObjectSet{object}});
|
2018-08-23 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-29 00:23:44 +00: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());
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectCache object_cache;
|
|
|
|
ObjectSet remove_objects;
|
2018-08-23 19:38:57 +00:00
|
|
|
};
|