Skip to content

Commit be99069

Browse files
DynamicAtlasManager: updated documentation
Also don't use auto where unnecessary
1 parent 802382a commit be99069

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

Graphics/GraphicsAccessories/interface/DynamicAtlasManager.hpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,15 +41,30 @@ namespace Diligent
4141
{
4242

4343
/// Dynamic 2D atlas manager
44+
45+
/// This class manages a 2D atlas of regions. It allows allocating and freeing
46+
/// rectangular regions of the atlas. The regions are represented by the
47+
/// Region structure, which contains the x and y coordinates of the top-left
48+
/// corner, as well as the width and height of the region.
49+
///
50+
/// \warning The class is not thread-safe. All operations on the atlas must be
51+
/// must be protected by a mutex or other synchronization mechanism.
4452
class DynamicAtlasManager
4553
{
4654
public:
55+
/// Structure representing a rectangular region in the atlas.
4756
struct Region
4857
{
58+
/// x coordinate of the top-left corner of the region
4959
Uint32 x = 0;
60+
61+
/// y coordinate of the top-left corner of the region
5062
Uint32 y = 0;
5163

52-
Uint32 width = 0;
64+
/// width of the region
65+
Uint32 width = 0;
66+
67+
/// height of the region
5368
Uint32 height = 0;
5469

5570
Region() = default;
@@ -70,6 +85,7 @@ class DynamicAtlasManager
7085
// clang-format on
7186
{}
7287

88+
/// Checks if the region is empty (width or height is zero).
7389
bool IsEmpty() const
7490
{
7591
return width == 0 || height == 0;
@@ -108,19 +124,43 @@ class DynamicAtlasManager
108124
DynamicAtlasManager& operator = ( DynamicAtlasManager&&) = delete;
109125
// clang-format on
110126

127+
128+
/// Allocates a rectangular region in the atlas.
129+
130+
/// \param Width - Width of the region to allocate.
131+
/// \param Height - Height of the region to allocate.
132+
/// \return The allocated region.
133+
///
134+
/// If the requested region cannot be allocated, an empty region is returned.
111135
Region Allocate(Uint32 Width, Uint32 Height);
112-
void Free(Region&& R);
113136

137+
138+
/// Frees a previously allocated region in the atlas.
139+
140+
/// \param R - The region to free.
141+
void Free(Region&& R);
142+
143+
144+
/// Returns the number of free regions in the atlas.
114145
Uint32 GetFreeRegionCount() const
115146
{
116147
VERIFY_EXPR(m_FreeRegionsByWidth.size() == m_FreeRegionsByHeight.size());
117148
return static_cast<Uint32>(m_FreeRegionsByWidth.size());
118149
}
119150

151+
/// Returns the atlas width.
120152
Uint32 GetWidth() const { return m_Width; }
153+
154+
/// Returns the atlas height.
121155
Uint32 GetHeight() const { return m_Height; }
156+
157+
/// Returns the total free area of the atlas.
158+
159+
/// The total free area is the sum of the areas of all free regions in the atlas,
160+
/// and thus may be fragmented.
122161
Uint64 GetTotalFreeArea() const { return m_TotalFreeArea; }
123162

163+
/// Checks if the atlas is empty, i.e. if there are no allocated regions.
124164
bool IsEmpty() const
125165
{
126166
VERIFY_EXPR(m_AllocatedRegions.empty() && (m_TotalFreeArea == Uint64{m_Width} * Uint64{m_Height}) ||

Graphics/GraphicsAccessories/src/DynamicAtlasManager.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,7 +46,7 @@ void DynamicAtlasManager::Node::Validate() const
4646
Uint32 Area = 0;
4747
for (Uint32 i = 0; i < NumChildren; ++i)
4848
{
49-
const auto& R0 = Child(i).R;
49+
const Region& R0 = Child(i).R;
5050

5151
VERIFY(!R0.IsEmpty(), "Region must not be empty");
5252
VERIFY(R0.x >= R.x && R0.x + R0.width <= R.x + R.width && R0.y >= R.y && R0.y + R0.height <= R.y + R.height,
@@ -57,7 +57,7 @@ void DynamicAtlasManager::Node::Validate() const
5757

5858
for (Uint32 j = i + 1; j < NumChildren; ++j)
5959
{
60-
const auto& R1 = Child(j).R;
60+
const Region& R1 = Child(j).R;
6161
if (CheckBox2DBox2DOverlap<false>(uint2{R0.x, R0.y}, uint2{R0.x + R0.width, R0.y + R0.height},
6262
uint2{R1.x, R1.y}, uint2{R1.x + R1.width, R1.y + R1.height}))
6363
{
@@ -79,7 +79,7 @@ void DynamicAtlasManager::Node::Split(const std::initializer_list<Region>& Regio
7979

8080
Children.reset(new Node[Regions.size()]);
8181
NumChildren = 0;
82-
for (const auto& ChildR : Regions)
82+
for (const Region& ChildR : Regions)
8383
{
8484
Children[NumChildren].Parent = this;
8585
Children[NumChildren].R = ChildR;
@@ -194,8 +194,8 @@ DynamicAtlasManager::Region DynamicAtlasManager::Allocate(Uint32 Width, Uint32 H
194194
++it_h;
195195
VERIFY_EXPR(it_h == m_FreeRegionsByHeight.end() || (it_h->first.width >= Width && it_h->first.height >= Height));
196196

197-
const auto AreaW = it_w != m_FreeRegionsByWidth.end() ? it_w->first.width * it_w->first.height : 0;
198-
const auto AreaH = it_h != m_FreeRegionsByHeight.end() ? it_h->first.width * it_h->first.height : 0;
197+
const Uint32 AreaW = it_w != m_FreeRegionsByWidth.end() ? it_w->first.width * it_w->first.height : 0;
198+
const Uint32 AreaH = it_h != m_FreeRegionsByHeight.end() ? it_h->first.width * it_h->first.height : 0;
199199
VERIFY_EXPR(AreaW == 0 || AreaW >= Width * Height);
200200
VERIFY_EXPR(AreaH == 0 || AreaH >= Width * Height);
201201

@@ -220,7 +220,7 @@ DynamicAtlasManager::Region DynamicAtlasManager::Allocate(Uint32 Width, Uint32 H
220220

221221
UnregisterNode(*pSrcNode);
222222

223-
auto R = pSrcNode->R;
223+
Region R = pSrcNode->R;
224224
if (R.width > Width && R.height > Height)
225225
{
226226
if (R.width > R.height)
@@ -341,7 +341,7 @@ void DynamicAtlasManager::Free(Region&& R)
341341
}
342342

343343
VERIFY_EXPR(node_it->first == R && node_it->second->R == R);
344-
auto* N = node_it->second;
344+
Node* N = node_it->second;
345345
VERIFY_EXPR(N->IsAllocated && !N->HasChildren());
346346
UnregisterNode(*N);
347347
N->IsAllocated = false;

0 commit comments

Comments
 (0)