Refactor RectPack into new RectPackNode and RectPackAtlas that uses more optimized memory allocations

This commit is contained in:
Wojtek Figat
2024-06-20 15:26:58 +02:00
parent a6324c8f65
commit 8e49a35e0e
9 changed files with 351 additions and 194 deletions

View File

@@ -252,17 +252,9 @@ bool FontManager::AddNewEntry(Font* font, Char c, FontCharacterEntry& entry)
// Find atlas for the character texture
int32 atlasIndex = 0;
const FontTextureAtlasSlot* slot = nullptr;
for (; atlasIndex < Atlases.Count(); atlasIndex++)
{
// Add the character to the texture
for (; atlasIndex < Atlases.Count() && slot == nullptr; atlasIndex++)
slot = Atlases[atlasIndex]->AddEntry(glyphWidth, glyphHeight, GlyphImageData);
// Check result, if not null char has been added
if (slot)
{
break;
}
}
atlasIndex--;
// Check if there is no atlas for this character
if (!slot)
@@ -271,6 +263,7 @@ bool FontManager::AddNewEntry(Font* font, Char c, FontCharacterEntry& entry)
auto atlas = Content::CreateVirtualAsset<FontTextureAtlas>();
atlas->Setup(PixelFormat::R8_UNorm, FontTextureAtlas::PaddingStyle::PadWithZero);
Atlases.Add(atlas);
atlasIndex++;
// Init atlas
const int32 fontAtlasSize = 512; // TODO: make it a configuration variable
@@ -286,12 +279,11 @@ bool FontManager::AddNewEntry(Font* font, Char c, FontCharacterEntry& entry)
}
// Fill with atlas dependant data
const uint32 padding = Atlases[atlasIndex]->GetPaddingAmount();
entry.TextureIndex = atlasIndex;
entry.UV.X = static_cast<float>(slot->X + padding);
entry.UV.Y = static_cast<float>(slot->Y + padding);
entry.UVSize.X = static_cast<float>(slot->Width - 2 * padding);
entry.UVSize.Y = static_cast<float>(slot->Height - 2 * padding);
entry.TextureIndex = (byte)atlasIndex;
entry.UV.X = (float)slot->X;
entry.UV.Y = (float)slot->Y;
entry.UVSize.X = (float)slot->Width;
entry.UVSize.Y = (float)slot->Height;
entry.Slot = slot;
return false;
@@ -302,12 +294,7 @@ void FontManager::Invalidate(FontCharacterEntry& entry)
if (entry.TextureIndex == MAX_uint8)
return;
auto atlas = Atlases[entry.TextureIndex];
const uint32 padding = atlas->GetPaddingAmount();
const uint32 slotX = static_cast<uint32>(entry.UV.X - padding);
const uint32 slotY = static_cast<uint32>(entry.UV.Y - padding);
const uint32 slotSizeX = static_cast<uint32>(entry.UVSize.X + 2 * padding);
const uint32 slotSizeY = static_cast<uint32>(entry.UVSize.Y + 2 * padding);
atlas->Invalidate(slotX, slotY, slotSizeX, slotSizeY);
atlas->Invalidate(entry.Slot);
}
void FontManager::Flush()