From 6e3b2712d2fafcd8fae7ba3653207980685708e6 Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 12 Dec 2014 08:53:48 +0100 Subject: [PATCH] VideoCommon: Move NativeVertexFormat cache to VertexLoaderManager --- Source/Core/VideoCommon/VertexLoader.cpp | 19 ------------- Source/Core/VideoCommon/VertexLoader.h | 7 +---- .../Core/VideoCommon/VertexLoaderManager.cpp | 28 ++++++++++++++----- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/Source/Core/VideoCommon/VertexLoader.cpp b/Source/Core/VideoCommon/VertexLoader.cpp index 2dea025d96..ddf5097986 100644 --- a/Source/Core/VideoCommon/VertexLoader.cpp +++ b/Source/Core/VideoCommon/VertexLoader.cpp @@ -591,22 +591,3 @@ void VertexLoader::AppendToString(std::string *dest) const } dest->append(StringFromFormat(" - %i v\n", m_numLoadedVertices)); } - -NativeVertexFormat* VertexLoader::GetNativeVertexFormat() -{ - if (m_native_vertex_format) - return m_native_vertex_format; - auto& native = s_native_vertex_map[m_native_vtx_decl]; - if (!native) - { - auto raw_pointer = g_vertex_manager->CreateNativeVertexFormat(); - native = std::unique_ptr(raw_pointer); - native->Initialize(m_native_vtx_decl); - native->m_components = m_native_components; - } - m_native_vertex_format = native.get(); - return native.get(); - -} - -std::unordered_map> VertexLoader::s_native_vertex_map; diff --git a/Source/Core/VideoCommon/VertexLoader.h b/Source/Core/VideoCommon/VertexLoader.h index 4594e43d47..92adfbeb54 100644 --- a/Source/Core/VideoCommon/VertexLoader.h +++ b/Source/Core/VideoCommon/VertexLoader.h @@ -8,9 +8,7 @@ // Metroid Prime: P I16-flt N I16-s16 T0 I16-u16 T1 i16-flt #include -#include #include -#include #include "Common/CommonTypes.h" #include "Common/x64Emitter.h" @@ -116,8 +114,7 @@ public: void AppendToString(std::string *dest) const; int GetNumLoadedVerts() const { return m_numLoadedVertices; } - NativeVertexFormat* GetNativeVertexFormat(); - static void ClearNativeVertexFormatCache() { s_native_vertex_map.clear(); } + NativeVertexFormat* m_native_vertex_format; // used by VertexLoaderManager to cache the NativeVertexFormat objects private: int m_VertexSize; // number of bytes of a raw GC vertex. Computed by CompileVertexTranslator. @@ -141,8 +138,6 @@ private: int m_numLoadedVertices; - NativeVertexFormat* m_native_vertex_format; - static std::unordered_map> s_native_vertex_map; void SetVAT(const VAT& vat); diff --git a/Source/Core/VideoCommon/VertexLoaderManager.cpp b/Source/Core/VideoCommon/VertexLoaderManager.cpp index 1bfd05cace..48f924d9d4 100644 --- a/Source/Core/VideoCommon/VertexLoaderManager.cpp +++ b/Source/Core/VideoCommon/VertexLoaderManager.cpp @@ -21,13 +21,16 @@ #include "VideoCommon/VertexShaderManager.h" #include "VideoCommon/VideoCommon.h" -static NativeVertexFormat* s_current_vtx_fmt; -typedef std::unordered_map> VertexLoaderMap; namespace VertexLoaderManager { +typedef std::unordered_map> NativeVertexFormatMap; +static NativeVertexFormatMap s_native_vertex_map; +static NativeVertexFormat* s_current_vtx_fmt; + +typedef std::unordered_map> VertexLoaderMap; static std::mutex s_vertex_loader_map_lock; static VertexLoaderMap s_vertex_loader_map; // TODO - change into array of pointers. Keep a map of all seen so far. @@ -46,7 +49,7 @@ void Shutdown() { std::lock_guard lk(s_vertex_loader_map_lock); s_vertex_loader_map.clear(); - VertexLoader::ClearNativeVertexFormatCache(); + s_native_vertex_map.clear(); } namespace @@ -106,6 +109,19 @@ static VertexLoader* RefreshLoader(int vtx_attr_group, CPState* state) { loader = new VertexLoader(state->vtx_desc, state->vtx_attr[vtx_attr_group]); s_vertex_loader_map[uid] = std::unique_ptr(loader); + + // search for a cached native vertex format + const PortableVertexDeclaration& format = loader->GetNativeVertexDeclaration(); + auto& native = s_native_vertex_map[format]; + if (!native) + { + auto raw_pointer = g_vertex_manager->CreateNativeVertexFormat(); + native = std::unique_ptr(raw_pointer); + native->Initialize(format); + native->m_components = loader->GetNativeComponents(); + } + loader->m_native_vertex_format = native.get(); + INCSTAT(stats.numVertexLoaders); } state->vertex_loaders[vtx_attr_group] = loader; @@ -135,12 +151,10 @@ int RunVertices(int vtx_attr_group, int primitive, int count, DataReader src, bo return size; } - NativeVertexFormat* native = loader->GetNativeVertexFormat(); - // If the native vertex format changed, force a flush. - if (native != s_current_vtx_fmt) + if (loader->m_native_vertex_format != s_current_vtx_fmt) VertexManager::Flush(); - s_current_vtx_fmt = native; + s_current_vtx_fmt = loader->m_native_vertex_format; DataReader dst = VertexManager::PrepareForAdditionalData(primitive, count, loader->GetNativeVertexDeclaration().stride);