resource_importer_layered_texture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 i = 0; i < mmc; i++) {
  110. if (i > 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) {
  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. if (compress_mode == COMPRESS_VIDEO_RAM) {
  205. //must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc).
  206. //Android, GLES 2.x
  207. bool ok_on_pc = false;
  208. bool encode_bptc = false;
  209. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc")) {
  210. encode_bptc = true;
  211. if (no_bptc_if_rgb) {
  212. Image::DetectChannels channels = image->get_detected_channels();
  213. if (channels != Image::DETECTED_LA && channels != Image::DETECTED_RGBA) {
  214. encode_bptc = false;
  215. }
  216. }
  217. }
  218. if (encode_bptc) {
  219. _save_tex(slices, p_save_path + ".bptc." + extension, compress_mode, Image::COMPRESS_BPTC, mipmaps, tex_flags);
  220. r_platform_variants->push_back("bptc");
  221. ok_on_pc = true;
  222. }
  223. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_s3tc")) {
  224. _save_tex(slices, p_save_path + ".s3tc." + extension, compress_mode, Image::COMPRESS_S3TC, mipmaps, tex_flags);
  225. r_platform_variants->push_back("s3tc");
  226. ok_on_pc = true;
  227. }
  228. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc2")) {
  229. _save_tex(slices, p_save_path + ".etc2." + extension, compress_mode, Image::COMPRESS_ETC2, mipmaps, tex_flags);
  230. r_platform_variants->push_back("etc2");
  231. }
  232. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc")) {
  233. _save_tex(slices, p_save_path + ".etc." + extension, compress_mode, Image::COMPRESS_ETC, mipmaps, tex_flags);
  234. r_platform_variants->push_back("etc");
  235. }
  236. if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_pvrtc")) {
  237. _save_tex(slices, p_save_path + ".pvrtc." + extension, compress_mode, Image::COMPRESS_PVRTC4, mipmaps, tex_flags);
  238. r_platform_variants->push_back("pvrtc");
  239. }
  240. if (!ok_on_pc) {
  241. EditorNode::add_io_error("Warning, no suitable PC VRAM compression enabled in Project Settings. This texture will not display correctly on PC.");
  242. }
  243. } else {
  244. //import normally
  245. _save_tex(slices, p_save_path + "." + extension, compress_mode, Image::COMPRESS_S3TC /*this is ignored */, mipmaps, tex_flags);
  246. }
  247. return OK;
  248. }
  249. ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = NULL;
  250. ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() {
  251. singleton = this;
  252. is_3d = true;
  253. }
  254. ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
  255. }