resource_importer_layered_texture.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*************************************************************************/
  2. /* resource_importer_layered_texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_layered_texture.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/error/error_macros.h"
  33. #include "core/io/config_file.h"
  34. #include "core/io/image_loader.h"
  35. #include "core/object/ref_counted.h"
  36. #include "editor/editor_file_system.h"
  37. #include "editor/editor_node.h"
  38. #include "resource_importer_texture.h"
  39. #include "scene/resources/texture.h"
  40. String ResourceImporterLayeredTexture::get_importer_name() const {
  41. switch (mode) {
  42. case MODE_CUBEMAP: {
  43. return "cubemap_texture";
  44. } break;
  45. case MODE_2D_ARRAY: {
  46. return "2d_array_texture";
  47. } break;
  48. case MODE_CUBEMAP_ARRAY: {
  49. return "cubemap_array_texture";
  50. } break;
  51. case MODE_3D: {
  52. return "3d_texture";
  53. } break;
  54. }
  55. ERR_FAIL_V("");
  56. }
  57. String ResourceImporterLayeredTexture::get_visible_name() const {
  58. switch (mode) {
  59. case MODE_CUBEMAP: {
  60. return "Cubemap";
  61. } break;
  62. case MODE_2D_ARRAY: {
  63. return "Texture2DArray";
  64. } break;
  65. case MODE_CUBEMAP_ARRAY: {
  66. return "CubemapArray";
  67. } break;
  68. case MODE_3D: {
  69. return "Texture3D";
  70. } break;
  71. }
  72. ERR_FAIL_V("");
  73. }
  74. void ResourceImporterLayeredTexture::get_recognized_extensions(List<String> *p_extensions) const {
  75. ImageLoader::get_recognized_extensions(p_extensions);
  76. }
  77. String ResourceImporterLayeredTexture::get_save_extension() const {
  78. switch (mode) {
  79. case MODE_CUBEMAP: {
  80. return "ccube";
  81. } break;
  82. case MODE_2D_ARRAY: {
  83. return "ctexarray";
  84. } break;
  85. case MODE_CUBEMAP_ARRAY: {
  86. return "ccubearray";
  87. } break;
  88. case MODE_3D: {
  89. return "ctex3d";
  90. } break;
  91. }
  92. ERR_FAIL_V(String());
  93. }
  94. String ResourceImporterLayeredTexture::get_resource_type() const {
  95. switch (mode) {
  96. case MODE_CUBEMAP: {
  97. return "CompressedCubemap";
  98. } break;
  99. case MODE_2D_ARRAY: {
  100. return "CompressedTexture2DArray";
  101. } break;
  102. case MODE_CUBEMAP_ARRAY: {
  103. return "CompressedCubemapArray";
  104. } break;
  105. case MODE_3D: {
  106. return "CompressedTexture3D";
  107. } break;
  108. }
  109. ERR_FAIL_V(String());
  110. }
  111. bool ResourceImporterLayeredTexture::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  112. if (p_option == "compress/lossy_quality" && p_options.has("compress/mode")) {
  113. return int(p_options["compress/mode"]) == COMPRESS_LOSSY;
  114. }
  115. return true;
  116. }
  117. int ResourceImporterLayeredTexture::get_preset_count() const {
  118. return 0;
  119. }
  120. String ResourceImporterLayeredTexture::get_preset_name(int p_idx) const {
  121. return "";
  122. }
  123. void ResourceImporterLayeredTexture::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  124. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Lossless (PNG),Lossy (WebP),Video RAM (S3TC/ETC/BPTC),Uncompressed,Basis Universal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1));
  125. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));
  126. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_compression", PROPERTY_HINT_ENUM, "Disabled,Opaque Only,Always"), 1));
  127. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/bptc_ldr", PROPERTY_HINT_ENUM, "Disabled,Enabled,RGBA Only"), 0));
  128. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized,Normal Map (RG Channels)"), 0));
  129. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "mipmaps/generate"), true));
  130. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "-1,256"), -1));
  131. if (mode == MODE_2D_ARRAY || mode == MODE_3D) {
  132. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/horizontal", PROPERTY_HINT_RANGE, "1,256,1"), 8));
  133. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/vertical", PROPERTY_HINT_RANGE, "1,256,1"), 8));
  134. }
  135. if (mode == MODE_CUBEMAP || mode == MODE_CUBEMAP_ARRAY) {
  136. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/arrangement", PROPERTY_HINT_ENUM, "1x6,2x3,3x2,6x1"), 1));
  137. if (mode == MODE_CUBEMAP_ARRAY) {
  138. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/layout", PROPERTY_HINT_ENUM, "Horizontal,Vertical"), 1));
  139. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/amount", PROPERTY_HINT_RANGE, "1,1024,1,or_greater"), 1));
  140. }
  141. }
  142. }
  143. void ResourceImporterLayeredTexture::_save_tex(Vector<Ref<Image>> p_images, const String &p_to_path, int p_compress_mode, float p_lossy, Image::CompressMode p_vram_compression, Image::CompressSource p_csource, Image::UsedChannels used_channels, bool p_mipmaps, bool p_force_po2) {
  144. Vector<Ref<Image>> mipmap_images; //for 3D
  145. if (mode == MODE_3D) {
  146. //3D saves in its own way
  147. for (int i = 0; i < p_images.size(); i++) {
  148. if (p_images.write[i]->has_mipmaps()) {
  149. p_images.write[i]->clear_mipmaps();
  150. }
  151. if (p_force_po2) {
  152. p_images.write[i]->resize_to_po2();
  153. }
  154. }
  155. if (p_mipmaps) {
  156. Vector<Ref<Image>> parent_images = p_images;
  157. //create 3D mipmaps, this is horrible, though not used very often
  158. int w = p_images[0]->get_width();
  159. int h = p_images[0]->get_height();
  160. int d = p_images.size();
  161. while (w > 1 || h > 1 || d > 1) {
  162. Vector<Ref<Image>> mipmaps;
  163. int mm_w = MAX(1, w >> 1);
  164. int mm_h = MAX(1, h >> 1);
  165. int mm_d = MAX(1, d >> 1);
  166. for (int i = 0; i < mm_d; i++) {
  167. Ref<Image> mm;
  168. mm.instantiate();
  169. mm->create(mm_w, mm_h, false, p_images[0]->get_format());
  170. Vector3 pos;
  171. pos.z = float(i) * float(d) / float(mm_d) + 0.5;
  172. for (int x = 0; x < mm_w; x++) {
  173. for (int y = 0; y < mm_h; y++) {
  174. pos.x = float(x) * float(w) / float(mm_w) + 0.5;
  175. pos.y = float(y) * float(h) / float(mm_h) + 0.5;
  176. Vector3i posi = Vector3i(pos);
  177. Vector3 fract = pos - Vector3(posi);
  178. Vector3i posi_n = posi;
  179. if (posi_n.x < w - 1) {
  180. posi_n.x++;
  181. }
  182. if (posi_n.y < h - 1) {
  183. posi_n.y++;
  184. }
  185. if (posi_n.z < d - 1) {
  186. posi_n.z++;
  187. }
  188. Color c000 = parent_images[posi.z]->get_pixel(posi.x, posi.y);
  189. Color c100 = parent_images[posi.z]->get_pixel(posi_n.x, posi.y);
  190. Color c010 = parent_images[posi.z]->get_pixel(posi.x, posi_n.y);
  191. Color c110 = parent_images[posi.z]->get_pixel(posi_n.x, posi_n.y);
  192. Color c001 = parent_images[posi_n.z]->get_pixel(posi.x, posi.y);
  193. Color c101 = parent_images[posi_n.z]->get_pixel(posi_n.x, posi.y);
  194. Color c011 = parent_images[posi_n.z]->get_pixel(posi.x, posi_n.y);
  195. Color c111 = parent_images[posi_n.z]->get_pixel(posi_n.x, posi_n.y);
  196. Color cx00 = c000.lerp(c100, fract.x);
  197. Color cx01 = c001.lerp(c101, fract.x);
  198. Color cx10 = c010.lerp(c110, fract.x);
  199. Color cx11 = c011.lerp(c111, fract.x);
  200. Color cy0 = cx00.lerp(cx10, fract.y);
  201. Color cy1 = cx01.lerp(cx11, fract.y);
  202. Color cz = cy0.lerp(cy1, fract.z);
  203. mm->set_pixel(x, y, cz);
  204. }
  205. }
  206. mipmaps.push_back(mm);
  207. }
  208. w = mm_w;
  209. h = mm_h;
  210. d = mm_d;
  211. mipmap_images.append_array(mipmaps);
  212. parent_images = mipmaps;
  213. }
  214. }
  215. } else {
  216. for (int i = 0; i < p_images.size(); i++) {
  217. if (p_force_po2) {
  218. p_images.write[i]->resize_to_po2();
  219. }
  220. if (p_mipmaps) {
  221. p_images.write[i]->generate_mipmaps(p_csource == Image::COMPRESS_SOURCE_NORMAL);
  222. } else {
  223. p_images.write[i]->clear_mipmaps();
  224. }
  225. }
  226. }
  227. Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);
  228. f->store_8('G');
  229. f->store_8('S');
  230. f->store_8('T');
  231. f->store_8('L');
  232. f->store_32(CompressedTextureLayered::FORMAT_VERSION);
  233. f->store_32(p_images.size()); // For 2d layers or 3d depth.
  234. f->store_32(mode);
  235. f->store_32(0);
  236. f->store_32(0);
  237. f->store_32(mipmap_images.size()); // Adjust the amount of mipmaps.
  238. f->store_32(0);
  239. f->store_32(0);
  240. for (int i = 0; i < p_images.size(); i++) {
  241. ResourceImporterTexture::save_to_ctex_format(f, p_images[i], ResourceImporterTexture::CompressMode(p_compress_mode), used_channels, p_vram_compression, p_lossy);
  242. }
  243. for (int i = 0; i < mipmap_images.size(); i++) {
  244. ResourceImporterTexture::save_to_ctex_format(f, mipmap_images[i], ResourceImporterTexture::CompressMode(p_compress_mode), used_channels, p_vram_compression, p_lossy);
  245. }
  246. }
  247. Error ResourceImporterLayeredTexture::import(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) {
  248. int compress_mode = p_options["compress/mode"];
  249. float lossy = p_options["compress/lossy_quality"];
  250. int hdr_compression = p_options["compress/hdr_compression"];
  251. int bptc_ldr = p_options["compress/bptc_ldr"];
  252. bool mipmaps = p_options["mipmaps/generate"];
  253. int channel_pack = p_options["compress/channel_pack"];
  254. int hslices = (p_options.has("slices/horizontal")) ? int(p_options["slices/horizontal"]) : 0;
  255. int vslices = (p_options.has("slices/vertical")) ? int(p_options["slices/vertical"]) : 0;
  256. int arrangement = (p_options.has("slices/arrangement")) ? int(p_options["slices/arrangement"]) : 0;
  257. int layout = (p_options.has("slices/layout")) ? int(p_options["slices/layout"]) : 0;
  258. int amount = (p_options.has("slices/amount")) ? int(p_options["slices/amount"]) : 0;
  259. if (mode == MODE_CUBEMAP || mode == MODE_CUBEMAP_ARRAY) {
  260. switch (arrangement) {
  261. case CUBEMAP_FORMAT_1X6: {
  262. hslices = 1;
  263. vslices = 6;
  264. } break;
  265. case CUBEMAP_FORMAT_2X3: {
  266. hslices = 2;
  267. vslices = 3;
  268. } break;
  269. case CUBEMAP_FORMAT_3X2: {
  270. hslices = 3;
  271. vslices = 2;
  272. } break;
  273. case CUBEMAP_FORMAT_6X1: {
  274. hslices = 6;
  275. vslices = 1;
  276. } break;
  277. }
  278. if (mode == MODE_CUBEMAP_ARRAY) {
  279. if (layout == 0) {
  280. hslices *= amount;
  281. } else {
  282. vslices *= amount;
  283. }
  284. }
  285. }
  286. Ref<Image> image;
  287. image.instantiate();
  288. Error err = ImageLoader::load_image(p_source_file, image);
  289. if (err != OK) {
  290. return err;
  291. }
  292. if (compress_mode == COMPRESS_BASIS_UNIVERSAL && image->get_format() >= Image::FORMAT_RF) {
  293. //basis universal does not support float formats, fall back
  294. compress_mode = COMPRESS_VRAM_COMPRESSED;
  295. }
  296. if (compress_mode == COMPRESS_VRAM_COMPRESSED) {
  297. mipmaps = true;
  298. //if using video ram, optimize
  299. if (channel_pack == 0) {
  300. //remove alpha if not needed, so compression is more efficient
  301. if (image->get_format() == Image::FORMAT_RGBA8 && !image->detect_alpha()) {
  302. image->convert(Image::FORMAT_RGB8);
  303. }
  304. } else if (image->get_format() < Image::FORMAT_RGBA8) {
  305. image->optimize_channels();
  306. }
  307. }
  308. Image::CompressSource csource = Image::COMPRESS_SOURCE_GENERIC;
  309. if (channel_pack == 0) {
  310. csource = Image::COMPRESS_SOURCE_SRGB;
  311. } else if (channel_pack == 2) {
  312. // force normal
  313. csource = Image::COMPRESS_SOURCE_NORMAL;
  314. }
  315. Image::UsedChannels used_channels = image->detect_used_channels(csource);
  316. Vector<Ref<Image>> slices;
  317. int slice_w = image->get_width() / hslices;
  318. int slice_h = image->get_height() / vslices;
  319. for (int i = 0; i < vslices; i++) {
  320. for (int j = 0; j < hslices; j++) {
  321. int x = slice_w * j;
  322. int y = slice_h * i;
  323. Ref<Image> slice = image->get_rect(Rect2i(x, y, slice_w, slice_h));
  324. ERR_CONTINUE(slice.is_null() || slice->is_empty());
  325. if (slice->get_width() != slice_w || slice->get_height() != slice_h) {
  326. slice->resize(slice_w, slice_h);
  327. }
  328. slices.push_back(slice);
  329. }
  330. }
  331. Array formats_imported;
  332. Ref<LayeredTextureImport> texture_import;
  333. texture_import.instantiate();
  334. texture_import->csource = &csource;
  335. texture_import->save_path = p_save_path;
  336. texture_import->options = p_options;
  337. texture_import->platform_variants = r_platform_variants;
  338. texture_import->image = image;
  339. texture_import->formats_imported = formats_imported;
  340. texture_import->slices = &slices;
  341. texture_import->compress_mode = compress_mode;
  342. texture_import->lossy = lossy;
  343. texture_import->hdr_compression = hdr_compression;
  344. texture_import->bptc_ldr = bptc_ldr;
  345. texture_import->mipmaps = mipmaps;
  346. texture_import->used_channels = used_channels;
  347. _check_compress_ctex(p_source_file, texture_import);
  348. if (r_metadata) {
  349. Dictionary metadata;
  350. metadata["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED;
  351. if (formats_imported.size()) {
  352. metadata["imported_formats"] = formats_imported;
  353. }
  354. *r_metadata = metadata;
  355. }
  356. return OK;
  357. }
  358. const char *ResourceImporterLayeredTexture::compression_formats[] = {
  359. "bptc",
  360. "s3tc",
  361. "etc",
  362. "etc2",
  363. nullptr
  364. };
  365. String ResourceImporterLayeredTexture::get_import_settings_string() const {
  366. String s;
  367. int index = 0;
  368. while (compression_formats[index]) {
  369. String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);
  370. bool test = ProjectSettings::get_singleton()->get(setting_path);
  371. if (test) {
  372. s += String(compression_formats[index]);
  373. }
  374. index++;
  375. }
  376. return s;
  377. }
  378. bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_path) const {
  379. //will become invalid if formats are missing to import
  380. Dictionary metadata = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);
  381. if (!metadata.has("vram_texture")) {
  382. return false;
  383. }
  384. bool vram = metadata["vram_texture"];
  385. if (!vram) {
  386. return true; //do not care about non vram
  387. }
  388. Vector<String> formats_imported;
  389. if (metadata.has("imported_formats")) {
  390. formats_imported = metadata["imported_formats"];
  391. }
  392. int index = 0;
  393. bool valid = true;
  394. while (compression_formats[index]) {
  395. String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);
  396. bool test = ProjectSettings::get_singleton()->get(setting_path);
  397. if (test) {
  398. if (!formats_imported.has(compression_formats[index])) {
  399. valid = false;
  400. break;
  401. }
  402. }
  403. index++;
  404. }
  405. return valid;
  406. }
  407. ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = nullptr;
  408. ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() {
  409. singleton = this;
  410. mode = MODE_CUBEMAP;
  411. }
  412. ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
  413. }
  414. void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) {
  415. String extension = get_save_extension();
  416. ERR_FAIL_NULL(r_texture_import->csource);
  417. if (r_texture_import->compress_mode != COMPRESS_VRAM_COMPRESSED) {
  418. // Import normally.
  419. _save_tex(*r_texture_import->slices, r_texture_import->save_path + "." + extension, r_texture_import->compress_mode, r_texture_import->lossy, Image::COMPRESS_S3TC /* IGNORED */, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, false);
  420. return;
  421. }
  422. // Must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc).
  423. // Android, GLES 2.x
  424. bool can_bptc = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_bptc");
  425. if (can_bptc) {
  426. r_texture_import->formats_imported.push_back("bptc"); // BPTC needs to be added anyway.
  427. }
  428. bool can_compress_hdr = r_texture_import->hdr_compression > 0;
  429. ERR_FAIL_NULL(r_texture_import->image);
  430. bool is_hdr = (r_texture_import->image->get_format() >= Image::FORMAT_RF && r_texture_import->image->get_format() <= Image::FORMAT_RGBE9995);
  431. bool is_ldr = (r_texture_import->image->get_format() >= Image::FORMAT_L8 && r_texture_import->image->get_format() <= Image::FORMAT_RGB565);
  432. bool can_s3tc = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_s3tc");
  433. ERR_FAIL_NULL(r_texture_import->slices);
  434. // Can compress hdr, but hdr with alpha is not compressible.
  435. if (r_texture_import->hdr_compression == 2) {
  436. // The user selected to compress hdr anyway, so force an alpha-less format.
  437. if (r_texture_import->image->get_format() == Image::FORMAT_RGBAF) {
  438. for (int i = 0; i < r_texture_import->slices->size(); i++) {
  439. r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBF);
  440. }
  441. } else if (r_texture_import->image->get_format() == Image::FORMAT_RGBAH) {
  442. for (int i = 0; i < r_texture_import->slices->size(); i++) {
  443. r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBH);
  444. }
  445. }
  446. } else {
  447. can_compress_hdr = false;
  448. }
  449. if (is_hdr && can_compress_hdr) {
  450. if (!can_bptc) {
  451. //default to rgbe
  452. if (r_texture_import->image->get_format() != Image::FORMAT_RGBE9995) {
  453. for (int i = 0; i < r_texture_import->slices->size(); i++) {
  454. r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBE9995);
  455. }
  456. }
  457. }
  458. } else {
  459. can_bptc = false;
  460. }
  461. if (is_ldr && can_bptc) {
  462. if (r_texture_import->bptc_ldr == 0 || (r_texture_import->bptc_ldr == 1 && !(r_texture_import->used_channels == Image::USED_CHANNELS_LA || r_texture_import->used_channels == Image::USED_CHANNELS_RGBA))) {
  463. can_bptc = false;
  464. }
  465. }
  466. if (!(r_texture_import->used_channels == Image::USED_CHANNELS_LA || r_texture_import->used_channels == Image::USED_CHANNELS_RGBA)) {
  467. if (ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_etc2")) {
  468. _save_tex(*r_texture_import->slices, r_texture_import->save_path + ".etc2." + extension, r_texture_import->compress_mode, r_texture_import->lossy, Image::COMPRESS_ETC2, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, true);
  469. r_texture_import->platform_variants->push_back("etc2");
  470. r_texture_import->formats_imported.push_back("etc2");
  471. }
  472. if (can_bptc || can_s3tc) {
  473. _save_tex(*r_texture_import->slices, r_texture_import->save_path + ".s3tc." + extension, r_texture_import->compress_mode, r_texture_import->lossy, can_bptc ? Image::COMPRESS_BPTC : Image::COMPRESS_S3TC, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, false);
  474. r_texture_import->platform_variants->push_back("s3tc");
  475. r_texture_import->formats_imported.push_back("s3tc");
  476. }
  477. return;
  478. }
  479. EditorNode::add_io_error(vformat(TTR("%s: No suitable PC VRAM compression algorithm enabled in Project Settings (S3TC or BPTC). This texture may not display correctly on desktop platforms."), p_source_file));
  480. }