resource_importer_layered_texture.cpp 14 KB

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