Path: blob/master/editor/import/resource_importer_texture.cpp
9903 views
/**************************************************************************/1/* resource_importer_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_importer_texture.h"3132#include "core/config/project_settings.h"33#include "core/io/config_file.h"34#include "core/io/image_loader.h"35#include "core/version.h"36#include "editor/file_system/editor_file_system.h"37#include "editor/gui/editor_toaster.h"38#include "editor/import/resource_importer_texture_settings.h"39#include "editor/settings/editor_settings.h"40#include "editor/themes/editor_scale.h"41#include "editor/themes/editor_theme_manager.h"42#include "scene/resources/compressed_texture.h"4344void ResourceImporterTexture::_texture_reimport_roughness(const Ref<CompressedTexture2D> &p_tex, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_channel) {45ERR_FAIL_COND(p_tex.is_null());4647MutexLock lock(singleton->mutex);48StringName path = p_tex->get_path();4950if (!singleton->make_flags.has(path)) {51singleton->make_flags[path] = MakeInfo();52}5354singleton->make_flags[path].flags |= MAKE_ROUGHNESS_FLAG;55singleton->make_flags[path].channel_for_roughness = p_channel;56singleton->make_flags[path].normal_path_for_roughness = p_normal_path;57}5859void ResourceImporterTexture::_texture_reimport_3d(const Ref<CompressedTexture2D> &p_tex) {60ERR_FAIL_COND(p_tex.is_null());6162MutexLock lock(singleton->mutex);63StringName path = p_tex->get_path();6465if (!singleton->make_flags.has(path)) {66singleton->make_flags[path] = MakeInfo();67}6869singleton->make_flags[path].flags |= MAKE_3D_FLAG;70}7172void ResourceImporterTexture::_texture_reimport_normal(const Ref<CompressedTexture2D> &p_tex) {73ERR_FAIL_COND(p_tex.is_null());7475MutexLock lock(singleton->mutex);76StringName path = p_tex->get_path();7778if (!singleton->make_flags.has(path)) {79singleton->make_flags[path] = MakeInfo();80}8182singleton->make_flags[path].flags |= MAKE_NORMAL_FLAG;83}8485void ResourceImporterTexture::update_imports() {86if (EditorFileSystem::get_singleton()->is_scanning() || EditorFileSystem::get_singleton()->is_importing()) {87return; // Don't update when EditorFileSystem is doing something else.88}8990MutexLock lock(mutex);91Vector<String> to_reimport;9293if (make_flags.is_empty()) {94return;95}9697for (const KeyValue<StringName, MakeInfo> &E : make_flags) {98Ref<ConfigFile> cf;99cf.instantiate();100String src_path = String(E.key) + ".import";101102Error err = cf->load(src_path);103ERR_CONTINUE(err != OK);104105bool changed = false;106107if (E.value.flags & MAKE_NORMAL_FLAG && int(cf->get_value("params", "compress/normal_map")) == 0) {108print_line(109vformat(TTR("%s: Texture detected as used as a normal map in 3D. Enabling red-green texture compression to reduce memory usage (blue channel is discarded)."),110String(E.key)));111112cf->set_value("params", "compress/normal_map", 1);113changed = true;114}115116if (E.value.flags & MAKE_ROUGHNESS_FLAG && int(cf->get_value("params", "roughness/mode")) == 0) {117print_line(118vformat(TTR("%s: Texture detected as used as a roughness map in 3D. Enabling roughness limiter based on the detected associated normal map at %s."),119String(E.key), E.value.normal_path_for_roughness));120121cf->set_value("params", "roughness/mode", E.value.channel_for_roughness + 2);122cf->set_value("params", "roughness/src_normal", E.value.normal_path_for_roughness);123changed = true;124}125126if (E.value.flags & MAKE_3D_FLAG && bool(cf->get_value("params", "detect_3d/compress_to"))) {127const int compress_to = cf->get_value("params", "detect_3d/compress_to");128129// 3D detected, disable the callback.130cf->set_value("params", "detect_3d/compress_to", 0);131132String compress_string;133if (compress_to == 1) {134cf->set_value("params", "compress/mode", COMPRESS_VRAM_COMPRESSED);135compress_string = "VRAM Compressed (S3TC/ETC/BPTC)";136137} else if (compress_to == 2) {138cf->set_value("params", "compress/mode", COMPRESS_BASIS_UNIVERSAL);139compress_string = "Basis Universal";140}141142print_line(143vformat(TTR("%s: Texture detected as used in 3D. Enabling mipmap generation and setting the texture compression mode to %s."),144String(E.key), compress_string));145146cf->set_value("params", "mipmaps/generate", true);147changed = true;148}149150if (changed) {151cf->save(src_path);152to_reimport.push_back(E.key);153}154}155156make_flags.clear();157158if (!to_reimport.is_empty()) {159EditorFileSystem::get_singleton()->reimport_files(to_reimport);160}161}162163String ResourceImporterTexture::get_importer_name() const {164return "texture";165}166167String ResourceImporterTexture::get_visible_name() const {168return "Texture2D";169}170171void ResourceImporterTexture::get_recognized_extensions(List<String> *p_extensions) const {172ImageLoader::get_recognized_extensions(p_extensions);173}174175String ResourceImporterTexture::get_save_extension() const {176return "ctex";177}178179String ResourceImporterTexture::get_resource_type() const {180return "CompressedTexture2D";181}182183bool ResourceImporterTexture::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {184if (p_option == "compress/high_quality" || p_option == "compress/hdr_compression") {185int compress_mode = int(p_options["compress/mode"]);186if (compress_mode != COMPRESS_VRAM_COMPRESSED) {187return false;188}189190} else if (p_option == "compress/lossy_quality") {191int compress_mode = int(p_options["compress/mode"]);192if (compress_mode != COMPRESS_LOSSY) {193return false;194}195196} else if (p_option == "compress/hdr_mode") {197int compress_mode = int(p_options["compress/mode"]);198if (compress_mode < COMPRESS_VRAM_COMPRESSED) {199return false;200}201202} else if (p_option == "compress/normal_map") {203int compress_mode = int(p_options["compress/mode"]);204if (compress_mode == COMPRESS_LOSSLESS) {205return false;206}207208} else if (p_option == "mipmaps/limit") {209return p_options["mipmaps/generate"];210211} else if (p_option == "compress/uastc_level" || p_option == "compress/rdo_quality_loss") {212return int(p_options["compress/mode"]) == COMPRESS_BASIS_UNIVERSAL;213}214215return true;216}217218int ResourceImporterTexture::get_preset_count() const {219return 3;220}221222String ResourceImporterTexture::get_preset_name(int p_idx) const {223static const char *preset_names[] = {224TTRC("2D/3D (Auto-Detect)"),225TTRC("2D"),226TTRC("3D"),227};228229return TTRGET(preset_names[p_idx]);230}231232void ResourceImporterTexture::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {233r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Lossless,Lossy,VRAM Compressed,VRAM Uncompressed,Basis Universal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), p_preset == PRESET_3D ? 2 : 0));234r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress/high_quality"), false));235r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));236237Image::BasisUniversalPackerParams basisu_params;238r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/uastc_level", PROPERTY_HINT_ENUM, "Fastest,Faster,Medium,Slower,Slowest"), basisu_params.uastc_level));239r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/rdo_quality_loss", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater"), basisu_params.rdo_quality_loss));240241r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_compression", PROPERTY_HINT_ENUM, "Disabled,Opaque Only,Always"), 1));242r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/normal_map", PROPERTY_HINT_ENUM, "Detect,Enable,Disabled"), 0));243r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized"), 0));244r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "mipmaps/generate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), (p_preset == PRESET_3D ? true : false)));245r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "-1,256"), -1));246r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "roughness/mode", PROPERTY_HINT_ENUM, "Detect,Disabled,Red,Green,Blue,Alpha,Gray"), 0));247r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "roughness/src_normal", PROPERTY_HINT_FILE, "*.bmp,*.dds,*.exr,*.jpeg,*.jpg,*.hdr,*.png,*.svg,*.tga,*.webp"), ""));248r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/channel_remap/red", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Inverted Red,Inverted Green,Inverted Blue,Inverted Alpha,Unused,Zero,One"), 0));249r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/channel_remap/green", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Inverted Red,Inverted Green,Inverted Blue,Inverted Alpha,Unused,Zero,One"), 1));250r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/channel_remap/blue", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Inverted Red,Inverted Green,Inverted Blue,Inverted Alpha,Unused,Zero,One"), 2));251r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/channel_remap/alpha", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Inverted Red,Inverted Green,Inverted Blue,Inverted Alpha,Unused,Zero,One"), 3));252r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/fix_alpha_border"), p_preset != PRESET_3D));253r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/premult_alpha"), false));254r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/normal_map_invert_y"), false));255r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/hdr_as_srgb"), false));256r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/hdr_clamp_exposure"), false));257258// Maximum bound is the highest allowed value for lossy compression (the lowest common denominator).259r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/size_limit", PROPERTY_HINT_RANGE, "0,16383,1"), 0));260261r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "detect_3d/compress_to", PROPERTY_HINT_ENUM, "Disabled,VRAM Compressed,Basis Universal"), (p_preset == PRESET_DETECT) ? 1 : 0));262263// Do path based customization only if a path was passed.264if (p_path.is_empty() || p_path.get_extension() == "svg") {265r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "svg/scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0));266267// Editor use only, applies to SVG.268r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "editor/scale_with_editor_scale"), false));269r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "editor/convert_colors_with_editor_theme"), false));270}271}272273void ResourceImporterTexture::save_to_ctex_format(Ref<FileAccess> f, const Ref<Image> &p_image, CompressMode p_compress_mode, Image::UsedChannels p_channels, Image::CompressMode p_compress_format, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params) {274switch (p_compress_mode) {275case COMPRESS_LOSSLESS: {276bool lossless_force_png = GLOBAL_GET("rendering/textures/lossless_compression/force_png") || !Image::_webp_mem_loader_func; // WebP module disabled or png is forced.277bool use_webp = !lossless_force_png && p_image->get_width() <= 16383 && p_image->get_height() <= 16383; // WebP has a size limit.278279f->store_32(use_webp ? CompressedTexture2D::DATA_FORMAT_WEBP : CompressedTexture2D::DATA_FORMAT_PNG);280f->store_16(p_image->get_width());281f->store_16(p_image->get_height());282f->store_32(p_image->get_mipmap_count());283f->store_32(p_image->get_format());284285for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {286Vector<uint8_t> data;287if (use_webp) {288data = Image::webp_lossless_packer(i ? p_image->get_image_from_mipmap(i) : p_image);289} else {290data = Image::png_packer(i ? p_image->get_image_from_mipmap(i) : p_image);291}292293const uint64_t data_size = data.size();294295f->store_32(data_size);296f->store_buffer(data.ptr(), data_size);297}298299} break;300case COMPRESS_LOSSY: {301f->store_32(CompressedTexture2D::DATA_FORMAT_WEBP);302f->store_16(p_image->get_width());303f->store_16(p_image->get_height());304f->store_32(p_image->get_mipmap_count());305f->store_32(p_image->get_format());306307for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {308Vector<uint8_t> data = Image::webp_lossy_packer(i ? p_image->get_image_from_mipmap(i) : p_image, p_lossy_quality);309const uint64_t data_size = data.size();310311f->store_32(data_size);312f->store_buffer(data.ptr(), data_size);313}314315} break;316case COMPRESS_VRAM_COMPRESSED: {317Ref<Image> image = p_image->duplicate();318image->compress_from_channels(p_compress_format, p_channels);319320f->store_32(CompressedTexture2D::DATA_FORMAT_IMAGE);321f->store_16(image->get_width());322f->store_16(image->get_height());323f->store_32(image->get_mipmap_count());324f->store_32(image->get_format());325f->store_buffer(image->get_data());326327} break;328case COMPRESS_VRAM_UNCOMPRESSED: {329f->store_32(CompressedTexture2D::DATA_FORMAT_IMAGE);330f->store_16(p_image->get_width());331f->store_16(p_image->get_height());332f->store_32(p_image->get_mipmap_count());333f->store_32(p_image->get_format());334f->store_buffer(p_image->get_data());335336} break;337case COMPRESS_BASIS_UNIVERSAL: {338f->store_32(CompressedTexture2D::DATA_FORMAT_BASIS_UNIVERSAL);339f->store_16(p_image->get_width());340f->store_16(p_image->get_height());341f->store_32(p_image->get_mipmap_count());342f->store_32(p_image->get_format());343344Vector<uint8_t> data = Image::basis_universal_packer(p_image, p_channels, p_basisu_params);345const uint64_t data_size = data.size();346347f->store_32(data_size);348f->store_buffer(data.ptr(), data_size);349} break;350}351}352353void ResourceImporterTexture::_save_ctex(const Ref<Image> &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_roughness, bool p_detect_normal, bool p_force_normal, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap, const Ref<Image> &p_normal, Image::RoughnessChannel p_roughness_channel) {354Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);355ERR_FAIL_COND(f.is_null());356357// Godot Streamable Texture 2D.358f->store_8('G');359f->store_8('S');360f->store_8('T');361f->store_8('2');362363// Current format version.364f->store_32(CompressedTexture2D::FORMAT_VERSION);365366// Texture may be resized later, so original size must be saved first.367f->store_32(p_image->get_width());368f->store_32(p_image->get_height());369370uint32_t flags = 0;371if (p_streamable) {372flags |= CompressedTexture2D::FORMAT_BIT_STREAM;373}374if (p_mipmaps) {375flags |= CompressedTexture2D::FORMAT_BIT_HAS_MIPMAPS;376}377if (p_detect_3d) {378flags |= CompressedTexture2D::FORMAT_BIT_DETECT_3D;379}380if (p_detect_roughness) {381flags |= CompressedTexture2D::FORMAT_BIT_DETECT_ROUGNESS;382}383if (p_detect_normal) {384flags |= CompressedTexture2D::FORMAT_BIT_DETECT_NORMAL;385}386387f->store_32(flags);388f->store_32(p_limit_mipmap);389390// Reserved.391f->store_32(0);392f->store_32(0);393f->store_32(0);394395if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() >= Image::FORMAT_RF) {396p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED; //these can't go as lossy397}398399Ref<Image> image = p_image->duplicate();400401if (p_mipmaps) {402if (p_force_po2_for_compressed && (p_compress_mode == COMPRESS_BASIS_UNIVERSAL || p_compress_mode == COMPRESS_VRAM_COMPRESSED)) {403image->resize_to_po2();404}405406if (!image->has_mipmaps() || p_force_normal) {407image->generate_mipmaps(p_force_normal);408}409410} else {411image->clear_mipmaps();412}413414// Generate roughness mipmaps from normal texture.415if (image->has_mipmaps() && p_normal.is_valid()) {416image->generate_mipmap_roughness(p_roughness_channel, p_normal);417}418419// Optimization: Only check for color channels when compressing as BasisU or VRAM.420Image::UsedChannels used_channels = Image::USED_CHANNELS_RGBA;421422if (p_compress_mode == COMPRESS_BASIS_UNIVERSAL || p_compress_mode == COMPRESS_VRAM_COMPRESSED) {423Image::CompressSource comp_source = Image::COMPRESS_SOURCE_GENERIC;424if (p_force_normal) {425comp_source = Image::COMPRESS_SOURCE_NORMAL;426} else if (p_srgb_friendly) {427comp_source = Image::COMPRESS_SOURCE_SRGB;428}429430used_channels = image->detect_used_channels(comp_source);431}432433save_to_ctex_format(f, image, p_compress_mode, used_channels, p_vram_compression, p_lossy_quality, p_basisu_params);434}435436void ResourceImporterTexture::_save_editor_meta(const Dictionary &p_metadata, const String &p_to_path) {437Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);438ERR_FAIL_COND(f.is_null());439440f->store_var(p_metadata);441}442443Dictionary ResourceImporterTexture::_load_editor_meta(const String &p_path) const {444Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);445ERR_FAIL_COND_V_MSG(f.is_null(), Dictionary(), vformat("Missing required editor-specific import metadata for a texture (please reimport it using the 'Import' tab): '%s'", p_path));446447return f->get_var();448}449450void ResourceImporterTexture::_remap_channels(Ref<Image> &r_image, ChannelRemap p_options[4]) {451ERR_FAIL_COND(r_image->is_compressed());452453// Currently HDR inverted remapping is not allowed.454bool attempted_hdr_inverted = false;455if (r_image->get_format() >= Image::FORMAT_RF && r_image->get_format() <= Image::FORMAT_RGBE9995) {456// Formats which can hold HDR data cannot be inverted the same way as unsigned normalized ones (1.0 - channel).457for (int i = 0; i < 4; i++) {458switch (p_options[i]) {459case REMAP_INV_R:460attempted_hdr_inverted = true;461p_options[i] = REMAP_R;462break;463case REMAP_INV_G:464attempted_hdr_inverted = true;465p_options[i] = REMAP_G;466break;467case REMAP_INV_B:468attempted_hdr_inverted = true;469p_options[i] = REMAP_B;470break;471case REMAP_INV_A:472attempted_hdr_inverted = true;473p_options[i] = REMAP_A;474break;475default:476break;477}478}479}480481if (attempted_hdr_inverted) {482WARN_PRINT("Attempted to use an inverted channel remap on an HDR image. The remap has been changed to its uninverted equivalent.");483}484485// Optimization: Set the remap from 'unused' to either 0 or 1 to avoid repeated checks in the conversion loop.486for (int i = 0; i < 4; i++) {487if (p_options[i] == REMAP_UNUSED) {488p_options[i] = i == 3 ? REMAP_1 : REMAP_0;489}490}491492// Expand the image's channel count in the event that the current set of channels doesn't allow for the desired remap.493const Image::Format original_format = r_image->get_format();494const uint32_t channel_mask = Image::get_format_component_mask(original_format);495496// Whether a channel is supported by the format itself.497const bool has_channel_r = channel_mask & 0x1;498const bool has_channel_g = channel_mask & 0x2;499const bool has_channel_b = channel_mask & 0x4;500const bool has_channel_a = channel_mask & 0x8;501502// Whether a certain channel needs to be remapped.503const bool remap_r = p_options[0] != REMAP_R ? !(!has_channel_r && p_options[0] == REMAP_0) : false;504const bool remap_g = p_options[1] != REMAP_G ? !(!has_channel_g && p_options[1] == REMAP_0) : false;505const bool remap_b = p_options[2] != REMAP_B ? !(!has_channel_b && p_options[2] == REMAP_0) : false;506const bool remap_a = p_options[3] != REMAP_A ? !(!has_channel_a && p_options[3] == REMAP_1) : false;507508if (!(remap_r || remap_g || remap_b || remap_a)) {509// Default color map, do nothing.510return;511}512513// Whether a certain channel set is needed, either from the source or the remap.514const bool needs_rg = remap_g || has_channel_g;515const bool needs_rgb = remap_b || has_channel_b;516const bool needs_rgba = remap_a || has_channel_a;517518bool could_not_expand = false;519switch (original_format) {520case Image::FORMAT_R8:521case Image::FORMAT_RG8:522case Image::FORMAT_RGB8: {523// Convert to either RGBA8, RGB8 or RG8.524if (needs_rgba) {525r_image->convert(Image::FORMAT_RGBA8);526} else if (needs_rgb) {527r_image->convert(Image::FORMAT_RGB8);528} else if (needs_rg) {529r_image->convert(Image::FORMAT_RG8);530}531} break;532case Image::FORMAT_RH:533case Image::FORMAT_RGH:534case Image::FORMAT_RGBH: {535// Convert to either RGBAH, RGBH or RGH.536if (needs_rgba) {537r_image->convert(Image::FORMAT_RGBAH);538} else if (needs_rgb) {539r_image->convert(Image::FORMAT_RGBH);540} else if (needs_rg) {541r_image->convert(Image::FORMAT_RGH);542}543} break;544case Image::FORMAT_RF:545case Image::FORMAT_RGF:546case Image::FORMAT_RGBF: {547// Convert to either RGBAF, RGBF or RGF.548if (needs_rgba) {549r_image->convert(Image::FORMAT_RGBAF);550} else if (needs_rgb) {551r_image->convert(Image::FORMAT_RGBF);552} else if (needs_rg) {553r_image->convert(Image::FORMAT_RGF);554}555} break;556case Image::FORMAT_L8: {557const bool uniform_rgb = (p_options[0] == p_options[1] && p_options[1] == p_options[2]) || !(remap_r || remap_g || remap_b);558if (uniform_rgb) {559// Uniform RGB.560if (needs_rgba) {561r_image->convert(Image::FORMAT_LA8);562}563} else {564// Non-uniform RGB.565if (needs_rgba) {566r_image->convert(Image::FORMAT_RGBA8);567} else {568r_image->convert(Image::FORMAT_RGB8);569}570could_not_expand = true;571}572} break;573case Image::FORMAT_LA8: {574const bool uniform_rgb = (p_options[0] == p_options[1] && p_options[1] == p_options[2]) || !(remap_r || remap_g || remap_b);575if (!uniform_rgb) {576// Non-uniform RGB.577r_image->convert(Image::FORMAT_RGBA8);578could_not_expand = true;579}580} break;581case Image::FORMAT_RGB565: {582if (needs_rgba) {583// RGB565 doesn't have an alpha expansion, convert to RGBA8.584r_image->convert(Image::FORMAT_RGBA8);585could_not_expand = true;586}587} break;588case Image::FORMAT_RGBE9995: {589if (needs_rgba) {590// RGB9995 doesn't have an alpha expansion, convert to RGBAH.591r_image->convert(Image::FORMAT_RGBAH);592could_not_expand = true;593}594} break;595596default: {597} break;598}599600if (could_not_expand) {601WARN_PRINT(vformat("Unable to expand image format %s's channels (the target format does not exist), converting to %s as a fallback.",602Image::get_format_name(original_format), Image::get_format_name(r_image->get_format())));603}604605// Remap the channels.606for (int x = 0; x < r_image->get_width(); x++) {607for (int y = 0; y < r_image->get_height(); y++) {608Color src = r_image->get_pixel(x, y);609Color dst;610611for (int i = 0; i < 4; i++) {612switch (p_options[i]) {613case REMAP_R:614dst[i] = src.r;615break;616case REMAP_G:617dst[i] = src.g;618break;619case REMAP_B:620dst[i] = src.b;621break;622case REMAP_A:623dst[i] = src.a;624break;625626case REMAP_INV_R:627dst[i] = 1.0f - src.r;628break;629case REMAP_INV_G:630dst[i] = 1.0f - src.g;631break;632case REMAP_INV_B:633dst[i] = 1.0f - src.b;634break;635case REMAP_INV_A:636dst[i] = 1.0f - src.a;637break;638639case REMAP_0:640dst[i] = 0.0f;641break;642case REMAP_1:643dst[i] = 1.0f;644break;645646default:647break;648}649}650651r_image->set_pixel(x, y, dst);652}653}654}655656void ResourceImporterTexture::_invert_y_channel(Ref<Image> &r_image) {657// Inverting the green channel can be used to flip a normal map's direction.658// There's no standard when it comes to normal map Y direction, so this is659// sometimes needed when using a normal map exported from another program.660// See <http://wiki.polycount.com/wiki/Normal_Map_Technical_Details#Common_Swizzle_Coordinates>.661const int height = r_image->get_height();662const int width = r_image->get_width();663664for (int i = 0; i < width; i++) {665for (int j = 0; j < height; j++) {666const Color color = r_image->get_pixel(i, j);667r_image->set_pixel(i, j, Color(color.r, 1 - color.g, color.b, color.a));668}669}670}671672void ResourceImporterTexture::_clamp_hdr_exposure(Ref<Image> &r_image) {673// Clamp HDR exposure following Filament's tonemapping formula.674// This can be used to reduce fireflies in environment maps or reduce the influence675// of the sun from an HDRI panorama on environment lighting (when a DirectionalLight3D is used instead).676const int height = r_image->get_height();677const int width = r_image->get_width();678679// These values are chosen arbitrarily and seem to produce good results with 4,096 samples.680const float linear = 4096.0;681const float compressed = 16384.0;682683for (int i = 0; i < width; i++) {684for (int j = 0; j < height; j++) {685const Color color = r_image->get_pixel(i, j);686const float luma = color.get_luminance();687688Color clamped_color;689if (luma <= linear) {690clamped_color = color;691} else {692clamped_color = (color / luma) * ((linear * linear - compressed * luma) / (2 * linear - compressed - luma));693}694695r_image->set_pixel(i, j, clamped_color);696}697}698}699700Error ResourceImporterTexture::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {701// Parse import options.702int32_t loader_flags = ImageFormatLoader::FLAG_NONE;703704// Compression.705CompressMode compress_mode = CompressMode(int(p_options["compress/mode"]));706const float lossy = p_options["compress/lossy_quality"];707const int pack_channels = p_options["compress/channel_pack"];708const int normal = p_options["compress/normal_map"];709const int hdr_compression = p_options["compress/hdr_compression"];710const int high_quality = p_options["compress/high_quality"];711712// Mipmaps.713const bool mipmaps = p_options["mipmaps/generate"];714const uint32_t mipmap_limit = mipmaps ? uint32_t(p_options["mipmaps/limit"]) : uint32_t(-1);715716// Roughness.717const int roughness = p_options["roughness/mode"];718const String normal_map = p_options["roughness/src_normal"];719720// Processing.721const int remap_r = p_options["process/channel_remap/red"];722const int remap_g = p_options["process/channel_remap/green"];723const int remap_b = p_options["process/channel_remap/blue"];724const int remap_a = p_options["process/channel_remap/alpha"];725const bool fix_alpha_border = p_options["process/fix_alpha_border"];726const bool premult_alpha = p_options["process/premult_alpha"];727const bool normal_map_invert_y = p_options["process/normal_map_invert_y"];728729const bool hdr_as_srgb = p_options["process/hdr_as_srgb"];730const bool hdr_clamp_exposure = p_options["process/hdr_clamp_exposure"];731int size_limit = p_options["process/size_limit"];732733const Image::BasisUniversalPackerParams basisu_params = {734p_options["compress/uastc_level"],735p_options["compress/rdo_quality_loss"],736};737738bool using_fallback_size_limit = false;739if (size_limit == 0) {740using_fallback_size_limit = true;741// If no size limit is defined, use a fallback size limit to prevent textures from looking incorrect or failing to import.742switch (compress_mode) {743case COMPRESS_LOSSY:744// Maximum WebP size on either axis.745size_limit = 16383;746break;747case COMPRESS_BASIS_UNIVERSAL:748// Maximum Basis Universal size on either axis.749size_limit = 16384;750break;751default:752// As of June 2024, no GPU can correctly display a texture larger than 32768 pixels on either axis.753size_limit = 32768;754break;755}756}757758// Support for texture streaming is not implemented yet.759const bool stream = false;760761// SVG-specific options.762float scale = p_options.has("svg/scale") ? float(p_options["svg/scale"]) : 1.0f;763764// Editor-specific options.765bool use_editor_scale = p_options.has("editor/scale_with_editor_scale") && p_options["editor/scale_with_editor_scale"];766bool convert_editor_colors = p_options.has("editor/convert_colors_with_editor_theme") && p_options["editor/convert_colors_with_editor_theme"];767768if (hdr_as_srgb) {769loader_flags |= ImageFormatLoader::FLAG_FORCE_LINEAR;770}771772// Start importing images.773LocalVector<Ref<Image>> images_imported;774775// Load the normal image.776Ref<Image> normal_image;777Image::RoughnessChannel roughness_channel = Image::ROUGHNESS_CHANNEL_R;778779if (mipmaps && roughness > 1 && FileAccess::exists(normal_map)) {780normal_image.instantiate();781if (ImageLoader::load_image(normal_map, normal_image) == OK) {782roughness_channel = Image::RoughnessChannel(roughness - 2);783}784}785786// Load the main image.787Ref<Image> image;788image.instantiate();789Error err = ImageLoader::load_image(p_source_file, image, nullptr, loader_flags, scale);790if (err != OK) {791return err;792}793images_imported.push_back(image);794795// Load the editor-only image.796Ref<Image> editor_image;797798if (use_editor_scale || convert_editor_colors) {799float editor_scale = use_editor_scale ? scale * EDSCALE : scale;800801int32_t editor_loader_flags = loader_flags;802if (convert_editor_colors) {803editor_loader_flags |= ImageFormatLoader::FLAG_CONVERT_COLORS;804}805806editor_image.instantiate();807err = ImageLoader::load_image(p_source_file, editor_image, nullptr, editor_loader_flags, editor_scale);808809if (err != OK) {810WARN_PRINT(vformat("Failed to import an image resource for editor use from '%s'.", p_source_file));811} else {812if (convert_editor_colors) {813float image_saturation = EDITOR_GET("interface/theme/icon_saturation");814editor_image->adjust_bcs(1.0, 1.0, image_saturation);815}816817images_imported.push_back(editor_image);818}819}820821for (Ref<Image> &target_image : images_imported) {822// Apply the size limit.823if (size_limit > 0 && (target_image->get_width() > size_limit || target_image->get_height() > size_limit)) {824if (target_image->get_width() >= target_image->get_height()) {825int new_width = size_limit;826int new_height = target_image->get_height() * new_width / target_image->get_width();827828if (using_fallback_size_limit) {829// Only warn if downsizing occurred when the user did not explicitly request it.830WARN_PRINT(vformat("%s: Texture was downsized on import as its width (%d pixels) exceeded the importable size limit (%d pixels).", p_source_file, target_image->get_width(), size_limit));831}832target_image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);833} else {834int new_height = size_limit;835int new_width = target_image->get_width() * new_height / target_image->get_height();836837if (using_fallback_size_limit) {838// Only warn if downsizing occurred when the user did not explicitly request it.839WARN_PRINT(vformat("%s: Texture was downsized on import as its height (%d pixels) exceeded the importable size limit (%d pixels).", p_source_file, target_image->get_height(), size_limit));840}841target_image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);842}843844if (normal == 1) {845target_image->normalize();846}847}848849{850ChannelRemap remaps[4] = {851(ChannelRemap)remap_r,852(ChannelRemap)remap_g,853(ChannelRemap)remap_b,854(ChannelRemap)remap_a,855};856857_remap_channels(target_image, remaps);858}859860// Fix alpha border.861if (fix_alpha_border) {862target_image->fix_alpha_edges();863}864865// Premultiply the alpha.866if (premult_alpha) {867target_image->premultiply_alpha();868}869870// Invert the green channel of the image to flip the normal map it contains.871if (normal_map_invert_y) {872_invert_y_channel(target_image);873}874875// Clamp HDR exposure.876if (hdr_clamp_exposure) {877_clamp_hdr_exposure(target_image);878}879}880881bool detect_3d = int(p_options["detect_3d/compress_to"]) > 0;882bool detect_roughness = roughness == 0;883bool detect_normal = normal == 0;884bool force_normal = normal == 1;885bool srgb_friendly_pack = pack_channels == 0;886887Array formats_imported;888889if (compress_mode == COMPRESS_VRAM_COMPRESSED) {890// Must import in desktop and mobile formats in order of priority, so platform chooses the best supported one (e.g. s3tc over etc2 on desktop).891const bool is_hdr = (image->get_format() >= Image::FORMAT_RF && image->get_format() <= Image::FORMAT_RGBE9995);892const bool can_s3tc_bptc = ResourceImporterTextureSettings::should_import_s3tc_bptc();893const bool can_etc2_astc = ResourceImporterTextureSettings::should_import_etc2_astc();894895// Add list of formats imported.896if (can_s3tc_bptc) {897formats_imported.push_back("s3tc_bptc");898}899if (can_etc2_astc) {900formats_imported.push_back("etc2_astc");901}902903bool can_compress_hdr = hdr_compression > 0;904bool has_alpha = image->detect_alpha() != Image::ALPHA_NONE;905bool force_uncompressed = false;906907if (is_hdr) {908if (has_alpha) {909// Can compress HDR, but HDR with alpha is not compressible.910if (hdr_compression == 2) {911// But user selected to compress HDR anyway, so force an alpha-less format.912if (image->get_format() == Image::FORMAT_RGBAF) {913image->convert(Image::FORMAT_RGBF);914} else if (image->get_format() == Image::FORMAT_RGBAH) {915image->convert(Image::FORMAT_RGBH);916}917} else {918can_compress_hdr = false;919}920}921922// Fall back to RGBE99995.923if (!can_compress_hdr && image->get_format() != Image::FORMAT_RGBE9995) {924image->convert(Image::FORMAT_RGBE9995);925force_uncompressed = true;926}927}928929if (force_uncompressed) {930_save_ctex(image, p_save_path + ".ctex", COMPRESS_VRAM_UNCOMPRESSED, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,931mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);932} else {933if (can_s3tc_bptc) {934Image::CompressMode image_compress_mode;935String image_compress_format;936if (high_quality || is_hdr) {937image_compress_mode = Image::COMPRESS_BPTC;938image_compress_format = "bptc";939} else {940image_compress_mode = Image::COMPRESS_S3TC;941image_compress_format = "s3tc";942}943944_save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps,945stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);946r_platform_variants->push_back(image_compress_format);947}948949if (can_etc2_astc) {950Image::CompressMode image_compress_mode;951String image_compress_format;952if (high_quality || is_hdr) {953image_compress_mode = Image::COMPRESS_ASTC;954image_compress_format = "astc";955} else {956image_compress_mode = Image::COMPRESS_ETC2;957image_compress_format = "etc2";958}959960_save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps, stream, detect_3d,961detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);962r_platform_variants->push_back(image_compress_format);963}964}965} else {966// Import normally.967_save_ctex(image, p_save_path + ".ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,968mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);969}970971if (editor_image.is_valid()) {972_save_ctex(editor_image, p_save_path + ".editor.ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,973mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);974975// Generate and save editor-specific metadata, which we cannot save to the .import file.976Dictionary editor_meta;977978if (use_editor_scale) {979editor_meta["editor_scale"] = EDSCALE;980}981982if (convert_editor_colors) {983editor_meta["editor_dark_theme"] = EditorThemeManager::is_dark_theme();984}985986_save_editor_meta(editor_meta, p_save_path + ".editor.meta");987}988989if (r_metadata) {990Dictionary meta;991meta["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED;992993if (formats_imported.size()) {994meta["imported_formats"] = formats_imported;995}996997if (editor_image.is_valid()) {998meta["has_editor_variant"] = true;999}10001001*r_metadata = meta;1002}10031004return OK;1005}10061007const char *ResourceImporterTexture::compression_formats[] = {1008"s3tc_bptc",1009"etc2_astc",1010nullptr1011};10121013String ResourceImporterTexture::get_import_settings_string() const {1014String s;10151016int index = 0;1017while (compression_formats[index]) {1018const String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);1019if (bool(GLOBAL_GET(setting_path))) {1020s += String(compression_formats[index]);1021}10221023index++;1024}10251026return s;1027}10281029bool ResourceImporterTexture::are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const {1030if (p_meta.has("has_editor_variant")) {1031String imported_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);1032if (!FileAccess::exists(imported_path)) {1033return false;1034}10351036String editor_meta_path = imported_path.replace(".editor.ctex", ".editor.meta");1037Dictionary editor_meta = _load_editor_meta(editor_meta_path);10381039if (editor_meta.has("editor_scale") && (float)editor_meta["editor_scale"] != EDSCALE) {1040return false;1041}10421043if (editor_meta.has("editor_dark_theme") && (bool)editor_meta["editor_dark_theme"] != EditorThemeManager::is_dark_theme()) {1044return false;1045}1046}10471048if (!p_meta.has("vram_texture")) {1049return false;1050}10511052if (!bool(p_meta["vram_texture"])) {1053return true; // Do not care about non-VRAM.1054}10551056// Will become invalid if formats are missing to import.1057Vector<String> formats_imported;1058if (p_meta.has("imported_formats")) {1059formats_imported = p_meta["imported_formats"];1060}10611062int index = 0;1063bool valid = true;1064while (compression_formats[index]) {1065const String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);1066if (ProjectSettings::get_singleton()->has_setting(setting_path)) {1067if (bool(GLOBAL_GET(setting_path)) && !formats_imported.has(compression_formats[index])) {1068valid = false;1069break;1070}1071} else {1072WARN_PRINT("Setting for imported format not found: " + setting_path);1073}10741075index++;1076}10771078return valid;1079}10801081ResourceImporterTexture *ResourceImporterTexture::singleton = nullptr;10821083ResourceImporterTexture::ResourceImporterTexture(bool p_singleton) {1084// This should only be set through the EditorNode.1085if (p_singleton) {1086singleton = this;1087}10881089CompressedTexture2D::request_3d_callback = _texture_reimport_3d;1090CompressedTexture2D::request_roughness_callback = _texture_reimport_roughness;1091CompressedTexture2D::request_normal_callback = _texture_reimport_normal;1092}10931094ResourceImporterTexture::~ResourceImporterTexture() {1095if (singleton == this) {1096singleton = nullptr;1097}1098}109911001101