1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright 2019-2021 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#pragma once
#include <mutex>
#include <vector>
#include <unordered_map>
#include <atomic>
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h"
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h"
#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp"
#include "../../../DiligentCore/Common/interface/ObjectBase.hpp"
#include "../../../DiligentCore/Graphics/GraphicsTools/interface/BufferSuballocator.h"
#include "../../../DiligentCore/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h"
namespace Diligent
{
namespace GLTF
{
/// GLTF resource manager
class ResourceManager final : public ObjectBase<IObject>
{
public:
using TBase = ObjectBase<IObject>;
struct CreateInfo
{
const BufferSuballocatorCreateInfo* BuffSuballocators = nullptr; // [NumBuffSuballocators]
const DynamicTextureAtlasCreateInfo* TexAtlases = nullptr; // [NumTexAtlases]
Uint32 NumBuffSuballocators = 0;
Uint32 NumTexAtlases = 0;
DynamicTextureAtlasCreateInfo DefaultAtlasDesc;
};
static RefCntAutoPtr<ResourceManager> Create(IRenderDevice* pDevice,
const CreateInfo& CI);
RefCntAutoPtr<IBufferSuballocation> AllocateBufferSpace(Uint32 BufferIndex,
Uint32 Size,
Uint32 Alignment)
{
RefCntAutoPtr<IBufferSuballocation> pSuballoc;
m_BufferSuballocators[BufferIndex]->Allocate(Size, Alignment, &pSuballoc);
return pSuballoc;
}
RefCntAutoPtr<ITextureAtlasSuballocation> AllocateTextureSpace(TEXTURE_FORMAT Fmt,
Uint32 Width,
Uint32 Height,
const char* CacheId = nullptr,
IObject* pUserData = nullptr);
RefCntAutoPtr<ITextureAtlasSuballocation> FindAllocation(const char* CacheId);
Uint32 GetTextureVersion()
{
Uint32 Version = 0;
std::lock_guard<std::mutex> Lock{m_AtlasesMtx};
for (auto atlas_it : m_Atlases)
Version += atlas_it.second->GetVersion();
return Version;
}
Uint32 GetBufferVersion(Uint32 Index) const
{
return m_BufferSuballocators[Index]->GetVersion();
}
IBuffer* GetBuffer(Uint32 Index, IRenderDevice* pDevice, IDeviceContext* pContext)
{
return m_BufferSuballocators[Index]->GetBuffer(pDevice, pContext);
}
ITexture* GetTexture(TEXTURE_FORMAT Fmt, IRenderDevice* pDevice, IDeviceContext* pContext)
{
decltype(m_Atlases)::iterator cache_it; // NB: can't initialize it without locking the mutex
{
std::lock_guard<std::mutex> Lock{m_AtlasesMtx};
cache_it = m_Atlases.find(Fmt);
if (cache_it == m_Atlases.end())
return nullptr;
}
return cache_it->second->GetTexture(pDevice, pContext);
}
// NB: can't return reference here!
TextureDesc GetAtlasDesc(TEXTURE_FORMAT Fmt)
{
{
std::lock_guard<std::mutex> Lock{m_AtlasesMtx};
auto cache_it = m_Atlases.find(Fmt);
if (cache_it != m_Atlases.end())
return cache_it->second->GetAtlasDesc();
}
// Atlas is not present in the map - use default description
TextureDesc Desc = m_DefaultAtlasDesc.Desc;
Desc.Format = Fmt;
return Desc;
}
private:
template <typename AllocatorType, typename ObjectType>
friend class Diligent::MakeNewRCObj;
ResourceManager(IReferenceCounters* pRefCounters,
IRenderDevice* pDevice,
const CreateInfo& CI);
std::vector<RefCntAutoPtr<IBufferSuballocator>> m_BufferSuballocators;
DynamicTextureAtlasCreateInfo m_DefaultAtlasDesc;
const std::string m_DefaultAtlasName;
using AtlasesHashMapType = std::unordered_map<TEXTURE_FORMAT, RefCntAutoPtr<IDynamicTextureAtlas>, std::hash<Uint32>>;
std::mutex m_AtlasesMtx;
AtlasesHashMapType m_Atlases;
using TexAllocationsHashMapType = std::unordered_map<std::string, RefCntWeakPtr<ITextureAtlasSuballocation>>;
std::mutex m_TexAllocationsMtx;
TexAllocationsHashMapType m_TexAllocations;
};
} // namespace GLTF
} // namespace Diligent
|