18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 00:35:59 +01:00

Merge pull request #287 from MaSzyna-EU07/fix/msaa-alpha-tested-tracks

fix(render): antialias alpha tested edges with multisampling (eu07/maszyna#449)
This commit is contained in:
maj00r
2026-11-08 21:03:47 +01:00
committed by GitHub
25 changed files with 138 additions and 35 deletions

View File

@@ -1820,6 +1820,22 @@ void opengl33_renderer::setup_matrices()
OpenGLMatrices.load_matrix(glm::mat4{glm::mat3{m_renderpass.pass_camera.modelview()}});
}
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)
return;
if (Enable)
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
else
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
m_alphatocoverageenabled = Enable;
}
void opengl33_renderer::setup_drawing(bool const Alpha)
{
if (Alpha)
@@ -1834,6 +1850,8 @@ void opengl33_renderer::setup_drawing(bool const Alpha)
glDepthMask(GL_TRUE);
m_blendingenabled = false;
}
// turned on again per material, by the ones which need it
setup_alpha_to_coverage(false);
switch (m_renderpass.draw_mode)
{
@@ -2374,6 +2392,18 @@ void opengl33_renderer::Bind_Material( material_handle const Material, TSubModel
else
model_ubs.alpha_mult = 1.0f;
// 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
// 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(
Global.iMultisampling > 0
&& false == m_blendingenabled
&& m_renderpass.draw_mode == rendermode::color
&& false == m_previewing
&& model_ubs.opacity > 0.f
&& material.shader->alpha_to_coverage );
// materials which declare texture variants provide a different texture set per model instance,
// picked from the seed of the rendered instance and the variant group of the submodel
auto const &materialtextures{

View File

@@ -277,6 +277,8 @@ class opengl33_renderer : public gfx_renderer {
void setup_pass(viewport_config &Viewport, renderpass_config &Config, rendermode const Mode, float const Znear = 0.f, float const Zfar = 1.f, bool const Ignoredebug = false);
void setup_matrices();
void setup_drawing(bool const Alpha = false);
// alpha to coverage for the alpha tested materials of the opaque colour pass, when multisampled
void setup_alpha_to_coverage(bool const Enable);
void setup_shadow_unbind_map();
void setup_shadow_bind_map();
void setup_shadow_color( glm::vec4 const &Shadowcolor );
@@ -560,6 +562,7 @@ class opengl33_renderer : public gfx_renderer {
material_handle m_invalid_material;
bool m_blendingenabled;
bool m_alphatocoverageenabled{ false };
bool m_widelines_supported;