Skip to content

Commit 5c049f5

Browse files
committed
Added performance mode settings so that Texture2D's can be downsized at runtime for performance reasons
2 parents 77bbe75 + acf17d2 commit 5c049f5

11 files changed

Lines changed: 59 additions & 62 deletions

File tree

core/io/image.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ Image::PerformanceMode Image::get_performance_mode() const {
783783

784784
void Image::set_performance_mode(PerformanceMode p_performance_mode) {
785785
switch (performance_mode) {
786-
case PERFORMANCE_MODE_ORIGINAL_RESOLUTION:
786+
case PERFORMANCE_MODE_ORIGINAL_SIZE:
787787
break;
788788

789789
default:
@@ -796,11 +796,11 @@ void Image::set_performance_mode(PerformanceMode p_performance_mode) {
796796
Vector2i desired_size = size;
797797

798798
switch (performance_mode) {
799-
case PERFORMANCE_MODE_ORIGINAL_RESOLUTION:
799+
case PERFORMANCE_MODE_ORIGINAL_SIZE:
800800
break;
801801

802-
case PERFORMANCE_MODE_MAX_RESOLUTION: {
803-
Vector2i max_size = GLOBAL_GET("rendering/textures/performance/max_resolution");
802+
case PERFORMANCE_MODE_MAX_SIZE: {
803+
Vector2i max_size = GLOBAL_GET("rendering/textures/performance/max_size");
804804

805805
if (desired_size.x > max_size.x) {
806806
desired_size.x = max_size.x;
@@ -813,7 +813,7 @@ void Image::set_performance_mode(PerformanceMode p_performance_mode) {
813813
break;
814814
}
815815

816-
case PERFORMANCE_MODE_DOWNSCALE_RESOLUTION: {
816+
case PERFORMANCE_MODE_DOWNSCALE_SIZE: {
817817
int factor = GLOBAL_GET("rendering/textures/performance/downscale_factor");
818818
factor = CLAMP(factor, 1, 14);
819819

@@ -1535,7 +1535,7 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
15351535
}
15361536

15371537
_copy_internals_from(dst);
1538-
performance_mode = PERFORMANCE_MODE_ORIGINAL_RESOLUTION;
1538+
performance_mode = PERFORMANCE_MODE_ORIGINAL_SIZE;
15391539
}
15401540

15411541
void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) {
@@ -2351,7 +2351,7 @@ void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Forma
23512351

23522352
mipmaps = p_use_mipmaps;
23532353

2354-
performance_mode = PERFORMANCE_MODE_ORIGINAL_RESOLUTION;
2354+
performance_mode = PERFORMANCE_MODE_ORIGINAL_SIZE;
23552355
}
23562356

23572357
void Image::initialize_data(const char **p_xpm) {
@@ -2361,7 +2361,7 @@ void Image::initialize_data(const char **p_xpm) {
23612361
mipmaps = false;
23622362
bool has_alpha = false;
23632363

2364-
performance_mode = PERFORMANCE_MODE_ORIGINAL_RESOLUTION;
2364+
performance_mode = PERFORMANCE_MODE_ORIGINAL_SIZE;
23652365

23662366
enum Status {
23672367
READING_HEADER,
@@ -3759,9 +3759,9 @@ void Image::_bind_methods() {
37593759
BIND_CONSTANT(MAX_WIDTH);
37603760
BIND_CONSTANT(MAX_HEIGHT);
37613761

3762-
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_ORIGINAL_RESOLUTION);
3763-
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_MAX_RESOLUTION);
3764-
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_DOWNSCALE_RESOLUTION);
3762+
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_ORIGINAL_SIZE);
3763+
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_MAX_SIZE);
3764+
BIND_ENUM_CONSTANT(PERFORMANCE_MODE_DOWNSCALE_SIZE);
37653765
BIND_ENUM_CONSTANT(FORMAT_L8);
37663766
BIND_ENUM_CONSTANT(FORMAT_LA8);
37673767
BIND_ENUM_CONSTANT(FORMAT_R8);

core/io/image.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class Image : public Resource {
7272
};
7373

7474
enum PerformanceMode {
75-
PERFORMANCE_MODE_ORIGINAL_RESOLUTION,
76-
PERFORMANCE_MODE_MAX_RESOLUTION,
77-
PERFORMANCE_MODE_DOWNSCALE_RESOLUTION
75+
PERFORMANCE_MODE_ORIGINAL_SIZE,
76+
PERFORMANCE_MODE_MAX_SIZE,
77+
PERFORMANCE_MODE_DOWNSCALE_SIZE
7878
};
7979

8080
enum Format : int32_t {
@@ -255,7 +255,7 @@ class Image : public Resource {
255255
static void _bind_methods();
256256

257257
private:
258-
PerformanceMode performance_mode = PERFORMANCE_MODE_ORIGINAL_RESOLUTION;
258+
PerformanceMode performance_mode = PERFORMANCE_MODE_ORIGINAL_SIZE;
259259
Format format = FORMAT_L8;
260260
Vector<uint8_t> data;
261261
int width = 0;

doc/classes/Image.xml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,35 @@
249249
Returns the number of mipmap levels or 0 if the image has no mipmaps. The largest main level image is not counted as a mipmap level by this method, so if you want to include it you can add 1 to this count.
250250
</description>
251251
</method>
252+
<method name="get_mipmap_offset" qualifiers="const">
253+
<return type="int" />
254+
<param index="0" name="mipmap" type="int" />
255+
<description>
256+
Returns the offset where the image's mipmap with index [param mipmap] is stored in the [member data] dictionary.
257+
</description>
258+
</method>
252259
<method name="get_original_height" qualifiers="const">
253260
<return type="int" />
254261
<description>
255-
Returns the original height of the [Image] after the performance mode has been set.
262+
Returns the original height of the [Image] after the [enum PerformanceMode] has been set.
256263
</description>
257264
</method>
258265
<method name="get_original_size" qualifiers="const">
259266
<return type="Vector2i" />
260267
<description>
261-
Returns the original size of the [Image] after the performance mode has been set.
268+
Returns the original size of the [Image] after the [enum PerformanceMode] has been set.
262269
</description>
263270
</method>
264271
<method name="get_original_width" qualifiers="const">
265272
<return type="int" />
266273
<description>
267-
Returns the original width of the [Image] after the performance mode has been set.
274+
Returns the original width of the [Image] after the [enum PerformanceMode] has been set.
268275
</description>
269276
</method>
270277
<method name="get_performance_mode" qualifiers="const">
271-
<return type="PerformanceMode" />
278+
<return type="int" enum="Image.PerformanceMode" />
272279
<description>
273-
Returns the image's current [PerformanceMode]
274-
</description>
275-
</method>
276-
<method name="get_mipmap_offset" qualifiers="const">
277-
<return type="int" />
278-
<param index="0" name="mipmap" type="int" />
279-
<description>
280-
Returns the offset where the image's mipmap with index [param mipmap] is stored in the [member data] dictionary.
280+
Returns the image's current [enum PerformanceMode]
281281
</description>
282282
</method>
283283
<method name="get_pixel" qualifiers="const">
@@ -584,7 +584,7 @@
584584
</method>
585585
<method name="set_performance_mode">
586586
<return type="void" />
587-
<param index="0" name="performance_mode" type="PerformanceMode" />
587+
<param index="0" name="performance_mode" type="int" enum="Image.PerformanceMode" />
588588
<description>
589589
Sets the performance mode of the [Image]. This will change the size of the [Image] based on settings in [ProjectSettings].
590590
[method get_size] will return this new size. The original size is always available using [method get_original_size].
@@ -655,7 +655,7 @@
655655
</method>
656656
</methods>
657657
<members>
658-
<member name="data" type="Dictionary" setter="_set_data" getter="_get_data" default="{ &quot;data&quot;: PackedByteArray(), &quot;format&quot;: &quot;Lum8&quot;, &quot;height&quot;: 0, &quot;mipmaps&quot;: false, &quot;width&quot;: 0 }">
658+
<member name="data" type="Dictionary" setter="_set_data" getter="_get_data" default="{ &quot;data&quot;: PackedByteArray(), &quot;format&quot;: &quot;Lum8&quot;, &quot;height&quot;: 0, &quot;mipmaps&quot;: false, &quot;original_height&quot;: 0, &quot;original_width&quot;: 0, &quot;width&quot;: 0 }">
659659
Holds all the image's color data in a given format. See [enum Format] constants.
660660
</member>
661661
</members>
@@ -666,14 +666,14 @@
666666
<constant name="MAX_HEIGHT" value="16777216">
667667
The maximal height allowed for [Image] resources.
668668
</constant>
669-
<constant name="PERFORMANCE_MODE_ORIGINAL_RESOLUTION" value="0" enum="PerformanceMode">
670-
Keeps the original width and height of the [Image].
669+
<constant name="PERFORMANCE_MODE_ORIGINAL_SIZE" value="0" enum="PerformanceMode">
670+
Keeps the original size of the [Image].
671671
</constant>
672-
<constant name="PERFORMANCE_MODE_MAX_RESOLUTION" value="1" enum="PerformanceMode">
673-
Resizes if needed the width and height of the [Image] to fit into [member ProjectSettings.rendering/textures/performance/max_resolution]'s.
672+
<constant name="PERFORMANCE_MODE_MAX_SIZE" value="1" enum="PerformanceMode">
673+
Ensures that the size of the [Image] is smaller then or equals to the size specified at [member ProjectSettings.rendering/textures/performance/max_size].
674674
</constant>
675-
<constant name="PERFORMANCE_MODE_DOWNSCALE_RESOLUTION" value="2" enum="PerformanceMode">
676-
Downscales the width and height of the [Image] [member ProjectSettings.rendering/textures/performance/downscale_factor] times.
675+
<constant name="PERFORMANCE_MODE_DOWNSCALE_SIZE" value="2" enum="PerformanceMode">
676+
Reduces the size of [Image] logarithmically according to the equation [code]size = max(floor(original_size / pow(2, downscale_factor)), Vector2i.ONE)[/code], getting downscale_factor from [member ProjectSettings.rendering/textures/performance/downscale_factor].
677677
</constant>
678678
<constant name="FORMAT_L8" value="0" enum="Format">
679679
Texture format with a single 8-bit depth representing luminance.

doc/classes/ProjectSettings.xml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,15 +3351,13 @@
33513351
If [code]true[/code], the texture importer will import lossless textures using the PNG format. Otherwise, it will default to using WebP.
33523352
</member>
33533353
<member name="rendering/textures/performance/default_mode" type="int" setter="" getter="" default="0">
3354-
The default [member Image.PerformanceMode] of imported [Texture2D]'s. In the [b]Original Resolution[/b], the [Texture2D] resolution is unaffected.
3355-
[b]Max Resolution[/b] will ensure the [Texture2D]'s width and height are below [member rendering/textures/performance/max_resolution]. The equation is [code]size = min(max_resolution, original_size)[/code].
3356-
[b]Downscale Resolution[/b] will half the [Texture2D]'s width and height [member rendering/textures/performance/downscale_factor] times. The equation is [code]code = floor(original_size / pow(2, downscale_factor))[/code].
3354+
Changes the [enum Image.PerformanceMode] used by [Texture2D] when loaded into a scene. [b]Original Size[/b] keeps the [Texture2D] original size. [b]Max Size[/b] ensures that the size of [Texture2D] is smaller then the size at [member rendering/textures/performance/max_size]. [b]Downscale Factor[/b] reduces the size logarithmically according to the equation [code]size = max(floor(original_size / pow(2, downscale_factor)), Vector2i.ONE)[/code], getting downscale_factor from [member rendering/textures/performance/downscale_factor].
33573355
</member>
33583356
<member name="rendering/textures/performance/downscale_factor" type="int" setter="" getter="" default="1">
3359-
If the performance mode is set to [code]Downscale Factor[/code], then this will be used to determin how many times each [Texture2D]'s width and height get's halved.
3357+
Determines how much a [Texture2D][Image] size will be reduces when the [enum Image.PerformanceMode] is set to [constant Image.PERFORMANCE_MODE_DOWNSCALE_SIZE].
33603358
</member>
3361-
<member name="rendering/textures/performance/max_resolution" type="Vector2i" setter="" getter="" default="Vector2i(2048, 2048)">
3362-
If the performance mode is set to [code]Max Resolution[/code], then this will be used to determin how big each [Texture2D]'s width and height can be.
3359+
<member name="rendering/textures/performance/max_size" type="Vector2i" setter="" getter="" default="Vector2i(2048, 2048)">
3360+
The maximum size a [Texture2D]/[Image] can be when the [enum Image.PerformanceMode] is set to [constant Image.PERFORMANCE_MODE_MAX_SIZE].
33633361
</member>
33643362
<member name="rendering/textures/vram_compression/cache_gpu_compressor" type="bool" setter="" getter="" default="true">
33653363
If [code]true[/code], the GPU texture compressor will cache the local RenderingDevice and its resources (shaders and pipelines), allowing for faster subsequent imports at a memory cost.

doc/classes/ResourceImporterTexture.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@
7373
<member name="mipmaps/limit" type="int" setter="" getter="" default="-1">
7474
Unimplemented. This currently has no effect when changed.
7575
</member>
76-
<member name="performance/force_original_resolution" type="bool" setter="" getter="" default="false">
77-
If [code]true[/code], then the imported image will always be at full resolution and will not be scaled down according to the active [member Image.PerformanceMode].
78-
[b]Node:[/b] This does nothing if the [member Image.PerformanceMode] is set to [code]Original Resolution[/code] in the [ProjectSettings].
76+
<member name="performance/force_original_size" type="bool" setter="" getter="" default="false">
77+
If [code]true[/code], then the engine will ignore [member ProjectSettings.rendering/textures/performance/default_mode] and will always load the image at full size.
7978
</member>
8079
<member name="process/channel_remap/alpha" type="int" setter="" getter="" default="3">
8180
Specifies the data source of the output image's alpha channel.

editor/import/resource_importer_texture.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ String ResourceImporterTexture::get_preset_name(int p_idx) const {
231231
}
232232

233233
void ResourceImporterTexture::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
234-
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "performance/force_original_resolution"), false));
234+
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "performance/force_original_size"), false));
235235

236236
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));
237237
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress/high_quality"), false));
@@ -353,7 +353,7 @@ void ResourceImporterTexture::save_to_ctex_format(Ref<FileAccess> f, const Ref<I
353353
}
354354
}
355355

356-
void ResourceImporterTexture::_save_ctex(const Ref<Image> &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_roughness, bool p_detect_normal, bool p_force_normal, bool p_force_original_resolution, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap, const Ref<Image> &p_normal, Image::RoughnessChannel p_roughness_channel) {
356+
void ResourceImporterTexture::_save_ctex(const Ref<Image> &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_roughness, bool p_detect_normal, bool p_force_normal, bool p_force_original_size, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap, const Ref<Image> &p_normal, Image::RoughnessChannel p_roughness_channel) {
357357
Ref<FileAccess> f = FileAccess::open(p_to_path, FileAccess::WRITE);
358358
ERR_FAIL_COND(f.is_null());
359359

@@ -386,8 +386,8 @@ void ResourceImporterTexture::_save_ctex(const Ref<Image> &p_image, const String
386386
if (p_detect_normal) {
387387
flags |= CompressedTexture2D::FORMAT_BIT_DETECT_NORMAL;
388388
}
389-
if (p_force_original_resolution) {
390-
flags |= CompressedTexture2D::FORMAT_BIT_FORCE_ORIGINAL_RESOLUTION;
389+
if (p_force_original_size) {
390+
flags |= CompressedTexture2D::FORMAT_BIT_FORCE_ORIGINAL_SIZE;
391391
}
392392

393393
f->store_32(flags);
@@ -707,7 +707,7 @@ Error ResourceImporterTexture::import(ResourceUID::ID p_source_id, const String
707707
// Parse import options.
708708
int32_t loader_flags = ImageFormatLoader::FLAG_NONE;
709709

710-
const bool force_original_resolution = p_options["performance/force_original_resolution"];
710+
const bool force_original_size = p_options["performance/force_original_size"];
711711

712712
// Compression.
713713
CompressMode compress_mode = CompressMode(int(p_options["compress/mode"]));
@@ -936,7 +936,7 @@ Error ResourceImporterTexture::import(ResourceUID::ID p_source_id, const String
936936

937937
if (force_uncompressed) {
938938
_save_ctex(image, p_save_path + ".ctex", COMPRESS_VRAM_UNCOMPRESSED, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
939-
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_resolution, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
939+
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_size, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
940940
} else {
941941
if (can_s3tc_bptc) {
942942
Image::CompressMode image_compress_mode;
@@ -950,7 +950,7 @@ Error ResourceImporterTexture::import(ResourceUID::ID p_source_id, const String
950950
}
951951

952952
_save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps,
953-
stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_resolution, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
953+
stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_size, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
954954
r_platform_variants->push_back(image_compress_format);
955955
}
956956

@@ -966,19 +966,19 @@ Error ResourceImporterTexture::import(ResourceUID::ID p_source_id, const String
966966
}
967967

968968
_save_ctex(image, p_save_path + "." + image_compress_format + ".ctex", compress_mode, lossy, basisu_params, image_compress_mode, mipmaps, stream, detect_3d,
969-
detect_roughness, detect_normal, force_normal, force_original_resolution, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
969+
detect_roughness, detect_normal, force_normal, force_original_size, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
970970
r_platform_variants->push_back(image_compress_format);
971971
}
972972
}
973973
} else {
974974
// Import normally.
975975
_save_ctex(image, p_save_path + ".ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
976-
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_resolution, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
976+
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_size, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
977977
}
978978

979979
if (editor_image.is_valid()) {
980980
_save_ctex(editor_image, p_save_path + ".editor.ctex", compress_mode, lossy, basisu_params, Image::COMPRESS_S3TC /* This is ignored. */,
981-
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_resolution, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
981+
mipmaps, stream, detect_3d, detect_roughness, detect_normal, force_normal, force_original_size, srgb_friendly_pack, false, mipmap_limit, normal_image, roughness_channel);
982982

983983
// Generate and save editor-specific metadata, which we cannot save to the .import file.
984984
Dictionary editor_meta;

editor/import/resource_importer_texture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class ResourceImporterTexture : public ResourceImporter {
8686
static ResourceImporterTexture *singleton;
8787
static const char *compression_formats[];
8888

89-
void _save_ctex(const Ref<Image> &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_detect_normal, bool p_force_normal, bool p_force_original_resolution, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap, const Ref<Image> &p_normal, Image::RoughnessChannel p_roughness_channel);
89+
void _save_ctex(const Ref<Image> &p_image, const String &p_to_path, CompressMode p_compress_mode, float p_lossy_quality, const Image::BasisUniversalPackerParams &p_basisu_params, Image::CompressMode p_vram_compression, bool p_mipmaps, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_detect_normal, bool p_force_normal, bool p_force_original_size, bool p_srgb_friendly, bool p_force_po2_for_compressed, uint32_t p_limit_mipmap, const Ref<Image> &p_normal, Image::RoughnessChannel p_roughness_channel);
9090
void _save_editor_meta(const Dictionary &p_metadata, const String &p_to_path);
9191
Dictionary _load_editor_meta(const String &p_to_path) const;
9292

editor/project_manager/project_dialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ void ProjectDialog::ok_pressed() {
568568
} else {
569569
fa_import->store_line("[params]");
570570
fa_import->store_line("");
571-
fa_import->store_line("performance/force_original_resolution=true");
571+
fa_import->store_line("performance/force_original_size=true");
572572
fa_import->close();
573573
}
574574

0 commit comments

Comments
 (0)