Replace deprecated alpha test in shader visitor
parent
70bd9d395d
commit
ce2bcba5d4
@ -0,0 +1,27 @@
|
||||
#include "removedalphafunc.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include <osg/State>
|
||||
|
||||
namespace Shader
|
||||
{
|
||||
std::array<osg::ref_ptr<RemovedAlphaFunc>, GL_ALWAYS - GL_NEVER> RemovedAlphaFunc::sInstances{
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
|
||||
};
|
||||
|
||||
osg::ref_ptr<RemovedAlphaFunc> RemovedAlphaFunc::getInstance(GLenum func)
|
||||
{
|
||||
assert(func >= GL_NEVER && func <= GL_ALWAYS);
|
||||
if (!sInstances[func - GL_NEVER])
|
||||
sInstances[func - GL_NEVER] = new RemovedAlphaFunc(static_cast<osg::AlphaFunc::ComparisonFunction>(func), 1.0);
|
||||
return sInstances[func - GL_NEVER];
|
||||
}
|
||||
|
||||
void RemovedAlphaFunc::apply(osg::State & state) const
|
||||
{
|
||||
// Hopefully, anything genuinely requiring the default alpha func of GL_ALWAYS explicitly sets it
|
||||
if (!state.getGlobalDefaultAttribute(ALPHAFUNC)->getType() != getType())
|
||||
state.setGlobalDefaultAttribute(static_cast<const osg::StateAttribute*>(cloneType()));
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
#ifndef OPENMW_COMPONENTS_REMOVEDALPHAFUNC_H
|
||||
#define OPENMW_COMPONENTS_REMOVEDALPHAFUNC_H
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <osg/AlphaFunc>
|
||||
|
||||
namespace Shader
|
||||
{
|
||||
// State attribute used when shader visitor replaces the deprecated alpha function with a shader
|
||||
// Prevents redundant glAlphaFunc calls and lets the shadowsbin know the stateset had alpha testing
|
||||
class RemovedAlphaFunc : public osg::AlphaFunc
|
||||
{
|
||||
public:
|
||||
// Get a singleton-like instance with the right func (but a default threshold)
|
||||
static osg::ref_ptr<RemovedAlphaFunc> getInstance(GLenum func);
|
||||
|
||||
RemovedAlphaFunc()
|
||||
: osg::AlphaFunc()
|
||||
{}
|
||||
|
||||
RemovedAlphaFunc(ComparisonFunction func, float ref)
|
||||
: osg::AlphaFunc(func, ref)
|
||||
{}
|
||||
|
||||
RemovedAlphaFunc(const RemovedAlphaFunc& raf, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
|
||||
: osg::AlphaFunc(raf, copyop)
|
||||
{}
|
||||
|
||||
META_StateAttribute(Shader, RemovedAlphaFunc, ALPHAFUNC);
|
||||
|
||||
void apply(osg::State& state) const override;
|
||||
|
||||
protected:
|
||||
virtual ~RemovedAlphaFunc() = default;
|
||||
|
||||
static std::array<osg::ref_ptr<RemovedAlphaFunc>, GL_ALWAYS - GL_NEVER> sInstances;
|
||||
};
|
||||
}
|
||||
#endif //OPENMW_COMPONENTS_REMOVEDALPHAFUNC_H
|
@ -0,0 +1,38 @@
|
||||
|
||||
#define FUNC_NEVER 0x0200
|
||||
#define FUNC_LESS 0x0201
|
||||
#define FUNC_EQUAL 0x0202
|
||||
#define FUNC_LEQUAL 0x0203
|
||||
#define FUNC_GREATER 0x0204
|
||||
#define FUNC_NOTEQUAL 0x0205
|
||||
#define FUNC_GEQUAL 0x0206
|
||||
#define FUNC_ALWAYS 0x0207
|
||||
|
||||
#if @alphaFunc != FUNC_ALWAYS && @alphaFunc != FUNC_NEVER
|
||||
uniform float alphaRef;
|
||||
#endif
|
||||
|
||||
void alphaTest()
|
||||
{
|
||||
#if @alphaFunc == FUNC_NEVER
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_LESS
|
||||
if (gl_FragData[0].a > alphaRef)
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_EQUAL
|
||||
if (gl_FragData[0].a != alphaRef)
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_LEQUAL
|
||||
if (gl_FragData[0].a >= alphaRef)
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_GREATER
|
||||
if (gl_FragData[0].a < alphaRef)
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_NOTEQUAL
|
||||
if (gl_FragData[0].a == alphaRef)
|
||||
discard;
|
||||
#elif @alphaFunc == FUNC_GEQUAL
|
||||
if (gl_FragData[0].a <= alphaRef)
|
||||
discard;
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue