Stop memory allocations by keeping the RectPacker around

This commit is contained in:
Magnus Norddahl 2025-05-12 14:26:24 +02:00
commit be4b0cb67b
4 changed files with 26 additions and 2 deletions

View file

@ -16,6 +16,23 @@ RectPacker::RectPacker(int width, int height, int padding) : PageWidth(width), P
{
}
void RectPacker::Clear()
{
Pages.clear();
ItemFreeList.resize(Items.size());
size_t i = Items.size();
for (auto& item : Items)
{
item->Shelf = nullptr;
item->PrevItem = nullptr;
item->NextItem = nullptr;
item->IsAvailable = false;
item->PrevAvailable = nullptr;
item->NextAvailable = nullptr;
ItemFreeList[--i] = item.get();
}
}
RectPackerItem* RectPacker::Alloc(int width, int height)
{
width += Padding * 2;

View file

@ -56,6 +56,8 @@ class RectPacker
public:
RectPacker(int width, int height, int padding);
void Clear();
RectPackerItem* Alloc(int width, int height);
void Free(RectPackerItem* item);

View file

@ -133,7 +133,10 @@ void VkLightmapper::SelectTiles(const TArray<LightmapTile*>& tiles)
selectedTiles.Clear();
// We use a 3 texel spacing between rectangles so that the blur pass will not pick up anything from a neighbour tile.
RectPacker packer(bakeImageSize, bakeImageSize, 3);
if (!packer)
packer = std::make_unique<RectPacker>(bakeImageSize, bakeImageSize, 3);
else
packer->Clear();
for (int i = 0, count = tiles.Size(); i < count; i++)
{
@ -143,7 +146,7 @@ void VkLightmapper::SelectTiles(const TArray<LightmapTile*>& tiles)
continue;
// Only grab surfaces until our bake texture is full
auto result = packer.Alloc(tile->AtlasLocation.Width, tile->AtlasLocation.Height);
auto result = packer->Alloc(tile->AtlasLocation.Width, tile->AtlasLocation.Height);
if (result->PageIndex == 0)
{
SelectedTile selected;

View file

@ -6,6 +6,7 @@
class VulkanRenderDevice;
class FString;
class ShaderIncludeResult;
class RectPacker;
struct Uniforms
{
@ -232,4 +233,5 @@ private:
LightmapBakeImage bakeImage;
static const int bakeImageSize = 2048;
std::unique_ptr<RectPacker> packer;
};