Skip to content

Commit

Permalink
Add dithering (#9014)
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog committed Dec 19, 2023
1 parent 5d3e830 commit b1aec1b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -580,6 +580,17 @@ enable_auto_exposure (Enable Automatic Exposure) bool false
# Requires: shaders, enable_auto_exposure
exposure_compensation (Exposure compensation) float 0.0 -1.0 1.0

# Apply dithering to reduce color banding artifacts.
# Dithering significantly increases the size of losslessly-compressed
# screenshots and it works incorrectly if the display or operating system
# performs additional dithering or if the color channels are not quantized
# to 8 bits.
# With OpenGL ES, dithering only works if the shader supports high
# floating-point precision and it may have a higher performance impact.
#
# Requires: shaders
debanding (Enable Debanding) bool true

[**Bloom]

# Set to true to enable bloom effect.
Expand Down
26 changes: 26 additions & 0 deletions client/shaders/second_stage/opengl_fragment.glsl
@@ -1,6 +1,13 @@
#define rendered texture0
#define bloom texture1

#ifdef GL_ES
// Dithering requires sufficient floating-point precision
#ifndef GL_FRAGMENT_PRECISION_HIGH
#undef ENABLE_DITHERING
#endif
#endif

struct ExposureParams {
float compensationFactor;
};
Expand Down Expand Up @@ -79,6 +86,20 @@ vec3 applySaturation(vec3 color, float factor)
}
#endif

#ifdef ENABLE_DITHERING
// From http://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
// and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
// NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
vec3 screen_space_dither(highp vec2 frag_coord) {
// Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
highp vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));

// Subtract 0.5 to avoid slightly brightening the whole viewport.
return (dither.rgb - 0.5) / 255.0;
}
#endif

void main(void)
{
vec2 uv = varTexCoord.st;
Expand Down Expand Up @@ -125,5 +146,10 @@ void main(void)
// return to sRGB colorspace (approximate)
color.rgb = pow(color.rgb, vec3(1.0 / 2.2));

#ifdef ENABLE_DITHERING
// Apply dithering just before quantisation
color.rgb += screen_space_dither(gl_FragCoord.xy);
#endif

gl_FragColor = vec4(color.rgb, 1.0); // force full alpha to avoid holes in the image.
}
3 changes: 3 additions & 0 deletions src/client/shader.cpp
Expand Up @@ -767,6 +767,9 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
shaders_header << "#define SSAA_SCALE " << ssaa_scale << ".\n";
}

if (g_settings->getBool("debanding"))
shaders_header << "#define ENABLE_DITHERING 1\n";

shaders_header << "#line 0\n"; // reset the line counter for meaningful diagnostics

std::string common_header = shaders_header.str();
Expand Down
2 changes: 2 additions & 0 deletions src/defaultsettings.cpp
Expand Up @@ -264,6 +264,7 @@ void set_default_settings()
settings->setDefault("enable_waving_plants", "false");
settings->setDefault("exposure_compensation", "0.0");
settings->setDefault("enable_auto_exposure", "false");
settings->setDefault("debanding", "true");
settings->setDefault("antialiasing", "none");
settings->setDefault("enable_bloom", "false");
settings->setDefault("enable_bloom_debug", "false");
Expand Down Expand Up @@ -499,6 +500,7 @@ void set_default_settings()
settings->setDefault("active_block_range", "2");
settings->setDefault("viewing_range", "50");
settings->setDefault("leaves_style", "simple");
settings->setDefault("debanding", "false");
settings->setDefault("curl_verify_cert", "false");

// Apply settings according to screen size
Expand Down

0 comments on commit b1aec1b

Please sign in to comment.