2018-08-23 22:38:57 +03:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2018-08-29 01:43:08 +03:00
|
|
|
#include "core/core.h"
|
2018-08-23 22:38:57 +03:00
|
|
|
#include "core/memory.h"
|
|
|
|
#include "video_core/memory_manager.h"
|
2018-08-29 01:43:08 +03:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
#include "video_core/renderer_base.h"
|
2018-08-23 22:38:57 +03:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class RasterizerCache : NonCopyable {
|
|
|
|
public:
|
|
|
|
/// Mark the specified region as being invalidated
|
2018-08-28 04:35:15 +03:00
|
|
|
void InvalidateRegion(VAddr region_addr, size_t region_size) {
|
2018-08-23 22:38:57 +03:00
|
|
|
for (auto iter = cached_objects.cbegin(); iter != cached_objects.cend();) {
|
|
|
|
const auto& object{iter->second};
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
|
|
|
|
if (object->GetAddr() <= (region_addr + region_size) &&
|
|
|
|
region_addr <= (object->GetAddr() + object->GetSizeInBytes())) {
|
|
|
|
// Regions overlap, so invalidate
|
|
|
|
Unregister(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Tries to get an object from the cache with the specified address
|
2018-08-28 04:35:15 +03:00
|
|
|
T TryGet(VAddr addr) const {
|
2018-08-23 22:38:57 +03:00
|
|
|
const auto& search{cached_objects.find(addr)};
|
|
|
|
if (search != cached_objects.end()) {
|
|
|
|
return search->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a reference to the cache
|
2018-08-28 04:35:15 +03:00
|
|
|
const std::unordered_map<VAddr, T>& GetCache() const {
|
2018-08-23 22:38:57 +03:00
|
|
|
return cached_objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register an object into the cache
|
|
|
|
void Register(const T& object) {
|
|
|
|
const auto& search{cached_objects.find(object->GetAddr())};
|
|
|
|
if (search != cached_objects.end()) {
|
|
|
|
// Registered already
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-29 01:43:08 +03:00
|
|
|
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
|
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1);
|
|
|
|
cached_objects[object->GetAddr()] = std::move(object);
|
2018-08-23 22:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregisters an object from the cache
|
|
|
|
void Unregister(const T& object) {
|
|
|
|
const auto& search{cached_objects.find(object->GetAddr())};
|
|
|
|
if (search == cached_objects.end()) {
|
|
|
|
// Unregistered already
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-29 01:43:08 +03:00
|
|
|
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
|
|
|
|
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1);
|
2018-08-23 22:38:57 +03:00
|
|
|
cached_objects.erase(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-28 04:35:15 +03:00
|
|
|
std::unordered_map<VAddr, T> cached_objects;
|
2018-08-23 22:38:57 +03:00
|
|
|
};
|