mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 13:15:02 +01:00
fix(render): alpha to coverage only where it is on, full coverage on flat alpha
Review of the alpha to coverage change for alpha tested surfaces: - An alpha which is flat over the pixel (fwidth 0) and equal to the opacity threshold got a coverage of 0.5, so e.g. opacity 1 on an opaque or DXT1 texture turned half see-through with multisampling. Without an alpha edge in the pixel the plain test decides now: step(opacity, alpha). - The shaders took the coverage path whenever they were built with multisampling, also where alpha to coverage is off (the reflections pass) or does nothing (the single-sampled model previews), and grew the cut-outs there by about half a pixel. model_ubo gets an alpha_to_coverage flag, set together with the GL state, and the shaders keep the plain alpha test without it. The previews no longer turn alpha to coverage on. The flag takes 4 of the 12 padding bytes after shadow_tone, so the std140 layout and the size of the block stay the same. - The shader validator never set iMultisampling, so it never compiled the alpha to coverage branches. Its "features on" configuration uses the default of 2 now, on desktop GL and GLES.
This commit is contained in:
@@ -46,6 +46,8 @@ bool check_shader_allopts(std::string filename)
|
|||||||
Global.gfx_skippipeline = false;
|
Global.gfx_skippipeline = false;
|
||||||
Global.gfx_extraeffects = true;
|
Global.gfx_extraeffects = true;
|
||||||
Global.gfx_shadergamma = true;
|
Global.gfx_shadergamma = true;
|
||||||
|
// the default of the simulator, which compiles the alpha to coverage branches of the shaders
|
||||||
|
Global.iMultisampling = 2;
|
||||||
if (!check_shader(filename))
|
if (!check_shader(filename))
|
||||||
suc = false;
|
suc = false;
|
||||||
|
|
||||||
@@ -56,6 +58,7 @@ bool check_shader_allopts(std::string filename)
|
|||||||
Global.gfx_skippipeline = true;
|
Global.gfx_skippipeline = true;
|
||||||
Global.gfx_extraeffects = false;
|
Global.gfx_extraeffects = false;
|
||||||
Global.gfx_shadergamma = false;
|
Global.gfx_shadergamma = false;
|
||||||
|
Global.iMultisampling = 0;
|
||||||
if (!check_shader(filename))
|
if (!check_shader(filename))
|
||||||
suc = false;
|
suc = false;
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ void gl::glsl_common_setup()
|
|||||||
float fog_density;
|
float fog_density;
|
||||||
float alpha_mult;
|
float alpha_mult;
|
||||||
float shadow_tone;
|
float shadow_tone;
|
||||||
|
float alpha_to_coverage;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout (std140) uniform scene_ubo
|
layout (std140) uniform scene_ubo
|
||||||
@@ -117,12 +118,19 @@ void gl::glsl_common_setup()
|
|||||||
// multisampling; otherwise the alpha test keeps or drops whole pixels and its edges stay
|
// 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
|
// 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
|
// 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
|
// test gave it. computed before any discard, which leaves the derivatives undefined.
|
||||||
|
// alpha_to_coverage of model_ubo tells whether alpha to coverage is on for this draw; where
|
||||||
|
// it isn't (reflections, previews) the shaders keep the plain alpha test
|
||||||
float alpha_coverage(float alpha)
|
float alpha_coverage(float alpha)
|
||||||
{
|
{
|
||||||
#if ALPHA_TO_COVERAGE_ENABLED
|
#if ALPHA_TO_COVERAGE_ENABLED
|
||||||
if (opacity > 0.0)
|
if (opacity > 0.0 && alpha_to_coverage > 0.0)
|
||||||
return clamp((alpha - opacity) / max(fwidth(alpha), 0.0001) + 0.5, 0.0, 1.0);
|
{
|
||||||
|
// a flat alpha has no edge in the pixel and gets the plain test, so an alpha equal
|
||||||
|
// to the threshold stays fully covered
|
||||||
|
float fw = fwidth(alpha);
|
||||||
|
return (fw > 0.0 ? clamp((alpha - opacity) / fw + 0.5, 0.0, 1.0) : step(opacity, alpha));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
@@ -133,7 +141,7 @@ void gl::glsl_common_setup()
|
|||||||
bool alpha_test_fail(float alpha, float coverage)
|
bool alpha_test_fail(float alpha, float coverage)
|
||||||
{
|
{
|
||||||
#if ALPHA_TO_COVERAGE_ENABLED
|
#if ALPHA_TO_COVERAGE_ENABLED
|
||||||
if (opacity > 0.0)
|
if (opacity > 0.0 && alpha_to_coverage > 0.0)
|
||||||
return coverage <= 0.0;
|
return coverage <= 0.0;
|
||||||
#endif
|
#endif
|
||||||
return (opacity >= 0.0 ? (alpha < opacity) : (alpha >= -opacity));
|
return (opacity >= 0.0 ? (alpha < opacity) : (alpha >= -opacity));
|
||||||
|
|||||||
6
gl/ubo.h
6
gl/ubo.h
@@ -76,7 +76,11 @@ namespace gl
|
|||||||
float fog_density;
|
float fog_density;
|
||||||
float alpha_mult;
|
float alpha_mult;
|
||||||
float shadow_tone;
|
float shadow_tone;
|
||||||
UBS_PAD(12);
|
// 1.0 while GL_SAMPLE_ALPHA_TO_COVERAGE is on, so the alpha tested shaders write the
|
||||||
|
// coverage of their test edge; 0.0 keeps their plain alpha test. set together with
|
||||||
|
// the GL state by opengl33_renderer::setup_alpha_to_coverage()
|
||||||
|
float alpha_to_coverage;
|
||||||
|
UBS_PAD(8);
|
||||||
|
|
||||||
void set_modelview(const glm::mat4 &mv)
|
void set_modelview(const glm::mat4 &mv)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1822,6 +1822,10 @@ void opengl33_renderer::setup_matrices()
|
|||||||
|
|
||||||
void opengl33_renderer::setup_alpha_to_coverage(bool const Enable)
|
void opengl33_renderer::setup_alpha_to_coverage(bool const Enable)
|
||||||
{
|
{
|
||||||
|
// the shaders keep the plain alpha test unless told the coverage of their alpha is used;
|
||||||
|
// uploaded with the rest of the model block at the next draw
|
||||||
|
model_ubs.alpha_to_coverage = (Enable ? 1.f : 0.f);
|
||||||
|
|
||||||
if (Enable == m_alphatocoverageenabled)
|
if (Enable == m_alphatocoverageenabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -2390,11 +2394,13 @@ void opengl33_renderer::Bind_Material( material_handle const Material, TSubModel
|
|||||||
|
|
||||||
// the alpha test drops or keeps whole pixels, so multisampling alone leaves its edges (e.g. sides
|
// the alpha test drops or keeps whole pixels, so multisampling alone leaves its edges (e.g. sides
|
||||||
// of the trackbed textures) jagged. the alpha tested materials of the opaque colour pass get them
|
// of the trackbed textures) jagged. the alpha tested materials of the opaque colour pass get them
|
||||||
// antialiased with alpha to coverage instead; their shaders give the coverage as the alpha
|
// antialiased with alpha to coverage instead; their shaders give the coverage as the alpha.
|
||||||
|
// the model previews draw in colour mode too, but to a single-sampled framebuffer
|
||||||
setup_alpha_to_coverage(
|
setup_alpha_to_coverage(
|
||||||
Global.iMultisampling > 0
|
Global.iMultisampling > 0
|
||||||
&& false == m_blendingenabled
|
&& false == m_blendingenabled
|
||||||
&& m_renderpass.draw_mode == rendermode::color
|
&& m_renderpass.draw_mode == rendermode::color
|
||||||
|
&& false == m_previewing
|
||||||
&& model_ubs.opacity > 0.f
|
&& model_ubs.opacity > 0.f
|
||||||
&& material.shader->alpha_to_coverage );
|
&& material.shader->alpha_to_coverage );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user