Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@ antialiasing (Antialiasing method) enum none none,fsaa,ssaa
# Value of 2 means taking 2x2 = 4 samples.
fsaa (Anti-aliasing scale) enum 2 2,4,8,16

# Anti-alias texel edges by using bilinear texture sampling together with a
# sample coordinate transformation in the shader.
# This setting is ONLY applied if bilinear_filter or trilinear_filter is enabled.
texel_antialiasing (Texel anti-aliasing) bool true

# Applies a post-processing filter to detect and smoothen high-contrast edges.
# Provides balance between speed and image quality.
# fxaa can used in combination with the other anti-aliasing methods
Expand Down Expand Up @@ -2067,9 +2072,12 @@ array_texture_max (Array texture max layers) int 65535 0 65535
# can be blurred, so this option automatically upscales them to preserve
# crisp pixels. This defines the minimum texture size for the upscaled textures;
# higher values look sharper, but require more memory.
# This setting is ONLY applied if any of the mentioned filters are enabled.
# This setting is ONLY applied if any of the mentioned filters are enabled and
# texel_antialiasing is disabled.
# This is also used as the base node texture size for world-aligned
# texture autoscaling.
#
# Requires: !texel_antialiasing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so won't this visibly change behavior for people who use bi/tri?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we shouldn't enable it by default

texture_min_size (Base texture size) int 192 192 16384

# Side length of a cube of map blocks that the client will consider together
Expand Down
32 changes: 31 additions & 1 deletion client/shaders/nodes_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#else
uniform sampler2D baseTexture;
#endif
#ifdef TEXEL_ANTIALIASING
uniform vec2 texelSize0;
#endif
#define crackTexture texture1
uniform sampler2D crackTexture;

Expand Down Expand Up @@ -432,14 +435,41 @@ vec2 uv_repeat(vec2 v)
return v;
}

#ifdef TEXEL_ANTIALIASING

// Move the uv coordinates within the texel such that when sampling the texture
// with bilinear filtering, the result looks like nearest neighbour sampling
// with anti-aliased texels.
// Based on t3ssel8r's code from https://www.patreon.com/posts/83276362.
vec2 uv_texel_antialias()
{
vec2 box_size = clamp(0.7 * fwidth(varTexCoord.st) / texelSize0, 1e-5, 1.0);
vec2 tx = varTexCoord.st / texelSize0 - 0.5 * box_size;
vec2 tx_off = clamp((fract(tx) - (1.0 - box_size)) / box_size, 0.0, 1.0);
return (floor(tx) + 0.5 + tx_off) * texelSize0;
}

#endif

void main(void)
{
vec2 uv = varTexCoord.st;
#ifdef TEXEL_ANTIALIASING
vec2 uv = uv_texel_antialias();
#ifdef USE_ARRAY_TEXTURE
vec4 base = textureGrad(baseTexture, vec3(uv, varTexLayer),
dFdx(varTexCoord.st), dFdy(varTexCoord.st)).rgba;
#else
// For the deprecated texture2D there is no texture2DGrad
vec4 base = texture2D(baseTexture, uv).rgba;
#endif

#else
vec2 uv = varTexCoord.st;
#ifdef USE_ARRAY_TEXTURE
vec4 base = texture(baseTexture, vec3(uv, varTexLayer)).rgba;
#else
vec4 base = texture2D(baseTexture, uv).rgba;
#endif
#endif

// Handle transparency by discarding pixel as appropriate.
Expand Down
26 changes: 25 additions & 1 deletion client/shaders/object_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#else
uniform sampler2D baseTexture;
#endif
#ifdef TEXEL_ANTIALIASING
uniform vec2 texelSize0;
#endif

uniform vec3 dayLight;
uniform lowp vec4 fogColor;
Expand Down Expand Up @@ -362,15 +365,36 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
#endif
#endif

#ifdef TEXEL_ANTIALIASING
// Copied from the nodes fragment shader
vec2 uv_texel_antialias()
{
vec2 box_size = clamp(0.7 * fwidth(varTexCoord.st) / texelSize0, 1e-5, 1.0);
vec2 tx = varTexCoord.st / texelSize0 - 0.5 * box_size;
vec2 tx_off = clamp((fract(tx) - (1.0 - box_size)) / box_size, 0.0, 1.0);
return (floor(tx) + 0.5 + tx_off) * texelSize0;
}
#endif


void main(void)
{
vec2 uv = varTexCoord.st;
#ifdef TEXEL_ANTIALIASING
vec2 uv = uv_texel_antialias();
#ifdef USE_ARRAY_TEXTURE
vec4 base = textureGrad(baseTexture, vec3(uv, varTexLayer),
dFdx(varTexCoord.st), dFdy(varTexCoord.st)).rgba;
#else
vec4 base = texture2D(baseTexture, uv).rgba;
#endif

#else
vec2 uv = varTexCoord.st;
#ifdef USE_ARRAY_TEXTURE
vec4 base = texture(baseTexture, vec3(uv, varTexLayer)).rgba;
#else
vec4 base = texture2D(baseTexture, uv).rgba;
#endif
#endif

// Handle transparency by discarding pixel as appropriate.
Expand Down
5 changes: 5 additions & 0 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ class NodeShaderConstantSetter : public IShaderConstantSetter

constants["ENABLE_WAVING_LEAVES"] = g_settings->getBool("enable_waving_leaves") ? 1 : 0;
constants["ENABLE_WAVING_PLANTS"] = g_settings->getBool("enable_waving_plants") ? 1 : 0;

if (g_settings->getBool("texel_antialiasing")
&& (g_settings->getBool("trilinear_filter")
|| g_settings->getBool("bilinear_filter")))
constants["TEXEL_ANTIALIASING"] = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this even shows up in object_shader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me when I test it, so I assume that it works in general.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I somehow forgot testing with objects.

It appears to work with dropped items, but not for mtg boat or mtg cart.

}
};

Expand Down
7 changes: 4 additions & 3 deletions src/client/imagesource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,8 +1499,8 @@ bool ImageSource::generateImagePart(std::string_view part_of_name,
* see <https://github.com/luanti-org/luanti/issues/15604> and related
* linked discussions.
*/
const bool filter = m_setting_trilinear_filter || m_setting_bilinear_filter;
if (filter) {
if (!m_setting_texel_antialiasing &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the comment to explain how texel AA makes this redundant.

(m_setting_trilinear_filter || m_setting_bilinear_filter)) {
const f32 scaleto = rangelim(g_settings->getU16("texture_min_size"),
TEXTURE_FILTER_MIN_SIZE, 16384);

Expand Down Expand Up @@ -1802,7 +1802,8 @@ ImageSource::ImageSource() :
m_setting_mipmap{g_settings->getBool("mip_map")},
m_setting_trilinear_filter{g_settings->getBool("trilinear_filter")},
m_setting_bilinear_filter{g_settings->getBool("bilinear_filter")},
m_setting_anisotropic_filter{g_settings->getBool("anisotropic_filter")}
m_setting_anisotropic_filter{g_settings->getBool("anisotropic_filter")},
m_setting_texel_antialiasing{g_settings->getBool("texel_antialiasing")}
{}

video::IImage* ImageSource::generateImage(std::string_view name,
Expand Down
1 change: 1 addition & 0 deletions src/client/imagesource.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct ImageSource {
bool m_setting_trilinear_filter;
bool m_setting_bilinear_filter;
bool m_setting_anisotropic_filter;
bool m_setting_texel_antialiasing;

// Cache of source images
SourceImageCache m_sourcecache;
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void set_default_settings()
settings->setDefault("opengl_debug", "true");
#endif
settings->setDefault("fsaa", "2");
settings->setDefault("texel_antialiasing", "true");
settings->setDefault("undersampling", "1");
settings->setDefault("world_aligned_mode", "enable");
settings->setDefault("autoscale_mode", "disable");
Expand Down
Loading