Path: blob/master/editor/import/resource_importer_layered_texture.cpp
9903 views
/**************************************************************************/1/* resource_importer_layered_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_importer_layered_texture.h"3132#include "core/config/project_settings.h"33#include "core/error/error_macros.h"34#include "core/io/image_loader.h"35#include "core/object/ref_counted.h"36#include "editor/file_system/editor_file_system.h"37#include "editor/import/resource_importer_texture.h"38#include "editor/import/resource_importer_texture_settings.h"39#include "scene/resources/compressed_texture.h"4041String ResourceImporterLayeredTexture::get_importer_name() const {42switch (mode) {43case MODE_CUBEMAP: {44return "cubemap_texture";45} break;46case MODE_2D_ARRAY: {47return "2d_array_texture";48} break;49case MODE_CUBEMAP_ARRAY: {50return "cubemap_array_texture";51} break;52case MODE_3D: {53return "3d_texture";54} break;55}5657ERR_FAIL_V("");58}5960String ResourceImporterLayeredTexture::get_visible_name() const {61switch (mode) {62case MODE_CUBEMAP: {63return "Cubemap";64} break;65case MODE_2D_ARRAY: {66return "Texture2DArray";67} break;68case MODE_CUBEMAP_ARRAY: {69return "CubemapArray";70} break;71case MODE_3D: {72return "Texture3D";73} break;74}7576ERR_FAIL_V("");77}7879void ResourceImporterLayeredTexture::get_recognized_extensions(List<String> *p_extensions) const {80ImageLoader::get_recognized_extensions(p_extensions);81}8283String ResourceImporterLayeredTexture::get_save_extension() const {84switch (mode) {85case MODE_CUBEMAP: {86return "ccube";87} break;88case MODE_2D_ARRAY: {89return "ctexarray";90} break;91case MODE_CUBEMAP_ARRAY: {92return "ccubearray";93} break;94case MODE_3D: {95return "ctex3d";96} break;97}9899ERR_FAIL_V(String());100}101102String ResourceImporterLayeredTexture::get_resource_type() const {103switch (mode) {104case MODE_CUBEMAP: {105return "CompressedCubemap";106} break;107case MODE_2D_ARRAY: {108return "CompressedTexture2DArray";109} break;110case MODE_CUBEMAP_ARRAY: {111return "CompressedCubemapArray";112} break;113case MODE_3D: {114return "CompressedTexture3D";115} break;116}117ERR_FAIL_V(String());118}119120bool ResourceImporterLayeredTexture::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {121if (p_option == "compress/lossy_quality" && p_options.has("compress/mode")) {122return int(p_options["compress/mode"]) == COMPRESS_LOSSY;123}124if ((p_option == "compress/high_quality" || p_option == "compress/hdr_compression") && p_options.has("compress/mode")) {125return int(p_options["compress/mode"]) == COMPRESS_VRAM_COMPRESSED;126}127if (p_option == "compress/uastc_level" || p_option == "compress/rdo_quality_loss") {128return int(p_options["compress/mode"]) == COMPRESS_BASIS_UNIVERSAL;129}130131return true;132}133134int ResourceImporterLayeredTexture::get_preset_count() const {135return 0;136}137138String ResourceImporterLayeredTexture::get_preset_name(int p_idx) const {139return "";140}141142void ResourceImporterLayeredTexture::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {143r_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));144r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress/high_quality"), false));145r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));146147Image::BasisUniversalPackerParams basisu_params;148r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/uastc_level", PROPERTY_HINT_ENUM, "Fastest,Faster,Medium,Slower,Slowest"), basisu_params.uastc_level));149r_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));150151r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_compression", PROPERTY_HINT_ENUM, "Disabled,Opaque Only,Always"), 1));152r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized,Normal Map (RG Channels)"), 0));153r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "mipmaps/generate"), true));154r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "-1,256"), -1));155156if (mode == MODE_2D_ARRAY || mode == MODE_3D) {157r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/horizontal", PROPERTY_HINT_RANGE, "1,256,1"), 8));158r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/vertical", PROPERTY_HINT_RANGE, "1,256,1"), 8));159}160if (mode == MODE_CUBEMAP || mode == MODE_CUBEMAP_ARRAY) {161r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/arrangement", PROPERTY_HINT_ENUM, "1x6,2x3,3x2,6x1"), 1));162if (mode == MODE_CUBEMAP_ARRAY) {163r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/layout", PROPERTY_HINT_ENUM, "Horizontal,Vertical"), 1));164r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/amount", PROPERTY_HINT_RANGE, "1,1024,1,or_greater"), 1));165}166}167}168169void 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) {170Vector<Ref<Image>> mipmap_images; //for 3D171172if (mode == MODE_3D) {173//3D saves in its own way174175for (int i = 0; i < p_images.size(); i++) {176if (p_images.write[i]->has_mipmaps()) {177p_images.write[i]->clear_mipmaps();178}179180if (p_force_po2) {181p_images.write[i]->resize_to_po2();182}183}184185if (p_mipmaps) {186Vector<Ref<Image>> parent_images = p_images;187//create 3D mipmaps, this is horrible, though not used very often188int w = p_images[0]->get_width();189int h = p_images[0]->get_height();190int d = p_images.size();191192while (w > 1 || h > 1 || d > 1) {193Vector<Ref<Image>> mipmaps;194int mm_w = MAX(1, w >> 1);195int mm_h = MAX(1, h >> 1);196int mm_d = MAX(1, d >> 1);197198for (int i = 0; i < mm_d; i++) {199Ref<Image> mm = Image::create_empty(mm_w, mm_h, false, p_images[0]->get_format());200Vector3 pos;201pos.z = float(i) * float(d) / float(mm_d) + 0.5;202for (int x = 0; x < mm_w; x++) {203for (int y = 0; y < mm_h; y++) {204pos.x = float(x) * float(w) / float(mm_w) + 0.5;205pos.y = float(y) * float(h) / float(mm_h) + 0.5;206207Vector3i posi = Vector3i(pos);208Vector3 fract = pos - Vector3(posi);209Vector3i posi_n = posi;210if (posi_n.x < w - 1) {211posi_n.x++;212}213if (posi_n.y < h - 1) {214posi_n.y++;215}216if (posi_n.z < d - 1) {217posi_n.z++;218}219220Color c000 = parent_images[posi.z]->get_pixel(posi.x, posi.y);221Color c100 = parent_images[posi.z]->get_pixel(posi_n.x, posi.y);222Color c010 = parent_images[posi.z]->get_pixel(posi.x, posi_n.y);223Color c110 = parent_images[posi.z]->get_pixel(posi_n.x, posi_n.y);224Color c001 = parent_images[posi_n.z]->get_pixel(posi.x, posi.y);225Color c101 = parent_images[posi_n.z]->get_pixel(posi_n.x, posi.y);226Color c011 = parent_images[posi_n.z]->get_pixel(posi.x, posi_n.y);227Color c111 = parent_images[posi_n.z]->get_pixel(posi_n.x, posi_n.y);228229Color cx00 = c000.lerp(c100, fract.x);230Color cx01 = c001.lerp(c101, fract.x);231Color cx10 = c010.lerp(c110, fract.x);232Color cx11 = c011.lerp(c111, fract.x);233234Color cy0 = cx00.lerp(cx10, fract.y);235Color cy1 = cx01.lerp(cx11, fract.y);236237Color cz = cy0.lerp(cy1, fract.z);238239mm->set_pixel(x, y, cz);240}241}242243mipmaps.push_back(mm);244}245246w = mm_w;247h = mm_h;248d = mm_d;249250mipmap_images.append_array(mipmaps);251parent_images = mipmaps;252}253}254} else {255for (int i = 0; i < p_images.size(); i++) {256if (p_force_po2) {257p_images.write[i]->resize_to_po2();258}259260if (p_mipmaps) {261p_images.write[i]->generate_mipmaps(p_csource == Image::COMPRESS_SOURCE_NORMAL);262} else {263p_images.write[i]->clear_mipmaps();264}265}266}267268Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);269f->store_8('G');270f->store_8('S');271f->store_8('T');272f->store_8('L');273274f->store_32(CompressedTextureLayered::FORMAT_VERSION);275f->store_32(p_images.size()); // For 2d layers or 3d depth.276f->store_32(mode);277f->store_32(0);278279f->store_32(0);280f->store_32(mipmap_images.size()); // Adjust the amount of mipmaps.281f->store_32(0);282f->store_32(0);283284if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_images[0]->get_format() >= Image::FORMAT_RF) {285p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED; // These can't go as lossy.286}287288for (int i = 0; i < p_images.size(); i++) {289ResourceImporterTexture::save_to_ctex_format(f, p_images[i], ResourceImporterTexture::CompressMode(p_compress_mode), used_channels, p_vram_compression, p_lossy, p_basisu_params);290}291292for (int i = 0; i < mipmap_images.size(); i++) {293ResourceImporterTexture::save_to_ctex_format(f, mipmap_images[i], ResourceImporterTexture::CompressMode(p_compress_mode), used_channels, p_vram_compression, p_lossy, p_basisu_params);294}295}296297Error 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) {298int compress_mode = p_options["compress/mode"];299float lossy = p_options["compress/lossy_quality"];300bool high_quality = p_options["compress/high_quality"];301int hdr_compression = p_options["compress/hdr_compression"];302bool mipmaps = p_options["mipmaps/generate"];303304int channel_pack = p_options["compress/channel_pack"];305int hslices = (p_options.has("slices/horizontal")) ? int(p_options["slices/horizontal"]) : 0;306int vslices = (p_options.has("slices/vertical")) ? int(p_options["slices/vertical"]) : 0;307int arrangement = (p_options.has("slices/arrangement")) ? int(p_options["slices/arrangement"]) : 0;308int layout = (p_options.has("slices/layout")) ? int(p_options["slices/layout"]) : 0;309int amount = (p_options.has("slices/amount")) ? int(p_options["slices/amount"]) : 0;310311if (mode == MODE_CUBEMAP || mode == MODE_CUBEMAP_ARRAY) {312switch (arrangement) {313case CUBEMAP_FORMAT_1X6: {314hslices = 1;315vslices = 6;316} break;317case CUBEMAP_FORMAT_2X3: {318hslices = 2;319vslices = 3;320} break;321case CUBEMAP_FORMAT_3X2: {322hslices = 3;323vslices = 2;324} break;325case CUBEMAP_FORMAT_6X1: {326hslices = 6;327vslices = 1;328} break;329}330331if (mode == MODE_CUBEMAP_ARRAY) {332if (layout == 0) {333hslices *= amount;334} else {335vslices *= amount;336}337}338}339340Ref<Image> image;341image.instantiate();342Error err = ImageLoader::load_image(p_source_file, image);343if (err != OK) {344return err;345}346347if (compress_mode == COMPRESS_VRAM_COMPRESSED) {348//if using video ram, optimize349if (channel_pack == 0) {350//remove alpha if not needed, so compression is more efficient351if (image->get_format() == Image::FORMAT_RGBA8 && image->detect_alpha() == Image::ALPHA_NONE) {352image->convert(Image::FORMAT_RGB8);353}354} else if (image->get_format() < Image::FORMAT_RGBA8) {355image->optimize_channels();356}357}358359Image::CompressSource csource = Image::COMPRESS_SOURCE_GENERIC;360if (channel_pack == 0) {361csource = Image::COMPRESS_SOURCE_SRGB;362} else if (channel_pack == 2) {363// force normal364csource = Image::COMPRESS_SOURCE_NORMAL;365}366367Image::UsedChannels used_channels = image->detect_used_channels(csource);368369Vector<Ref<Image>> slices;370371int slice_w = image->get_width() / hslices;372int slice_h = image->get_height() / vslices;373374for (int i = 0; i < vslices; i++) {375for (int j = 0; j < hslices; j++) {376int x = slice_w * j;377int y = slice_h * i;378Ref<Image> slice = image->get_region(Rect2i(x, y, slice_w, slice_h));379ERR_CONTINUE(slice.is_null() || slice->is_empty());380if (slice->get_width() != slice_w || slice->get_height() != slice_h) {381slice->resize(slice_w, slice_h);382}383slices.push_back(slice);384}385}386387const Image::BasisUniversalPackerParams basisu_params = {388p_options["compress/uastc_level"],389p_options["compress/rdo_quality_loss"],390};391392Array formats_imported;393Ref<LayeredTextureImport> texture_import;394texture_import.instantiate();395texture_import->csource = &csource;396texture_import->save_path = p_save_path;397texture_import->options = p_options;398texture_import->platform_variants = r_platform_variants;399texture_import->image = image;400texture_import->formats_imported = formats_imported;401texture_import->slices = &slices;402texture_import->compress_mode = compress_mode;403texture_import->lossy = lossy;404texture_import->hdr_compression = hdr_compression;405texture_import->mipmaps = mipmaps;406texture_import->used_channels = used_channels;407texture_import->high_quality = high_quality;408409texture_import->basisu_params = basisu_params;410411_check_compress_ctex(p_source_file, texture_import);412if (r_metadata) {413Dictionary meta;414meta["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED;415if (formats_imported.size()) {416meta["imported_formats"] = formats_imported;417}418*r_metadata = meta;419}420421return OK;422}423424const char *ResourceImporterLayeredTexture::compression_formats[] = {425"s3tc_bptc",426"etc2_astc",427nullptr428};429430String ResourceImporterLayeredTexture::get_import_settings_string() const {431String s;432433int index = 0;434while (compression_formats[index]) {435String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);436bool test = GLOBAL_GET(setting_path);437if (test) {438s += String(compression_formats[index]);439}440index++;441}442443return s;444}445446bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const {447//will become invalid if formats are missing to import448if (!p_meta.has("vram_texture")) {449return false;450}451452bool vram = p_meta["vram_texture"];453if (!vram) {454return true; //do not care about non vram455}456457Vector<String> formats_imported;458if (p_meta.has("imported_formats")) {459formats_imported = p_meta["imported_formats"];460}461462int index = 0;463bool valid = true;464while (compression_formats[index]) {465String setting_path = "rendering/textures/vram_compression/import_" + String(compression_formats[index]);466if (ProjectSettings::get_singleton()->has_setting(setting_path)) {467bool test = GLOBAL_GET(setting_path);468if (test) {469if (!formats_imported.has(compression_formats[index])) {470valid = false;471break;472}473}474} else {475WARN_PRINT("Setting for imported format not found: " + setting_path);476}477index++;478}479480return valid;481}482483ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = nullptr;484485ResourceImporterLayeredTexture::ResourceImporterLayeredTexture(bool p_singleton) {486// This should only be set through the EditorNode.487if (p_singleton) {488singleton = this;489}490491mode = MODE_CUBEMAP;492}493494ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {495if (singleton == this) {496singleton = nullptr;497}498}499500void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) {501String extension = get_save_extension();502ERR_FAIL_NULL(r_texture_import->csource);503if (r_texture_import->compress_mode != COMPRESS_VRAM_COMPRESSED) {504// Import normally.505_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,506Image::COMPRESS_S3TC /* IGNORED */, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, false);507return;508}509// Must import in all formats, in order of priority (so platform chooses the best supported one. IE, etc2 over etc).510// Android, GLES 2.x511512const bool can_s3tc_bptc = ResourceImporterTextureSettings::should_import_s3tc_bptc();513const bool can_etc2_astc = ResourceImporterTextureSettings::should_import_etc2_astc();514515// Add list of formats imported516if (can_s3tc_bptc) {517r_texture_import->formats_imported.push_back("s3tc_bptc");518}519if (can_etc2_astc) {520r_texture_import->formats_imported.push_back("etc2_astc");521}522523bool can_compress_hdr = r_texture_import->hdr_compression > 0;524ERR_FAIL_COND(r_texture_import->image.is_null());525bool is_hdr = (r_texture_import->image->get_format() >= Image::FORMAT_RF && r_texture_import->image->get_format() <= Image::FORMAT_RGBE9995);526ERR_FAIL_NULL(r_texture_import->slices);527// Can compress hdr, but hdr with alpha is not compressible.528bool use_uncompressed = false;529530if (is_hdr) {531if (r_texture_import->used_channels == Image::USED_CHANNELS_LA || r_texture_import->used_channels == Image::USED_CHANNELS_RGBA) {532if (r_texture_import->hdr_compression == 2) {533// The user selected to compress hdr anyway, so force an alpha-less format.534if (r_texture_import->image->get_format() == Image::FORMAT_RGBAF) {535for (int i = 0; i < r_texture_import->slices->size(); i++) {536r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBF);537}538539} else if (r_texture_import->image->get_format() == Image::FORMAT_RGBAH) {540for (int i = 0; i < r_texture_import->slices->size(); i++) {541r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBH);542}543}544} else {545can_compress_hdr = false;546}547}548549if (!can_compress_hdr) {550//default to rgbe551if (r_texture_import->image->get_format() != Image::FORMAT_RGBE9995) {552for (int i = 0; i < r_texture_import->slices->size(); i++) {553r_texture_import->slices->write[i]->convert(Image::FORMAT_RGBE9995);554}555}556use_uncompressed = true;557}558}559560if (use_uncompressed) {561_save_tex(*r_texture_import->slices, r_texture_import->save_path + "." + extension, COMPRESS_VRAM_UNCOMPRESSED, r_texture_import->lossy, r_texture_import->basisu_params,562Image::COMPRESS_S3TC /* IGNORED */, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, false);563} else {564if (can_s3tc_bptc) {565Image::CompressMode image_compress_mode;566String image_compress_format;567if (r_texture_import->high_quality || is_hdr) {568image_compress_mode = Image::COMPRESS_BPTC;569image_compress_format = "bptc";570} else {571image_compress_mode = Image::COMPRESS_S3TC;572image_compress_format = "s3tc";573}574_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);575r_texture_import->platform_variants->push_back(image_compress_format);576}577578if (can_etc2_astc) {579Image::CompressMode image_compress_mode;580String image_compress_format;581if (r_texture_import->high_quality || is_hdr) {582image_compress_mode = Image::COMPRESS_ASTC;583image_compress_format = "astc";584} else {585image_compress_mode = Image::COMPRESS_ETC2;586image_compress_format = "etc2";587}588_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);589r_texture_import->platform_variants->push_back(image_compress_format);590}591}592}593594595