summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2018-10-16 16:51:53 -0400
committerGravatar bunnei2018-10-16 16:51:53 -0400
commit91602de7f27e391c8e322a2670ef9d50a64f7517 (patch)
tree82338c004e99032bf195b173f0c0ea7ed34c4f02 /src
parentgl_rasterizer_cache: Refactor to only call GetRegionEnd on surface creation. (diff)
downloadyuzu-91602de7f27e391c8e322a2670ef9d50a64f7517.tar.gz
yuzu-91602de7f27e391c8e322a2670ef9d50a64f7517.tar.xz
yuzu-91602de7f27e391c8e322a2670ef9d50a64f7517.zip
rasterizer_cache: Refactor to support in-order flushing.
Diffstat (limited to 'src')
-rw-r--r--src/video_core/rasterizer_cache.h132
-rw-r--r--src/video_core/renderer_opengl/gl_buffer_cache.h8
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp9
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp3
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.h17
-rw-r--r--src/video_core/renderer_opengl/gl_shader_cache.h10
6 files changed, 116 insertions, 63 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};
diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.h b/src/video_core/renderer_opengl/gl_buffer_cache.h
index b389ca684..be29dc8be 100644
--- a/src/video_core/renderer_opengl/gl_buffer_cache.h
+++ b/src/video_core/renderer_opengl/gl_buffer_cache.h
@@ -15,17 +15,17 @@
15 15
16namespace OpenGL { 16namespace OpenGL {
17 17
18struct CachedBufferEntry final { 18struct CachedBufferEntry final : public RasterizerCacheObject {
19 VAddr GetAddr() const { 19 VAddr GetAddr() const override {
20 return addr; 20 return addr;
21 } 21 }
22 22
23 std::size_t GetSizeInBytes() const { 23 std::size_t GetSizeInBytes() const override {
24 return size; 24 return size;
25 } 25 }
26 26
27 // We do not have to flush this cache as things in it are never modified by us. 27 // We do not have to flush this cache as things in it are never modified by us.
28 void Flush() {} 28 void Flush() override {}
29 29
30 VAddr addr; 30 VAddr addr;
31 std::size_t size; 31 std::size_t size;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 0485dfb7a..6ce183c25 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -428,7 +428,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
428 if (color_surface) { 428 if (color_surface) {
429 // Assume that a surface will be written to if it is used as a framebuffer, even if 429 // Assume that a surface will be written to if it is used as a framebuffer, even if
430 // the shader doesn't actually write to it. 430 // the shader doesn't actually write to it.
431 color_surface->MarkAsDirty(); 431 color_surface->MarkAsModified(true, res_cache);
432 } 432 }
433 433
434 glFramebufferTexture2D( 434 glFramebufferTexture2D(
@@ -445,7 +445,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
445 if (color_surface) { 445 if (color_surface) {
446 // Assume that a surface will be written to if it is used as a framebuffer, even 446 // Assume that a surface will be written to if it is used as a framebuffer, even
447 // if the shader doesn't actually write to it. 447 // if the shader doesn't actually write to it.
448 color_surface->MarkAsDirty(); 448 color_surface->MarkAsModified(true, res_cache);
449 } 449 }
450 450
451 buffers[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index); 451 buffers[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index);
@@ -469,7 +469,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
469 if (depth_surface) { 469 if (depth_surface) {
470 // Assume that a surface will be written to if it is used as a framebuffer, even if 470 // Assume that a surface will be written to if it is used as a framebuffer, even if
471 // the shader doesn't actually write to it. 471 // the shader doesn't actually write to it.
472 depth_surface->MarkAsDirty(); 472 depth_surface->MarkAsModified(true, res_cache);
473 473
474 if (regs.stencil_enable) { 474 if (regs.stencil_enable) {
475 // Attach both depth and stencil 475 // Attach both depth and stencil
@@ -642,9 +642,6 @@ void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
642 // Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit 642 // Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit
643 res_cache.FlushRegion(addr, size); 643 res_cache.FlushRegion(addr, size);
644 } 644 }
645
646 shader_cache.FlushRegion(addr, size);
647 buffer_cache.FlushRegion(addr, size);
648} 645}
649 646
650void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) { 647void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index a1f541e75..f79b4f221 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -905,8 +905,6 @@ void CachedSurface::LoadGLBuffer() {
905 } 905 }
906 906
907 ConvertFormatAsNeeded_LoadGLBuffer(gl_buffer, params.pixel_format, params.width, params.height); 907 ConvertFormatAsNeeded_LoadGLBuffer(gl_buffer, params.pixel_format, params.width, params.height);
908
909 dirty = false;
910} 908}
911 909
912MICROPROFILE_DEFINE(OpenGL_SurfaceFlush, "OpenGL", "Surface Flush", MP_RGB(128, 192, 64)); 910MICROPROFILE_DEFINE(OpenGL_SurfaceFlush, "OpenGL", "Surface Flush", MP_RGB(128, 192, 64));
@@ -1111,6 +1109,7 @@ Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool pre
1111void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) { 1109void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) {
1112 surface->LoadGLBuffer(); 1110 surface->LoadGLBuffer();
1113 surface->UploadGLTexture(read_framebuffer.handle, draw_framebuffer.handle); 1111 surface->UploadGLTexture(read_framebuffer.handle, draw_framebuffer.handle);
1112 surface->MarkAsModified(false, *this);
1114} 1113}
1115 1114
1116Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool preserve_contents) { 1115Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool preserve_contents) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index 39fd7cd75..77d925250 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -819,28 +819,20 @@ struct hash<SurfaceReserveKey> {
819 819
820namespace OpenGL { 820namespace OpenGL {
821 821
822class CachedSurface final { 822class CachedSurface final : public RasterizerCacheObject {
823public: 823public:
824 CachedSurface(const SurfaceParams& params); 824 CachedSurface(const SurfaceParams& params);
825 825
826 VAddr GetAddr() const { 826 VAddr GetAddr() const override {
827 return params.addr; 827 return params.addr;
828 } 828 }
829 829
830 std::size_t GetSizeInBytes() const { 830 std::size_t GetSizeInBytes() const override {
831 return cached_size_in_bytes; 831 return cached_size_in_bytes;
832 } 832 }
833 833
834 void Flush() { 834 void Flush() override {
835 // There is no need to flush the surface if it hasn't been modified by us.
836 if (!dirty)
837 return;
838 FlushGLBuffer(); 835 FlushGLBuffer();
839 dirty = false;
840 }
841
842 void MarkAsDirty() {
843 dirty = true;
844 } 836 }
845 837
846 const OGLTexture& Texture() const { 838 const OGLTexture& Texture() const {
@@ -868,7 +860,6 @@ private:
868 SurfaceParams params; 860 SurfaceParams params;
869 GLenum gl_target; 861 GLenum gl_target;
870 std::size_t cached_size_in_bytes; 862 std::size_t cached_size_in_bytes;
871 bool dirty = false;
872}; 863};
873 864
874class RasterizerCacheOpenGL final : public RasterizerCache<Surface> { 865class RasterizerCacheOpenGL final : public RasterizerCache<Surface> {
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.h b/src/video_core/renderer_opengl/gl_shader_cache.h
index d9157ec3c..a210f1731 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.h
+++ b/src/video_core/renderer_opengl/gl_shader_cache.h
@@ -19,22 +19,20 @@ class CachedShader;
19using Shader = std::shared_ptr<CachedShader>; 19using Shader = std::shared_ptr<CachedShader>;
20using Maxwell = Tegra::Engines::Maxwell3D::Regs; 20using Maxwell = Tegra::Engines::Maxwell3D::Regs;
21 21
22class CachedShader final { 22class CachedShader final : public RasterizerCacheObject {
23public: 23public:
24 CachedShader(VAddr addr, Maxwell::ShaderProgram program_type); 24 CachedShader(VAddr addr, Maxwell::ShaderProgram program_type);
25 25
26 /// Gets the address of the shader in guest memory, required for cache management 26 VAddr GetAddr() const override {
27 VAddr GetAddr() const {
28 return addr; 27 return addr;
29 } 28 }
30 29
31 /// Gets the size of the shader in guest memory, required for cache management 30 std::size_t GetSizeInBytes() const override {
32 std::size_t GetSizeInBytes() const {
33 return GLShader::MAX_PROGRAM_CODE_LENGTH * sizeof(u64); 31 return GLShader::MAX_PROGRAM_CODE_LENGTH * sizeof(u64);
34 } 32 }
35 33
36 // We do not have to flush this cache as things in it are never modified by us. 34 // We do not have to flush this cache as things in it are never modified by us.
37 void Flush() {} 35 void Flush() override {}
38 36
39 /// Gets the shader entries for the shader 37 /// Gets the shader entries for the shader
40 const GLShader::ShaderEntries& GetShaderEntries() const { 38 const GLShader::ShaderEntries& GetShaderEntries() const {