Skip to content

Commit ca1e96f

Browse files
DynamicAtlasManager: add Reset() for atlas state cleanup
1 parent 479a184 commit ca1e96f

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Graphics/GraphicsAccessories/interface/DynamicAtlasManager.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2025 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -86,7 +86,7 @@ class DynamicAtlasManager
8686
{}
8787

8888
/// Checks if the region is empty (width or height is zero).
89-
bool IsEmpty() const
89+
constexpr bool IsEmpty() const
9090
{
9191
return width == 0 || height == 0;
9292
}
@@ -105,6 +105,11 @@ class DynamicAtlasManager
105105
return !(*this == rhs);
106106
}
107107

108+
constexpr operator bool() const
109+
{
110+
return !IsEmpty();
111+
}
112+
108113
struct Hasher
109114
{
110115
size_t operator()(const Region& R) const
@@ -160,6 +165,9 @@ class DynamicAtlasManager
160165
/// and thus may be fragmented.
161166
Uint64 GetTotalFreeArea() const { return m_TotalFreeArea; }
162167

168+
/// Resets the atlas to the initial state, where the entire atlas is free and there are no allocated regions.
169+
void Reset();
170+
163171
/// Checks if the atlas is empty, i.e. if there are no allocated regions.
164172
bool IsEmpty() const
165173
{

Graphics/GraphicsAccessories/src/DynamicAtlasManager.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2025 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -369,6 +369,18 @@ void DynamicAtlasManager::Free(Region&& R)
369369
R = InvalidRegion;
370370
}
371371

372+
void DynamicAtlasManager::Reset()
373+
{
374+
m_AllocatedRegions.clear();
375+
m_FreeRegionsByWidth.clear();
376+
m_FreeRegionsByHeight.clear();
377+
378+
m_TotalFreeArea = Uint64{m_Width} * Uint64{m_Height};
379+
380+
m_Root = std::make_unique<Node>();
381+
m_Root->R = Region{0, 0, m_Width, m_Height};
382+
RegisterNode(*m_Root);
383+
}
372384

373385
#if DILIGENT_DEBUG
374386

Tests/DiligentCoreTest/src/GraphicsAccessories/DynamicAtlasManagerTest.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -465,4 +465,17 @@ TEST(GraphicsAccessories_DynamicAtlasManager, AllocateRandom)
465465
}
466466
}
467467

468+
TEST(GraphicsAccessories_DynamicAtlasManager, Reset)
469+
{
470+
DynamicAtlasManager Mgr{256, 256};
471+
EXPECT_TRUE(Mgr.Allocate(128, 128));
472+
EXPECT_TRUE(Mgr.Allocate(64, 64));
473+
EXPECT_TRUE(Mgr.Allocate(32, 32));
474+
Mgr.Reset();
475+
EXPECT_EQ(Mgr.GetFreeRegionCount(), 1U);
476+
EXPECT_EQ(Mgr.GetTotalFreeArea(), 256u * 256u);
477+
EXPECT_TRUE(Mgr.Allocate(256, 256));
478+
Mgr.Reset();
479+
}
480+
468481
} // namespace

0 commit comments

Comments
 (0)