resource_importer_texture.cpp 32 KB

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