From 0f9cb7dcd260435733fb33a86b0934703a53ca7a Mon Sep 17 00:00:00 2001 From: rachytski Date: Sat, 12 Nov 2011 19:29:18 +0400 Subject: [PATCH] different lock/unlock behavior in different RenderPolicy's. --- base/resource_pool.hpp | 24 ++++++++++- map/partial_render_policy.cpp | 3 +- map/render_policy_mt.cpp | 3 +- map/render_policy_st.cpp | 3 +- map/tiling_render_policy_mt.cpp | 3 +- map/tiling_render_policy_st.cpp | 3 +- qt/widgets.cpp | 3 +- qt_tstfrm/tstwidgets.cpp | 3 +- yg/blitter.cpp | 6 +++ yg/geometry_batcher.cpp | 6 +++ yg/resource_manager.cpp | 70 ++++++++++++++++++++++---------- yg/resource_manager.hpp | 21 +++++++--- yg/yg_tests/skin_loader_test.cpp | 2 +- yg/yg_tests/skin_test.cpp | 2 +- 14 files changed, 115 insertions(+), 37 deletions(-) diff --git a/base/resource_pool.hpp b/base/resource_pool.hpp index 6cc0297968..69edc011c1 100644 --- a/base/resource_pool.hpp +++ b/base/resource_pool.hpp @@ -47,6 +47,11 @@ struct BasePoolTraits { return m_pool.IsCancelled(); } + + /// non-intuitive name, should refactor to more meaningfull + void Merge() + { + } }; /// This traits stores the free elements in a separate pool and has @@ -157,10 +162,25 @@ struct AllocateOnDemandPoolTraits : TBase } }; +/// resource pool interface +template +class ResourcePool +{ +public: + virtual TElem const Reserve() = 0; + virtual void Free(TElem const & elem) = 0; + virtual size_t Size() const = 0; + virtual void EnterForeground() = 0; + virtual void EnterBackground() = 0; + virtual void Cancel() = 0; + virtual bool IsCancelled() const = 0; + virtual void Merge() = 0; +}; + // This class tracks OpenGL resources allocation in // a multithreaded environment. template -class ResourcePool +class ResourcePoolImpl : public ResourcePool { private: @@ -170,7 +190,7 @@ public: typedef typename TPoolTraits::elem_t elem_t; - ResourcePool(TPoolTraits * traits) + ResourcePoolImpl(TPoolTraits * traits) : m_traits(traits) { /// quick trick to perform lazy initialization diff --git a/map/partial_render_policy.cpp b/map/partial_render_policy.cpp index ded9c7dba1..bd35b8894f 100644 --- a/map/partial_render_policy.cpp +++ b/map/partial_render_policy.cpp @@ -39,7 +39,8 @@ PartialRenderPolicy::PartialRenderPolicy(VideoTimer * videoTimer, 2 * 1024 * 1024, 3, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + true)); m_resourceManager->initTinyStorage(300 * sizeof(yg::gl::Vertex), 600 * sizeof(unsigned short), 20); diff --git a/map/render_policy_mt.cpp b/map/render_policy_mt.cpp index 25bed89ef9..3962346365 100644 --- a/map/render_policy_mt.cpp +++ b/map/render_policy_mt.cpp @@ -40,7 +40,8 @@ RenderPolicyMT::RenderPolicyMT(VideoTimer * videoTimer, 2 * 1024 * 1024, GetPlatform().CpuCores() + 2, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); m_resourceManager->initTinyStorage(300 * sizeof(yg::gl::Vertex), 600 * sizeof(unsigned short), 20); diff --git a/map/render_policy_st.cpp b/map/render_policy_st.cpp index bbc40be591..e82c7fa87f 100644 --- a/map/render_policy_st.cpp +++ b/map/render_policy_st.cpp @@ -37,7 +37,8 @@ RenderPolicyST::RenderPolicyST(VideoTimer * videoTimer, 2 * 1024 * 1024, 1, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); Platform::FilesList fonts; GetPlatform().GetFontNames(fonts); diff --git a/map/tiling_render_policy_mt.cpp b/map/tiling_render_policy_mt.cpp index 58cb66793f..48edcc8a24 100644 --- a/map/tiling_render_policy_mt.cpp +++ b/map/tiling_render_policy_mt.cpp @@ -39,7 +39,8 @@ TilingRenderPolicyMT::TilingRenderPolicyMT(VideoTimer * videoTimer, 2 * 1024 * 1024, GetPlatform().CpuCores() + 2, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); m_resourceManager->initMultiBlitStorage(500 * sizeof(yg::gl::AuxVertex), 500 * sizeof(unsigned short), 10); m_resourceManager->initRenderTargets(GetPlatform().TileSize(), GetPlatform().TileSize(), GetPlatform().MaxTilesCount()); diff --git a/map/tiling_render_policy_st.cpp b/map/tiling_render_policy_st.cpp index 72efb6f290..f035b0fa00 100644 --- a/map/tiling_render_policy_st.cpp +++ b/map/tiling_render_policy_st.cpp @@ -41,7 +41,8 @@ TilingRenderPolicyST::TilingRenderPolicyST(VideoTimer * videoTimer, 2 * 1024 * 1024, GetPlatform().CpuCores() + 2, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); m_resourceManager->initMultiBlitStorage(500 * sizeof(yg::gl::AuxVertex), 500 * sizeof(unsigned short), 10); m_resourceManager->initRenderTargets(GetPlatform().TileSize(), GetPlatform().TileSize(), GetPlatform().MaxTilesCount()); diff --git a/qt/widgets.cpp b/qt/widgets.cpp index a70a295c2b..030ae07ebc 100644 --- a/qt/widgets.cpp +++ b/qt/widgets.cpp @@ -73,7 +73,8 @@ namespace qt 2 * 1024 * 1024, GetPlatform().CpuCores() + 2, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); m_resourceManager->initMultiBlitStorage(500 * sizeof(yg::gl::AuxVertex), 500 * sizeof(unsigned short), 10); diff --git a/qt_tstfrm/tstwidgets.cpp b/qt_tstfrm/tstwidgets.cpp index 9ffcccf1e3..862acd984f 100644 --- a/qt_tstfrm/tstwidgets.cpp +++ b/qt_tstfrm/tstwidgets.cpp @@ -60,7 +60,8 @@ void GLDrawWidget::initializeGL() 2 * 1024 * 1024, GetPlatform().CpuCores() + 2, yg::Rt8Bpp, - !yg::gl::g_isBufferObjectsSupported)); + !yg::gl::g_isBufferObjectsSupported, + false)); m_resourceManager->initMultiBlitStorage(500 * sizeof(yg::gl::AuxVertex), 500 * sizeof(unsigned short), 10); diff --git a/yg/blitter.cpp b/yg/blitter.cpp index acb36f573f..ea44cf76e8 100644 --- a/yg/blitter.cpp +++ b/yg/blitter.cpp @@ -318,6 +318,12 @@ namespace yg LOG(LINFO, ("performing IMMDrawTexturedPrimitives command")); yg::gl::Storage blitStorage = m_resourceManager->blitStorages()->Reserve(); + if (!blitStorage.m_vertices->isLocked()) + blitStorage.m_vertices->lock(); + + if (!blitStorage.m_indices->isLocked()) + blitStorage.m_indices->lock(); + AuxVertex * pointsData = (AuxVertex*)blitStorage.m_vertices->data(); for (size_t i = 0; i < m_ptsCount; ++i) diff --git a/yg/geometry_batcher.cpp b/yg/geometry_batcher.cpp index e69a106d41..200827aaa5 100644 --- a/yg/geometry_batcher.cpp +++ b/yg/geometry_batcher.cpp @@ -69,8 +69,14 @@ namespace yg m_maxVertices = m_storage.m_vertices->size() / sizeof(Vertex); m_maxIndices = m_storage.m_indices->size() / sizeof(unsigned short); + if (!m_storage.m_vertices->isLocked()) + m_storage.m_vertices->lock(); + if (!m_storage.m_indices->isLocked()) + m_storage.m_indices->lock(); + m_vertices = (Vertex*)m_storage.m_vertices->data(); m_indices = (unsigned short *)m_storage.m_indices->data(); + m_hasStorage = true; } } diff --git a/yg/resource_manager.cpp b/yg/resource_manager.cpp index d216f5cf41..cb9855ccdd 100644 --- a/yg/resource_manager.cpp +++ b/yg/resource_manager.cpp @@ -31,32 +31,41 @@ namespace yg return shared_ptr(new TDynamicTexture(m_w, m_h)); } - TStorageFactory::TStorageFactory(size_t vbSize, size_t ibSize, bool useVA, char const * resName) + TStorageFactory::TStorageFactory(size_t vbSize, size_t ibSize, bool useVA, bool isMergeable, char const * resName) : BasePoolElemFactory(resName, vbSize + ibSize), - m_vbSize(vbSize), m_ibSize(ibSize), m_useVA(useVA) + m_vbSize(vbSize), + m_ibSize(ibSize), + m_useVA(useVA), + m_isMergeable(isMergeable) {} gl::Storage const TStorageFactory::Create() { gl::Storage res(m_vbSize, m_ibSize, m_useVA); - res.m_indices->lock(); - res.m_vertices->lock(); + if (m_isMergeable) + { + res.m_indices->lock(); + res.m_vertices->lock(); + } return res; } void TStorageFactory::BeforeMerge(gl::Storage const & e) { - if (e.m_indices->isLocked()) - e.m_indices->unlock(); + if (m_isMergeable) + { + if (e.m_indices->isLocked()) + e.m_indices->unlock(); - e.m_indices->lock(); + e.m_indices->lock(); - if (e.m_vertices->isLocked()) - e.m_vertices->unlock(); + if (e.m_vertices->isLocked()) + e.m_vertices->unlock(); - e.m_vertices->lock(); + e.m_vertices->lock(); + } } ResourceManager::ResourceManager(size_t vbSize, size_t ibSize, size_t storagesCount, @@ -68,14 +77,16 @@ namespace yg size_t glyphCacheSize, size_t glyphCacheCount, RtFormat fmt, - bool useVA) + bool useVA, + bool isMergeable) : m_dynamicTextureWidth(dynamicTexWidth), m_dynamicTextureHeight(dynamicTexHeight), m_fontTextureWidth(fontTexWidth), m_fontTextureHeight(fontTexHeight), m_vbSize(vbSize), m_ibSize(ibSize), m_smallVBSize(smallVBSize), m_smallIBSize(smallIBSize), m_blitVBSize(blitVBSize), m_blitIBSize(blitIBSize), m_format(fmt), - m_useVA(useVA) + m_useVA(useVA), + m_isMergeable(isMergeable) { LOG(LDEBUG, ("allocating ", glyphCacheCount, " glyphCaches, ", glyphCacheSize, " bytes total.")); @@ -85,12 +96,23 @@ namespace yg if (useVA) LOG(LINFO, ("buffer objects are unsupported. using client vertex array instead.")); - m_storages.reset(new TStoragePool(new TStoragePoolTraits(TStorageFactory(vbSize, ibSize, useVA, "primaryStorage"), storagesCount))); - m_smallStorages.reset(new TStoragePool(new TStoragePoolTraits(TStorageFactory(smallVBSize, smallIBSize, useVA, "smallStorage"), smallStoragesCount))); - m_blitStorages.reset(new TStoragePool(new TStoragePoolTraits(TStorageFactory(blitVBSize, blitIBSize, useVA, "blitStorage"), blitStoragesCount))); + if (m_isMergeable) + m_storages.reset(new TMergeableStoragePoolImpl(new TMergeableStoragePoolTraits(TStorageFactory(vbSize, ibSize, useVA, m_isMergeable, "primaryStorage"), storagesCount))); + else + m_storages.reset(new TNonMergeableStoragePoolImpl(new TNonMergeableStoragePoolTraits(TStorageFactory(vbSize, ibSize, useVA, m_isMergeable, "primaryStorage"), storagesCount))); - m_dynamicTextures.reset(new TTexturePool(new TTexturePoolTraits(TTextureFactory(dynamicTexWidth, dynamicTexHeight, "dynamicTexture"), dynamicTexCount))); - m_fontTextures.reset(new TTexturePool(new TTexturePoolTraits(TTextureFactory(fontTexWidth, fontTexHeight, "fontTexture"), fontTexCount))); + if (m_isMergeable) + m_smallStorages.reset(new TMergeableStoragePoolImpl(new TMergeableStoragePoolTraits(TStorageFactory(smallVBSize, smallIBSize, useVA, m_isMergeable, "smallStorage"), smallStoragesCount))); + else + m_smallStorages.reset(new TNonMergeableStoragePoolImpl(new TNonMergeableStoragePoolTraits(TStorageFactory(smallVBSize, smallIBSize, useVA, m_isMergeable, "smallStorage"), smallStoragesCount))); + + if (m_isMergeable) + m_blitStorages.reset(new TMergeableStoragePoolImpl(new TMergeableStoragePoolTraits(TStorageFactory(blitVBSize, blitIBSize, useVA, m_isMergeable, "blitStorage"), blitStoragesCount))); + else + m_blitStorages.reset(new TNonMergeableStoragePoolImpl(new TNonMergeableStoragePoolTraits(TStorageFactory(blitVBSize, blitIBSize, useVA, m_isMergeable, "blitStorage"), blitStoragesCount))); + + m_dynamicTextures.reset(new TTexturePoolImpl(new TTexturePoolTraits(TTextureFactory(dynamicTexWidth, dynamicTexHeight, "dynamicTexture"), dynamicTexCount))); + m_fontTextures.reset(new TTexturePoolImpl(new TTexturePoolTraits(TTextureFactory(fontTexWidth, fontTexHeight, "fontTexture"), fontTexCount))); } void ResourceManager::initMultiBlitStorage(size_t multiBlitVBSize, size_t multiBlitIBSize, size_t multiBlitStoragesCount) @@ -98,7 +120,10 @@ namespace yg m_multiBlitVBSize = multiBlitVBSize; m_multiBlitIBSize = multiBlitIBSize; - m_multiBlitStorages.reset(new TStoragePool(new TStoragePoolTraits(TStorageFactory(multiBlitVBSize, multiBlitIBSize, m_useVA, "multiBlitStorage"), multiBlitStoragesCount))); + if (m_isMergeable) + m_multiBlitStorages.reset(new TMergeableStoragePoolImpl(new TMergeableStoragePoolTraits(TStorageFactory(multiBlitVBSize, multiBlitIBSize, m_useVA, m_isMergeable, "multiBlitStorage"), multiBlitStoragesCount))); + else + m_multiBlitStorages.reset(new TNonMergeableStoragePoolImpl(new TNonMergeableStoragePoolTraits(TStorageFactory(multiBlitVBSize, multiBlitIBSize, m_useVA, m_isMergeable, "multiBlitStorage"), multiBlitStoragesCount))); } void ResourceManager::initTinyStorage(size_t tinyVBSize, size_t tinyIBSize, size_t tinyStoragesCount) @@ -106,7 +131,10 @@ namespace yg m_tinyVBSize = tinyVBSize; m_tinyIBSize = tinyIBSize; - m_tinyStorages.reset(new TStoragePool(new TStoragePoolTraits(TStorageFactory(tinyVBSize, tinyIBSize, m_useVA, "tinyStorage"), tinyStoragesCount))); + if (m_isMergeable) + m_tinyStorages.reset(new TMergeableStoragePoolImpl(new TMergeableStoragePoolTraits(TStorageFactory(tinyVBSize, tinyIBSize, m_useVA, m_isMergeable, "tinyStorage"), tinyStoragesCount))); + else + m_tinyStorages.reset(new TNonMergeableStoragePoolImpl(new TNonMergeableStoragePoolTraits(TStorageFactory(tinyVBSize, tinyIBSize, m_useVA, m_isMergeable, "tinyStorage"), tinyStoragesCount))); } void ResourceManager::initRenderTargets(size_t renderTargetWidth, size_t renderTargetHeight, size_t renderTargetsCount) @@ -114,7 +142,7 @@ namespace yg m_renderTargetWidth = renderTargetWidth; m_renderTargetHeight = renderTargetHeight; - m_renderTargets.reset(new TTexturePool(new TTexturePoolTraits(TTextureFactory(renderTargetWidth, renderTargetHeight, "renderTargets"), renderTargetsCount))); + m_renderTargets.reset(new TTexturePoolImpl(new TTexturePoolTraits(TTextureFactory(renderTargetWidth, renderTargetHeight, "renderTargets"), renderTargetsCount))); } void ResourceManager::initStyleCacheTextures(size_t styleCacheTextureWidth, size_t styleCacheTextureHeight, size_t styleCacheTexturesCount) @@ -122,7 +150,7 @@ namespace yg m_styleCacheTextureWidth = styleCacheTextureWidth; m_styleCacheTextureHeight = styleCacheTextureHeight; - m_styleCacheTextures.reset(new TTexturePool(new TTexturePoolTraits(TTextureFactory(styleCacheTextureWidth, styleCacheTextureHeight, "styleCacheTextures"), styleCacheTexturesCount))); + m_styleCacheTextures.reset(new TTexturePoolImpl(new TTexturePoolTraits(TTextureFactory(styleCacheTextureWidth, styleCacheTextureHeight, "styleCacheTextures"), styleCacheTexturesCount))); } shared_ptr const & ResourceManager::getTexture(string const & fileName) diff --git a/yg/resource_manager.hpp b/yg/resource_manager.hpp index 5fa2c69f1d..4962072cb1 100644 --- a/yg/resource_manager.hpp +++ b/yg/resource_manager.hpp @@ -43,7 +43,8 @@ namespace yg size_t m_vbSize; size_t m_ibSize; bool m_useVA; - TStorageFactory(size_t vbSize, size_t ibSize, bool useVA, char const * resName); + bool m_isMergeable; + TStorageFactory(size_t vbSize, size_t ibSize, bool useVA, bool isMergeable, char const * resName); gl::Storage const Create(); void BeforeMerge(gl::Storage const & e); }; @@ -54,12 +55,20 @@ namespace yg typedef BasePoolTraits, TTextureFactory> TBaseTexturePoolTraits; typedef FixedSizePoolTraits TTexturePoolTraits; - typedef ResourcePool TTexturePool; + typedef ResourcePoolImpl TTexturePoolImpl; + + typedef ResourcePool > TTexturePool; typedef BasePoolTraits TBaseStoragePoolTraits; typedef SeparateFreePoolTraits TSeparateFreeStoragePoolTraits; - typedef FixedSizePoolTraits TStoragePoolTraits; - typedef ResourcePool TStoragePool; + + typedef FixedSizePoolTraits TMergeableStoragePoolTraits; + typedef ResourcePoolImpl TMergeableStoragePoolImpl; + + typedef FixedSizePoolTraits TNonMergeableStoragePoolTraits; + typedef ResourcePoolImpl TNonMergeableStoragePoolImpl; + + typedef ResourcePool TStoragePool; private: @@ -115,6 +124,7 @@ namespace yg RtFormat m_format; bool m_useVA; + bool m_isMergeable; public: @@ -127,7 +137,8 @@ namespace yg size_t glyphCacheSize, size_t glyphCacheCount, RtFormat fmt, - bool useVA); + bool useVA, + bool isMergeable); void initMultiBlitStorage(size_t multiBlitVBSize, size_t multiBlitIBSize, size_t multiBlitStoragesCount); void initRenderTargets(size_t renderTargetWidth, size_t renderTargetHeight, size_t renderTargetCount); diff --git a/yg/yg_tests/skin_loader_test.cpp b/yg/yg_tests/skin_loader_test.cpp index a7172e246d..d951590a50 100644 --- a/yg/yg_tests/skin_loader_test.cpp +++ b/yg/yg_tests/skin_loader_test.cpp @@ -8,6 +8,6 @@ UNIT_TEST(SkinLoaderTest_Main) { GL_TEST_START; - shared_ptr rm(new yg::ResourceManager(1000, 1000, 2, 1000, 1000, 2, 1000, 1000, 2, 128, 128, 15, 256, 256, 5, "unicode_blocks.txt", "fonts_whitelist.txt", "fonts_blacklist.txt", 2 * 1024 * 1024, 3, yg::Rt8Bpp, false)); + shared_ptr rm(new yg::ResourceManager(1000, 1000, 2, 1000, 1000, 2, 1000, 1000, 2, 128, 128, 15, 256, 256, 5, "unicode_blocks.txt", "fonts_whitelist.txt", "fonts_blacklist.txt", 2 * 1024 * 1024, 3, yg::Rt8Bpp, false, false)); loadSkin(rm, "basic.skn", 2, 2); }; diff --git a/yg/yg_tests/skin_test.cpp b/yg/yg_tests/skin_test.cpp index 74f1890069..2ac25dc4ce 100644 --- a/yg/yg_tests/skin_test.cpp +++ b/yg/yg_tests/skin_test.cpp @@ -10,7 +10,7 @@ UNIT_TEST(SkinTest_Main) { GL_TEST_START; - shared_ptr rm(new yg::ResourceManager(100, 100, 1, 100, 100, 1, 100, 100, 1, 128, 128, 15, 256, 256, 5, "unicode_blocks.txt", "fonts_whitelist.txt", "fonts_blacklist.txt", 2 * 1024 * 1024, 3, yg::Rt8Bpp, false)); + shared_ptr rm(new yg::ResourceManager(100, 100, 1, 100, 100, 1, 100, 100, 1, 128, 128, 15, 256, 256, 5, "unicode_blocks.txt", "fonts_whitelist.txt", "fonts_blacklist.txt", 2 * 1024 * 1024, 3, yg::Rt8Bpp, false, false)); yg::Skin * skin = loadSkin(rm, "basic.skn", 2, 2); double p0 [] = {1, 1};