resource_importer_texture.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /**************************************************************************/
  2. /* resource_importer_texture.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "resource_importer_texture.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/image_loader.h"
  34. #include "core/version.h"
  35. #include "editor/editor_file_system.h"
  36. #include "editor/gui/editor_toaster.h"
  37. #include "editor/import/resource_importer_texture_settings.h"
  38. #include "editor/themes/editor_scale.h"
  39. #include "editor/themes/editor_theme_manager.h"
  40. #include "scene/resources/compressed_texture.h"
  41. void ResourceImporterTexture::_texture_reimport_roughness(const Ref<CompressedTexture2D> &p_tex, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_channel) {
  42. ERR_FAIL_COND(p_tex.is_null());
  43. MutexLock lock(singleton->mutex);
  44. StringName path = p_tex->get_path();
  45. if (!singleton->make_flags.has(path)) {
  46. singleton->make_flags[path] = MakeInfo();
  47. }
  48. singleton->make_flags[path].flags |= MAKE_ROUGHNESS_FLAG;
  49. singleton->make_flags[path].channel_for_roughness = p_channel;
  50. singleton->make_flags[path].normal_path_for_roughness = p_normal_path;
  51. }
  52. void ResourceImporterTexture::_texture_reimport_3d(const Ref<CompressedTexture2D> &p_tex) {
  53. ERR_FAIL_COND(p_tex.is_null());
  54. MutexLock lock(singleton->mutex);
  55. StringName path = p_tex->get_path();
  56. if (!singleton->make_flags.has(path)) {
  57. singleton->make_flags[path] = MakeInfo();
  58. }
  59. singleton->make_flags[path].flags |= MAKE_3D_FLAG;
  60. }
  61. void ResourceImporterTexture::_texture_reimport_normal(const Ref<CompressedTexture2D> &p_tex) {
  62. ERR_FAIL_COND(p_tex.is_null());
  63. MutexLock lock(singleton->mutex);
  64. StringName path = p_tex->get_path();
  65. if (!singleton->make_flags.has(path)) {
  66. singleton->make_flags[path] = MakeInfo();
  67. }
  68. singleton->make_flags[path].flags |= MAKE_NORMAL_FLAG;
  69. }
  70. void ResourceImporterTexture::update_imports() {
  71. if (EditorFileSystem::get_singleton()->is_scanning() || EditorFileSystem::get_singleton()->is_importing()) {
  72. return; // Don't update when EditorFileSystem is doing something else.
  73. }
  74. MutexLock lock(mutex);
  75. Vector<String> to_reimport;
  76. if (make_flags.is_empty()) {
  77. return;
  78. }
  79. for (const KeyValue<StringName, MakeInfo> &E : make_flags) {
  80. Ref<ConfigFile> cf;
  81. cf.instantiate();
  82. String src_path = String(E.key) + ".import";
  83. Error err = cf->load(src_path);
  84. ERR_CONTINUE(err != OK);
  85. bool changed = false;
  86. if (E.value.flags & MAKE_NORMAL_FLAG && int(cf->get_value("params", "compress/normal_map")) == 0) {
  87. print_line(
  88. vformat(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)."),
  89. String(E.key)));
  90. cf->set_value("params", "compress/normal_map", 1);
  91. changed = true;
  92. }
  93. if (E.value.flags & MAKE_ROUGHNESS_FLAG && int(cf->get_value("params", "roughness/mode")) == 0) {
  94. print_line(
  95. vformat(TTR("%s: Texture detected as used as a roughness map in 3D. Enabling roughness limiter based on the detected associated normal map at %s."),
  96. String(E.key), E.value.normal_path_for_roughness));
  97. cf->set_value("params", "roughness/mode", E.value.channel_for_roughness + 2);
  98. cf->set_value("params", "roughness/src_normal", E.value.normal_path_for_roughness);
  99. changed = true;
  100. }
  101. if (E.value.flags & MAKE_3D_FLAG && bool(cf->get_value("params", "detect_3d/compress_to"))) {
  102. const int compress_to = cf->get_value("params", "detect_3d/compress_to");
  103. // 3D detected, disable the callback.
  104. cf->set_value("params", "detect_3d/compress_to", 0);
  105. String compress_string;
  106. if (compress_to == 1) {
  107. cf->set_value("params", "compress/mode", COMPRESS_VRAM_COMPRESSED);
  108. compress_string = "VRAM Compressed (S3TC/ETC/BPTC)";
  109. } else if (compress_to == 2) {
  110. cf->set_value("params", "compress/mode", COMPRESS_BASIS_UNIVERSAL);
  111. compress_string = "Basis Universal";
  112. }
  113. print_line(
  114. vformat(TTR("%s: Texture detected as used in 3D. Enabling mipmap generation and setting the texture compression mode to %s."),
  115. String(E.key), compress_string));
  116. cf->set_value("params", "mipmaps/generate", true);
  117. changed = true;
  118. }
  119. if (changed) {
  120. cf->save(src_path);
  121. to_reimport.push_back(E.key);
  122. }
  123. }
  124. make_flags.clear();
  125. if (!to_reimport.is_empty()) {
  126. EditorFileSystem::get_singleton()->reimport_files(to_reimport);
  127. }
  128. }
  129. String ResourceImporterTexture::get_importer_name() const {
  130. return "texture";
  131. }
  132. String ResourceImporterTexture::get_visible_name() const {
  133. return "Texture2D";
  134. }
  135. void ResourceImporterTexture::get_recognized_extensions(List<String> *p_extensions) const {
  136. ImageLoader::get_recognized_extensions(p_extensions);
  137. }
  138. String ResourceImporterTexture::get_save_extension() const {
  139. return "ctex";
  140. }
  141. String ResourceImporterTexture::get_resource_type() const {
  142. return "CompressedTexture2D";
  143. }
  144. bool ResourceImporterTexture::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  145. if (p_option == "compress/high_quality" || p_option == "compress/hdr_compression") {
  146. int compress_mode = int(p_options["compress/mode"]);
  147. if (compress_mode != COMPRESS_VRAM_COMPRESSED) {
  148. return false;
  149. }
  150. } else if (p_option == "compress/lossy_quality") {
  151. int compress_mode = int(p_options["compress/mode"]);
  152. if (compress_mode != COMPRESS_LOSSY) {
  153. return false;
  154. }
  155. } else if (p_option == "compress/hdr_mode") {
  156. int compress_mode = int(p_options["compress/mode"]);
  157. if (compress_mode < COMPRESS_VRAM_COMPRESSED) {
  158. return false;
  159. }
  160. } else if (p_option == "compress/normal_map") {
  161. int compress_mode = int(p_options["compress/mode"]);
  162. if (compress_mode == COMPRESS_LOSSLESS) {
  163. return false;
  164. }
  165. } else if (p_option == "mipmaps/limit") {
  166. return p_options["mipmaps/generate"];
  167. } else if (p_option == "compress/uastc_level" || p_option == "compress/rdo_quality_loss") {
  168. return int(p_options["compress/mode"]) == COMPRESS_BASIS_UNIVERSAL;
  169. }
  170. return true;
  171. }
  172. int ResourceImporterTexture::get_preset_count() const {
  173. return 3;
  174. }
  175. String ResourceImporterTexture::get_preset_name(int p_idx) const {
  176. static const char *preset_names[] = {
  177. TTRC("2D/3D (Auto-Detect)"),
  178. TTRC("2D"),
  179. TTRC("3D"),
  180. };
  181. return TTRGET(preset_names[p_idx]);
  182. }
  183. void ResourceImporterTexture::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  184. r_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));
  185. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress/high_quality"), false));
  186. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));
  187. Image::BasisUniversalPackerParams basisu_params;
  188. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/uastc_level", PROPERTY_HINT_ENUM, "Fastest,Faster,Medium,Slower,Slowest"), basisu_params.uastc_level));
  189. r_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));
  190. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_compression", PROPERTY_HINT_ENUM, "Disabled,Opaque Only,Always"), 1));
  191. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/normal_map", PROPERTY_HINT_ENUM, "Detect,Enable,Disabled"), 0));
  192. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized"), 0));
  193. r_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)));
  194. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "-1,256"), -1));
  195. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "roughness/mode", PROPERTY_HINT_ENUM, "Detect,Disabled,Red,Green,Blue,Alpha,Gray"), 0));
  196. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "roughness/src_normal", PROPERTY_HINT_FILE, "*.bmp,*.dds,*.exr,*.jpeg,*.jpg,*.hdr,*.png,*.svg,*.tga,*.webp"), ""));
  197. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/fix_alpha_border"), p_preset != PRESET_3D));
  198. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/premult_alpha"), false));
  199. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/normal_map_invert_y"), false));
  200. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/hdr_as_srgb"), false));
  201. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/hdr_clamp_exposure"), false));
  202. // Maximum bound is the highest allowed value for lossy compression (the lowest common denominator).
  203. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/size_limit", PROPERTY_HINT_RANGE, "0,16383,1"), 0));
  204. r_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));
  205. // Do path based customization only if a path was passed.
  206. if (p_path.is_empty() || p_path.get_extension() == "svg") {
  207. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "svg/scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0));
  208. // Editor use only, applies to SVG.
  209. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "editor/scale_with_editor_scale"), false));
  210. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "editor/convert_colors_with_editor_theme"), false));
  211. }
  212. }
  213. void 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) {
  214. switch (p_compress_mode) {
  215. case COMPRESS_LOSSLESS: {
  216. bool lossless_force_png = GLOBAL_GET("rendering/textures/lossless_compression/force_png") || !Image::_webp_mem_loader_func; // WebP module disabled or png is forced.
  217. bool use_webp = !lossless_force_png && p_image->get_width() <= 16383 && p_image->get_height() <= 16383; // WebP has a size limit.
  218. f->store_32(use_webp ? CompressedTexture2D::DATA_FORMAT_WEBP : CompressedTexture2D::DATA_FORMAT_PNG);
  219. f->store_16(p_image->get_width());
  220. f->store_16(p_image->get_height());
  221. f->store_32(p_image->get_mipmap_count());
  222. f->store_32(p_image->get_format());
  223. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  224. Vector<uint8_t> data;
  225. if (use_webp) {
  226. data = Image::webp_lossless_packer(i ? p_image->get_image_from_mipmap(i) : p_image);
  227. } else {
  228. data = Image::png_packer(i ? p_image->get_image_from_mipmap(i) : p_image);
  229. }
  230. const uint64_t data_size = data.size();
  231. f->store_32(data_size);
  232. f->store_buffer(data.ptr(), data_size);
  233. }
  234. } break;
  235. case COMPRESS_LOSSY: {
  236. f->store_32(CompressedTexture2D::DATA_FORMAT_WEBP);
  237. f->store_16(p_image->get_width());
  238. f->store_16(p_image->get_height());
  239. f->store_32(p_image->get_mipmap_count());
  240. f->store_32(p_image->get_format());
  241. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  242. Vector<uint8_t> data = Image::webp_lossy_packer(i ? p_image->get_image_from_mipmap(i) : p_image, p_lossy_quality);
  243. const uint64_t data_size = data.size();
  244. f->store_32(data_size);
  245. f->store_buffer(data.ptr(), data_size);
  246. }
  247. } break;
  248. case COMPRESS_VRAM_COMPRESSED: {
  249. Ref<Image> image = p_image->duplicate();
  250. image->compress_from_channels(p_compress_format, p_channels);
  251. f->store_32(CompressedTexture2D::DATA_FORMAT_IMAGE);
  252. f->store_16(image->get_width());
  253. f->store_16(image->get_height());
  254. f->store_32(image->get_mipmap_count());
  255. f->store_32(image->get_format());
  256. f->store_buffer(image->get_data());
  257. } break;
  258. case COMPRESS_VRAM_UNCOMPRESSED: {
  259. f->store_32(CompressedTexture2D::DATA_FORMAT_IMAGE);
  260. f->store_16(p_image->get_width());
  261. f->store_16(p_image->get_height());
  262. f->store_32(p_image->get_mipmap_count());
  263. f->store_32(p_image->get_format());
  264. f->store_buffer(p_image->get_data());
  265. } break;
  266. case COMPRESS_BASIS_UNIVERSAL: {
  267. f->store_32(CompressedTexture2D::DATA_FORMAT_BASIS_UNIVERSAL);
  268. f->store_16(p_image->get_width());
  269. f->store_16(p_image->get_height());
  270. f->store_32(p_image->get_mipmap_count());
  271. f->store_32(p_image->get_format());
  272. Vector<uint8_t> data = Image::basis_universal_packer(p_image, p_channels, p_basisu_params);
  273. const uint64_t data_size = data.size();
  274. f->store_32(data_size);
  275. f->store_buffer(data.ptr(), data_size);
  276. } break;
  277. }
  278. }
  279. void 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) {
  280. Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);
  281. ERR_FAIL_COND(f.is_null());
  282. // Godot Streamable Texture 2D.
  283. f->store_8('G');
  284. f->store_8('S');
  285. f->store_8('T');
  286. f->store_8('2');
  287. // Current format version.
  288. f->store_32(CompressedTexture2D::FORMAT_VERSION);
  289. // Texture may be resized later, so original size must be saved first.
  290. f->store_32(p_image->get_width());
  291. f->store_32(p_image->get_height());
  292. uint32_t flags = 0;
  293. if (p_streamable) {
  294. flags |= CompressedTexture2D::FORMAT_BIT_STREAM;
  295. }
  296. if (p_mipmaps) {
  297. flags |= CompressedTexture2D::FORMAT_BIT_HAS_MIPMAPS;
  298. }
  299. if (p_detect_3d) {
  300. flags |= CompressedTexture2D::FORMAT_BIT_DETECT_3D;
  301. }
  302. if (p_detect_roughness) {
  303. flags |= CompressedTexture2D::FORMAT_BIT_DETECT_ROUGNESS;
  304. }
  305. if (p_detect_normal) {
  306. flags |= CompressedTexture2D::FORMAT_BIT_DETECT_NORMAL;
  307. }
  308. f->store_32(flags);
  309. f->store_32(p_limit_mipmap);
  310. // Reserved.
  311. f->store_32(0);
  312. f->store_32(0);
  313. f->store_32(0);
  314. if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() >= Image::FORMAT_RF) {
  315. p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED; //these can't go as lossy
  316. }
  317. Ref<Image> image = p_image->duplicate();
  318. if (p_mipmaps) {
  319. if (p_force_po2_for_compressed && (p_compress_mode == COMPRESS_BASIS_UNIVERSAL || p_compress_mode == COMPRESS_VRAM_COMPRESSED)) {
  320. image->resize_to_po2();
  321. }
  322. if (!image->has_mipmaps() || p_force_normal) {
  323. image->generate_mipmaps(p_force_normal);
  324. }
  325. } else {
  326. image->clear_mipmaps();
  327. }
  328. // Generate roughness mipmaps from normal texture.
  329. if (image->has_mipmaps() && p_normal.is_valid()) {
  330. image->generate_mipmap_roughness(p_roughness_channel, p_normal);
  331. }
  332. // Optimization: Only check for color channels when compressing as BasisU or VRAM.
  333. Image::UsedChannels used_channels = Image::USED_CHANNELS_RGBA;
  334. if (p_compress_mode == COMPRESS_BASIS_UNIVERSAL || p_compress_mode == COMPRESS_VRAM_COMPRESSED) {
  335. Image::CompressSource comp_source = Image::COMPRESS_SOURCE_GENERIC;
  336. if (p_force_normal) {
  337. comp_source = Image::COMPRESS_SOURCE_NORMAL;
  338. } else if (p_srgb_friendly) {
  339. comp_source = Image::COMPRESS_SOURCE_SRGB;
  340. }
  341. used_channels = image->detect_used_channels(comp_source);
  342. }
  343. save_to_ctex_format(f, image, p_compress_mode, used_channels, p_vram_compression, p_lossy_quality, p_basisu_params);
  344. }
  345. void ResourceImporterTexture::_save_editor_meta(const Dictionary &p_metadata, const String &p_to_path) {
  346. Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);
  347. ERR_FAIL_COND(f.is_null());
  348. f->store_var(p_metadata);
  349. }
  350. Dictionary ResourceImporterTexture::_load_editor_meta(const String &p_path) const {
  351. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  352. ERR_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));
  353. return f->get_var();
  354. }
  355. void ResourceImporterTexture::_invert_y_channel(Ref<Image> &r_image) {
  356. // Inverting the green channel can be used to flip a normal map's direction.
  357. // There's no standard when it comes to normal map Y direction, so this is
  358. // sometimes needed when using a normal map exported from another program.
  359. // See <http://wiki.polycount.com/wiki/Normal_Map_Technical_Details#Common_Swizzle_Coordinates>.
  360. const int height = r_image->get_height();
  361. const int width = r_image->get_width();
  362. for (int i = 0; i < width; i++) {
  363. for (int j = 0; j < height; j++) {
  364. const Color color = r_image->get_pixel(i, j);
  365. r_image->set_pixel(i, j, Color(color.r, 1 - color.g, color.b, color.a));
  366. }
  367. }
  368. }
  369. void ResourceImporterTexture::_clamp_hdr_exposure(Ref<Image> &r_image) {
  370. // Clamp HDR exposure following Filament's tonemapping formula.
  371. // This can be used to reduce fireflies in environment maps or reduce the influence
  372. // of the sun from an HDRI panorama on environment lighting (when a DirectionalLight3D is used instead).
  373. const int height = r_image->get_height();
  374. const int width = r_image->get_width();
  375. // These values are chosen arbitrarily and seem to produce good results with 4,096 samples.
  376. const float linear = 4096.0;
  377. const float compressed = 16384.0;
  378. for (int i = 0; i < width; i++) {
  379. for (int j = 0; j < height; j++) {
  380. const Color color = r_image->get_pixel(i, j);
  381. const float luma = color.get_luminance();
  382. Color clamped_color;
  383. if (luma <= linear) {
  384. clamped_color = color;
  385. } else {
  386. clamped_color = (color / luma) * ((linear * linear - compressed * luma) / (2 * linear - compressed - luma));
  387. }
  388. r_image->set_pixel(i, j, clamped_color);
  389. }
  390. }
  391. }
  392. Error 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) {
  393. // Parse import options.
  394. int32_t loader_flags = ImageFormatLoader::FLAG_NONE;
  395. // Compression.
  396. CompressMode compress_mode = CompressMode(int(p_options["compress/mode"]));
  397. const float lossy = p_options["compress/lossy_quality"];
  398. const int pack_channels = p_options["compress/channel_pack"];
  399. const int normal = p_options["compress/normal_map"];
  400. const int hdr_compression = p_options["compress/hdr_compression"];
  401. const int high_quality = p_options["compress/high_quality"];
  402. // Mipmaps.
  403. const bool mipmaps = p_options["mipmaps/generate"];
  404. const uint32_t mipmap_limit = mipmaps ? uint32_t(p_options["mipmaps/limit"]) : uint32_t(-1);
  405. // Roughness.
  406. const int roughness = p_options["roughness/mode"];
  407. const String normal_map = p_options["roughness/src_normal"];
  408. // Processing.
  409. const bool fix_alpha_border = p_options["process/fix_alpha_border"];
  410. const bool premult_alpha = p_options["process/premult_alpha"];
  411. const bool normal_map_invert_y = p_options["process/normal_map_invert_y"];
  412. const bool hdr_as_srgb = p_options["process/hdr_as_srgb"];
  413. const bool hdr_clamp_exposure = p_options["process/hdr_clamp_exposure"];
  414. int size_limit = p_options["process/size_limit"];
  415. const Image::BasisUniversalPackerParams basisu_params = {
  416. p_options["compress/uastc_level"],
  417. p_options["compress/rdo_quality_loss"],
  418. };
  419. bool using_fallback_size_limit = false;
  420. if (size_limit == 0) {
  421. using_fallback_size_limit = true;
  422. // If no size limit is defined, use a fallback size limit to prevent textures from looking incorrect or failing to import.
  423. switch (compress_mode) {
  424. case COMPRESS_LOSSY:
  425. // Maximum WebP size on either axis.
  426. size_limit = 16383;
  427. break;
  428. case COMPRESS_BASIS_UNIVERSAL:
  429. // Maximum Basis Universal size on either axis.
  430. size_limit = 16384;
  431. break;
  432. default:
  433. // As of June 2024, no GPU can correctly display a texture larger than 32768 pixels on either axis.
  434. size_limit = 32768;
  435. break;
  436. }
  437. }
  438. // Support for texture streaming is not implemented yet.
  439. const bool stream = false;
  440. // SVG-specific options.
  441. float scale = p_options.has("svg/scale") ? float(p_options["svg/scale"]) : 1.0f;
  442. // Editor-specific options.
  443. bool use_editor_scale = p_options.has("editor/scale_with_editor_scale") && p_options["editor/scale_with_editor_scale"];
  444. bool convert_editor_colors = p_options.has("editor/convert_colors_with_editor_theme") && p_options["editor/convert_colors_with_editor_theme"];
  445. if (hdr_as_srgb) {
  446. loader_flags |= ImageFormatLoader::FLAG_FORCE_LINEAR;
  447. }
  448. // Start importing images.
  449. LocalVector<Ref<Image>> images_imported;
  450. // Load the normal image.
  451. Ref<Image> normal_image;
  452. Image::RoughnessChannel roughness_channel = Image::ROUGHNESS_CHANNEL_R;
  453. if (mipmaps && roughness > 1 && FileAccess::exists(normal_map)) {
  454. normal_image.instantiate();
  455. if (ImageLoader::load_image(normal_map, normal_image) == OK) {
  456. roughness_channel = Image::RoughnessChannel(roughness - 2);
  457. }
  458. }
  459. // Load the main image.
  460. Ref<Image> image;
  461. image.instantiate();
  462. Error err = ImageLoader::load_image(p_source_file, image, nullptr, loader_flags, scale);
  463. if (err != OK) {
  464. return err;
  465. }
  466. images_imported.push_back(image);
  467. // Load the editor-only image.
  468. Ref<Image> editor_image;
  469. bool import_editor_image = use_editor_scale || convert_editor_colors;
  470. if (import_editor_image) {
  471. float editor_scale = use_editor_scale ? scale * EDSCALE : scale;
  472. int32_t editor_loader_flags = loader_flags;
  473. if (convert_editor_colors) {
  474. editor_loader_flags |= ImageFormatLoader::FLAG_CONVERT_COLORS;
  475. }
  476. editor_image.instantiate();
  477. err = ImageLoader::load_image(p_source_file, editor_image, nullptr, editor_loader_flags, editor_scale);
  478. if (err != OK) {
  479. WARN_PRINT(vformat("Failed to import an image resource for editor use from '%s'.", p_source_file));
  480. } else {
  481. images_imported.push_back(editor_image);
  482. }
  483. }
  484. for (Ref<Image> &target_image : images_imported) {
  485. // Apply the size limit.
  486. if (size_limit > 0 && (target_image->get_width() > size_limit || target_image->get_height() > size_limit)) {
  487. if (target_image->get_width() >= target_image->get_height()) {
  488. int new_width = size_limit;
  489. int new_height = target_image->get_height() * new_width / target_image->get_width();
  490. if (using_fallback_size_limit) {
  491. // Only warn if downsizing occurred when the user did not explicitly request it.
  492. WARN_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));
  493. }
  494. target_image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);
  495. } else {
  496. int new_height = size_limit;
  497. int new_width = target_image->get_width() * new_height / target_image->get_height();
  498. if (using_fallback_size_limit) {
  499. // Only warn if downsizing occurred when the user did not explicitly request it.
  500. WARN_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));
  501. }
  502. target_image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);
  503. }
  504. if (normal == 1) {
  505. target_image->normalize();
  506. }
  507. }
  508. // Fix alpha border.
  509. if (fix_alpha_border) {
  510. target_image->fix_alpha_edges();
  511. }
  512. // Premultiply the alpha.
  513. if (premult_alpha) {
  514. target_image->premultiply_alpha();
  515. }
  516. // Invert the green channel of the image to flip the normal map it contains.
  517. if (normal_map_invert_y) {
  518. _invert_y_channel(target_image);
  519. }
  520. // Clamp HDR exposure.
  521. if (hdr_clamp_exposure) {
  522. _clamp_hdr_exposure(target_image);
  523. }
  524. }
  525. bool detect_3d = int(p_options["detect_3d/compress_to"]) > 0;
  526. bool detect_roughness = roughness == 0;
  527. bool detect_normal = normal == 0;
  528. bool force_normal = normal == 1;
  529. bool srgb_friendly_pack = pack_channels == 0;
  530. Array formats_imported;
  531. if (compress_mode == COMPRESS_VRAM_COMPRESSED) {
  532. // 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).
  533. const bool is_hdr = (image->get_format() >= Image::FORMAT_RF && image->get_format() <= Image::FORMAT_RGBE9995);
  534. const bool can_s3tc_bptc = ResourceImporterTextureSettings::should_import_s3tc_bptc();
  535. const bool can_etc2_astc = ResourceImporterTextureSettings::should_import_etc2_astc();
  536. // Add list of formats imported.
  537. if (can_s3tc_bptc) {
  538. formats_imported.push_back("s3tc_bptc");
  539. }
  540. if (can_etc2_astc) {
  541. formats_imported.push_back("etc2_astc");
  542. }
  543. bool can_compress_hdr = hdr_compression > 0;
  544. bool has_alpha = image->detect_alpha() != Image::ALPHA_NONE;
  545. bool force_uncompressed = false;
  546. if (is_hdr) {
  547. if (has_alpha) {
  548. // Can compress HDR, but HDR with alpha is not compressible.
  549. if (hdr_compression == 2) {
  550. // But user selected to compress HDR anyway, so force an alpha-less format.
  551. if (image->get_format() == Image::FORMAT_RGBAF) {
  552. image->convert(Image::FORMAT_RGBF);
  553. } else if (image->get_format() == Image::FORMAT_RGBAH) {
  554. image->convert(Image::FORMAT_RGBH);
  555. }
  556. } else {
  557. can_compress_hdr = false;
  558. }
  559. }
  560. // Fall back to RGBE99995.
  561. if (!can_compress_hdr && image->get_format() != Image::FORMAT_RGBE9995) {
  562. image->convert(Image::FORMAT_RGBE9995);
  563. force_uncompressed = true;
  564. }
  565. }
  566. if (force_uncompressed) {
  567. _save_ctex(image, p_save_path + ".ctex", COMPRESS_VRAM_UNCOMPRESSED, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
  568. mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  569. } else {
  570. if (can_s3tc_bptc) {
  571. Image::CompressMode image_compress_mode;
  572. String image_compress_format;
  573. if (high_quality || is_hdr) {
  574. image_compress_mode = Image::COMPRESS_BPTC;
  575. image_compress_format = "bptc";
  576. } else {
  577. image_compress_mode = Image::COMPRESS_S3TC;
  578. image_compress_format = "s3tc";
  579. }
  580. _save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps,
  581. stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  582. r_platform_variants->push_back(image_compress_format);
  583. }
  584. if (can_etc2_astc) {
  585. Image::CompressMode image_compress_mode;
  586. String image_compress_format;
  587. if (high_quality || is_hdr) {
  588. image_compress_mode = Image::COMPRESS_ASTC;
  589. image_compress_format = "astc";
  590. } else {
  591. image_compress_mode = Image::COMPRESS_ETC2;
  592. image_compress_format = "etc2";
  593. }
  594. _save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps, stream, detect_3d,
  595. detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  596. r_platform_variants->push_back(image_compress_format);
  597. }
  598. }
  599. } else {
  600. // Import normally.
  601. _save_ctex(image, p_save_path + ".ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
  602. mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  603. }
  604. if (editor_image.is_valid()) {
  605. _save_ctex(editor_image, p_save_path + ".editor.ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
  606. mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  607. // Generate and save editor-specific metadata, which we cannot save to the .import file.
  608. Dictionary editor_meta;
  609. if (use_editor_scale) {
  610. editor_meta["editor_scale"] = EDSCALE;
  611. }
  612. if (convert_editor_colors) {
  613. editor_meta["editor_dark_theme"] = EditorThemeManager::is_dark_theme();
  614. }
  615. _save_editor_meta(editor_meta, p_save_path + ".editor.meta");
  616. }
  617. if (r_metadata) {
  618. Dictionary meta;
  619. meta["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED;
  620. if (formats_imported.size()) {
  621. meta["imported_formats"] = formats_imported;
  622. }
  623. if (editor_image.is_valid()) {
  624. meta["has_editor_variant"] = true;
  625. }
  626. *r_metadata = meta;
  627. }
  628. return OK;
  629. }
  630. const char *ResourceImporterTexture::compression_formats[] = {
  631. "s3tc_bptc",
  632. "etc2_astc",
  633. nullptr
  634. };
  635. String ResourceImporterTexture::get_import_settings_string() const {
  636. String s;
  637. int index = 0;
  638. while (compression_formats[index]) {
  639. const String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);
  640. if (bool(GLOBAL_GET(setting_path))) {
  641. s += String(compression_formats[index]);
  642. }
  643. index++;
  644. }
  645. return s;
  646. }
  647. bool ResourceImporterTexture::are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const {
  648. if (p_meta.has("has_editor_variant")) {
  649. String imported_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  650. if (!FileAccess::exists(imported_path)) {
  651. return false;
  652. }
  653. String editor_meta_path = imported_path.replace(".editor.ctex", ".editor.meta");
  654. Dictionary editor_meta = _load_editor_meta(editor_meta_path);
  655. if (editor_meta.has("editor_scale") && (float)editor_meta["editor_scale"] != EDSCALE) {
  656. return false;
  657. }
  658. if (editor_meta.has("editor_dark_theme") && (bool)editor_meta["editor_dark_theme"] != EditorThemeManager::is_dark_theme()) {
  659. return false;
  660. }
  661. }
  662. if (!p_meta.has("vram_texture")) {
  663. return false;
  664. }
  665. if (!bool(p_meta["vram_texture"])) {
  666. return true; // Do not care about non-VRAM.
  667. }
  668. // Will become invalid if formats are missing to import.
  669. Vector<String> formats_imported;
  670. if (p_meta.has("imported_formats")) {
  671. formats_imported = p_meta["imported_formats"];
  672. }
  673. int index = 0;
  674. bool valid = true;
  675. while (compression_formats[index]) {
  676. const String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);
  677. if (ProjectSettings::get_singleton()->has_setting(setting_path)) {
  678. if (bool(GLOBAL_GET(setting_path)) && !formats_imported.has(compression_formats[index])) {
  679. valid = false;
  680. break;
  681. }
  682. } else {
  683. WARN_PRINT("Setting for imported format not found: " + setting_path);
  684. }
  685. index++;
  686. }
  687. return valid;
  688. }
  689. ResourceImporterTexture *ResourceImporterTexture::singleton = nullptr;
  690. ResourceImporterTexture::ResourceImporterTexture(bool p_singleton) {
  691. // This should only be set through the EditorNode.
  692. if (p_singleton) {
  693. singleton = this;
  694. }
  695. CompressedTexture2D::request_3d_callback = _texture_reimport_3d;
  696. CompressedTexture2D::request_roughness_callback = _texture_reimport_roughness;
  697. CompressedTexture2D::request_normal_callback = _texture_reimport_normal;
  698. }
  699. ResourceImporterTexture::~ResourceImporterTexture() {
  700. if (singleton == this) {
  701. singleton = nullptr;
  702. }
  703. }