From e3fd5efcfefc22d9323463c7f274252b895c1b01 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sat, 2 Jan 2021 19:09:06 +0000 Subject: [PATCH] Disable A2C for alpha-blended drawables --- components/shader/shadervisitor.cpp | 14 +++++++++++++- components/shader/shadervisitor.hpp | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/components/shader/shadervisitor.cpp b/components/shader/shadervisitor.cpp index 4762549a7a..165be6745d 100644 --- a/components/shader/shadervisitor.cpp +++ b/components/shader/shadervisitor.cpp @@ -26,8 +26,10 @@ namespace Shader , mColorMode(0) , mMaterialOverridden(false) , mAlphaTestOverridden(false) + , mAlphaBlendOverridden(false) , mAlphaFunc(GL_ALWAYS) , mAlphaRef(1.0) + , mAlphaBlend(false) , mNormalHeight(false) , mTexStageRequiringTangents(-1) , mNode(nullptr) @@ -330,6 +332,15 @@ namespace Shader } } } + + unsigned int alphaBlend = stateset->getMode(GL_BLEND); + if (alphaBlend != osg::StateAttribute::INHERIT && (!mRequirements.back().mAlphaBlendOverridden || alphaBlend & osg::StateAttribute::PROTECTED)) + { + if (alphaBlend & osg::StateAttribute::OVERRIDE) + mRequirements.back().mAlphaBlendOverridden = true; + + mRequirements.back().mAlphaBlend = alphaBlend & osg::StateAttribute::ON; + } } void ShaderVisitor::pushRequirements(osg::Node& node) @@ -394,7 +405,8 @@ namespace Shader // This prevents redundant glAlphaFunc calls while letting the shadows bin still see the test writableStateSet->setAttribute(RemovedAlphaFunc::getInstance(reqs.mAlphaFunc), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); - if (mConvertAlphaTestToAlphaToCoverage) + // Blending won't work with A2C as we use the alpha channel for coverage. gl_SampleCoverage from ARB_sample_shading would save the day, but requires GLSL 130 + if (mConvertAlphaTestToAlphaToCoverage && !reqs.mAlphaBlend) { writableStateSet->setMode(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB, osg::StateAttribute::ON); defineMap["alphaToCoverage"] = "1"; diff --git a/components/shader/shadervisitor.hpp b/components/shader/shadervisitor.hpp index 606e06df97..9daeab29ba 100644 --- a/components/shader/shadervisitor.hpp +++ b/components/shader/shadervisitor.hpp @@ -84,9 +84,11 @@ namespace Shader bool mMaterialOverridden; bool mAlphaTestOverridden; + bool mAlphaBlendOverridden; GLenum mAlphaFunc; float mAlphaRef; + bool mAlphaBlend; bool mNormalHeight; // true if normal map has height info in alpha channel