resource_importer_layered_texture.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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-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_layered_texture.h"
  31. #include "resource_importer_texture.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/image_loader.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "scene/resources/texture.h"
  37. #if 0
  38. String ResourceImporterLayeredTexture::get_importer_name() const {
  39. switch (mode) {
  40. case MODE_CUBEMAP: {
  41. return "cubemap_texture";
  42. } break;
  43. case MODE_2D_ARRAY: {
  44. return "2d_array_texture";
  45. } break;
  46. case MODE_CUBEMAP_ARRAY: {
  47. return "cubemap_array_texture";
  48. } break;
  49. }
  50. ERR_FAIL_V("");
  51. }
  52. String ResourceImporterLayeredTexture::get_visible_name() const {
  53. switch (mode) {
  54. case MODE_CUBEMAP: {
  55. return "Cubemap";
  56. } break;
  57. case MODE_2D_ARRAY: {
  58. return "Texture2DArray";
  59. } break;
  60. case MODE_CUBEMAP_ARRAY: {
  61. return "CubemapArray";
  62. } break;
  63. }
  64. ERR_FAIL_V("");
  65. }
  66. void ResourceImporterLayeredTexture::get_recognized_extensions(List<String> *p_extensions) const {
  67. ImageLoader::get_recognized_extensions(p_extensions);
  68. }
  69. String ResourceImporterLayeredTexture::get_save_extension() const {
  70. switch (mode) {
  71. case MODE_CUBEMAP: {
  72. return "cube";
  73. } break;
  74. case MODE_2D_ARRAY: {
  75. return "tex2darr";
  76. } break;
  77. case MODE_CUBEMAP_ARRAY: {
  78. return "cubearr";
  79. } break;
  80. }
  81. ERR_FAIL_V(String());
  82. }
  83. String ResourceImporterLayeredTexture::get_resource_type() const {
  84. switch (mode) {
  85. case MODE_CUBEMAP: {
  86. return "Cubemap";
  87. } break;
  88. case MODE_2D_ARRAY: {
  89. return "Texture2DArray";
  90. } break;
  91. case MODE_CUBEMAP_ARRAY: {
  92. return "CubemapArray";
  93. } break;
  94. }
  95. ERR_FAIL_V(String());
  96. }
  97. bool ResourceImporterLayeredTexture::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  98. return true;
  99. }
  100. int ResourceImporterLayeredTexture::get_preset_count() const {
  101. return 0;
  102. }
  103. String ResourceImporterLayeredTexture::get_preset_name(int p_idx) const {
  104. return "";
  105. }
  106. void ResourceImporterLayeredTexture::get_import_options(List<ImportOption> *r_options, int p_preset) const {
  107. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Lossless,Video RAM,Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1));
  108. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress/no_bptc_if_rgb"), false));
  109. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized"), 0));
  110. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "flags/mipmaps"), true));
  111. if (mode == MODE_2D_ARRAY) {
  112. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/horizontal", PROPERTY_HINT_RANGE, "1,256,1"), 8));
  113. }
  114. if (mode == MODE_2D_ARRAY || mode == MODE_CUBEMAP_ARRAY) {
  115. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/vertical", PROPERTY_HINT_RANGE, "1,256,1"), 8));
  116. }
  117. }
  118. void ResourceImporterLayeredTexture::_save_tex(const Vector<Ref<Image> > &p_images, const String &p_to_path, int p_compress_mode, Image::CompressMode p_vram_compression, bool p_mipmaps) {
  119. FileAccess *f = FileAccess::open(p_to_path, FileAccess::WRITE);
  120. f->store_8('G');
  121. f->store_8('D');
  122. switch (mode) {
  123. case MODE_2D_ARRAY: f->store_8('A'); break;
  124. case MODE_CUBEMAP: f->store_8('C'); break;
  125. case MODE_CUBEMAP_ARRAY: f->store_8('X'); break;
  126. }
  127. f->store_8('T'); //godot streamable texture
  128. f->store_32(p_images[0]->get_width());
  129. f->store_32(p_images[0]->get_height());
  130. f->store_32(p_images.size()); //depth
  131. uint32_t flags = 0;
  132. if (p_mipmaps) {
  133. flags |= TEXTURE_FLAGS_MIPMAPS;
  134. }
  135. f->store_32(flags);
  136. if (p_compress_mode != COMPRESS_VIDEO_RAM) {
  137. //vram needs to do a first compression to tell what the format is, for the rest its ok
  138. f->store_32(p_images[0]->get_format());
  139. f->store_32(p_compress_mode); // 0 - lossless (PNG), 1 - vram, 2 - uncompressed
  140. }
  141. if ((p_compress_mode == COMPRESS_LOSSLESS) && p_images[0]->get_format() > Image::FORMAT_RGBA8) {
  142. p_compress_mode = COMPRESS_UNCOMPRESSED; //these can't go as lossy
  143. }
  144. for (int i = 0; i < p_images.size(); i++) {
  145. switch (p_compress_mode) {
  146. case COMPRESS_LOSSLESS: {
  147. Ref<Image> image = p_images[i]->duplicate();
  148. if (p_mipmaps) {
  149. image->generate_mipmaps();
  150. } else {
  151. image->clear_mipmaps();
  152. }
  153. int mmc = image->get_mipmap_count() + 1;
  154. f->store_32(mmc);
  155. for (int j = 0; j < mmc; j++) {
  156. if (j > 0) {
  157. image->shrink_x2();
  158. }
  159. PoolVector<uint8_t> data = Image::lossless_packer(image);
  160. int data_len = data.size();
  161. f->store_32(data_len);
  162. PoolVector<uint8_t>::Read r = data.read();
  163. f->store_buffer(r.ptr(), data_len);
  164. }
  165. } break;
  166. case COMPRESS_VIDEO_RAM: {
  167. Ref<Image> image = p_images[i]->duplicate();
  168. image->generate_mipmaps(false);
  169. Image::CompressSource csource = Image::COMPRESS_SOURCE_LAYERED;
  170. image->compress(p_vram_compression, csource, 0.7);
  171. if (i == 0) {
  172. //hack so we can properly tell the format
  173. f->store_32(image->get_format());
  174. f->store_32(p_compress_mode); // 0 - lossless (PNG), 1 - vram, 2 - uncompressed
  175. }
  176. PoolVector<uint8_t> data = image->get_data();
  177. int dl = data.size();
  178. PoolVector<uint8_t>::Read r = data.read();
  179. f->store_buffer(r.ptr(), dl);
  180. } break;
  181. case COMPRESS_UNCOMPRESSED: {
  182. Ref<Image> image = p_images[i]->duplicate();
  183. if (p_mipmaps) {
  184. image->generate_mipmaps();
  185. } else {
  186. image->clear_mipmaps();
  187. }
  188. PoolVector<uint8_t> data = image->get_data();
  189. int dl = data.size();
  190. PoolVector<uint8_t>::Read r = data.read();
  191. f->store_buffer(r.ptr(), dl);
  192. } break;
  193. }
  194. }
  195. memdelete(f);
  196. }
  197. Error ResourceImporterLayeredTexture::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) {
  198. int compress_mode = p_options["compress/mode"];
  199. int no_bptc_if_rgb = p_options["compress/no_bptc_if_rgb"];
  200. bool mipmaps = p_options["flags/mipmaps"];
  201. int channel_pack = p_options["compress/channel_pack"];
  202. int hslices = (p_options.has("slices/horizontal")) ? int(p_options["slices/horizontal"]) : 0;
  203. int vslices = (p_options.has("slices/vertical")) ? int(p_options["slices/vertical"]) : 0;
  204. if (mode == MODE_CUBEMAP) {
  205. hslices = 3;
  206. vslices = 2;
  207. } else if (mode == MODE_CUBEMAP_ARRAY) {
  208. hslices = 3;
  209. vslices *= 2; //put cubemaps vertically
  210. }
  211. Ref<Image> image;
  212. image.instance();
  213. Error err = ImageLoader::load_image(p_source_file, image, NULL, false, 1.0);
  214. if (err != OK)
  215. return err;
  216. if (compress_mode == COMPRESS_VIDEO_RAM) {
  217. mipmaps = true;
  218. }
  219. Vector<Ref<Image> > slices;
  220. int slice_w = image->get_width() / hslices;
  221. int slice_h = image->get_height() / vslices;
  222. //optimize
  223. if (compress_mode == COMPRESS_VIDEO_RAM) {
  224. //if using video ram, optimize
  225. if (channel_pack == 0) {
  226. //remove alpha if not needed, so compression is more efficient
  227. if (image->get_format() == Image::FORMAT_RGBA8 && !image->detect_alpha()) {
  228. image->convert(Image::FORMAT_RGB8);
  229. }
  230. } else {
  231. image->optimize_channels();
  232. }
  233. }
  234. for (int i = 0; i < vslices; i++) {
  235. for (int j = 0; j < hslices; j++) {
  236. int x = slice_w * j;
  237. int y = slice_h * i;
  238. Ref<Image> slice = image->get_rect(Rect2(x, y, slice_w, slice_h));
  239. ERR_CONTINUE(slice.is_null() || slice->empty());
  240. if (slice->get_width() != slice_w || slice->get_height() != slice_h) {
  241. slice->resize(slice_w, slice_h);
  242. }
  243. slices.push_back(slice);
  244. }
  245. }
  246. String extension = get_save_extension();
  247. Array formats_imported;
  248. if (compress_mode == COMPRESS_VIDEO_RAM) {
  249. //must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc).
  250. //Android, GLES 2.x
  251. bool ok_on_pc = false;
  252. bool encode_bptc = false;
  253. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc")) {
  254. encode_bptc = true;
  255. if (no_bptc_if_rgb) {
  256. Image::UsedChannels channels = image->detect_used_channels();
  257. if (channels != Image::USED_CHANNELS_LA && channels != Image::USED_CHANNELS_RGBA) {
  258. encode_bptc = false;
  259. }
  260. }
  261. formats_imported.push_back("bptc");
  262. }
  263. if (encode_bptc) {
  264. _save_tex(slices, p_save_path + ".bptc." + extension, compress_mode, Image::COMPRESS_BPTC, mipmaps);
  265. r_platform_variants->push_back("bptc");
  266. ok_on_pc = true;
  267. }
  268. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_s3tc")) {
  269. _save_tex(slices, p_save_path + ".s3tc." + extension, compress_mode, Image::COMPRESS_S3TC, mipmaps);
  270. r_platform_variants->push_back("s3tc");
  271. ok_on_pc = true;
  272. formats_imported.push_back("s3tc");
  273. }
  274. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc2")) {
  275. _save_tex(slices, p_save_path + ".etc2." + extension, compress_mode, Image::COMPRESS_ETC2, mipmaps);
  276. r_platform_variants->push_back("etc2");
  277. formats_imported.push_back("etc2");
  278. }
  279. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc")) {
  280. _save_tex(slices, p_save_path + ".etc." + extension, compress_mode, Image::COMPRESS_ETC, mipmaps);
  281. r_platform_variants->push_back("etc");
  282. formats_imported.push_back("etc");
  283. }
  284. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_pvrtc")) {
  285. _save_tex(slices, p_save_path + ".pvrtc." + extension, compress_mode, Image::COMPRESS_PVRTC4, mipmaps);
  286. r_platform_variants->push_back("pvrtc");
  287. formats_imported.push_back("pvrtc");
  288. }
  289. if (!ok_on_pc) {
  290. EditorNode::add_io_error("Warning, no suitable PC VRAM compression enabled in Project Settings. This texture will not display correctly on PC.");
  291. }
  292. } else {
  293. //import normally
  294. _save_tex(slices, p_save_path + "." + extension, compress_mode, Image::COMPRESS_S3TC /*this is ignored */, mipmaps);
  295. }
  296. if (r_metadata) {
  297. Dictionary metadata;
  298. metadata["vram_texture"] = compress_mode == COMPRESS_VIDEO_RAM;
  299. if (formats_imported.size()) {
  300. metadata["imported_formats"] = formats_imported;
  301. }
  302. *r_metadata = metadata;
  303. }
  304. return OK;
  305. }
  306. const char *ResourceImporterLayeredTexture::compression_formats[] = {
  307. "bptc",
  308. "s3tc",
  309. "etc",
  310. "etc2",
  311. "pvrtc",
  312. NULL
  313. };
  314. String ResourceImporterLayeredTexture::get_import_settings_string() const {
  315. String s;
  316. int index = 0;
  317. while (compression_formats[index]) {
  318. String setting_path = "rendering/vram_compression/import_" + String(compression_formats[index]);
  319. bool test = ProjectSettings::get_singleton()->get(setting_path);
  320. if (test) {
  321. s += String(compression_formats[index]);
  322. }
  323. index++;
  324. }
  325. return s;
  326. }
  327. bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_path) const {
  328. //will become invalid if formats are missing to import
  329. Dictionary metadata = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);
  330. if (!metadata.has("vram_texture")) {
  331. return false;
  332. }
  333. bool vram = metadata["vram_texture"];
  334. if (!vram) {
  335. return true; //do not care about non vram
  336. }
  337. Vector<String> formats_imported;
  338. if (metadata.has("imported_formats")) {
  339. formats_imported = metadata["imported_formats"];
  340. }
  341. int index = 0;
  342. bool valid = true;
  343. while (compression_formats[index]) {
  344. String setting_path = "rendering/vram_compression/import_" + String(compression_formats[index]);
  345. bool test = ProjectSettings::get_singleton()->get(setting_path);
  346. if (test) {
  347. if (formats_imported.find(compression_formats[index]) == -1) {
  348. valid = false;
  349. break;
  350. }
  351. }
  352. index++;
  353. }
  354. return valid;
  355. }
  356. ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = NULL;
  357. ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() {
  358. singleton = this;
  359. mode = MODE_CUBEMAP;
  360. }
  361. ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
  362. }
  363. #endif