resource_importer_texture.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*************************************************************************/
  2. /* resource_importer_texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/io/config_file.h"
  32. #include "core/io/image_loader.h"
  33. #include "core/version.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. void ResourceImporterTexture::_texture_reimport_roughness(const Ref<StreamTexture> &p_tex, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_channel) {
  37. MutexLock lock(singleton->mutex);
  38. StringName path = p_tex->get_path();
  39. if (!singleton->make_flags.has(path)) {
  40. singleton->make_flags[path] = MakeInfo();
  41. }
  42. singleton->make_flags[path].flags |= MAKE_ROUGHNESS_FLAG;
  43. singleton->make_flags[path].channel_for_roughness = p_channel;
  44. singleton->make_flags[path].normal_path_for_roughness = p_normal_path;
  45. }
  46. void ResourceImporterTexture::_texture_reimport_3d(const Ref<StreamTexture> &p_tex) {
  47. MutexLock lock(singleton->mutex);
  48. StringName path = p_tex->get_path();
  49. if (!singleton->make_flags.has(path)) {
  50. singleton->make_flags[path] = MakeInfo();
  51. }
  52. singleton->make_flags[path].flags |= MAKE_3D_FLAG;
  53. }
  54. void ResourceImporterTexture::_texture_reimport_normal(const Ref<StreamTexture> &p_tex) {
  55. MutexLock lock(singleton->mutex);
  56. StringName path = p_tex->get_path();
  57. if (!singleton->make_flags.has(path)) {
  58. singleton->make_flags[path] = MakeInfo();
  59. }
  60. singleton->make_flags[path].flags |= MAKE_NORMAL_FLAG;
  61. }
  62. void ResourceImporterTexture::update_imports() {
  63. if (EditorFileSystem::get_singleton()->is_scanning() || EditorFileSystem::get_singleton()->is_importing()) {
  64. return; // do nothing for now
  65. }
  66. MutexLock lock(mutex);
  67. Vector<String> to_reimport;
  68. {
  69. if (make_flags.empty()) {
  70. return;
  71. }
  72. for (Map<StringName, MakeInfo>::Element *E = make_flags.front(); E; E = E->next()) {
  73. Ref<ConfigFile> cf;
  74. cf.instance();
  75. String src_path = String(E->key()) + ".import";
  76. Error err = cf->load(src_path);
  77. ERR_CONTINUE(err != OK);
  78. bool changed = false;
  79. if (E->get().flags & MAKE_NORMAL_FLAG && int(cf->get_value("params", "compress/normal_map")) == 0) {
  80. cf->set_value("params", "compress/normal_map", 1);
  81. changed = true;
  82. }
  83. if (E->get().flags & MAKE_ROUGHNESS_FLAG && int(cf->get_value("params", "roughness/mode")) == 0) {
  84. cf->set_value("params", "roughness/mode", E->get().channel_for_roughness + 2);
  85. cf->set_value("params", "roughness/src_normal", E->get().normal_path_for_roughness);
  86. changed = true;
  87. }
  88. if (E->get().flags & MAKE_3D_FLAG && bool(cf->get_value("params", "detect_3d/compress_to"))) {
  89. int compress_to = cf->get_value("params", "detect_3d/compress_to");
  90. cf->set_value("params", "detect_3d/compress_to", 0);
  91. if (compress_to == 1) {
  92. cf->set_value("params", "compress/mode", COMPRESS_VRAM_COMPRESSED);
  93. } else if (compress_to == 2) {
  94. cf->set_value("params", "compress/mode", COMPRESS_BASIS_UNIVERSAL);
  95. }
  96. cf->set_value("params", "mipmaps/generate", true);
  97. changed = true;
  98. }
  99. if (changed) {
  100. cf->save(src_path);
  101. to_reimport.push_back(E->key());
  102. }
  103. }
  104. make_flags.clear();
  105. }
  106. if (to_reimport.size()) {
  107. EditorFileSystem::get_singleton()->reimport_files(to_reimport);
  108. }
  109. }
  110. String ResourceImporterTexture::get_importer_name() const {
  111. return "texture";
  112. }
  113. String ResourceImporterTexture::get_visible_name() const {
  114. return "Texture2D";
  115. }
  116. void ResourceImporterTexture::get_recognized_extensions(List<String> *p_extensions) const {
  117. ImageLoader::get_recognized_extensions(p_extensions);
  118. }
  119. String ResourceImporterTexture::get_save_extension() const {
  120. return "stex";
  121. }
  122. String ResourceImporterTexture::get_resource_type() const {
  123. return "StreamTexture";
  124. }
  125. bool ResourceImporterTexture::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  126. if (p_option == "compress/lossy_quality") {
  127. int compress_mode = int(p_options["compress/mode"]);
  128. if (compress_mode != COMPRESS_LOSSY && compress_mode != COMPRESS_VRAM_COMPRESSED) {
  129. return false;
  130. }
  131. } else if (p_option == "compress/hdr_mode") {
  132. int compress_mode = int(p_options["compress/mode"]);
  133. if (compress_mode < COMPRESS_VRAM_COMPRESSED) {
  134. return false;
  135. }
  136. } else if (p_option == "mipmaps/limit") {
  137. return p_options["mipmaps/generate"];
  138. } else if (p_option == "compress/bptc_ldr") {
  139. int compress_mode = int(p_options["compress/mode"]);
  140. if (compress_mode < COMPRESS_VRAM_COMPRESSED) {
  141. return false;
  142. }
  143. if (!ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc")) {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. int ResourceImporterTexture::get_preset_count() const {
  150. return 4;
  151. }
  152. String ResourceImporterTexture::get_preset_name(int p_idx) const {
  153. static const char *preset_names[] = {
  154. "2D, Detect 3D",
  155. "2D",
  156. "2D Pixel",
  157. "3D"
  158. };
  159. return preset_names[p_idx];
  160. }
  161. void ResourceImporterTexture::get_import_options(List<ImportOption> *r_options, int p_preset) const {
  162. 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));
  163. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));
  164. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_mode", PROPERTY_HINT_ENUM, "Enabled,Force RGBE"), 0));
  165. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/bptc_ldr", PROPERTY_HINT_ENUM, "Enabled,RGBA Only"), 0));
  166. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/normal_map", PROPERTY_HINT_ENUM, "Detect,Enable,Disabled"), 0));
  167. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized"), 0));
  168. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/streamed"), false));
  169. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "mipmaps/generate"), (p_preset == PRESET_3D ? true : false)));
  170. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "-1,256"), -1));
  171. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "roughness/mode", PROPERTY_HINT_ENUM, "Detect,Disabled,Red,Green,Blue,Alpha,Gray"), 0));
  172. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "roughness/src_normal", PROPERTY_HINT_FILE, "*.png,*.jpg"), ""));
  173. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/fix_alpha_border"), p_preset != PRESET_3D));
  174. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/premult_alpha"), false));
  175. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/invert_color"), false));
  176. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/HDR_as_SRGB"), false));
  177. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/size_limit", PROPERTY_HINT_RANGE, "0,4096,1"), 0));
  178. 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));
  179. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "svg/scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0));
  180. }
  181. void ResourceImporterTexture::save_to_stex_format(FileAccess *f, const Ref<Image> &p_image, CompressMode p_compress_mode, Image::UsedChannels p_channels, Image::CompressMode p_compress_format, float p_lossy_quality, bool p_force_rgbe) {
  182. switch (p_compress_mode) {
  183. case COMPRESS_LOSSLESS: {
  184. f->store_32(StreamTexture::DATA_FORMAT_LOSSLESS);
  185. f->store_16(p_image->get_width());
  186. f->store_16(p_image->get_height());
  187. f->store_32(p_image->get_mipmap_count());
  188. f->store_32(p_image->get_format());
  189. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  190. Vector<uint8_t> data = Image::lossless_packer(p_image->get_image_from_mipmap(i));
  191. int data_len = data.size();
  192. f->store_32(data_len);
  193. const uint8_t *r = data.ptr();
  194. f->store_buffer(r, data_len);
  195. }
  196. } break;
  197. case COMPRESS_LOSSY: {
  198. f->store_32(StreamTexture::DATA_FORMAT_LOSSY);
  199. f->store_16(p_image->get_width());
  200. f->store_16(p_image->get_height());
  201. f->store_32(p_image->get_mipmap_count());
  202. f->store_32(p_image->get_format());
  203. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  204. Vector<uint8_t> data = Image::lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality);
  205. int data_len = data.size();
  206. f->store_32(data_len);
  207. const uint8_t *r = data.ptr();
  208. f->store_buffer(r, data_len);
  209. }
  210. } break;
  211. case COMPRESS_VRAM_COMPRESSED: {
  212. Ref<Image> image = p_image->duplicate();
  213. if (p_force_rgbe && image->get_format() >= Image::FORMAT_RF && image->get_format() < Image::FORMAT_RGBE9995) {
  214. image->convert(Image::FORMAT_RGBE9995);
  215. } else {
  216. image->compress_from_channels(p_compress_format, p_channels, p_lossy_quality);
  217. }
  218. f->store_32(StreamTexture::DATA_FORMAT_IMAGE);
  219. f->store_16(image->get_width());
  220. f->store_16(image->get_height());
  221. f->store_32(image->get_mipmap_count());
  222. f->store_32(image->get_format());
  223. Vector<uint8_t> data = image->get_data();
  224. int dl = data.size();
  225. const uint8_t *r = data.ptr();
  226. f->store_buffer(r, dl);
  227. } break;
  228. case COMPRESS_VRAM_UNCOMPRESSED: {
  229. f->store_32(StreamTexture::DATA_FORMAT_IMAGE);
  230. f->store_16(p_image->get_width());
  231. f->store_16(p_image->get_height());
  232. f->store_32(p_image->get_mipmap_count());
  233. f->store_32(p_image->get_format());
  234. Vector<uint8_t> data = p_image->get_data();
  235. int dl = data.size();
  236. const uint8_t *r = data.ptr();
  237. f->store_buffer(r, dl);
  238. } break;
  239. case COMPRESS_BASIS_UNIVERSAL: {
  240. f->store_32(StreamTexture::DATA_FORMAT_BASIS_UNIVERSAL);
  241. f->store_16(p_image->get_width());
  242. f->store_16(p_image->get_height());
  243. f->store_32(p_image->get_mipmap_count());
  244. f->store_32(p_image->get_format());
  245. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  246. Vector<uint8_t> data = Image::basis_universal_packer(p_image->get_image_from_mipmap(i), p_channels);
  247. int data_len = data.size();
  248. f->store_32(data_len);
  249. const uint8_t *r = data.ptr();
  250. f->store_buffer(r, data_len);
  251. }
  252. } break;
  253. }
  254. }
  255. void ResourceImporterTexture::_save_stex(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_force_rgbe, 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) {
  256. FileAccess *f = FileAccess::open(p_to_path, FileAccess::WRITE);
  257. f->store_8('G');
  258. f->store_8('S');
  259. f->store_8('T');
  260. f->store_8('2'); //godot streamable texture 2D
  261. //format version
  262. f->store_32(StreamTexture::FORMAT_VERSION);
  263. //texture may be resized later, so original size must be saved first
  264. f->store_32(p_image->get_width());
  265. f->store_32(p_image->get_height());
  266. uint32_t flags = 0;
  267. if (p_streamable)
  268. flags |= StreamTexture::FORMAT_BIT_STREAM;
  269. if (p_mipmaps)
  270. flags |= StreamTexture::FORMAT_BIT_HAS_MIPMAPS; //mipmaps bit
  271. if (p_detect_3d)
  272. flags |= StreamTexture::FORMAT_BIT_DETECT_3D;
  273. if (p_detect_roughness)
  274. flags |= StreamTexture::FORMAT_BIT_DETECT_ROUGNESS;
  275. if (p_detect_normal)
  276. flags |= StreamTexture::FORMAT_BIT_DETECT_NORMAL;
  277. f->store_32(flags);
  278. f->store_32(p_limit_mipmap);
  279. //reserved for future use
  280. f->store_32(0);
  281. f->store_32(0);
  282. f->store_32(0);
  283. /*
  284. print_line("streamable " + itos(p_streamable));
  285. print_line("mipmaps " + itos(p_mipmaps));
  286. print_line("detect_3d " + itos(p_detect_3d));
  287. print_line("roughness " + itos(p_detect_roughness));
  288. print_line("normal " + itos(p_detect_normal));
  289. */
  290. if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() > Image::FORMAT_RGBA8) {
  291. p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED; //these can't go as lossy
  292. }
  293. Ref<Image> image = p_image->duplicate();
  294. if (((p_compress_mode == COMPRESS_BASIS_UNIVERSAL) || (p_compress_mode == COMPRESS_VRAM_COMPRESSED && p_force_po2_for_compressed)) && p_mipmaps) {
  295. image->resize_to_po2();
  296. }
  297. if (p_mipmaps && (!image->has_mipmaps() || p_force_normal)) {
  298. image->generate_mipmaps(p_force_normal);
  299. }
  300. if (!p_mipmaps) {
  301. image->clear_mipmaps();
  302. }
  303. if (image->has_mipmaps() && p_normal.is_valid()) {
  304. image->generate_mipmap_roughness(p_roughness_channel, p_normal);
  305. }
  306. if (p_force_rgbe && image->get_format() >= Image::FORMAT_RF && image->get_format() < Image::FORMAT_RGBE9995) {
  307. image->convert(Image::FORMAT_RGBE9995);
  308. }
  309. Image::CompressSource csource = Image::COMPRESS_SOURCE_GENERIC;
  310. if (p_force_normal) {
  311. csource = Image::COMPRESS_SOURCE_NORMAL;
  312. } else if (p_srgb_friendly) {
  313. csource = Image::COMPRESS_SOURCE_SRGB;
  314. }
  315. Image::UsedChannels used_channels = image->detect_used_channels(csource);
  316. save_to_stex_format(f, image, p_compress_mode, used_channels, p_vram_compression, p_lossy_quality, p_force_rgbe);
  317. memdelete(f);
  318. }
  319. Error ResourceImporterTexture::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  320. CompressMode compress_mode = CompressMode(int(p_options["compress/mode"]));
  321. float lossy = p_options["compress/lossy_quality"];
  322. int pack_channels = p_options["compress/channel_pack"];
  323. bool mipmaps = p_options["mipmaps/generate"];
  324. uint32_t mipmap_limit = int(mipmaps ? int(p_options["mipmaps/limit"]) : int(-1));
  325. bool fix_alpha_border = p_options["process/fix_alpha_border"];
  326. bool premult_alpha = p_options["process/premult_alpha"];
  327. bool invert_color = p_options["process/invert_color"];
  328. bool stream = p_options["compress/streamed"];
  329. int size_limit = p_options["process/size_limit"];
  330. bool hdr_as_srgb = p_options["process/HDR_as_SRGB"];
  331. int normal = p_options["compress/normal_map"];
  332. float scale = p_options["svg/scale"];
  333. bool force_rgbe = int(p_options["compress/hdr_mode"]) == 1;
  334. int bptc_ldr = p_options["compress/bptc_ldr"];
  335. int roughness = p_options["roughness/mode"];
  336. String normal_map = p_options["roughness/src_normal"];
  337. Ref<Image> normal_image;
  338. Image::RoughnessChannel roughness_channel;
  339. if (mipmaps && roughness > 1 && FileAccess::exists(normal_map)) {
  340. normal_image.instance();
  341. if (ImageLoader::load_image(normal_map, normal_image) == OK) {
  342. roughness_channel = Image::RoughnessChannel(roughness - 2);
  343. }
  344. }
  345. Ref<Image> image;
  346. image.instance();
  347. Error err = ImageLoader::load_image(p_source_file, image, NULL, hdr_as_srgb, scale);
  348. if (err != OK)
  349. return err;
  350. Array formats_imported;
  351. if (size_limit > 0 && (image->get_width() > size_limit || image->get_height() > size_limit)) {
  352. //limit size
  353. if (image->get_width() >= image->get_height()) {
  354. int new_width = size_limit;
  355. int new_height = image->get_height() * new_width / image->get_width();
  356. image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);
  357. } else {
  358. int new_height = size_limit;
  359. int new_width = image->get_width() * new_height / image->get_height();
  360. image->resize(new_width, new_height, Image::INTERPOLATE_CUBIC);
  361. }
  362. if (normal == 1) {
  363. image->normalize();
  364. }
  365. }
  366. if (fix_alpha_border) {
  367. image->fix_alpha_edges();
  368. }
  369. if (premult_alpha) {
  370. image->premultiply_alpha();
  371. }
  372. if (invert_color) {
  373. int height = image->get_height();
  374. int width = image->get_width();
  375. for (int i = 0; i < width; i++) {
  376. for (int j = 0; j < height; j++) {
  377. image->set_pixel(i, j, image->get_pixel(i, j).inverted());
  378. }
  379. }
  380. }
  381. if (compress_mode == COMPRESS_BASIS_UNIVERSAL && image->get_format() >= Image::FORMAT_RF) {
  382. //basis universal does not support float formats, fall back
  383. compress_mode = COMPRESS_VRAM_COMPRESSED;
  384. }
  385. bool detect_3d = int(p_options["detect_3d/compress_to"]) > 0;
  386. bool detect_roughness = roughness == 0;
  387. bool detect_normal = normal == 0;
  388. bool force_normal = normal == 1;
  389. bool srgb_friendly_pack = pack_channels == 0;
  390. if (compress_mode == COMPRESS_VRAM_COMPRESSED) {
  391. //must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc).
  392. //Android, GLES 2.x
  393. bool ok_on_pc = false;
  394. bool is_hdr = (image->get_format() >= Image::FORMAT_RF && image->get_format() <= Image::FORMAT_RGBE9995);
  395. bool is_ldr = (image->get_format() >= Image::FORMAT_L8 && image->get_format() <= Image::FORMAT_RGB565);
  396. bool can_bptc = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc");
  397. bool can_s3tc = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_s3tc");
  398. if (can_bptc) {
  399. Image::UsedChannels channels = image->detect_used_channels();
  400. if (is_hdr) {
  401. if (channels == Image::USED_CHANNELS_LA || channels == Image::USED_CHANNELS_RGBA) {
  402. can_bptc = false;
  403. }
  404. } else if (is_ldr) {
  405. //handle "RGBA Only" setting
  406. if (bptc_ldr == 1 && channels != Image::USED_CHANNELS_LA && channels != Image::USED_CHANNELS_RGBA) {
  407. can_bptc = false;
  408. }
  409. }
  410. formats_imported.push_back("bptc");
  411. }
  412. if (!can_bptc && is_hdr && !force_rgbe) {
  413. //convert to ldr if this can't be stored hdr
  414. image->convert(Image::FORMAT_RGBA8);
  415. }
  416. if (can_bptc || can_s3tc) {
  417. _save_stex(image, p_save_path + ".s3tc.stex", compress_mode, lossy, can_bptc ? Image::COMPRESS_BPTC : Image::COMPRESS_S3TC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  418. r_platform_variants->push_back("s3tc");
  419. formats_imported.push_back("s3tc");
  420. ok_on_pc = true;
  421. }
  422. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc2")) {
  423. _save_stex(image, p_save_path + ".etc2.stex", compress_mode, lossy, Image::COMPRESS_ETC2, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit, normal_image, roughness_channel);
  424. r_platform_variants->push_back("etc2");
  425. formats_imported.push_back("etc2");
  426. }
  427. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc")) {
  428. _save_stex(image, p_save_path + ".etc.stex", compress_mode, lossy, Image::COMPRESS_ETC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit, normal_image, roughness_channel);
  429. r_platform_variants->push_back("etc");
  430. formats_imported.push_back("etc");
  431. }
  432. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_pvrtc")) {
  433. _save_stex(image, p_save_path + ".pvrtc.stex", compress_mode, lossy, Image::COMPRESS_PVRTC4, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit, normal_image, roughness_channel);
  434. r_platform_variants->push_back("pvrtc");
  435. formats_imported.push_back("pvrtc");
  436. }
  437. if (!ok_on_pc) {
  438. EditorNode::add_io_error("Warning, no suitable PC VRAM compression enabled in Project Settings. This texture will not display correctly on PC.");
  439. }
  440. } else {
  441. //import normally
  442. _save_stex(image, p_save_path + ".stex", compress_mode, lossy, Image::COMPRESS_S3TC /*this is ignored */, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
  443. }
  444. if (r_metadata) {
  445. Dictionary metadata;
  446. metadata["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED;
  447. if (formats_imported.size()) {
  448. metadata["imported_formats"] = formats_imported;
  449. }
  450. *r_metadata = metadata;
  451. }
  452. return OK;
  453. }
  454. const char *ResourceImporterTexture::compression_formats[] = {
  455. "bptc",
  456. "s3tc",
  457. "etc",
  458. "etc2",
  459. "pvrtc",
  460. NULL
  461. };
  462. String ResourceImporterTexture::get_import_settings_string() const {
  463. String s;
  464. int index = 0;
  465. while (compression_formats[index]) {
  466. String setting_path = "rendering/vram_compression/import_" + String(compression_formats[index]);
  467. bool test = ProjectSettings::get_singleton()->get(setting_path);
  468. if (test) {
  469. s += String(compression_formats[index]);
  470. }
  471. index++;
  472. }
  473. return s;
  474. }
  475. bool ResourceImporterTexture::are_import_settings_valid(const String &p_path) const {
  476. //will become invalid if formats are missing to import
  477. Dictionary metadata = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);
  478. if (!metadata.has("vram_texture")) {
  479. return false;
  480. }
  481. bool vram = metadata["vram_texture"];
  482. if (!vram) {
  483. return true; //do not care about non vram
  484. }
  485. Vector<String> formats_imported;
  486. if (metadata.has("imported_formats")) {
  487. formats_imported = metadata["imported_formats"];
  488. }
  489. int index = 0;
  490. bool valid = true;
  491. while (compression_formats[index]) {
  492. String setting_path = "rendering/vram_compression/import_" + String(compression_formats[index]);
  493. bool test = ProjectSettings::get_singleton()->get(setting_path);
  494. if (test) {
  495. if (formats_imported.find(compression_formats[index]) == -1) {
  496. valid = false;
  497. break;
  498. }
  499. }
  500. index++;
  501. }
  502. return valid;
  503. }
  504. ResourceImporterTexture *ResourceImporterTexture::singleton = NULL;
  505. ResourceImporterTexture::ResourceImporterTexture() {
  506. singleton = this;
  507. StreamTexture::request_3d_callback = _texture_reimport_3d;
  508. StreamTexture::request_roughness_callback = _texture_reimport_roughness;
  509. StreamTexture::request_normal_callback = _texture_reimport_normal;
  510. }
  511. ResourceImporterTexture::~ResourceImporterTexture() {
  512. }