summaryrefslogtreecommitdiff
path: root/src/video_core/rasterizer_cache.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/rasterizer_cache.h')
-rw-r--r--src/video_core/rasterizer_cache.h132
1 files changed, 100 insertions, 32 deletions
diff --git a/src/video_core/rasterizer_cache.h b/src/video_core/rasterizer_cache.h
index 76743a85b..3ec01b967 100644
--- a/src/video_core/rasterizer_cache.h
+++ b/src/video_core/rasterizer_cache.h
@@ -15,45 +15,73 @@
15#include "video_core/rasterizer_interface.h" 15#include "video_core/rasterizer_interface.h"
16#include "video_core/renderer_base.h" 16#include "video_core/renderer_base.h"
17 17
18class RasterizerCacheObject {
19public:
20 /// Gets the address of the shader in guest memory, required for cache management
21 virtual VAddr GetAddr() const = 0;
22
23 /// Gets the size of the shader in guest memory, required for cache management
24 virtual std::size_t GetSizeInBytes() const = 0;
25
26 /// Wriets any cached resources back to memory
27 virtual void Flush() = 0;
28
29 /// Sets whether the cached object should be considered registered
30 void SetIsRegistered(bool registered) {
31 is_registered = registered;
32 }
33
34 /// Returns true if the cached object is registered
35 bool IsRegistered() const {
36 return is_registered;
37 }
38
39 /// Returns true if the cached object is dirty
40 bool IsDirty() const {
41 return is_dirty;
42 }
43
44 /// Returns ticks from when this cached object was last modified
45 u64 GetLastModifiedTicks() const {
46 return last_modified_ticks;
47 }
48
49 /// Marks an object as recently modified, used to specify whether it is clean or dirty
50 template <class T>
51 void MarkAsModified(bool dirty, T& cache) {
52 is_dirty = dirty;
53 last_modified_ticks = cache.GetModifiedTicks();
54 }
55
56private:
57 bool is_registered{}; ///< Whether the object is currently registered with the cache
58 bool is_dirty{}; ///< Whether the object is dirty (out of sync with guest memory)
59 u64 last_modified_ticks{}; ///< When the object was last modified, used for in-order flushing
60};
61
18template <class T> 62template <class T>
19class RasterizerCache : NonCopyable { 63class RasterizerCache : NonCopyable {
64 friend class RasterizerCacheObject;
65
20public: 66public:
21 /// Write any cached resources overlapping the region back to memory (if dirty) 67 /// Write any cached resources overlapping the specified region back to memory
22 void FlushRegion(Tegra::GPUVAddr addr, size_t size) { 68 void FlushRegion(Tegra::GPUVAddr addr, size_t size) {
23 if (size == 0) 69 const auto& objects{GetSortedObjectsFromRegion(addr, size)};
24 return; 70 for (auto& object : objects) {
25 71 FlushObject(object);
26 const ObjectInterval interval{addr, addr + size};
27 for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) {
28 for (auto& cached_object : pair.second) {
29 if (!cached_object)
30 continue;
31
32 cached_object->Flush();
33 }
34 } 72 }
35 } 73 }
36 74
37 /// Mark the specified region as being invalidated 75 /// Mark the specified region as being invalidated
38 void InvalidateRegion(VAddr addr, u64 size) { 76 void InvalidateRegion(VAddr addr, u64 size) {
39 if (size == 0) 77 const auto& objects{GetSortedObjectsFromRegion(addr, size)};
40 return; 78 for (auto& object : objects) {
41 79 if (!object->IsRegistered()) {
42 const ObjectInterval interval{addr, addr + size}; 80 // Skip duplicates
43 for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) { 81 continue;
44 for (auto& cached_object : pair.second) {
45 if (!cached_object)
46 continue;
47
48 remove_objects.emplace(cached_object);
49 } 82 }
83 Unregister(object);
50 } 84 }
51
52 for (auto& remove_object : remove_objects) {
53 Unregister(remove_object);
54 }
55
56 remove_objects.clear();
57 } 85 }
58 86
59 /// Invalidates everything in the cache 87 /// Invalidates everything in the cache
@@ -79,6 +107,7 @@ protected:
79 107
80 /// Register an object into the cache 108 /// Register an object into the cache
81 void Register(const T& object) { 109 void Register(const T& object) {
110 object->SetIsRegistered(true);
82 object_cache.add({GetInterval(object), ObjectSet{object}}); 111 object_cache.add({GetInterval(object), ObjectSet{object}});
83 auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer(); 112 auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
84 rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1); 113 rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1);
@@ -86,18 +115,57 @@ protected:
86 115
87 /// Unregisters an object from the cache 116 /// Unregisters an object from the cache
88 void Unregister(const T& object) { 117 void Unregister(const T& object) {
118 object->SetIsRegistered(false);
89 auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer(); 119 auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
90 rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1); 120 rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1);
91 121
122 // Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit
92 if (Settings::values.use_accurate_framebuffers) { 123 if (Settings::values.use_accurate_framebuffers) {
93 // Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit 124 FlushObject(object);
94 object->Flush();
95 } 125 }
96 126
97 object_cache.subtract({GetInterval(object), ObjectSet{object}}); 127 object_cache.subtract({GetInterval(object), ObjectSet{object}});
98 } 128 }
99 129
130 /// Returns a ticks counter used for tracking when cached objects were last modified
131 u64 GetModifiedTicks() {
132 return ++modified_ticks;
133 }
134
100private: 135private:
136 /// Returns a list of cached objects from the specified memory region, ordered by access time
137 std::vector<T> GetSortedObjectsFromRegion(VAddr addr, u64 size) {
138 if (size == 0) {
139 return {};
140 }
141
142 std::vector<T> objects;
143 const ObjectInterval interval{addr, addr + size};
144 for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) {
145 for (auto& cached_object : pair.second) {
146 if (!cached_object) {
147 continue;
148 }
149 objects.push_back(cached_object);
150 }
151 }
152
153 std::sort(objects.begin(), objects.end(), [](const T& a, const T& b) -> bool {
154 return a->GetLastModifiedTicks() < b->GetLastModifiedTicks();
155 });
156
157 return objects;
158 }
159
160 /// Flushes the specified object, updating appropriate cache state as needed
161 void FlushObject(const T& object) {
162 if (!object->IsDirty()) {
163 return;
164 }
165 object->Flush();
166 object->MarkAsModified(false, *this);
167 }
168
101 using ObjectSet = std::set<T>; 169 using ObjectSet = std::set<T>;
102 using ObjectCache = boost::icl::interval_map<VAddr, ObjectSet>; 170 using ObjectCache = boost::icl::interval_map<VAddr, ObjectSet>;
103 using ObjectInterval = typename ObjectCache::interval_type; 171 using ObjectInterval = typename ObjectCache::interval_type;
@@ -107,6 +175,6 @@ private:
107 object->GetAddr() + object->GetSizeInBytes()); 175 object->GetAddr() + object->GetSizeInBytes());
108 } 176 }
109 177
110 ObjectCache object_cache; 178 ObjectCache object_cache; ///< Cache of objects
111 ObjectSet remove_objects; 179 u64 modified_ticks{}; ///< Counter of cache state ticks, used for in-order flushing
112}; 180};