Diligent Engine  v.2.4.g
DynamicAtlasManager.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 
29 #pragma once
30 
33 
34 #include <map>
35 #include <unordered_map>
36 
37 #include "../../../Primitives/interface/BasicTypes.h"
38 #include "../../../Common/interface/HashUtils.hpp"
39 
40 namespace Diligent
41 {
42 
45 {
46 public:
47  struct Region
48  {
49  Uint32 x = 0;
50  Uint32 y = 0;
51 
54 
55  Region() = default;
56 
57  // clang-format off
58  Region (const Region&) = default;
59  Region ( Region&&) = default;
60  Region& operator=(const Region&) = default;
61  Region& operator=( Region&&) = default;
62  // clang-format on
63 
64  Region(Uint32 _x, Uint32 _y, Uint32 _width, Uint32 _height) :
65  // clang-format off
66  x {_x},
67  y {_y},
68  width {_width},
69  height{_height}
70  // clang-format on
71  {}
72 
73  bool IsEmpty() const
74  {
75  return width == 0 || height == 0;
76  }
77 
78  bool operator==(const Region& rhs) const
79  {
80  // clang-format off
81  return x == rhs.x &&
82  y == rhs.y &&
83  width == rhs.width &&
84  height == rhs.height;
85  // clang-format on
86  }
87  bool operator!=(const Region& rhs) const
88  {
89  return !(*this == rhs);
90  }
91 
92  struct Hasher
93  {
94  size_t operator()(const Region& R) const
95  {
96  return ComputeHash(R.width, R.height, R.x, R.y);
97  }
98  };
99  };
100 
101  DynamicAtlasManager(Uint32 Width, Uint32 Height);
103 
104  // clang-format off
105  DynamicAtlasManager (const DynamicAtlasManager&) = delete;
109  // clang-format on
110 
111  Region Allocate(Uint32 Width, Uint32 Height);
112  void Free(Region&& R);
113 
115  {
116  VERIFY_EXPR(m_FreeRegionsByWidth.size() == m_FreeRegionsByHeight.size());
117  return static_cast<Uint32>(m_FreeRegionsByWidth.size());
118  }
119 
120 
121 #define CMP(Member) \
122  if (R0.Member < R1.Member) \
123  return true; \
124  else if (R0.Member > R1.Member) \
125  return false;
126 
128  {
129  bool operator()(const Region& R0, const Region& R1) const
130  {
131  CMP(width)
132  CMP(height)
133  CMP(x)
134  CMP(y)
135  return false;
136  }
137  };
139  {
140  bool operator()(const Region& R0, const Region& R1) const
141  {
142  CMP(height)
143  CMP(width)
144  CMP(y)
145  CMP(x)
146  return false;
147  }
148  };
149 #undef CMP
150 
151 private:
152 #if DILIGENT_DEBUG
153  void DbgVerifyRegion(const Region& R) const;
154  void DbgVerifyConsistency() const;
155  struct Node;
156  void DbgRecursiveVerifyConsistency(const Node& N, Uint32& Area) const;
157 #endif
158 
159  const Uint32 m_Width;
160  const Uint32 m_Height;
161 
162  struct Node
163  {
164  Region R;
165  bool IsAllocated = false;
166  Node* Parent = nullptr;
167 
168  void Split(const std::initializer_list<Region>& Regions);
169  bool CanMergeChildren() const;
170  void MergeChildren();
171  bool HasChildren() const
172  {
173  VERIFY_EXPR(NumChildren == 0 && !Children || NumChildren != 0 && Children);
174  VERIFY(!IsAllocated || NumChildren == 0, "Allocated nodes can't have children");
175  return NumChildren != 0;
176  }
177 
178  const Node& Child(Uint32 i) const
179  {
180  VERIFY_EXPR(i < NumChildren);
181  return Children[i];
182  }
183  Node& Child(Uint32 i)
184  {
185  VERIFY_EXPR(i < NumChildren);
186  return Children[i];
187  }
188 
189  template <typename ProcessChildType>
190  void ProcessChildren(ProcessChildType ProcessChild) const
191  {
192  for (Uint32 i = 0; i < NumChildren; ++i)
193  ProcessChild(Child(i));
194  }
195  template <typename ProcessChildType>
196  void ProcessChildren(ProcessChildType ProcessChild)
197  {
198  for (Uint32 i = 0; i < NumChildren; ++i)
199  ProcessChild(Child(i));
200  }
201 
202 #if DILIGENT_DEBUG
203  void Validate() const;
204 #endif
205  private:
206  Uint32 NumChildren = 0;
207  std::unique_ptr<Node[]> Children;
208  };
209  std::unique_ptr<Node> m_Root{new Node};
210 
211  void RegisterNode(Node& N);
212  void UnregisterNode(const Node& N);
213 
214  // Free regions ordered by width->height->x->y
215  std::map<Region, Node*, WidthFirstCompare> m_FreeRegionsByWidth;
216  // Free regions ordered by height->width->y->x
217  std::map<Region, Node*, HeightFirstCompare> m_FreeRegionsByHeight;
218  // Allocated regions
219  std::unordered_map<Region, Node*, Region::Hasher> m_AllocatedRegions;
220 };
221 
222 } // namespace Diligent
Diligent::DynamicAtlasManager::DynamicAtlasManager
DynamicAtlasManager(Uint32 Width, Uint32 Height)
Definition: DynamicAtlasManager.cpp:113
Diligent::DynamicAtlasManager
Dynamic 2D atlas manager.
Definition: DynamicAtlasManager.hpp:44
Diligent::DynamicAtlasManager::HeightFirstCompare::operator()
bool operator()(const Region &R0, const Region &R1) const
Definition: DynamicAtlasManager.hpp:140
Diligent::DynamicAtlasManager::WidthFirstCompare::operator()
bool operator()(const Region &R0, const Region &R1) const
Definition: DynamicAtlasManager.hpp:129
Diligent::DynamicAtlasManager::Region::y
Uint32 y
Definition: DynamicAtlasManager.hpp:50
Diligent::DynamicAtlasManager::Region
Definition: DynamicAtlasManager.hpp:47
Diligent::DynamicAtlasManager::Region::x
Uint32 x
Definition: DynamicAtlasManager.hpp:49
Diligent::DynamicAtlasManager::GetFreeRegionCount
Uint32 GetFreeRegionCount() const
Definition: DynamicAtlasManager.hpp:114
Diligent::DynamicAtlasManager::Region::Hasher
Definition: DynamicAtlasManager.hpp:92
Diligent::DynamicAtlasManager::Region::Region
Region(Uint32 _x, Uint32 _y, Uint32 _width, Uint32 _height)
Definition: DynamicAtlasManager.hpp:64
Diligent::DynamicAtlasManager::Region::operator==
bool operator==(const Region &rhs) const
Definition: DynamicAtlasManager.hpp:78
Diligent::DynamicAtlasManager::~DynamicAtlasManager
~DynamicAtlasManager()
Definition: DynamicAtlasManager.cpp:122
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::DynamicAtlasManager::operator=
DynamicAtlasManager & operator=(const DynamicAtlasManager &)=delete
Diligent::ComputeHash
std::size_t ComputeHash(const ArgsType &... Args)
Definition: HashUtils.hpp:57
Diligent::DynamicAtlasManager::Allocate
Region Allocate(Uint32 Width, Uint32 Height)
Definition: DynamicAtlasManager.cpp:184
Diligent::DynamicAtlasManager::WidthFirstCompare
Definition: DynamicAtlasManager.hpp:127
CMP
#define CMP(Member)
Definition: DynamicAtlasManager.hpp:121
Diligent::DynamicAtlasManager::Region::width
Uint32 width
Definition: DynamicAtlasManager.hpp:52
Diligent::DynamicAtlasManager::HeightFirstCompare
Definition: DynamicAtlasManager.hpp:138
Diligent::DynamicAtlasManager::Region::IsEmpty
bool IsEmpty() const
Definition: DynamicAtlasManager.hpp:73
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::DynamicAtlasManager::Region::operator=
Region & operator=(const Region &)=default
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::DynamicAtlasManager::Region::Hasher::operator()
size_t operator()(const Region &R) const
Definition: DynamicAtlasManager.hpp:94
Diligent::DynamicAtlasManager::Region::height
Uint32 height
Definition: DynamicAtlasManager.hpp:53
Diligent::DynamicAtlasManager::Region::Region
Region()=default
Diligent::DynamicAtlasManager::Free
void Free(Region &&R)
Definition: DynamicAtlasManager.cpp:326
Diligent::DynamicAtlasManager::Region::operator!=
bool operator!=(const Region &rhs) const
Definition: DynamicAtlasManager.hpp:87
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37