Avoid regenerating the UI Atlas if unnecessary (faster resize)

This commit is contained in:
Henrik Rydgård committed 2025-11-06 23:44:55 +01:00
1 parent 602418aa1f
commit 56d3dae334
7 files changed
+43 -23

No files matched your search

+26 -12
View File
@@ -178,7 +178,7 @@ static bool IsImageID(std::string_view id) {
return GetImageIndex(id) != -1;
}
Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiScale) {
static bool GenerateUIAtlasImage(Atlas *atlas, float dpiScale, Image *dest) {
Bucket bucket;
// Script fully read, now read images and rasterize the fonts.
@@ -368,7 +368,6 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiS
INFO_LOG(Log::G3D, " - Added %zu images to bucket in %.2f ms", bucket.data.size(), addStart.ElapsedMs());
int image_width = 512;
Image dest;
Instant bucketStart = Instant::Now();
std::vector<Data> results = bucket.Resolve(image_width, dest);
@@ -379,7 +378,7 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiS
std::vector<AtlasImage> genAtlasImages;
genAtlasImages.reserve(ARRAY_SIZE(imageIDs));
for (int i = 0; i < ARRAY_SIZE(imageIDs); i++) {
genAtlasImages.push_back(ToAtlasImage(resultIds[i], imageIDs[i].id, (float)dest.width(), (float)dest.height(), results));
genAtlasImages.push_back(ToAtlasImage(resultIds[i], imageIDs[i].id, (float)dest->width(), (float)dest->height(), results));
}
atlas->Clear();
@@ -389,21 +388,36 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiS
// For debug, write out the atlas.
if (SAVE_DEBUG_IMAGES) {
dest.SavePNG("../ui_atlas_gen.png");
dest->SavePNG("../ui_atlas_gen.png");
}
INFO_LOG(Log::G3D, "UI atlas generated in %.2f ms, size %dx%d with %zu images\n", svgStart.ElapsedMs(), dest->width(), dest->height(), genAtlasImages.size());
return true;
}
// Then, create the texture too.
static Image g_cachedUIAtlasImage;
static float g_cachedDpiScale = 0.0f;
// The caller must cache the Atlas.
Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiScale, bool invalidate) {
if (g_cachedUIAtlasImage.IsEmpty() || dpiScale != g_cachedDpiScale || invalidate) {
g_cachedUIAtlasImage.clear();
if (!GenerateUIAtlasImage(atlas, dpiScale, &g_cachedUIAtlasImage)) {
ERROR_LOG(Log::G3D, "Failed to generate UI atlas!");
return nullptr;
}
}
g_cachedDpiScale = dpiScale;
// Create the texture.
Draw::TextureDesc desc{};
desc.width = image_width;
desc.height = dest.height();
desc.width = g_cachedUIAtlasImage.width();
desc.height = g_cachedUIAtlasImage.height();
desc.depth = 1;
desc.mipLevels = 1;
desc.format = Draw::DataFormat::R8G8B8A8_UNORM;
desc.type = Draw::TextureType::LINEAR2D;
desc.initData.push_back((const u8 *)dest.data());
desc.initData.push_back((const u8 *)g_cachedUIAtlasImage.data());
desc.tag = "UIAtlas";
INFO_LOG(Log::G3D, "UI atlas generated in %.2f ms, size %dx%d with %zu images\n", svgStart.ElapsedMs(), desc.width, desc.height, genAtlasImages.size());
return draw->CreateTexture(desc);
}
@@ -418,7 +432,7 @@ static void LoadAtlasMetadata(Atlas &metadata, const char *filename) {
delete[] atlas_data;
}
AtlasData AtlasProvider(Draw::DrawContext *draw, AtlasChoice atlas, float dpiScale) {
AtlasData AtlasProvider(Draw::DrawContext *draw, AtlasChoice atlas, float dpiScale, bool invalidate) {
// Clamp the dpiScale to sane values. Might increase the range later.
dpiScale = std::clamp(dpiScale, 0.5f, 4.0f);
@@ -426,7 +440,7 @@ AtlasData AtlasProvider(Draw::DrawContext *draw, AtlasChoice atlas, float dpiSca
case AtlasChoice::General:
{
// Generate the atlas from scratch.
Draw::Texture *tex = GenerateUIAtlas(draw, &ui_atlas, dpiScale);
Draw::Texture *tex = GenerateUIAtlas(draw, &ui_atlas, dpiScale, invalidate);
return {&ui_atlas, tex};
}
case AtlasChoice::Font: