Skip to content

Commit

Permalink
Stop handling imagetextures in the custom texture loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Nov 6, 2024
1 parent d903bcc commit 52302a8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
135 changes: 72 additions & 63 deletions compat/texture_loader_compat.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "compat/texture_loader_compat.h"
#include "compat/image_enum_compat.h"
#include "compat/resource_compat_binary.h"
#include "compat/resource_loader_compat.h"
#include "compat/webp_compat.h"
#include "core/io/resource_loader.h"
#include "utility/resource_info.h"

#include "core/error/error_list.h"
Expand Down Expand Up @@ -704,62 +706,25 @@ Vector<Ref<Image>> TextureLoaderCompat::load_images_from_layered_tex(const Strin
}

bool ResourceConverterTexture2D::handles_type(const String &p_type, int ver_major) const {
return (p_type == "Texture" && ver_major == 2) || (p_type == "Texture2D") || (p_type == "ImageTexture") || (p_type == "StreamTexture") || (p_type == "CompressedTexture2D");
return (p_type == "Texture" && ver_major <= 3) || (p_type == "Texture2D") || (p_type == "StreamTexture") || (p_type == "CompressedTexture2D");
}

Ref<Resource> ResourceConverterTexture2D::convert(const Ref<MissingResource> &res, ResourceInfo::LoadType p_type, int ver_major, Error *r_error) {
String name;
Vector2 size;
int tw = 0;
int th = 0;
int tw_custom = 0;
int th_custom = 0;
int flags = 0;
Ref<Image> image;
Ref<Resource> image_res;
Ref<Resource> texture;

if (p_type == ResourceInfo::LoadType::NON_GLOBAL_LOAD) {
return res;
}
Dictionary compat_dict = (res->get_meta("compat", Dictionary()));
String type = res->get_original_class();

auto convert_image = [&](const Ref<Resource> &image_res) -> Ref<Image> {
Ref<Image> image = image_res;
if (image.is_null() && image_res->get_class() == "MissingResource") {
ImageConverterCompat ic;
if (ic.handles_type("Image", ver_major)) {
image = ic.convert(image_res, p_type, ver_major, r_error);
}
}
return image;
};
if ((type == "Texture" && ver_major == 2) || type == "ImageTexture") {
name = get_resource_name(res, ver_major);
image = convert_image(res->get("image"));
ERR_FAIL_COND_V_MSG(image.is_null(), res, "Cannot load resource '" + name + "'.");

size = res->get("size");
flags = res->get("flags");
bool mipmaps = flags & 1 || image->has_mipmaps();

image->set_name(name);
tw = image->get_width();
th = image->get_height();
if (size.width && tw != size.width) {
tw_custom = size.width;
}
if (size.height && th != size.height) {
th_custom = size.height;
}
texture = TextureLoaderCompat::create_image_texture(res->get_path(), p_type, tw, th, tw_custom, th_custom, mipmaps, image);
} else if (ver_major >= 3) {
if (p_type == ResourceInfo::LoadType::NON_GLOBAL_LOAD) {
return res;
}
flags = res->get("flags");
String load_path = res->get("load_path");
ResourceFormatLoaderCompatTexture2D tlc;
texture = tlc.custom_load(load_path, p_type, r_error);
} else {
ERR_FAIL_V_MSG(res, "Unsupported type: " + type);
int flags = res->get("flags");
String load_path = res->get("load_path");
if (p_type == ResourceInfo::GLTF_LOAD) {
texture = ResourceCompatLoader::gltf_load(load_path, type, r_error);
} else if (p_type == ResourceInfo::REAL_LOAD) {
texture = ResourceCompatLoader::real_load(load_path, type, ResourceFormatLoader::CACHE_MODE_IGNORE, r_error);
}
if (compat_dict.size() > 0) {
texture->set_meta("compat", compat_dict);
Expand All @@ -771,12 +736,11 @@ Ref<Resource> ResourceConverterTexture2D::convert(const Ref<MissingResource> &re
void ResourceFormatLoaderCompatTexture2D::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("stex");
p_extensions->push_back("ctex");
p_extensions->push_back("tex");
}

// handles type
bool ResourceFormatLoaderCompatTexture2D::handles_type(const String &p_type) const {
return p_type == "CompressedTexture2D" || (p_type == "Texture2D") || p_type == "ImageTexture" || p_type == "StreamTexture" || p_type == "Texture";
return p_type == "CompressedTexture2D" || p_type == "StreamTexture";
}

// get resource type
Expand Down Expand Up @@ -855,13 +819,9 @@ Ref<Resource> ResourceFormatLoaderCompatTexture2D::custom_load(const String &p_p
return Ref<Resource>();
}
int lw, lh, lwc, lhc, lflags;
int ver_major = TextureLoaderCompat::get_ver_major_from_textype(t);
Ref<Resource> texture;
Ref<Image> image;
bool convert = false;
if (TextureLoaderCompat::is_binary_resource(t)) {
convert = true;
} else if (t == TextureLoaderCompat::FORMAT_V3_STREAM_TEXTURE2D) {
if (t == TextureLoaderCompat::FORMAT_V3_STREAM_TEXTURE2D) {
err = TextureLoaderCompat::_load_data_stex2d_v3(p_path, lw, lh, lwc, lhc, lflags, image);
} else if (t == TextureLoaderCompat::FORMAT_V4_COMPRESSED_TEXTURE2D) {
err = TextureLoaderCompat::_load_data_ctex2d_v4(p_path, lw, lh, lwc, lhc, image);
Expand All @@ -872,14 +832,7 @@ Ref<Resource> ResourceFormatLoaderCompatTexture2D::custom_load(const String &p_p
*r_error = err;
}
ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Failed to load texture " + p_path);
if (!convert) {
texture = _set_tex(p_path, p_type, lw, lh, lwc, lhc, lflags, image);
} else {
ResourceFormatLoaderCompatBinary rlcb;
Ref<Resource> res = rlcb.custom_load(p_path, ResourceInfo::LoadType::FAKE_LOAD, &err);
ResourceConverterTexture2D rc;
texture = rc.convert(res, p_type, ver_major, &err);
}
texture = _set_tex(p_path, p_type, lw, lh, lwc, lhc, lflags, image);
// TODO: Take care of the cache mode
set_res_path(texture, p_path, p_type);
auto info = TextureLoaderCompat::_get_resource_info(t);
Expand Down Expand Up @@ -1059,6 +1012,62 @@ Ref<Resource> ResourceFormatLoaderCompatTextureLayered::custom_load(const String
return texture;
}

bool ImageTextureConverterCompat::handles_type(const String &p_type, int ver_major) const {
return (p_type == "ImageTexture" && ver_major <= 3);
}

Ref<Resource> ImageTextureConverterCompat::convert(const Ref<MissingResource> &res, ResourceInfo::LoadType p_type, int ver_major, Error *r_error) {
String name;
Vector2 size;
int tw = 0;
int th = 0;
int tw_custom = 0;
int th_custom = 0;
int flags = 0;
Ref<Image> image;
Ref<Resource> image_res;
Ref<Resource> texture;
Dictionary compat_dict = (res->get_meta("compat", Dictionary()));
String type = res->get_original_class();

auto convert_image = [&](const Ref<Resource> &image_res) -> Ref<Image> {
Ref<Image> image = image_res;
if (image.is_null() && image_res->get_class() == "MissingResource") {
ImageConverterCompat ic;
if (ic.handles_type("Image", ver_major)) {
image = ic.convert(image_res, p_type, ver_major, r_error);
}
}
return image;
};
if (type == "ImageTexture") {
name = get_resource_name(res, ver_major);
image = convert_image(res->get("image"));
ERR_FAIL_COND_V_MSG(image.is_null(), res, "Cannot load resource '" + name + "'.");

size = res->get("size");
flags = res->get("flags");
bool mipmaps = flags & 1 || image->has_mipmaps();

image->set_name(name);
tw = image->get_width();
th = image->get_height();
if (size.width && tw != size.width) {
tw_custom = size.width;
}
if (size.height && th != size.height) {
th_custom = size.height;
}
texture = TextureLoaderCompat::create_image_texture(res->get_path(), p_type, tw, th, tw_custom, th_custom, mipmaps, image);
} else {
ERR_FAIL_V_MSG(res, "Unsupported type: " + type);
}
if (compat_dict.size() > 0) {
texture->set_meta("compat", compat_dict);
}
return texture;
}

bool ImageConverterCompat::handles_type(const String &p_type, int ver_major) const {
return (p_type == "Image") && ver_major == 3;
}
Expand Down
10 changes: 7 additions & 3 deletions compat/texture_loader_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class ResourceConverterTexture2D : public ResourceCompatConverter {
public:
virtual Ref<Resource> convert(const Ref<MissingResource> &res, ResourceInfo::LoadType p_type, int ver_major, Error *r_error = nullptr) override;
virtual bool handles_type(const String &p_type, int ver_major) const override;

// static Ref<CompressedTexture2D> _load_texture2d(const String &p_path, Ref<Image> &image, bool &size_override, int ver_major, Error *r_err) const;
};

class ImageConverterCompat : public ResourceCompatConverter {
Expand All @@ -70,8 +68,14 @@ class ImageConverterCompat : public ResourceCompatConverter {
public:
virtual Ref<Resource> convert(const Ref<MissingResource> &res, ResourceInfo::LoadType p_type, int ver_major, Error *r_error = nullptr) override;
virtual bool handles_type(const String &p_type, int ver_major) const override;
};

class ImageTextureConverterCompat : public ResourceCompatConverter {
GDCLASS(ImageTextureConverterCompat, ResourceCompatConverter);

// static Ref<CompressedTexture2D> _load_texture2d(const String &p_path, Ref<Image> &image, bool &size_override, int ver_major, Error *r_err) const;
public:
virtual Ref<Resource> convert(const Ref<MissingResource> &res, ResourceInfo::LoadType p_type, int ver_major, Error *r_error = nullptr) override;
virtual bool handles_type(const String &p_type, int ver_major) const override;
};

class ResourceFormatLoaderCompatTexture2D : public CompatFormatLoader {
Expand Down
11 changes: 9 additions & 2 deletions exporters/texture_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,17 @@ Error TextureExporter::_convert_atex(const String &p_path, const String &dest_pa

Error TextureExporter::export_file(const String &out_path, const String &res_path) {
Error err;
auto recognized = TextureLoaderCompat::recognize(res_path, &err);
if (recognized == TextureLoaderCompat::FORMAT_NOT_TEXTURE) {
auto res_info = ResourceCompatLoader::get_resource_info(res_path, "", &err);
if (!handles_import("", res_info.type)) {
return ERR_FILE_UNRECOGNIZED;
}
if (res_info.type == "BitMap") {
return _convert_bitmap(res_path, out_path);
}
String fmt_name;
if (res_info.type == "AtlasTexture") {
return _convert_atex(res_path, out_path, false, fmt_name);
}
return _convert_tex(res_path, out_path, false, fmt_name);
}

Expand Down Expand Up @@ -314,6 +320,7 @@ Ref<ExportReport> TextureExporter::export_resource(const String &output_dir, Ref
}

void TextureExporter::get_handled_types(List<String> *out) const {
out->push_back("Texture");
out->push_back("Texture2D");
out->push_back("ImageTexture");
out->push_back("StreamTexture");
Expand Down
8 changes: 8 additions & 0 deletions register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static Ref<ResourceFormatLoaderCompatTextureLayered> texture_layered_loader = nu
static Ref<SampleConverterCompat> sample_converter = nullptr;
static Ref<ResourceConverterTexture2D> texture_converter = nullptr;
static Ref<ImageConverterCompat> image_converter = nullptr;
static Ref<ImageTextureConverterCompat> image_texture_converter = nullptr;
static Ref<OggStreamConverterCompat> ogg_converter = nullptr;

//exporters
Expand Down Expand Up @@ -90,6 +91,7 @@ void init_loaders() {
sample_converter = memnew(SampleConverterCompat);
texture_converter = memnew(ResourceConverterTexture2D);
image_converter = memnew(ImageConverterCompat);
image_texture_converter = memnew(ImageTextureConverterCompat);
ogg_converter = memnew(OggStreamConverterCompat);
ResourceCompatLoader::add_resource_format_loader(binary_loader, true);
ResourceCompatLoader::add_resource_format_loader(text_loader, true);
Expand All @@ -99,6 +101,7 @@ void init_loaders() {
ResourceCompatLoader::add_resource_object_converter(sample_converter, true);
ResourceCompatLoader::add_resource_object_converter(texture_converter, true);
ResourceCompatLoader::add_resource_object_converter(image_converter, true);
ResourceCompatLoader::add_resource_object_converter(image_texture_converter, true);
ResourceCompatLoader::add_resource_object_converter(ogg_converter, true);
}

Expand Down Expand Up @@ -181,6 +184,9 @@ void deinit_loaders() {
if (image_converter.is_valid()) {
ResourceCompatLoader::remove_resource_object_converter(image_converter);
}
if (image_texture_converter.is_valid()) {
ResourceCompatLoader::remove_resource_object_converter(image_texture_converter);
}
if (ogg_converter.is_valid()) {
ResourceCompatLoader::remove_resource_object_converter(ogg_converter);
}
Expand All @@ -192,6 +198,7 @@ void deinit_loaders() {
sample_converter = nullptr;
texture_converter = nullptr;
image_converter = nullptr;
image_texture_converter = nullptr;
ogg_converter = nullptr;
}

Expand Down Expand Up @@ -237,6 +244,7 @@ void initialize_gdsdecomp_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<SampleConverterCompat>();
ClassDB::register_class<ResourceConverterTexture2D>();
ClassDB::register_class<ImageConverterCompat>();
ClassDB::register_class<ImageTextureConverterCompat>();
ClassDB::register_class<OggStreamConverterCompat>();

ClassDB::register_class<ImportInfoModern>();
Expand Down

0 comments on commit 52302a8

Please sign in to comment.