resource_importer_layered_texture.cpp 13 KB

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