- abstraction of render style in render state.

This commit is contained in:
Christoph Oelckers 2018-10-20 23:14:57 +02:00
parent 9ef5e00cdf
commit fac7c7a31c
6 changed files with 63 additions and 41 deletions

View file

@ -42,9 +42,6 @@ static int op2gl[] = { GL_KEEP, GL_INCR, GL_DECR };
FGLRenderState gl_RenderState; FGLRenderState gl_RenderState;
CVAR(Bool, gl_direct_state_change, true, 0)
static VSMatrix identityMatrix(1); static VSMatrix identityMatrix(1);
TArray<VSMatrix> gl_MatrixStack; TArray<VSMatrix> gl_MatrixStack;
@ -213,19 +210,10 @@ bool FGLRenderState::ApplyShader()
void FGLRenderState::Apply() void FGLRenderState::Apply()
{ {
if (!gl_direct_state_change) if (mRenderStyle != stRenderStyle)
{ {
if (mSrcBlend != stSrcBlend || mDstBlend != stDstBlend) ApplyBlendMode();
{ stRenderStyle = mRenderStyle;
stSrcBlend = mSrcBlend;
stDstBlend = mDstBlend;
glBlendFunc(mSrcBlend, mDstBlend);
}
if (mBlendEquation != stBlendEquation)
{
stBlendEquation = mBlendEquation;
glBlendEquation(mBlendEquation);
}
} }
if (mMaterial.mChanged) if (mMaterial.mChanged)
@ -343,4 +331,40 @@ void FGLRenderState::ApplyMaterial(FMaterial *mat, int clampmode, int translatio
} }
} }
//==========================================================================
//
// Apply blend mode from RenderStyle
//
//==========================================================================
void FGLRenderState::ApplyBlendMode()
{
static int blendstyles[] = { GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, };
static int renderops[] = { 0, GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1 };
int srcblend = blendstyles[mRenderStyle.SrcAlpha%STYLEALPHA_MAX];
int dstblend = blendstyles[mRenderStyle.DestAlpha%STYLEALPHA_MAX];
int blendequation = renderops[mRenderStyle.BlendOp & 15];
if (blendequation == -1) // This was a fuzz style.
{
srcblend = GL_DST_COLOR;
dstblend = GL_ONE_MINUS_SRC_ALPHA;
blendequation = GL_FUNC_ADD;
}
// Checks must be disabled until all draw code has been converted.
//if (srcblend != stSrcBlend || dstblend != stDstBlend)
{
stSrcBlend = srcblend;
stDstBlend = dstblend;
glBlendFunc(srcblend, dstblend);
}
//if (blendequation != stBlendEquation)
{
stBlendEquation = blendequation;
glBlendEquation(blendequation);
}
}

View file

@ -39,8 +39,6 @@ class FShader;
struct GLSectorPlane; struct GLSectorPlane;
extern TArray<VSMatrix> gl_MatrixStack; extern TArray<VSMatrix> gl_MatrixStack;
EXTERN_CVAR(Bool, gl_direct_state_change)
enum EPassType enum EPassType
{ {
NORMAL_PASS, NORMAL_PASS,
@ -67,6 +65,7 @@ class FGLRenderState : public FRenderState
int mEffectState; int mEffectState;
int mTempTM = TM_MODULATE; int mTempTM = TM_MODULATE;
FRenderStyle stRenderStyle;
int stSrcBlend, stDstBlend; int stSrcBlend, stDstBlend;
bool stAlphaTest; bool stAlphaTest;
int stBlendEquation; int stBlendEquation;
@ -103,6 +102,7 @@ public:
void Apply(); void Apply();
void ApplyLightIndex(int index); void ApplyLightIndex(int index);
void ApplyBlendMode();
void SetVertexBuffer(FVertexBuffer *vb) void SetVertexBuffer(FVertexBuffer *vb)
{ {
@ -163,28 +163,15 @@ public:
} }
void BlendFunc(int src, int dst) void BlendFunc(int src, int dst)
{
if (!gl_direct_state_change)
{
mSrcBlend = src;
mDstBlend = dst;
}
else
{ {
glBlendFunc(src, dst); glBlendFunc(src, dst);
} stRenderStyle = LegacyRenderStyles[STYLE_OptFuzzy]; // temporary hack: Set to something that never can get here to invalidate the cache
} }
void BlendEquation(int eq) void BlendEquation(int eq)
{
if (!gl_direct_state_change)
{
mBlendEquation = eq;
}
else
{ {
glBlendEquation(eq); glBlendEquation(eq);
} stRenderStyle = LegacyRenderStyles[STYLE_OptFuzzy]; // temporary hack: Set to something that never can get here to invalidate the cache
} }
// This wraps the depth clamp setting because we frequently need to read it which OpenGL is not particularly performant at... // This wraps the depth clamp setting because we frequently need to read it which OpenGL is not particularly performant at...

View file

@ -216,7 +216,7 @@ void FDrawInfo::DrawFlat(GLFlat *flat, int pass, bool trans) // trans only has m
break; break;
case GLPASS_TRANSLUCENT: case GLPASS_TRANSLUCENT:
if (flat->renderstyle==STYLE_Add) gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE); gl_RenderState.SetRenderStyle(flat->renderstyle);
SetColor(flat->lightlevel, rel, flat->Colormap, flat->alpha); SetColor(flat->lightlevel, rel, flat->Colormap, flat->alpha);
SetFog(flat->lightlevel, rel, &flat->Colormap, false); SetFog(flat->lightlevel, rel, &flat->Colormap, false);
if (!flat->gltexture || !flat->gltexture->tex->isFullbright()) if (!flat->gltexture || !flat->gltexture->tex->isFullbright())
@ -237,7 +237,7 @@ void FDrawInfo::DrawFlat(GLFlat *flat, int pass, bool trans) // trans only has m
DrawSubsectors(flat, pass, true); DrawSubsectors(flat, pass, true);
gl_RenderState.EnableTextureMatrix(false); gl_RenderState.EnableTextureMatrix(false);
} }
if (flat->renderstyle==STYLE_Add) gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl_RenderState.SetRenderStyle(DefaultRenderStyle());
gl_RenderState.SetObjectColor(0xffffffff); gl_RenderState.SetObjectColor(0xffffffff);
break; break;
} }

View file

@ -129,6 +129,7 @@ protected:
PalEntry mObjectColor; PalEntry mObjectColor;
PalEntry mObjectColor2; PalEntry mObjectColor2;
FStateVec4 mDynColor; FStateVec4 mDynColor;
FRenderStyle mRenderStyle;
FMaterialState mMaterial; FMaterialState mMaterial;
FStencilState mStencil; FStencilState mStencil;
@ -157,6 +158,7 @@ public:
mLightParms[3] = -1.f; mLightParms[3] = -1.f;
mSpecialEffect = EFF_NONE; mSpecialEffect = EFF_NONE;
mLightIndex = -1; mLightIndex = -1;
mRenderStyle = DefaultRenderStyle();
mMaterial.Reset(); mMaterial.Reset();
mStencil.Reset(); mStencil.Reset();
mBias.Reset(); mBias.Reset();
@ -333,6 +335,15 @@ public:
mLightIndex = index; mLightIndex = index;
} }
void SetRenderStyle(FRenderStyle rs)
{
mRenderStyle = rs;
}
void SetRenderStyle(ERenderStyle rs)
{
mRenderStyle = rs;
}
void SetDepthBias(float a, float b) void SetDepthBias(float a, float b)
{ {

View file

@ -60,6 +60,8 @@ FRenderStyle LegacyRenderStyles[STYLE_Count] =
{ { STYLEOP_Add, STYLEALPHA_Src, STYLEALPHA_One, STYLEF_RedIsAlpha | STYLEF_ColorIsFixed } }, /* STYLE_AddShaded */ { { STYLEOP_Add, STYLEALPHA_Src, STYLEALPHA_One, STYLEF_RedIsAlpha | STYLEF_ColorIsFixed } }, /* STYLE_AddShaded */
{ { STYLEOP_Add, STYLEALPHA_DstCol, STYLEALPHA_Zero, 0 } }, /* STYLE_Multiply */ { { STYLEOP_Add, STYLEALPHA_DstCol, STYLEALPHA_Zero, 0 } }, /* STYLE_Multiply */
{ { STYLEOP_Add, STYLEALPHA_InvDstCol, STYLEALPHA_Zero, 0 } }, /* STYLE_InverseMultiply */ { { STYLEOP_Add, STYLEALPHA_InvDstCol, STYLEALPHA_Zero, 0 } }, /* STYLE_InverseMultiply */
{ { STYLEOP_Add, STYLEALPHA_SrcCol, STYLEALPHA_InvSrcCol, 0 } }, /* STYLE_ColorBlend */
{ { STYLEOP_Add, STYLEALPHA_One, STYLEALPHA_Zero, 0 } }, /* STYLE_Source */
}; };
double GetAlpha(int type, double alpha) double GetAlpha(int type, double alpha)

View file

@ -63,6 +63,8 @@ enum ERenderStyle
STYLE_AddShaded, // Treat patch data as alpha values for alphacolor STYLE_AddShaded, // Treat patch data as alpha values for alphacolor
STYLE_Multiply, // Multiply source with destination (HW renderer only.) STYLE_Multiply, // Multiply source with destination (HW renderer only.)
STYLE_InverseMultiply, // Multiply source with inverse of destination (HW renderer only.) STYLE_InverseMultiply, // Multiply source with inverse of destination (HW renderer only.)
STYLE_ColorBlend, // Use color intensity as transparency factor
STYLE_Source, // No blending (only used internally)
STYLE_Count STYLE_Count
}; };
@ -141,6 +143,7 @@ union FRenderStyle
inline FRenderStyle &operator= (ERenderStyle legacy); inline FRenderStyle &operator= (ERenderStyle legacy);
bool operator==(const FRenderStyle &o) const { return AsDWORD == o.AsDWORD; } bool operator==(const FRenderStyle &o) const { return AsDWORD == o.AsDWORD; }
bool operator!=(const FRenderStyle &o) const { return AsDWORD != o.AsDWORD; }
void CheckFuzz(); void CheckFuzz();
bool IsVisible(double alpha) const throw(); bool IsVisible(double alpha) const throw();
private: private:
@ -157,11 +160,6 @@ inline FRenderStyle DefaultRenderStyle()
return LegacyRenderStyles[STYLE_Normal]; return LegacyRenderStyles[STYLE_Normal];
} }
inline FRenderStyle BadRenderStyle() // This is just a marker to find places where work is still needed.
{
return LegacyRenderStyles[STYLE_Normal];
}
inline FRenderStyle &FRenderStyle::operator= (ERenderStyle legacy) inline FRenderStyle &FRenderStyle::operator= (ERenderStyle legacy)
{ {
if (legacy < STYLE_None || legacy >= STYLE_Count) if (legacy < STYLE_None || legacy >= STYLE_Count)