Alpha testing cleanup in both OpenGL and Direct3D.

This commit is contained in:
Jean-Philip Desjardins 2014-11-22 01:50:17 -05:00
parent a9ae0a446c
commit 993944c75c
3 changed files with 55 additions and 57 deletions

View File

@ -678,45 +678,28 @@ void CGSH_OpenGL::SetupTestFunctions(uint64 nData)
if(tst.nAlphaEnabled) if(tst.nAlphaEnabled)
{ {
unsigned int nFunc = GL_NEVER; static const GLenum g_alphaTestFunc[ALPHA_TEST_MAX] =
switch(tst.nAlphaMethod)
{ {
case 0: GL_NEVER,
nFunc = GL_NEVER; GL_ALWAYS,
break; GL_LESS,
case 1: GL_LEQUAL,
nFunc = GL_ALWAYS; GL_EQUAL,
break; GL_GEQUAL,
case 2: GL_GREATER,
nFunc = GL_LESS; GL_NOTEQUAL
break; };
case 4:
nFunc = GL_EQUAL;
break;
case 5:
nFunc = GL_GEQUAL;
break;
case 6:
nFunc = GL_GREATER;
break;
case 7:
nFunc = GL_NOTEQUAL;
break;
default:
assert(0);
break;
}
//Special way of turning off depth writes: //Special way of turning off depth writes:
//Always fail alpha testing but write RGBA and not depth if it fails //Always fail alpha testing but write RGBA and not depth if it fails
if(tst.nAlphaMethod == 0 && tst.nAlphaFail == 1) if(tst.nAlphaMethod == ALPHA_TEST_NEVER && tst.nAlphaFail == ALPHA_TEST_FAIL_FBONLY)
{ {
glDisable(GL_ALPHA_TEST); glDisable(GL_ALPHA_TEST);
} }
else else
{ {
float nValue = (float)tst.nAlphaRef / 255.0f; float nValue = (float)tst.nAlphaRef / 255.0f;
glAlphaFunc(nFunc, nValue); glAlphaFunc(g_alphaTestFunc[tst.nAlphaMethod], nValue);
glEnable(GL_ALPHA_TEST); glEnable(GL_ALPHA_TEST);
} }

View File

@ -570,6 +570,27 @@ protected:
MIN_FILTER_LINEAR_MIP_LINEAR = 5 MIN_FILTER_LINEAR_MIP_LINEAR = 5
}; };
enum ALPHA_TEST_METHOD
{
ALPHA_TEST_NEVER,
ALPHA_TEST_ALWAYS,
ALPHA_TEST_LESS,
ALPHA_TEST_LEQUAL,
ALPHA_TEST_EQUAL,
ALPHA_TEST_GEQUAL,
ALPHA_TEST_GREATER,
ALPHA_TEST_NOTEQUAL,
ALPHA_TEST_MAX
};
enum ALPHA_TEST_FAIL_METHOD
{
ALPHA_TEST_FAIL_KEEP,
ALPHA_TEST_FAIL_FBONLY,
ALPHA_TEST_FAIL_ZBONLY,
ALPHA_TEST_FAIL_RGBONLY
};
//----------------------------------- //-----------------------------------
//Private Registers //Private Registers

View File

@ -774,36 +774,30 @@ void CGSH_Direct3D9::SetupTestFunctions(uint64 nData)
if(tst.nAlphaEnabled) if(tst.nAlphaEnabled)
{ {
unsigned int alphaFunc = D3DCMP_NEVER; static const D3DCMPFUNC g_alphaTestFunc[ALPHA_TEST_MAX] =
switch(tst.nAlphaMethod)
{ {
case 0: D3DCMP_NEVER,
alphaFunc = D3DCMP_NEVER; D3DCMP_ALWAYS,
break; D3DCMP_LESS,
case 1: D3DCMP_LESSEQUAL,
alphaFunc = D3DCMP_ALWAYS; D3DCMP_EQUAL,
break; D3DCMP_GREATEREQUAL,
case 2: D3DCMP_GREATER,
alphaFunc = D3DCMP_LESS; D3DCMP_NOTEQUAL
break; };
case 5:
alphaFunc = D3DCMP_GREATEREQUAL;
break;
case 6:
alphaFunc = D3DCMP_GREATER;
break;
case 7:
alphaFunc = D3DCMP_NOTEQUAL;
break;
default:
assert(0);
break;
}
m_device->SetRenderState(D3DRS_ALPHAFUNC, alphaFunc); //Special way of turning off depth writes:
m_device->SetRenderState(D3DRS_ALPHAREF, tst.nAlphaRef); //Always fail alpha testing but write RGBA and not depth if it fails
m_device->SetRenderState(D3DRS_ALPHATESTENABLE, D3DZB_TRUE); if(tst.nAlphaMethod == ALPHA_TEST_NEVER && tst.nAlphaFail == ALPHA_TEST_FAIL_FBONLY)
{
m_device->SetRenderState(D3DRS_ALPHATESTENABLE, D3DZB_FALSE);
}
else
{
m_device->SetRenderState(D3DRS_ALPHAFUNC, g_alphaTestFunc[tst.nAlphaMethod]);
m_device->SetRenderState(D3DRS_ALPHAREF, tst.nAlphaRef);
m_device->SetRenderState(D3DRS_ALPHATESTENABLE, D3DZB_TRUE);
}
} }
else else
{ {