From 8f6a6ac8d0a47b07f5bb283d4965bcd208a5b05c Mon Sep 17 00:00:00 2001 From: Theogen Ratkin Date: Thu, 17 Dec 2020 11:46:34 -0400 Subject: [PATCH] Add interpolation parameter to resize_to_po2() Image::resize_to_po2() now takes an optional p_interpolation parameter that it passes directly to resize() with default value INTERPOLATE_BILINEAR. GLES2: call resize_to_po2() with interpolate argument Call resize_to_po2() in GLES2 rasterizer storage with either INTERPOLATE_BILINEAR or INTERPOLATE_NEAREST depending on TEXTURE_FLAG_FILTER. This avoids filtering issues with non power of two pixel art textures. See #44379 --- core/image.cpp | 6 +++--- core/image.h | 2 +- doc/classes/Image.xml | 6 ++++-- drivers/gles2/rasterizer_storage_gles2.cpp | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/image.cpp b/core/image.cpp index 674c3e99767..d47de1b3e8c 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -867,7 +867,7 @@ bool Image::is_size_po2() const { return uint32_t(width) == next_power_of_2(width) && uint32_t(height) == next_power_of_2(height); } -void Image::resize_to_po2(bool p_square) { +void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) { ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); @@ -883,7 +883,7 @@ void Image::resize_to_po2(bool p_square) { return; //nothing to do } - resize(w, h); + resize(w, h, p_interpolation); } void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { @@ -2725,7 +2725,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset); - ClassDB::bind_method(D_METHOD("resize_to_po2", "square"), &Image::resize_to_po2, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR)); ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR)); ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2); ClassDB::bind_method(D_METHOD("expand_x2_hq2x"), &Image::expand_x2_hq2x); diff --git a/core/image.h b/core/image.h index 2fff89bc6e2..327efc4c62b 100644 --- a/core/image.h +++ b/core/image.h @@ -225,7 +225,7 @@ public: /** * Resize the image, using the preferred interpolation method. */ - void resize_to_po2(bool p_square = false); + void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR); void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR); void shrink_x2(); void expand_x2_hq2x(); diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 7b32e6eb9b6..9340b3f94cb 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -414,7 +414,7 @@ - Resizes the image to the given [code]width[/code] and [code]height[/code]. New pixels are calculated using [code]interpolation[/code]. See [code]interpolation[/code] constants. + Resizes the image to the given [code]width[/code] and [code]height[/code]. New pixels are calculated using the [code]interpolation[/code] mode defined via [enum Interpolation] constants. @@ -422,8 +422,10 @@ + + - Resizes the image to the nearest power of 2 for the width and height. If [code]square[/code] is [code]true[/code] then set width and height to be the same. + Resizes the image to the nearest power of 2 for the width and height. If [code]square[/code] is [code]true[/code] then set width and height to be the same. New pixels are calculated using the [code]interpolation[/code] mode defined via [enum Interpolation] constants. diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp index 322f63488cb..86e9db66a04 100644 --- a/drivers/gles2/rasterizer_storage_gles2.cpp +++ b/drivers/gles2/rasterizer_storage_gles2.cpp @@ -682,7 +682,7 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref &p if (img == p_image) { img = img->duplicate(); } - img->resize_to_po2(false); + img->resize_to_po2(false, texture->flags & VS::TEXTURE_FLAG_FILTER ? Image::INTERPOLATE_BILINEAR : Image::INTERPOLATE_NEAREST); } if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {