#include "texture_util.h" #include #include #include #include #include #include using namespace godot; namespace mtgodot { namespace { int g_state = -1; // -1 = uninit, 0 = off, 1 = on bool g_warned = false; void lazy_init() { if (g_state != -1) { return; } if (const char *e = std::getenv("MTGODOT_TEXCOMP")) { g_state = (e[0] == '1') ? 1 : 0; return; } // No explicit override: on for mobile targets, off for desktop (keeps the // Phase-1 parity path byte-identical). OS *os = OS::get_singleton(); g_state = (os && os->has_feature("mobile")) ? 1 : 0; } } // namespace bool texcomp_enabled() { lazy_init(); return g_state == 1; } void texcomp_set_enabled(bool on) { g_state = on ? 1 : 0; } Ref make_color_texture(int w, int h, const uint8_t *rgba, size_t len, bool mipmaps, TexUse use) { if (w <= 0 || h <= 0 || rgba == nullptr || len < size_t(w) * size_t(h) * 4) { return Ref(); } PackedByteArray bytes; bytes.resize(int64_t(w) * h * 4); std::memcpy(bytes.ptrw(), rgba, size_t(w) * size_t(h) * 4); Ref img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, bytes); if (img.is_null()) { return Ref(); } if (mipmaps) { img->generate_mipmaps(); } if (use == TexUse::COLOR && texcomp_enabled()) { // ASTC 8x8: ~2 bpp vs 32 for RGBA8. GENERIC source hint (sRGB colour). Error err = img->compress(Image::COMPRESS_ASTC, Image::COMPRESS_SOURCE_GENERIC, Image::ASTC_FORMAT_8x8); if (err != OK && !g_warned) { g_warned = true; UtilityFunctions::push_warning( "mtgodot: runtime ASTC compression unavailable, using RGBA8"); } } return ImageTexture::create_from_image(img); } } // namespace mtgodot