resource_importer_texture.cpp 20 KB

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