Diligent Engine  v.2.4.g
ResourceMappingImpl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include <unordered_map>
34 
35 #include "ResourceMapping.h"
36 #include "ObjectBase.hpp"
37 #include "HashUtils.hpp"
38 #include "STDAllocator.hpp"
39 #include "RefCntAutoPtr.hpp"
40 
41 namespace Diligent
42 {
43 
44 class FixedBlockMemoryAllocator;
45 
47 class ResourceMappingImpl : public ObjectBase<IResourceMapping>
48 {
49 public:
51 
54  ResourceMappingImpl(IReferenceCounters* pRefCounters, IMemoryAllocator& RawMemAllocator) :
55  TObjectBase{pRefCounters},
56  m_HashTable{STD_ALLOCATOR_RAW_MEM(HashTableElem, RawMemAllocator, "Allocator for unordered_map<ResMappingHashKey, RefCntAutoPtr<IDeviceObject>>")}
57  {}
58 
60 
62 
63 
64  virtual void DILIGENT_CALL_TYPE AddResource(const Char* Name,
65  IDeviceObject* pObject,
66  bool bIsUnique) override final;
67 
69  virtual void DILIGENT_CALL_TYPE AddResourceArray(const Char* Name,
70  Uint32 StartIndex,
71  IDeviceObject* const* ppObjects,
72  Uint32 NumElements,
73  bool bIsUnique) override final;
74 
76  virtual void DILIGENT_CALL_TYPE RemoveResourceByName(const Char* Name, Uint32 ArrayIndex) override final;
77 
79  virtual void DILIGENT_CALL_TYPE GetResource(const Char* Name,
80  IDeviceObject** ppResource,
81  Uint32 ArrayIndex) override final;
82 
84  virtual size_t DILIGENT_CALL_TYPE GetSize() override final;
85 
86 private:
87  struct ResMappingHashKey : public HashMapStringKey
88  {
89  using TBase = HashMapStringKey;
90 
91  ResMappingHashKey(const Char* Str, bool bMakeCopy, Uint32 ArrInd) noexcept :
92  HashMapStringKey{Str, bMakeCopy},
93  ArrayIndex{ArrInd}
94  {
95  Ownership_Hash = (ComputeHash(GetHash(), ArrInd) & HashMask) | (Ownership_Hash & StrOwnershipMask);
96  }
97 
98  ResMappingHashKey(ResMappingHashKey&& rhs) noexcept :
99  HashMapStringKey{std::move(rhs)},
100  ArrayIndex{rhs.ArrayIndex}
101  {}
102 
103  // clang-format off
104  ResMappingHashKey ( const ResMappingHashKey& ) = delete;
105  ResMappingHashKey& operator = ( const ResMappingHashKey& ) = delete;
106  ResMappingHashKey& operator = ( ResMappingHashKey&& ) = delete;
107  // clang-format on
108 
109  bool operator==(const ResMappingHashKey& RHS) const
110  {
111  if (ArrayIndex != RHS.ArrayIndex)
112  {
113  // We must check array index first because TBase::operator==()
114  // expects that if the hashes are different, the strings must be
115  // different too. This will not be the case for different array
116  // elements of the same variable.
117  return false;
118  }
119 
120  return static_cast<const TBase&>(*this) == static_cast<const TBase&>(RHS);
121  }
122 
123  const Uint32 ArrayIndex;
124  };
125 
127 
128  ThreadingTools::LockFlag m_LockFlag;
129 
130  using HashTableElem = std::pair<const ResMappingHashKey, RefCntAutoPtr<IDeviceObject>>;
131  std::unordered_map<ResMappingHashKey,
132  RefCntAutoPtr<IDeviceObject>,
133  ResMappingHashKey::Hasher,
134  std::equal_to<ResMappingHashKey>,
135  STDAllocatorRawMem<HashTableElem>>
136  m_HashTable;
137 };
138 
139 } // namespace Diligent
ObjectBase.hpp
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
Diligent::ResourceMappingImpl::RemoveResourceByName
virtual void RemoveResourceByName(const Char *Name, Uint32 ArrayIndex) override final
Implementation of IResourceMapping::RemoveResourceByName()
Definition: ResourceMappingBase.cpp:76
ThreadingTools::LockHelper
Definition: LockHelper.hpp:59
Diligent::ResourceMappingImpl::GetResource
virtual void GetResource(const Char *Name, IDeviceObject **ppResource, Uint32 ArrayIndex) override final
Implementation of IResourceMapping::GetResource()
Definition: ResourceMappingBase.cpp:87
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::ResourceMappingImpl::ResourceMappingImpl
ResourceMappingImpl(IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator)
Definition: ResourceMappingImpl.hpp:54
ThreadingTools::LockFlag
Definition: LockHelper.hpp:36
STDAllocator.hpp
Diligent::operator==
bool operator==(const Plane3D &p1, const Plane3D &p2)
Definition: AdvancedMath.hpp:442
Diligent::ResourceMappingImpl::AddResourceArray
virtual void AddResourceArray(const Char *Name, Uint32 StartIndex, IDeviceObject *const *ppObjects, Uint32 NumElements, bool bIsUnique) override final
Implementation of IResourceMapping::AddResourceArray()
Definition: ResourceMappingBase.cpp:43
Diligent::ObjectBase
Template class implementing base functionality for an object.
Definition: ObjectBase.hpp:66
IMPLEMENT_QUERY_INTERFACE_IN_PLACE
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
Diligent::ResourceMappingImpl::AddResource
virtual void AddResource(const Char *Name, IDeviceObject *pObject, bool bIsUnique) override final
Implementation of IResourceMapping::AddResource()
Definition: ResourceMappingBase.cpp:71
ResourceMapping.h
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ComputeHash
std::size_t ComputeHash(const ArgsType &... Args)
Definition: HashUtils.hpp:57
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
HashUtils.hpp
STD_ALLOCATOR_RAW_MEM
#define STD_ALLOCATOR_RAW_MEM(Type, Allocator, Description)
Definition: STDAllocator.hpp:179
Diligent::ResourceMappingImpl::~ResourceMappingImpl
~ResourceMappingImpl()
Definition: ResourceMappingBase.cpp:34
RefCntAutoPtr.hpp
Diligent::ResourceMappingImpl::TObjectBase
ObjectBase< IResourceMapping > TObjectBase
Definition: ResourceMappingImpl.hpp:50
Diligent::ResourceMappingImpl
Implementation of the resource mapping.
Definition: ResourceMappingImpl.hpp:47
Diligent::ResourceMappingImpl::GetSize
virtual size_t GetSize() override final
Returns number of resources in the resource mapping.
Definition: ResourceMappingBase.cpp:113
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37