yuzu-emu
/
yuzu-android
Archived
1
0
Fork 0

rasterizer_cache: Refactor to support in-order flushing.

This commit is contained in:
bunnei 2018-10-16 16:51:53 -04:00
parent 0e59291310
commit 91602de7f2
6 changed files with 116 additions and 63 deletions

View File

@ -15,45 +15,73 @@
#include "video_core/rasterizer_interface.h" #include "video_core/rasterizer_interface.h"
#include "video_core/renderer_base.h" #include "video_core/renderer_base.h"
class RasterizerCacheObject {
public:
/// 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
};
template <class T> template <class T>
class RasterizerCache : NonCopyable { class RasterizerCache : NonCopyable {
friend class RasterizerCacheObject;
public: public:
/// Write any cached resources overlapping the region back to memory (if dirty) /// Write any cached resources overlapping the specified region back to memory
void FlushRegion(Tegra::GPUVAddr addr, size_t size) { void FlushRegion(Tegra::GPUVAddr addr, size_t size) {
if (size == 0) const auto& objects{GetSortedObjectsFromRegion(addr, size)};
return; for (auto& object : objects) {
FlushObject(object);
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();
}
} }
} }
/// Mark the specified region as being invalidated /// Mark the specified region as being invalidated
void InvalidateRegion(VAddr addr, u64 size) { void InvalidateRegion(VAddr addr, u64 size) {
if (size == 0) const auto& objects{GetSortedObjectsFromRegion(addr, size)};
return; for (auto& object : objects) {
if (!object->IsRegistered()) {
const ObjectInterval interval{addr, addr + size}; // Skip duplicates
for (auto& pair : boost::make_iterator_range(object_cache.equal_range(interval))) { continue;
for (auto& cached_object : pair.second) {
if (!cached_object)
continue;
remove_objects.emplace(cached_object);
} }
Unregister(object);
} }
for (auto& remove_object : remove_objects) {
Unregister(remove_object);
}
remove_objects.clear();
} }
/// Invalidates everything in the cache /// Invalidates everything in the cache
@ -79,6 +107,7 @@ protected:
/// Register an object into the cache /// Register an object into the cache
void Register(const T& object) { void Register(const T& object) {
object->SetIsRegistered(true);
object_cache.add({GetInterval(object), ObjectSet{object}}); object_cache.add({GetInterval(object), ObjectSet{object}});
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer(); auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1); rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), 1);
@ -86,18 +115,57 @@ protected:
/// Unregisters an object from the cache /// Unregisters an object from the cache
void Unregister(const T& object) { void Unregister(const T& object) {
object->SetIsRegistered(false);
auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer(); auto& rasterizer = Core::System::GetInstance().Renderer().Rasterizer();
rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1); rasterizer.UpdatePagesCachedCount(object->GetAddr(), object->GetSizeInBytes(), -1);
// Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit
if (Settings::values.use_accurate_framebuffers) { if (Settings::values.use_accurate_framebuffers) {
// Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit FlushObject(object);
object->Flush();
} }
object_cache.subtract({GetInterval(object), ObjectSet{object}}); object_cache.subtract({GetInterval(object), ObjectSet{object}});
} }
/// Returns a ticks counter used for tracking when cached objects were last modified
u64 GetModifiedTicks() {
return ++modified_ticks;
}
private: private:
/// 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);
}
using ObjectSet = std::set<T>; using ObjectSet = std::set<T>;
using ObjectCache = boost::icl::interval_map<VAddr, ObjectSet>; using ObjectCache = boost::icl::interval_map<VAddr, ObjectSet>;
using ObjectInterval = typename ObjectCache::interval_type; using ObjectInterval = typename ObjectCache::interval_type;
@ -107,6 +175,6 @@ private:
object->GetAddr() + object->GetSizeInBytes()); object->GetAddr() + object->GetSizeInBytes());
} }
ObjectCache object_cache; ObjectCache object_cache; ///< Cache of objects
ObjectSet remove_objects; u64 modified_ticks{}; ///< Counter of cache state ticks, used for in-order flushing
}; };

View File

@ -15,17 +15,17 @@
namespace OpenGL { namespace OpenGL {
struct CachedBufferEntry final { struct CachedBufferEntry final : public RasterizerCacheObject {
VAddr GetAddr() const { VAddr GetAddr() const override {
return addr; return addr;
} }
std::size_t GetSizeInBytes() const { std::size_t GetSizeInBytes() const override {
return size; return size;
} }
// We do not have to flush this cache as things in it are never modified by us. // We do not have to flush this cache as things in it are never modified by us.
void Flush() {} void Flush() override {}
VAddr addr; VAddr addr;
std::size_t size; std::size_t size;

View File

@ -428,7 +428,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
if (color_surface) { if (color_surface) {
// Assume that a surface will be written to if it is used as a framebuffer, even if // Assume that a surface will be written to if it is used as a framebuffer, even if
// the shader doesn't actually write to it. // the shader doesn't actually write to it.
color_surface->MarkAsDirty(); color_surface->MarkAsModified(true, res_cache);
} }
glFramebufferTexture2D( glFramebufferTexture2D(
@ -445,7 +445,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
if (color_surface) { if (color_surface) {
// Assume that a surface will be written to if it is used as a framebuffer, even // Assume that a surface will be written to if it is used as a framebuffer, even
// if the shader doesn't actually write to it. // if the shader doesn't actually write to it.
color_surface->MarkAsDirty(); color_surface->MarkAsModified(true, res_cache);
} }
buffers[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index); buffers[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index);
@ -469,7 +469,7 @@ void RasterizerOpenGL::ConfigureFramebuffers(bool using_color_fb, bool using_dep
if (depth_surface) { if (depth_surface) {
// Assume that a surface will be written to if it is used as a framebuffer, even if // Assume that a surface will be written to if it is used as a framebuffer, even if
// the shader doesn't actually write to it. // the shader doesn't actually write to it.
depth_surface->MarkAsDirty(); depth_surface->MarkAsModified(true, res_cache);
if (regs.stencil_enable) { if (regs.stencil_enable) {
// Attach both depth and stencil // Attach both depth and stencil
@ -642,9 +642,6 @@ void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
// Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit // Only flush if use_accurate_framebuffers is enabled, as it incurs a performance hit
res_cache.FlushRegion(addr, size); res_cache.FlushRegion(addr, size);
} }
shader_cache.FlushRegion(addr, size);
buffer_cache.FlushRegion(addr, size);
} }
void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) { void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {

View File

@ -905,8 +905,6 @@ void CachedSurface::LoadGLBuffer() {
} }
ConvertFormatAsNeeded_LoadGLBuffer(gl_buffer, params.pixel_format, params.width, params.height); ConvertFormatAsNeeded_LoadGLBuffer(gl_buffer, params.pixel_format, params.width, params.height);
dirty = false;
} }
MICROPROFILE_DEFINE(OpenGL_SurfaceFlush, "OpenGL", "Surface Flush", MP_RGB(128, 192, 64)); MICROPROFILE_DEFINE(OpenGL_SurfaceFlush, "OpenGL", "Surface Flush", MP_RGB(128, 192, 64));
@ -1111,6 +1109,7 @@ Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool pre
void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) { void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) {
surface->LoadGLBuffer(); surface->LoadGLBuffer();
surface->UploadGLTexture(read_framebuffer.handle, draw_framebuffer.handle); surface->UploadGLTexture(read_framebuffer.handle, draw_framebuffer.handle);
surface->MarkAsModified(false, *this);
} }
Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool preserve_contents) { Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool preserve_contents) {

View File

@ -819,28 +819,20 @@ struct hash<SurfaceReserveKey> {
namespace OpenGL { namespace OpenGL {
class CachedSurface final { class CachedSurface final : public RasterizerCacheObject {
public: public:
CachedSurface(const SurfaceParams& params); CachedSurface(const SurfaceParams& params);
VAddr GetAddr() const { VAddr GetAddr() const override {
return params.addr; return params.addr;
} }
std::size_t GetSizeInBytes() const { std::size_t GetSizeInBytes() const override {
return cached_size_in_bytes; return cached_size_in_bytes;
} }
void Flush() { void Flush() override {
// There is no need to flush the surface if it hasn't been modified by us.
if (!dirty)
return;
FlushGLBuffer(); FlushGLBuffer();
dirty = false;
}
void MarkAsDirty() {
dirty = true;
} }
const OGLTexture& Texture() const { const OGLTexture& Texture() const {
@ -868,7 +860,6 @@ private:
SurfaceParams params; SurfaceParams params;
GLenum gl_target; GLenum gl_target;
std::size_t cached_size_in_bytes; std::size_t cached_size_in_bytes;
bool dirty = false;
}; };
class RasterizerCacheOpenGL final : public RasterizerCache<Surface> { class RasterizerCacheOpenGL final : public RasterizerCache<Surface> {

View File

@ -19,22 +19,20 @@ class CachedShader;
using Shader = std::shared_ptr<CachedShader>; using Shader = std::shared_ptr<CachedShader>;
using Maxwell = Tegra::Engines::Maxwell3D::Regs; using Maxwell = Tegra::Engines::Maxwell3D::Regs;
class CachedShader final { class CachedShader final : public RasterizerCacheObject {
public: public:
CachedShader(VAddr addr, Maxwell::ShaderProgram program_type); CachedShader(VAddr addr, Maxwell::ShaderProgram program_type);
/// Gets the address of the shader in guest memory, required for cache management VAddr GetAddr() const override {
VAddr GetAddr() const {
return addr; return addr;
} }
/// Gets the size of the shader in guest memory, required for cache management std::size_t GetSizeInBytes() const override {
std::size_t GetSizeInBytes() const {
return GLShader::MAX_PROGRAM_CODE_LENGTH * sizeof(u64); return GLShader::MAX_PROGRAM_CODE_LENGTH * sizeof(u64);
} }
// We do not have to flush this cache as things in it are never modified by us. // We do not have to flush this cache as things in it are never modified by us.
void Flush() {} void Flush() override {}
/// Gets the shader entries for the shader /// Gets the shader entries for the shader
const GLShader::ShaderEntries& GetShaderEntries() const { const GLShader::ShaderEntries& GetShaderEntries() const {