Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(native): a compressed mip chain stops where the blocks stop

esengineafaf124a7775
diff --git a/native/host/media/ktx2_decode.cpp b/native/host/media/ktx2_decode.cpp
index f99deb132..f73bc19bd 100644
--- a/native/host/media/ktx2_decode.cpp
+++ b/native/host/media/ktx2_decode.cpp
@@ -60,19 +60,17 @@ KTX2Result transcodeKTX2(const uint8_t* bytes, size_t n, bool srgb,
basist::transcoder_texture_format basisFmt = basist::transcoder_texture_format::cTFRGBA32;
GfxCompressedFormat gfxFmt = GfxCompressedFormat::ETC2_RGBA8;
bool compressed = false;
- // Every choice below is a 4x4 block format, and WebGPU refuses a compressed
- // texture whose size is not whole blocks — a 70x70 sprite fails CreateTexture
- // and the game draws nothing. Device support is not the only question.
- const bool wholeBlocks = (w % 4 == 0) && (h % 4 == 0);
+ // WebGPU refuses a compressed copy whose size is not whole blocks — a 70x70
+ // sprite fails CreateTexture and the game draws nothing. Device support is not
+ // the only question, and the block is the FORMAT's, not always 4x4.
for (const FormatChoice& c : kChoices) {
- if (!wholeBlocks) break;
const GfxCompressedFormat want = srgb ? c.srgb : c.linear;
- if (device.supportsCompressedFormat(want)) {
- basisFmt = c.basisFmt;
- gfxFmt = want;
- compressed = true;
- break;
- }
+ if (!device.supportsCompressedFormat(want)) continue;
+ if (gfxWholeBlockLevels(want, w, h, 1) == 0) continue;
+ basisFmt = c.basisFmt;
+ gfxFmt = want;
+ compressed = true;
+ break;
}
// A compressed texture keeps the KTX2's pre-baked mip chain (basis cannot
@@ -82,7 +80,12 @@ KTX2Result transcodeKTX2(const uint8_t* bytes, size_t n, bool srgb,
const uint32_t bw = basist::basis_get_block_width(basisFmt);
const uint32_t bh = basist::basis_get_block_height(basisFmt);
const uint32_t bpb = basist::basis_get_bytes_per_block_or_pixel(basisFmt);
- const uint32_t numLevels = compressed ? (t.get_levels() ? t.get_levels() : 1u) : 1u;
+ // The rule is about each LEVEL, not the image: 72x72 is whole blocks and its
+ // third mip, 18x18, is not — and that copy is refused exactly as a 70x70 base
+ // would be. The chain stops there, whole as far as it goes.
+ const uint32_t numLevels = compressed
+ ? gfxWholeBlockLevels(gfxFmt, w, h, t.get_levels() ? t.get_levels() : 1u)
+ : 1u;
// Pack each level tightly, level 0 first — the layout the device unpacks.
std::vector<uint8_t> out;
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 9239766be..e3babc980 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -299,6 +299,23 @@ inline GfxBlockInfo gfxCompressedBlockInfo(GfxCompressedFormat fmt) {
}
}
+/** @brief How many mip levels of a @p width x @p height image are WHOLE blocks,
+ * counting from the base — the levels a compressed copy may write, since a
+ * copy whose size is not a multiple of the block is refused. 72x72 has two
+ * (72, 36; 18 stops it) and 70x70 has none, which is what makes an image
+ * uncompressible rather than merely short of mips. */
+inline u32 gfxWholeBlockLevels(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels) {
+ const GfxBlockInfo bi = gfxCompressedBlockInfo(fmt);
+ u32 levels = 0;
+ for (u32 level = 0; level < (mipLevels ? mipLevels : 1); ++level) {
+ const u32 lw = (width >> level) ? (width >> level) : 1u;
+ const u32 lh = (height >> level) ? (height >> level) : 1u;
+ if ((lw % bi.blockWidth) != 0 || (lh % bi.blockHeight) != 0) break;
+ ++levels;
+ }
+ return levels;
+}
+
/** @brief Total bytes of a full mip pyramid (levels 0..mipLevels-1) for a
* base-size compressed texture — the block-aligned sum both the transcoder
* (packing) and the device (uploading) agree on. */
diff --git a/tests/renderer/test_compressed_format.cpp b/tests/renderer/test_compressed_format.cpp
index 92da0d206..02d6792e0 100644
--- a/tests/renderer/test_compressed_format.cpp
+++ b/tests/renderer/test_compressed_format.cpp
@@ -75,6 +75,27 @@ int main() {
CHECK(d.lastCompressedByteLength == 512, "ETC2 byte length forwarded");
}
+ // --- how many mip levels of an image are whole blocks ---
+ // The rule a compressed copy obeys at every level, not only the base: 72x72
+ // passed a base-only check and its 18x18 third mip was uploaded anyway.
+ {
+ using esengine::gfxWholeBlockLevels;
+ const auto etc2 = GfxCompressedFormat::ETC2_RGBA8; // 4x4
+ const auto astc8 = GfxCompressedFormat::ASTC_8x8; // 8x8
+
+ CHECK(gfxWholeBlockLevels(etc2, 70, 70, 1) == 0, "70x70 base is not whole blocks");
+ CHECK(gfxWholeBlockLevels(etc2, 72, 72, 8) == 2, "72x72 gives 72 and 36, and stops at 18");
+ CHECK(gfxWholeBlockLevels(etc2, 512, 512, 10) == 8, "512x512 runs down to 4, not to 1");
+ CHECK(gfxWholeBlockLevels(etc2, 64, 64, 1) == 1, "a single level is counted");
+ CHECK(gfxWholeBlockLevels(etc2, 64, 64, 0) == 1, "zero levels reads as the base");
+ CHECK(gfxWholeBlockLevels(etc2, 4, 4, 4) == 1, "a 4x4 base has exactly one level");
+ // The block belongs to the format, not to the number 4: 32x32 runs down to
+ // 8 for an 8x8 format where a 4x4 one would keep going to 4.
+ CHECK(gfxWholeBlockLevels(astc8, 32, 32, 6) == 3, "ASTC 8x8 stops at 8");
+ CHECK(gfxWholeBlockLevels(etc2, 32, 32, 6) == 4, "the same image keeps one more level at 4x4");
+ CHECK(gfxWholeBlockLevels(astc8, 68, 68, 4) == 0, "4-aligned is not 8-aligned");
+ }
+
if (g_failures == 0) {
std::printf("\nALL COMPRESSED-FORMAT TESTS PASSED\n");
return 0;