mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-18 23:26:03 +01:00
fix(render): antialias alpha tested edges with multisampling (eu07/maszyna#449)
The sides of the trackbed (and grass, fences, foliage) are cut out of their textures by the alpha test, which keeps or drops whole pixels, so their edges stayed jagged at any multisampling level; only geometry edges got smoothed. With multisampling on, the alpha tested materials of the opaque colour pass now draw with GL_SAMPLE_ALPHA_TO_COVERAGE. Their shaders do the alpha test with alpha_test_fail() of glsl_common and write as alpha the coverage of the test edge: the alpha sharpened around the opacity threshold to go from 0 to 1 over about a pixel, so the edge keeps the place it had and gets the samples it covers. The renderer turns alpha to coverage on per material, only for shaders which use the helper and only in the opaque pass with a positive opacity, so translucent, water, terrain and the other shaders are unchanged. Without multisampling the shaders work as before.
This commit is contained in:
@@ -11,6 +11,7 @@ void gl::glsl_common_setup()
|
||||
"#define MOTIONBLUR_ENABLED " + std::to_string((int)Global.gfx_postfx_motionblur_enabled) + "\n" +
|
||||
"#define POSTFX_ENABLED " + std::to_string((int)!Global.gfx_skippipeline) + "\n" +
|
||||
"#define EXTRAEFFECTS_ENABLED " + std::to_string((int)Global.gfx_extraeffects) + "\n" +
|
||||
"#define ALPHA_TO_COVERAGE_ENABLED " + std::to_string((int)(Global.iMultisampling > 0)) + "\n" +
|
||||
"#define MAX_LIGHTS " + std::to_string(MAX_LIGHTS) + "U\n" +
|
||||
"#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" +
|
||||
"#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" +
|
||||
@@ -110,5 +111,34 @@ void gl::glsl_common_setup()
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef STAGE_FRAGMENT
|
||||
// alpha tested surfaces (positive opacity, the opaque pass) get the coverage of their alpha
|
||||
// test edge from GL_SAMPLE_ALPHA_TO_COVERAGE, which the renderer turns on for them with
|
||||
// multisampling; otherwise the alpha test keeps or drops whole pixels and its edges stay
|
||||
// jagged whatever the multisampling. the alpha is sharpened to go from 0 to 1 over about
|
||||
// a pixel around the test threshold, so the edge keeps the place and sharpness the alpha
|
||||
// test gave it. computed before any discard, which leaves the derivatives undefined
|
||||
float alpha_coverage(float alpha)
|
||||
{
|
||||
#if ALPHA_TO_COVERAGE_ENABLED
|
||||
if (opacity > 0.0)
|
||||
return clamp((alpha - opacity) / max(fwidth(alpha), 0.0001) + 0.5, 0.0, 1.0);
|
||||
#endif
|
||||
return alpha;
|
||||
}
|
||||
|
||||
// true for the fragments the alpha test drops: below the opacity threshold in the opaque
|
||||
// pass, at or above it (negative opacity) in the translucent pass. with alpha to coverage
|
||||
// only the fragments which cover no sample at all
|
||||
bool alpha_test_fail(float alpha, float coverage)
|
||||
{
|
||||
#if ALPHA_TO_COVERAGE_ENABLED
|
||||
if (opacity > 0.0)
|
||||
return coverage <= 0.0;
|
||||
#endif
|
||||
return (opacity >= 0.0 ? (alpha < opacity) : (alpha >= -opacity));
|
||||
}
|
||||
#endif
|
||||
|
||||
)STRING";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user