From 212c9d56ecea034ba637164a54847b24bcc82fbd Mon Sep 17 00:00:00 2001 From: assiduous Date: Fri, 6 Nov 2020 01:30:57 -0800 Subject: Updated ResourceMappingImpl --- .../GraphicsEngine/include/ResourceMappingImpl.hpp | 102 +++++++++------------ .../GraphicsEngine/src/ResourceMappingBase.cpp | 13 +-- 2 files changed, 48 insertions(+), 67 deletions(-) (limited to 'Graphics/GraphicsEngine') diff --git a/Graphics/GraphicsEngine/include/ResourceMappingImpl.hpp b/Graphics/GraphicsEngine/include/ResourceMappingImpl.hpp index b64c8804..ad70ae9d 100644 --- a/Graphics/GraphicsEngine/include/ResourceMappingImpl.hpp +++ b/Graphics/GraphicsEngine/include/ResourceMappingImpl.hpp @@ -40,60 +40,7 @@ namespace Diligent { -struct ResMappingHashKey -{ - ResMappingHashKey(const Char* Str, bool bMakeCopy, Uint32 ArrInd) : - StrKey{Str, bMakeCopy}, - ArrayIndex{ArrInd} - { - } - - ResMappingHashKey(ResMappingHashKey&& rhs) : - StrKey{std::move(rhs.StrKey)}, - ArrayIndex{rhs.ArrayIndex} - {} - - bool operator==(const ResMappingHashKey& RHS) const - { - return StrKey == RHS.StrKey && ArrayIndex == RHS.ArrayIndex; - } - - size_t GetHash() const - { - if (Hash == 0) - { - Hash = ComputeHash(StrKey.GetHash(), ArrayIndex); - } - - return Hash; - } - - // clang-format off - ResMappingHashKey ( const ResMappingHashKey& ) = delete; - ResMappingHashKey& operator = ( const ResMappingHashKey& ) = delete; - ResMappingHashKey& operator = ( ResMappingHashKey&& ) = delete; - // clang-format on - HashMapStringKey StrKey; - Uint32 ArrayIndex; - mutable size_t Hash = 0; -}; -} // namespace Diligent - -namespace std -{ -template <> -struct hash -{ - size_t operator()(const Diligent::ResMappingHashKey& Key) const - { - return Key.GetHash(); - } -}; -} // namespace std - -namespace Diligent -{ class FixedBlockMemoryAllocator; /// Implementation of the resource mapping @@ -105,13 +52,13 @@ public: /// \param pRefCounters - reference counters object that controls the lifetime of this resource mapping /// \param RawMemAllocator - raw memory allocator that is used by the m_HashTable member ResourceMappingImpl(IReferenceCounters* pRefCounters, IMemoryAllocator& RawMemAllocator) : - TObjectBase(pRefCounters), - m_HashTable(STD_ALLOCATOR_RAW_MEM(HashTableElem, RawMemAllocator, "Allocator for unordered_map< ResMappingHashKey, RefCntAutoPtr >")) + TObjectBase{pRefCounters}, + m_HashTable{STD_ALLOCATOR_RAW_MEM(HashTableElem, RawMemAllocator, "Allocator for unordered_map>")} {} ~ResourceMappingImpl(); - virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final; + IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ResourceMapping, TObjectBase) /// Implementation of IResourceMapping::AddResource() virtual void DILIGENT_CALL_TYPE AddResource(const Char* Name, @@ -137,10 +84,47 @@ public: virtual size_t DILIGENT_CALL_TYPE GetSize() override final; private: + struct ResMappingHashKey : public HashMapStringKey + { + using TBase = HashMapStringKey; + + ResMappingHashKey(const Char* Str, bool bMakeCopy, Uint32 ArrInd) : + HashMapStringKey{Str, bMakeCopy}, + ArrayIndex{ArrInd} + { + Ownership_Hash = (ComputeHash(GetHash(), ArrInd) & HashMask) | (Ownership_Hash & StrOwnershipMask); + } + + ResMappingHashKey(ResMappingHashKey&& rhs) : + HashMapStringKey{std::move(rhs)}, + ArrayIndex{rhs.ArrayIndex} + {} + + // clang-format off + ResMappingHashKey ( const ResMappingHashKey& ) = delete; + ResMappingHashKey& operator = ( const ResMappingHashKey& ) = delete; + ResMappingHashKey& operator = ( ResMappingHashKey&& ) = delete; + // clang-format on + + bool operator==(const ResMappingHashKey& RHS) const + { + return static_cast(*this) == static_cast(RHS) && ArrayIndex == RHS.ArrayIndex; + } + + const Uint32 ArrayIndex; + }; + ThreadingTools::LockHelper Lock(); - ThreadingTools::LockFlag m_LockFlag; - typedef std::pair> HashTableElem; - std::unordered_map, std::hash, std::equal_to, STDAllocatorRawMem> m_HashTable; + ThreadingTools::LockFlag m_LockFlag; + + using HashTableElem = std::pair>; + std::unordered_map, + ResMappingHashKey::Hasher, + std::equal_to, + STDAllocatorRawMem> + m_HashTable; }; + } // namespace Diligent diff --git a/Graphics/GraphicsEngine/src/ResourceMappingBase.cpp b/Graphics/GraphicsEngine/src/ResourceMappingBase.cpp index 5ab951b4..859dcf88 100644 --- a/Graphics/GraphicsEngine/src/ResourceMappingBase.cpp +++ b/Graphics/GraphicsEngine/src/ResourceMappingBase.cpp @@ -30,12 +30,11 @@ namespace Diligent { + ResourceMappingImpl::~ResourceMappingImpl() { } -IMPLEMENT_QUERY_INTERFACE(ResourceMappingImpl, IID_ResourceMapping, TObjectBase) - ThreadingTools::LockHelper ResourceMappingImpl::Lock() { return ThreadingTools::LockHelper(m_LockFlag); @@ -52,10 +51,7 @@ void ResourceMappingImpl::AddResourceArray(const Char* Name, Uint32 StartIndex, auto* pObject = ppObjects[Elem]; // Try to construct new element in place - auto Elems = - m_HashTable.emplace( - std::make_pair(Diligent::ResMappingHashKey(Name, true, StartIndex + Elem), // Make a copy of the source string - Diligent::RefCntAutoPtr(pObject))); + auto Elems = m_HashTable.emplace(ResMappingHashKey{Name, true /*Make copy*/, StartIndex + Elem}, pObject); // If there is already element with the same name, replace it if (!Elems.second && Elems.first->second != pObject) { @@ -85,7 +81,7 @@ void ResourceMappingImpl::RemoveResourceByName(const Char* Name, Uint32 ArrayInd auto LockHelper = Lock(); // Remove object with the given name // Name will be implicitly converted to HashMapStringKey without making a copy - m_HashTable.erase(ResMappingHashKey(Name, false, ArrayIndex)); + m_HashTable.erase(ResMappingHashKey{Name, false, ArrayIndex}); } void ResourceMappingImpl::GetResource(const Char* Name, IDeviceObject** ppResource, Uint32 ArrayIndex) @@ -105,7 +101,7 @@ void ResourceMappingImpl::GetResource(const Char* Name, IDeviceObject** ppResour // Find an object with the requested name // Name will be implicitly converted to HashMapStringKey without making a copy - auto It = m_HashTable.find(ResMappingHashKey(Name, false, ArrayIndex)); + auto It = m_HashTable.find(ResMappingHashKey{Name, false, ArrayIndex}); if (It != m_HashTable.end()) { *ppResource = It->second.RawPtr(); @@ -118,4 +114,5 @@ size_t ResourceMappingImpl::GetSize() { return m_HashTable.size(); } + } // namespace Diligent -- cgit v1.2.3