From 0298536efa521b89c3f1c28d5554705e0eaedc19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 17 Sep 2025 10:21:17 -0600 Subject: [PATCH] Separate images from data in bucketer --- Common/Render/AtlasGen.cpp | 65 +++++++++++++++++++++------------- Common/Render/AtlasGen.h | 17 ++++----- UI/Theme.cpp | 7 ++-- ext/native/tools/atlastool.cpp | 8 ++--- 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/Common/Render/AtlasGen.cpp b/Common/Render/AtlasGen.cpp index 3076251ce4..e1804d5933 100644 --- a/Common/Render/AtlasGen.cpp +++ b/Common/Render/AtlasGen.cpp @@ -71,19 +71,41 @@ void Image::SaveZIM(const char *zim_name, int zim_format) { fclose(f); } +void Bucket::AddImage(Image &&img, int id) { + Data dat{}; + dat.id = id; + dat.sx = 0; + dat.sy = 0; + dat.ex = (int)img.width(); + dat.ey = (int)img.height(); + dat.w = dat.ex; + dat.h = dat.ey; + dat.redToWhiteAlpha = false; + images.emplace_back(img); + data.push_back(dat); +} + +inline bool CompareByID(const Data &lhs, const Data &rhs) { + return lhs.id < rhs.id; // should be unique +} + +inline bool CompareByArea(const Data& lhs, const Data& rhs) { + return lhs.w * lhs.h > rhs.w * rhs.h; +} + std::vector Bucket::Resolve(int image_width, Image &dest) { // Place all the little images - whatever they are. // Uses greedy fill algorithm. Slow but works surprisingly well, CPUs are fast. ImageU8 masq; masq.resize(image_width, 1); dest.resize(image_width, 1); - sort(items.begin(), items.end()); - for (int i = 0; i < (int)items.size(); i++) { + std::sort(data.begin(), data.end(), CompareByArea); + for (int i = 0; i < (int)data.size(); i++) { if ((i + 1) % 2000 == 0) { - printf("Resolving (%i / %i)\n", i, (int)items.size()); + printf("Resolving (%i / %i)\n", i, (int)data.size()); } - int idx = (int)items[i].first.width(); - int idy = (int)items[i].first.height(); + int idx = (int)data[i].w; + int idy = (int)data[i].h; if (idx > 1 && idy > 1) { assert(idx <= image_width); for (int ty = 0; ty < 2047; ty++) { @@ -93,7 +115,7 @@ std::vector Bucket::Resolve(int image_width, Image &dest) { dest.resize(image_width, ty + idy + 16); } // Brute force packing. - int sz = (int)items[i].first.width(); + int sz = (int)data[i].w; auto &masq_ty = masq.dat[ty]; auto &masq_idy = masq.dat[ty + idy - 1]; for (int tx = 0; tx < image_width - sz; tx++) { @@ -108,11 +130,11 @@ std::vector Bucket::Resolve(int image_width, Image &dest) { } masq.set(tx, ty, tx + idx + 1, ty + idy + 1, 255); - items[i].second.sx = tx; - items[i].second.sy = ty; + data[i].sx = tx; + data[i].sy = ty; - items[i].second.ex = tx + idx; - items[i].second.ey = ty + idy; + data[i].ex = tx + idx; + data[i].ey = ty + idy; // printf("Placed %d at %dx%d-%dx%d\n", items[i].second.id, tx, ty, tx + idx, ty + idy); goto found; @@ -131,16 +153,15 @@ std::vector Bucket::Resolve(int image_width, Image &dest) { exit(1); } + // Sort the data back by ID. + std::sort(data.begin(), data.end(), CompareByID); + // Actually copy the image data in place, after doing the layout. - for (int i = 0; i < (int)items.size(); i++) { - dest.copyfrom(items[i].first, items[i].second.sx, items[i].second.sy, items[i].second.redToWhiteAlpha); + for (int i = 0; i < (int)data.size(); i++) { + dest.copyfrom(images[i], data[i].sx, data[i].sy, data[i].redToWhiteAlpha); } - // Output the glyph data. - std::vector dats; - for (int i = 0; i < (int)items.size(); i++) - dats.push_back(items[i].second); - return dats; + return data; } bool LoadImage(const char *imagefile, Bucket *bucket, int &global_id) { @@ -157,14 +178,8 @@ bool LoadImage(const char *imagefile, Bucket *bucket, int &global_id) { } } - Data dat{}; - dat.id = global_id++; - dat.sx = 0; - dat.sy = 0; - dat.ex = (int)img.width(); - dat.ey = (int)img.height(); - dat.redToWhiteAlpha = false; - bucket->AddItem(std::move(img), dat); + bucket->AddImage(std::move(img), global_id); + global_id++; return true; } diff --git a/Common/Render/AtlasGen.h b/Common/Render/AtlasGen.h index 71a4a40558..6ae0cf48e7 100644 --- a/Common/Render/AtlasGen.h +++ b/Common/Render/AtlasGen.h @@ -69,6 +69,8 @@ private: struct Data { // item ID int id; + // item width and height + int w, h; // dimensions of its spot in the world int sx, sy, ex, ey; // offset from the origin @@ -81,22 +83,17 @@ struct Data { int charNum; }; -inline bool operator<(const Data &lhs, const Data &rhs) { - return lhs.id < rhs.id; // should be unique -} - struct Bucket { - std::vector > items; + std::vector images; + std::vector data; void AddItem(Image &&img, const Data &dat) { - items.emplace_back(std::move(img), dat); + images.emplace_back(std::move(img)); + data.emplace_back(dat); } + void AddImage(Image &&img, int id); std::vector Resolve(int image_width, Image &dest); }; -inline bool operator<(const Image &lhs, const Image &rhs) { - return lhs.width() * lhs.height() > rhs.width() * rhs.height(); -} - struct ImageDesc { std::string name; std::string fileName; diff --git a/UI/Theme.cpp b/UI/Theme.cpp index abe7c6fa3d..eb8e207262 100644 --- a/UI/Theme.cpp +++ b/UI/Theme.cpp @@ -383,12 +383,12 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas) { } else { name.append(image.fileName); } - image.result_index = (int)bucket.items.size(); + image.result_index = (int)bucket.data.size(); if (!LoadImage(name.c_str(), &bucket, global_id)) { ERROR_LOG(Log::G3D, "Failed to load image %s\n", image.fileName.c_str()); } } - INFO_LOG(Log::G3D, " - Loaded %zu images in %.2f ms\n", bucket.items.size(), start.ElapsedMs()); + INFO_LOG(Log::G3D, " - Loaded %zu images in %.2f ms\n", bucket.data.size(), start.ElapsedMs()); int image_width = 512; Image dest; @@ -397,9 +397,6 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas) { std::vector results = bucket.Resolve(image_width, dest); INFO_LOG(Log::G3D, " - Bucketed %zu images in %.2f ms\n", results.size(), now.ElapsedMs()); - // Need to sort the results by ID, after the bucket messed up the order. - sort(results.begin(), results.end()); - // Fill out the atlas structure. std::vector genAtlasImages; genAtlasImages.reserve(ARRAY_SIZE(images)); diff --git a/ext/native/tools/atlastool.cpp b/ext/native/tools/atlastool.cpp index 55960381c4..1535115266 100644 --- a/ext/native/tools/atlastool.cpp +++ b/ext/native/tools/atlastool.cpp @@ -277,6 +277,8 @@ void RasterizeFonts(const FontReferenceList &fontRefs, vector &ranges dat.sy = 0; dat.ex = (int)img.width(); dat.ey = (int)img.height(); + dat.w = dat.ex; + dat.h = dat.ey; dat.ox = (float)font->glyph->metrics.horiBearingX / 64 / supersample - bord; dat.oy = -(float)font->glyph->metrics.horiBearingY / 64 / supersample - bord; dat.voffset = vertOffset; @@ -661,7 +663,7 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig // Script fully read, now read images and rasterize the fonts. for (auto &image : images) { - image.result_index = (int)bucket.items.size(); + image.result_index = (int)bucket.data.size(); if (!LoadImage(image.fileName.c_str(), &bucket, global_id)) { fprintf(stderr, "Failed to load image %s\n", image.fileName.c_str()); } @@ -669,7 +671,7 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig for (auto it = fontRefs.begin(), end = fontRefs.end(); it != end; ++it) { FontDesc fnt; - fnt.first_char_id = (int)bucket.items.size(); + fnt.first_char_id = (int)bucket.data.size(); vector finalRanges; float metrics_height; @@ -704,8 +706,6 @@ int GenerateFromScript(const char *script_file, const char *atlas_name, bool hig dest.SavePNG((image_name + ".png").c_str()); printf("Done. Outputting source and meta files %s_atlas.cpp/h/meta.\n", out_prefix.c_str()); - // Sort items by ID. - sort(results.begin(), results.end()); // Save all the metadata. {