Diligent Engine API Reference
ResourceMappingImpl.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 
29 #include "ResourceMapping.h"
30 #include "ObjectBase.h"
31 #include <unordered_map>
32 #include "HashUtils.h"
33 #include "STDAllocator.h"
34 
35 namespace Diligent
36 {
37  struct ResMappingHashKey
38  {
39  ResMappingHashKey(const Char* Str, bool bMakeCopy, Uint32 ArrInd):
40  StrKey(Str, bMakeCopy),
41  ArrayIndex(ArrInd)
42  {
43  }
44 
45  ResMappingHashKey(ResMappingHashKey &&rhs) :
46  StrKey(std::move(rhs.StrKey)),
47  ArrayIndex(rhs.ArrayIndex)
48  {}
49 
50  bool operator == (const ResMappingHashKey& RHS)const
51  {
52  return StrKey == RHS.StrKey && ArrayIndex == RHS.ArrayIndex;
53  }
54 
55  size_t GetHash()const
56  {
57  if( Hash == 0 )
58  {
59  Hash = ComputeHash( StrKey, ArrayIndex );
60  }
61 
62  return Hash;
63  }
64 
65 
66  ResMappingHashKey( const ResMappingHashKey& ) = delete;
67  ResMappingHashKey& operator = ( const ResMappingHashKey& ) = delete;
68  ResMappingHashKey& operator = ( ResMappingHashKey&& ) = delete;
69 
70  HashMapStringKey StrKey;
71  Uint32 ArrayIndex;
72  mutable size_t Hash = 0;
73  };
74 }
75 
76 namespace std
77 {
78  template<>
79  struct hash<Diligent::ResMappingHashKey>
80  {
81  size_t operator()( const Diligent::ResMappingHashKey &Key ) const
82  {
83  return Key.GetHash();
84  }
85  };
86 }
87 
88 namespace Diligent
89 {
90  class FixedBlockMemoryAllocator;
91 
93  class ResourceMappingImpl : public ObjectBase<IResourceMapping>
94  {
95  public:
97 
100  ResourceMappingImpl(IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator) :
101  TObjectBase(pRefCounters),
102  m_HashTable(STD_ALLOCATOR_RAW_MEM(HashTableElem, RawMemAllocator, "Allocator for unordered_map< ResMappingHashKey, RefCntAutoPtr<IDeviceObject> >") )
103  {}
104 
106 
107  virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
108 
110  virtual void AddResource( const Char *Name, IDeviceObject *pObject, bool bIsUnique )override final;
111 
113  virtual void AddResourceArray( const Char *Name, Uint32 StartIndex, IDeviceObject * const* ppObjects, Uint32 NumElements, bool bIsUnique )override final;
114 
116  virtual void RemoveResourceByName( const Char *Name, Uint32 ArrayIndex )override final;
117 
119  virtual void GetResource( const Char *Name, IDeviceObject **ppResource, Uint32 ArrayIndex )override final;
120 
122  virtual size_t GetSize()override final;
123 
124  private:
125 
126  ThreadingTools::LockHelper Lock();
127 
128  ThreadingTools::LockFlag m_LockFlag;
129  typedef std::pair<const ResMappingHashKey, RefCntAutoPtr<IDeviceObject> > HashTableElem;
130  std::unordered_map< ResMappingHashKey, RefCntAutoPtr<IDeviceObject>, std::hash<ResMappingHashKey>, std::equal_to<ResMappingHashKey>, STDAllocatorRawMem<HashTableElem> > m_HashTable;
131  };
132 }
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:40
Template class implementing base functionality for an object.
Definition: ObjectBase.h:62
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Definition: AdvancedMath.h:316
ResourceMappingImpl(IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator)
Definition: ResourceMappingImpl.h:100
virtual void QueryInterface(const Diligent::INTERFACE_ID &IID, IObject **ppInterface) override final
Queries the specific interface, see IObject::QueryInterface() for details.
virtual size_t GetSize() override final
Returns number of resources in the resource mapping.
Definition: ResourceMapping.cpp:117
virtual void RemoveResourceByName(const Char *Name, Uint32 ArrayIndex) override final
Implementation of IResourceMapping::RemoveResourceByName()
Definition: ResourceMapping.cpp:80
virtual void AddResource(const Char *Name, IDeviceObject *pObject, bool bIsUnique) override final
Implementation of IResourceMapping::AddResource()
Definition: ResourceMapping.cpp:75
Template class that implements reference counting.
Definition: RefCntAutoPtr.h:71
virtual void GetResource(const Char *Name, IDeviceObject **ppResource, Uint32 ArrayIndex) override final
Implementation of IResourceMapping::GetResource()
Definition: ResourceMapping.cpp:91
Implementation of the resource mapping.
Definition: ResourceMappingImpl.h:93
virtual void AddResourceArray(const Char *Name, Uint32 StartIndex, IDeviceObject *const *ppObjects, Uint32 NumElements, bool bIsUnique) override final
Implementation of IResourceMapping::AddResourceArray()
Definition: ResourceMapping.cpp:43
Definition: LockHelper.h:32