mojira.dev
MC-310051

Floating point cancellation artifacts from `bits.fsh`

This issue dates back to 13w38a.
The artifacts described here first appeared in version 1.7.3.

The bug

In the current version of bits.fsh and all previous iterations, fract and subtraction have always been used to cancel out the decimals.

vec2 oneTexel = 1.0 / InSize;
vec2 mosaicInSize = InSize / MosaicSize;
vec2 fractPix = fract(texCoord * mosaicInSize) / mosaicInSize;
 
vec4 baseTexel = texture(InSampler, texCoord - fractPix);

But instead of fully cancelling out, it produces artifacts due to floating point inprecision.

Steps to reproduce

  1. /posteffect add @s minecraft:creeper

  2. Look closely at some of the scaled pixels

Observed result

Thin lines in a grid-like pattern, thinner than the desired mosaic size.

Expected result

No line artifacts, but instead perfect nearest neighbor scaling.

Additional information

Instead of using fract and subtraction to cancel out the decimals, one could instead use floor to achieve the same result:

vec2 oneTexel = 1.0 / InSize;
vec2 mosaicInSize = InSize / MosaicSize;
vec2 mosaicCoord = floor(texCoord * mosaicInSize) / mosaicInSize;
 
vec4 baseTexel = texture(InSampler, mosaicCoord);

Though I didn't look into it in detail, the next following lines may also have an issue with its use of fract:

vec3 fractTexel = baseTexel.rgb - fract(baseTexel.rgb * Resolution) / Resolution
float luma = dot(fractTexel, vec3(0.3, 0.59, 0.11))
vec3 chroma = (fractTexel - luma) * Saturation
baseTexel.rgb = luma + chroma
baseTexel.a = 1.0;

Note that the last two images were made using the bits shader with a MosaicSize of 48, to make the bug more noticeable.

Attachments

Comments 1

When filling out my report it uploaded it pre-maturly without confirmation. Here are the images referenced in the report:

26.3-Snapshot-3-fixed-bits-Creeper-Effect.png
26.3-Snapshot-3-Creeper-Effect.png
26.3-Snapshot-3-fixed-bits-Custom-48x-scale.png
26.3-Snapshot-3-Custom-48x-scale.png

Really Super Otter

Adrien Givry

Confirmed

Platform G

Low

Rendering

26.3 Snapshot 3

26.4 Snapshot 1

Retrieved