From 4aea9f74e650743fe6585d5230dd6e5f6c94c478 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 26 Sep 2019 23:16:44 -0300 Subject: Rewritten StreamTexture for better code reuse, added basis universal support --- editor/editor_node.cpp | 4 +- .../import/resource_importer_layered_texture.cpp | 6 +- editor/import/resource_importer_layered_texture.h | 4 + editor/import/resource_importer_texture.cpp | 306 +++++++++++---------- editor/import/resource_importer_texture.h | 25 +- 5 files changed, 192 insertions(+), 153 deletions(-) (limited to 'editor') diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 567d0fe33d..6256dff967 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -5694,7 +5694,7 @@ EditorNode::EditorNode() { import_texture.instance(); ResourceFormatImporter::get_singleton()->add_importer(import_texture); - Ref import_cubemap; + /* Ref import_cubemap; import_cubemap.instance(); import_cubemap->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP); ResourceFormatImporter::get_singleton()->add_importer(import_cubemap); @@ -5708,7 +5708,7 @@ EditorNode::EditorNode() { import_cubemap_array.instance(); import_cubemap_array->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP_ARRAY); ResourceFormatImporter::get_singleton()->add_importer(import_cubemap_array); - +*/ Ref import_image; import_image.instance(); ResourceFormatImporter::get_singleton()->add_importer(import_image); diff --git a/editor/import/resource_importer_layered_texture.cpp b/editor/import/resource_importer_layered_texture.cpp index 01b938e690..9ea2911c63 100644 --- a/editor/import/resource_importer_layered_texture.cpp +++ b/editor/import/resource_importer_layered_texture.cpp @@ -38,6 +38,7 @@ #include "editor/editor_node.h" #include "scene/resources/texture.h" +#if 0 String ResourceImporterLayeredTexture::get_importer_name() const { switch (mode) { @@ -312,8 +313,8 @@ Error ResourceImporterLayeredTexture::import(const String &p_source_file, const encode_bptc = true; if (no_bptc_if_rgb) { - Image::DetectChannels channels = image->get_detected_channels(); - if (channels != Image::DETECTED_LA && channels != Image::DETECTED_RGBA) { + Image::UsedChannels channels = image->detect_used_channels(); + if (channels != Image::USED_CHANNELS_LA && channels != Image::USED_CHANNELS_RGBA) { encode_bptc = false; } } @@ -447,3 +448,4 @@ ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() { ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() { } +#endif diff --git a/editor/import/resource_importer_layered_texture.h b/editor/import/resource_importer_layered_texture.h index 3dcf50e004..84c7aeb35d 100644 --- a/editor/import/resource_importer_layered_texture.h +++ b/editor/import/resource_importer_layered_texture.h @@ -1,3 +1,4 @@ +#if 0 /*************************************************************************/ /* resource_importer_layered_texture.h */ /*************************************************************************/ @@ -34,6 +35,7 @@ #include "core/image.h" #include "core/io/resource_importer.h" + class StreamTexture; class ResourceImporterLayeredTexture : public ResourceImporter { @@ -95,3 +97,5 @@ public: ~ResourceImporterLayeredTexture(); }; #endif // RESOURCE_IMPORTER_LAYERED_TEXTURE_H + +#endif diff --git a/editor/import/resource_importer_texture.cpp b/editor/import/resource_importer_texture.cpp index b29f78c236..fdfee44a26 100644 --- a/editor/import/resource_importer_texture.cpp +++ b/editor/import/resource_importer_texture.cpp @@ -32,9 +32,9 @@ #include "core/io/config_file.h" #include "core/io/image_loader.h" +#include "core/version.h" #include "editor/editor_file_system.h" #include "editor/editor_node.h" -#include "scene/resources/texture.h" void ResourceImporterTexture::_texture_reimport_roughness(const Ref &p_tex, const String &p_normal_path, VS::TextureDetectRoughnessChannel p_channel) { @@ -115,10 +115,15 @@ void ResourceImporterTexture::update_imports() { changed = true; } - if (E->get().flags & MAKE_3D_FLAG && bool(cf->get_value("params", "usage/detect_3d"))) { - cf->set_value("params", "usage/detect_3d", false); - cf->set_value("params", "compress/mode", 2); - cf->set_value("params", "format/mipmaps", true); + if (E->get().flags & MAKE_3D_FLAG && bool(cf->get_value("params", "detect_3d/compress_to"))) { + int compress_to = cf->get_value("params", "detect_3d/compress_to"); + cf->set_value("params", "detect_3d/compress_to", 0); + if (compress_to == 1) { + cf->set_value("params", "compress/mode", COMPRESS_VRAM_COMPRESSED); + } else if (compress_to == 2) { + cf->set_value("params", "compress/mode", COMPRESS_BASIS_UNIVERSAL); + } + cf->set_value("params", "mipmaps/generate", true); changed = true; } @@ -163,17 +168,20 @@ bool ResourceImporterTexture::get_option_visibility(const String &p_option, cons if (p_option == "compress/lossy_quality") { int compress_mode = int(p_options["compress/mode"]); - if (compress_mode != COMPRESS_LOSSY && compress_mode != COMPRESS_VIDEO_RAM) { + if (compress_mode != COMPRESS_LOSSY && compress_mode != COMPRESS_VRAM_COMPRESSED) { return false; } } else if (p_option == "compress/hdr_mode") { int compress_mode = int(p_options["compress/mode"]); - if (compress_mode != COMPRESS_VIDEO_RAM) { + if (compress_mode < COMPRESS_VRAM_COMPRESSED) { return false; } + } else if (p_option == "mipmaps/limit") { + return p_options["mipmaps/generate"]; + } else if (p_option == "compress/bptc_ldr") { int compress_mode = int(p_options["compress/mode"]); - if (compress_mode != COMPRESS_VIDEO_RAM) { + if (compress_mode < COMPRESS_VRAM_COMPRESSED) { return false; } if (!ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc")) { @@ -201,13 +209,15 @@ String ResourceImporterTexture::get_preset_name(int p_idx) const { void ResourceImporterTexture::get_import_options(List *r_options, int p_preset) const { - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Lossless,Lossy,Video RAM,Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), p_preset == PRESET_3D ? 2 : 0)); + r_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), p_preset == PRESET_3D ? 2 : 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_mode", PROPERTY_HINT_ENUM, "Enabled,Force RGBE"), 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/bptc_ldr", PROPERTY_HINT_ENUM, "Enabled,RGBA Only"), 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/normal_map", PROPERTY_HINT_ENUM, "Detect,Enable,Disabled"), 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/channel_pack", PROPERTY_HINT_ENUM, "sRGB Friendly,Optimized"), 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "format/mipmaps", PROPERTY_HINT_ENUM, "Detect,Disabled,Enabled"), p_preset == PRESET_DETECT ? 0 : (p_preset == PRESET_3D ? 2 : 1))); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/streamed"), false)); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "mipmaps/generate"), (p_preset == PRESET_3D ? true : false))); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "mipmaps/limit", PROPERTY_HINT_RANGE, "0,64"), -1)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "roughness/mode", PROPERTY_HINT_ENUM, "Detect,Disabled,Red,Green,Blue,Alpha,Gray"), 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "roughness/src_normal", PROPERTY_HINT_FILE, "*.png,*.jpg"), "")); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/fix_alpha_border"), p_preset != PRESET_3D)); @@ -215,82 +225,24 @@ void ResourceImporterTexture::get_import_options(List *r_options, r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/invert_color"), false)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/HDR_as_SRGB"), false)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/size_limit", PROPERTY_HINT_RANGE, "0,4096,1"), 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "process/mipmap_limit", PROPERTY_HINT_RANGE, "0,999,1"), 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "usage/stream"), false)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "usage/detect_3d"), p_preset == PRESET_DETECT)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "detect_3d/compress_to", PROPERTY_HINT_ENUM, "Disabled,VRAM Compressed,Basis Universal"), (p_preset == PRESET_DETECT) ? 2 : 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "svg/scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0)); } -void ResourceImporterTexture::_save_stex(const Ref &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_roughness, bool p_force_rgbe, bool p_detect_normal, bool p_force_normal, bool p_srgb_friendly, bool p_force_po2_for_compressed) { - - FileAccess *f = FileAccess::open(p_to_path, FileAccess::WRITE); - f->store_8('G'); - f->store_8('D'); - f->store_8('S'); - f->store_8('T'); //godot streamable texture - - bool resize_to_po2 = false; - - if (p_compress_mode == COMPRESS_VIDEO_RAM && p_force_po2_for_compressed && p_mipmaps) { - resize_to_po2 = true; - f->store_16(next_power_of_2(p_image->get_width())); - f->store_16(p_image->get_width()); - f->store_16(next_power_of_2(p_image->get_height())); - f->store_16(p_image->get_height()); - } else { - f->store_16(p_image->get_width()); - f->store_16(0); - f->store_16(p_image->get_height()); - f->store_16(0); - } - f->store_32(0); //texture flags deprecated - - uint32_t format = 0; - /* - print_line("streamable " + itos(p_streamable)); - print_line("mipmaps " + itos(p_mipmaps)); - print_line("detect_3d " + itos(p_detect_3d)); - print_line("roughness " + itos(p_detect_roughness)); - print_line("normal " + itos(p_detect_normal)); -*/ - if (p_streamable) - format |= StreamTexture::FORMAT_BIT_STREAM; - if (p_mipmaps) - format |= StreamTexture::FORMAT_BIT_HAS_MIPMAPS; //mipmaps bit - if (p_detect_3d) - format |= StreamTexture::FORMAT_BIT_DETECT_3D; - if (p_detect_roughness) - format |= StreamTexture::FORMAT_BIT_DETECT_ROUGNESS; - if (p_detect_normal) - format |= StreamTexture::FORMAT_BIT_DETECT_NORMAL; - - if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() > Image::FORMAT_RGBA8) { - p_compress_mode = COMPRESS_UNCOMPRESSED; //these can't go as lossy - } +void ResourceImporterTexture::save_to_stex_format(FileAccess *f, const Ref &p_image, CompressMode p_compress_mode, Image::UsedChannels p_channels, Image::CompressMode p_compress_format, float p_lossy_quality, bool p_force_rgbe) { switch (p_compress_mode) { case COMPRESS_LOSSLESS: { - Ref image = p_image->duplicate(); - if (p_mipmaps) { - image->generate_mipmaps(); - } else { - image->clear_mipmaps(); - } + f->store_32(StreamTexture::DATA_FORMAT_LOSSLESS); + f->store_16(p_image->get_width()); + f->store_16(p_image->get_height()); + f->store_32(p_image->get_mipmap_count()); + f->store_32(p_image->get_format()); - int mmc = image->get_mipmap_count() + 1; + for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) { - format |= StreamTexture::FORMAT_BIT_LOSSLESS; - f->store_32(format); - f->store_32(mmc); - - for (int i = 0; i < mmc; i++) { - - if (i > 0) { - image->shrink_x2(); - } - - PoolVector data = Image::lossless_packer(image); + PoolVector data = Image::lossless_packer(p_image->get_image_from_mipmap(i)); int data_len = data.size(); f->store_32(data_len); @@ -300,26 +252,16 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String } break; case COMPRESS_LOSSY: { - Ref image = p_image->duplicate(); - if (p_mipmaps) { - image->generate_mipmaps(); - } else { - image->clear_mipmaps(); - } - - int mmc = image->get_mipmap_count() + 1; - format |= StreamTexture::FORMAT_BIT_LOSSY; - f->store_32(format); - f->store_32(mmc); + f->store_32(StreamTexture::DATA_FORMAT_LOSSY); + f->store_16(p_image->get_width()); + f->store_16(p_image->get_height()); + f->store_32(p_image->get_mipmap_count()); + f->store_32(p_image->get_format()); - for (int i = 0; i < mmc; i++) { + for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) { - if (i > 0) { - image->shrink_x2(); - } - - PoolVector data = Image::lossy_packer(image, p_lossy_quality); + PoolVector data = Image::lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality); int data_len = data.size(); f->store_32(data_len); @@ -327,77 +269,156 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String f->store_buffer(r.ptr(), data_len); } } break; - case COMPRESS_VIDEO_RAM: { + case COMPRESS_VRAM_COMPRESSED: { Ref image = p_image->duplicate(); - if (resize_to_po2) { - image->resize_to_po2(); - } - if (p_mipmaps) { - image->generate_mipmaps(p_force_normal); - } - if (p_force_rgbe && image->get_format() >= Image::FORMAT_R8 && image->get_format() <= Image::FORMAT_RGBE9995) { + if (p_force_rgbe && image->get_format() >= Image::FORMAT_RF && image->get_format() < Image::FORMAT_RGBE9995) { image->convert(Image::FORMAT_RGBE9995); } else { - Image::CompressSource csource = Image::COMPRESS_SOURCE_GENERIC; - if (p_force_normal) { - csource = Image::COMPRESS_SOURCE_NORMAL; - } else if (p_srgb_friendly) { - csource = Image::COMPRESS_SOURCE_SRGB; - } - - image->compress(p_vram_compression, csource, p_lossy_quality); + image->compress_from_channels(p_compress_format, p_channels, p_lossy_quality); } - format |= image->get_format(); - - f->store_32(format); + f->store_32(StreamTexture::DATA_FORMAT_IMAGE); + f->store_16(image->get_width()); + f->store_16(image->get_height()); + f->store_32(image->get_mipmap_count()); + f->store_32(image->get_format()); PoolVector data = image->get_data(); int dl = data.size(); PoolVector::Read r = data.read(); f->store_buffer(r.ptr(), dl); } break; - case COMPRESS_UNCOMPRESSED: { + case COMPRESS_VRAM_UNCOMPRESSED: { - Ref image = p_image->duplicate(); - if (p_mipmaps) { - image->generate_mipmaps(); - } else { - image->clear_mipmaps(); - } + f->store_32(StreamTexture::DATA_FORMAT_IMAGE); + f->store_16(p_image->get_width()); + f->store_16(p_image->get_height()); + f->store_32(p_image->get_mipmap_count()); + f->store_32(p_image->get_format()); - format |= image->get_format(); - f->store_32(format); - - PoolVector data = image->get_data(); + PoolVector data = p_image->get_data(); int dl = data.size(); PoolVector::Read r = data.read(); f->store_buffer(r.ptr(), dl); } break; + case COMPRESS_BASIS_UNIVERSAL: { + + f->store_32(StreamTexture::DATA_FORMAT_BASIS_UNIVERSAL); + f->store_16(p_image->get_width()); + f->store_16(p_image->get_height()); + f->store_32(p_image->get_mipmap_count()); + f->store_32(p_image->get_format()); + + for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) { + + PoolVector data = Image::basis_universal_packer(p_image->get_image_from_mipmap(i), p_channels); + int data_len = data.size(); + f->store_32(data_len); + + PoolVector::Read r = data.read(); + f->store_buffer(r.ptr(), data_len); + } + } break; } +} + +void ResourceImporterTexture::_save_stex(const Ref &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_roughness, bool p_force_rgbe, bool p_detect_normal, bool p_force_normal, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap) { + + FileAccess *f = FileAccess::open(p_to_path, FileAccess::WRITE); + f->store_8('G'); + f->store_8('S'); + f->store_8('T'); + f->store_8('2'); //godot streamable texture 2D + + //format version + f->store_32(StreamTexture::FORMAT_VERSION); + //texture may be resized later, so original size must be saved first + f->store_32(p_image->get_width()); + f->store_32(p_image->get_height()); + + uint32_t flags = 0; + if (p_streamable) + flags |= StreamTexture::FORMAT_BIT_STREAM; + if (p_mipmaps) + flags |= StreamTexture::FORMAT_BIT_HAS_MIPMAPS; //mipmaps bit + if (p_detect_3d) + flags |= StreamTexture::FORMAT_BIT_DETECT_3D; + if (p_detect_roughness) + flags |= StreamTexture::FORMAT_BIT_DETECT_ROUGNESS; + if (p_detect_normal) + flags |= StreamTexture::FORMAT_BIT_DETECT_NORMAL; + + f->store_32(flags); + f->store_32(p_limit_mipmap); + //reserverd for future use + f->store_32(0); + f->store_32(0); + f->store_32(0); + + /* + print_line("streamable " + itos(p_streamable)); + print_line("mipmaps " + itos(p_mipmaps)); + print_line("detect_3d " + itos(p_detect_3d)); + print_line("roughness " + itos(p_detect_roughness)); + print_line("normal " + itos(p_detect_normal)); +*/ + + if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() > Image::FORMAT_RGBA8) { + p_compress_mode = COMPRESS_VRAM_UNCOMPRESSED; //these can't go as lossy + } + + Ref image = p_image->duplicate(); + + if (((p_compress_mode == COMPRESS_BASIS_UNIVERSAL) || (p_compress_mode == COMPRESS_VRAM_COMPRESSED && p_force_po2_for_compressed)) && p_mipmaps) { + image->resize_to_po2(); + } + + if (p_mipmaps && (!image->has_mipmaps() || p_force_normal)) { + image->generate_mipmaps(p_force_normal); + } + + if (!p_mipmaps) { + image->clear_mipmaps(); + } + + if (p_force_rgbe && image->get_format() >= Image::FORMAT_RF && image->get_format() < Image::FORMAT_RGBE9995) { + image->convert(Image::FORMAT_RGBE9995); + } + + Image::CompressSource csource = Image::COMPRESS_SOURCE_GENERIC; + if (p_force_normal) { + csource = Image::COMPRESS_SOURCE_NORMAL; + } else if (p_srgb_friendly) { + csource = Image::COMPRESS_SOURCE_SRGB; + } + + Image::UsedChannels used_channels = image->detect_used_channels(csource); + + save_to_stex_format(f, image, p_compress_mode, used_channels, p_vram_compression, p_lossy_quality, p_force_rgbe); memdelete(f); } Error ResourceImporterTexture::import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata) { - int compress_mode = p_options["compress/mode"]; + CompressMode compress_mode = CompressMode(int(p_options["compress/mode"])); float lossy = p_options["compress/lossy_quality"]; int pack_channels = p_options["compress/channel_pack"]; - bool mipmaps = p_options["format/mipmaps"]; + bool mipmaps = p_options["mipmaps/generate"]; + uint32_t mipmap_limit = int(mipmaps ? int(p_options["mipmaps/limit"]) : int(-1)); bool fix_alpha_border = p_options["process/fix_alpha_border"]; bool premult_alpha = p_options["process/premult_alpha"]; bool invert_color = p_options["process/invert_color"]; - bool stream = p_options["usage/stream"]; + bool stream = p_options["compress/streamed"]; int size_limit = p_options["process/size_limit"]; bool hdr_as_srgb = p_options["process/HDR_as_SRGB"]; int normal = p_options["compress/normal_map"]; float scale = p_options["svg/scale"]; - bool force_rgbe = p_options["compress/hdr_mode"]; + bool force_rgbe = int(p_options["compress/hdr_mode"]) == 1; int bptc_ldr = p_options["compress/bptc_ldr"]; int roughness = p_options["roughness/mode"]; @@ -450,33 +471,38 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String image->unlock(); } - bool detect_3d = p_options["usage/detect_3d"]; + if (compress_mode == COMPRESS_BASIS_UNIVERSAL && image->get_format() >= Image::FORMAT_RF) { + //basis universal does not support float formats, fall back + compress_mode = COMPRESS_VRAM_COMPRESSED; + } + + bool detect_3d = int(p_options["detect_3d/compress_to"]) > 0; bool detect_roughness = roughness == 0; bool detect_normal = normal == 0; bool force_normal = normal == 1; bool srgb_friendly_pack = pack_channels == 0; - if (compress_mode == COMPRESS_VIDEO_RAM) { + if (compress_mode == COMPRESS_VRAM_COMPRESSED) { //must import in all formats, in order of priority (so platform choses the best supported one. IE, etc2 over etc). //Android, GLES 2.x bool ok_on_pc = false; bool is_hdr = (image->get_format() >= Image::FORMAT_RF && image->get_format() <= Image::FORMAT_RGBE9995); - bool is_ldr = (image->get_format() >= Image::FORMAT_L8 && image->get_format() <= Image::FORMAT_RGBA5551); + bool is_ldr = (image->get_format() >= Image::FORMAT_L8 && image->get_format() <= Image::FORMAT_RGB565); bool can_bptc = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_bptc"); bool can_s3tc = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_s3tc"); if (can_bptc) { - Image::DetectChannels channels = image->get_detected_channels(); + Image::UsedChannels channels = image->detect_used_channels(); if (is_hdr) { - if (channels == Image::DETECTED_LA || channels == Image::DETECTED_RGBA) { + if (channels == Image::USED_CHANNELS_LA || channels == Image::USED_CHANNELS_RGBA) { can_bptc = false; } } else if (is_ldr) { //handle "RGBA Only" setting - if (bptc_ldr == 1 && channels != Image::DETECTED_LA && channels != Image::DETECTED_RGBA) { + if (bptc_ldr == 1 && channels != Image::USED_CHANNELS_LA && channels != Image::USED_CHANNELS_RGBA) { can_bptc = false; } } @@ -490,7 +516,7 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String } if (can_bptc || can_s3tc) { - _save_stex(image, p_save_path + ".s3tc.stex", compress_mode, lossy, can_bptc ? Image::COMPRESS_BPTC : Image::COMPRESS_S3TC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false); + _save_stex(image, p_save_path + ".s3tc.stex", compress_mode, lossy, can_bptc ? Image::COMPRESS_BPTC : Image::COMPRESS_S3TC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit); r_platform_variants->push_back("s3tc"); formats_imported.push_back("s3tc"); ok_on_pc = true; @@ -498,20 +524,20 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc2")) { - _save_stex(image, p_save_path + ".etc2.stex", compress_mode, lossy, Image::COMPRESS_ETC2, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true); + _save_stex(image, p_save_path + ".etc2.stex", compress_mode, lossy, Image::COMPRESS_ETC2, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit); r_platform_variants->push_back("etc2"); formats_imported.push_back("etc2"); } if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc")) { - _save_stex(image, p_save_path + ".etc.stex", compress_mode, lossy, Image::COMPRESS_ETC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true); + _save_stex(image, p_save_path + ".etc.stex", compress_mode, lossy, Image::COMPRESS_ETC, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit); r_platform_variants->push_back("etc"); formats_imported.push_back("etc"); } if (ProjectSettings::get_singleton()->get("rendering/vram_compression/import_pvrtc")) { - _save_stex(image, p_save_path + ".pvrtc.stex", compress_mode, lossy, Image::COMPRESS_PVRTC4, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true); + _save_stex(image, p_save_path + ".pvrtc.stex", compress_mode, lossy, Image::COMPRESS_PVRTC4, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, true, mipmap_limit); r_platform_variants->push_back("pvrtc"); formats_imported.push_back("pvrtc"); } @@ -521,12 +547,12 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String } } else { //import normally - _save_stex(image, p_save_path + ".stex", compress_mode, lossy, Image::COMPRESS_S3TC /*this is ignored */, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false); + _save_stex(image, p_save_path + ".stex", compress_mode, lossy, Image::COMPRESS_S3TC /*this is ignored */, mipmaps, stream, detect_3d, detect_roughness, force_rgbe, detect_normal, force_normal, srgb_friendly_pack, false, mipmap_limit); } if (r_metadata) { Dictionary metadata; - metadata["vram_texture"] = compress_mode == COMPRESS_VIDEO_RAM; + metadata["vram_texture"] = compress_mode == COMPRESS_VRAM_COMPRESSED; if (formats_imported.size()) { metadata["imported_formats"] = formats_imported; } diff --git a/editor/import/resource_importer_texture.h b/editor/import/resource_importer_texture.h index acbfde8430..bd10924950 100644 --- a/editor/import/resource_importer_texture.h +++ b/editor/import/resource_importer_texture.h @@ -33,12 +33,24 @@ #include "core/image.h" #include "core/io/resource_importer.h" +#include "core/os/file_access.h" +#include "scene/resources/texture.h" #include "servers/visual_server.h" + class StreamTexture; class ResourceImporterTexture : public ResourceImporter { GDCLASS(ResourceImporterTexture, ResourceImporter); +public: + enum CompressMode { + COMPRESS_LOSSLESS, + COMPRESS_LOSSY, + COMPRESS_VRAM_COMPRESSED, + COMPRESS_VRAM_UNCOMPRESSED, + COMPRESS_BASIS_UNIVERSAL + }; + protected: enum { MAKE_3D_FLAG = 1, @@ -67,7 +79,11 @@ protected: static ResourceImporterTexture *singleton; static const char *compression_formats[]; + void _save_stex(const Ref &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_force_rgbe, bool p_detect_normal, bool p_force_normal, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap); + public: + void save_to_stex_format(FileAccess *f, const Ref &p_image, CompressMode p_compress_mode, Image::UsedChannels p_channels, Image::CompressMode p_compress_format, float p_lossy_quality, bool p_force_rgbe); + static ResourceImporterTexture *get_singleton() { return singleton; } virtual String get_importer_name() const; virtual String get_visible_name() const; @@ -82,21 +98,12 @@ public: PRESET_3D, }; - enum CompressMode { - COMPRESS_LOSSLESS, - COMPRESS_LOSSY, - COMPRESS_VIDEO_RAM, - COMPRESS_UNCOMPRESSED - }; - virtual int get_preset_count() const; virtual String get_preset_name(int p_idx) const; virtual void get_import_options(List *r_options, int p_preset = 0) const; virtual bool get_option_visibility(const String &p_option, const Map &p_options) const; - void _save_stex(const Ref &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_force_rgbe, bool p_detect_normal, bool p_force_normal, bool p_srgb_friendly, bool p_force_po2_for_compressed); - virtual Error import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); void update_imports(); -- cgit v1.2.3