resource_importer_layered_texture.cpp 22 KB

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