- moved render style handling to the render state and simplified its storage to use FRenderStyle instead of its components.

This commit is contained in:
Christoph Oelckers 2020-01-03 10:09:38 +01:00
parent 6574c34543
commit cfc0ba48cb
5 changed files with 49 additions and 42 deletions

View file

@ -550,11 +550,11 @@ void handle_blend(uint8_t enable, uint8_t blend, uint8_t def)
{
if (!enable)
{
GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]);
return;
}
auto rs = GetBlend(blend, def);
GLInterface.SetBlendFunc(rs.SrcAlpha, rs.DestAlpha);
GLInterface.SetRenderStyle(rs);
}
float float_trans(uint32_t maskprops, uint8_t blend)

View file

@ -2,7 +2,9 @@
#include "PalEntry.h"
#include "gl_buffers.h"
#include "renderstyle.h"
class PolymostShader;
struct GLState;
enum PRSFlags
{
@ -53,6 +55,7 @@ struct PolymostRenderState
bool AlphaTest = true;
int StateFlags = STF_COLORMASK|STF_DEPTHMASK;
FRenderStyle Style{};
PalEntry ClearColor = 0;
PalEntry FogColor;
@ -61,5 +64,5 @@ struct PolymostRenderState
int VB_Offset[2] = {};
IIndexBuffer* IndexBuffer = nullptr;
void Apply(PolymostShader *shader, int &oldstate);
void Apply(PolymostShader *shader, GLState &oldstate);
};

View file

@ -51,6 +51,10 @@
float shadediv[MAXPALOOKUPS];
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, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA };
static int renderops[] = { GL_FUNC_ADD, GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT };
FileReader GetResource(const char* fn)
{
auto fr = fileSystem.OpenFileReader(fn, 0);
@ -360,20 +364,6 @@ void GLInstance::SetDepthFunc(int func)
glDepthFunc(f[func]);
}
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, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA };
void GLInstance::SetBlendFunc(int src, int dst)
{
glBlendFunc(blendstyles[src], blendstyles[dst]);
}
static int renderops[] = { GL_FUNC_ADD, GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT };
void GLInstance::SetBlendOp(int op)
{
glBlendEquation(renderops[op]);
}
void GLInstance::SetViewport(int x, int y, int w, int h)
{
glViewport(x, y, w, h);
@ -430,26 +420,26 @@ void GLInstance::DrawImGui(ImDrawData* data)
#endif
}
void PolymostRenderState::Apply(PolymostShader* shader, int &oldstate)
void PolymostRenderState::Apply(PolymostShader* shader, GLState &oldState)
{
if (StateFlags != oldstate)
if (StateFlags != oldState.Flags)
{
if ((StateFlags ^ oldstate) & STF_DEPTHTEST)
if ((StateFlags ^ oldState.Flags) & STF_DEPTHTEST)
{
if (StateFlags & STF_DEPTHTEST) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
}
if ((StateFlags ^ oldstate) & STF_BLEND)
if ((StateFlags ^ oldState.Flags) & STF_BLEND)
{
if (StateFlags & STF_BLEND) glEnable(GL_BLEND);
else glDisable(GL_BLEND);
}
if ((StateFlags ^ oldstate) & STF_MULTISAMPLE)
if ((StateFlags ^ oldState.Flags) & STF_MULTISAMPLE)
{
if (StateFlags & STF_MULTISAMPLE) glEnable(GL_MULTISAMPLE);
else glDisable(GL_MULTISAMPLE);
}
if ((StateFlags ^ oldstate) & (STF_STENCILTEST|STF_STENCILWRITE))
if ((StateFlags ^ oldState.Flags) & (STF_STENCILTEST|STF_STENCILWRITE))
{
if (StateFlags & STF_STENCILWRITE)
{
@ -470,7 +460,7 @@ void PolymostRenderState::Apply(PolymostShader* shader, int &oldstate)
glDisable(GL_STENCIL_TEST);
}
}
if ((StateFlags ^ oldstate) & (STF_CULLCW | STF_CULLCCW))
if ((StateFlags ^ oldState.Flags) & (STF_CULLCW | STF_CULLCCW))
{
if (StateFlags & (STF_CULLCW | STF_CULLCCW))
{
@ -483,17 +473,17 @@ void PolymostRenderState::Apply(PolymostShader* shader, int &oldstate)
glDisable(GL_CULL_FACE);
}
}
if ((StateFlags ^ oldstate) & STF_COLORMASK)
if ((StateFlags ^ oldState.Flags) & STF_COLORMASK)
{
if (StateFlags & STF_COLORMASK) glColorMask(1, 1, 1, 1);
else glColorMask(0, 0, 0, 0);
}
if ((StateFlags ^ oldstate) & STF_DEPTHMASK)
if ((StateFlags ^ oldState.Flags) & STF_DEPTHMASK)
{
if (StateFlags & STF_DEPTHMASK) glDepthMask(1);
else glDepthMask(0);
}
if ((StateFlags ^ oldstate) & STF_WIREFRAME)
if ((StateFlags ^ oldState.Flags) & STF_WIREFRAME)
{
glPolygonMode(GL_FRONT_AND_BACK, (StateFlags & STF_WIREFRAME) ? GL_LINE : GL_FILL);
}
@ -506,9 +496,14 @@ void PolymostRenderState::Apply(PolymostShader* shader, int &oldstate)
glClear(bit);
StateFlags &= ~(STF_CLEARCOLOR|STF_CLEARDEPTH);
}
oldstate = StateFlags;
oldState.Flags = StateFlags;
}
if (Style != oldState.Style)
{
glBlendFunc(blendstyles[Style.SrcAlpha], blendstyles[Style.DestAlpha]);
if (Style.BlendOp != oldState.Style.BlendOp) glBlendEquation(renderops[Style.BlendOp]);
oldState.Style = Style;
// Flags are not being checked yet, the current shader has no implementation for them.
}
// Disable brightmaps if non-black fog is used.
if (!(Flags & RF_FogDisabled) && !FogColor.isBlack()) Flags &= ~RF_Brightmapping;

View file

@ -177,6 +177,12 @@ struct ImDrawData;
struct palette_t;
extern float shadediv[256];
struct GLState
{
int Flags = STF_COLORMASK | STF_DEPTHMASK;
FRenderStyle Style{};
};
class GLInstance
{
enum
@ -197,7 +203,9 @@ class GLInstance
int MatrixChange = 0;
bool istrans = false;
bool g_nontransparent255 = false; // Ugh... This is for movie playback and needs to be maintained as global state.
int lastState = STF_COLORMASK | STF_DEPTHMASK;
// Cached GL state.
GLState lastState;
IVertexBuffer* LastVertexBuffer = nullptr;
int LastVB_Offset[2] = {};
@ -282,8 +290,6 @@ public:
void SetScissor(int x1, int y1, int x2, int y2);
void DisableScissor();
void SetDepthFunc(int func);
void SetBlendFunc(int src, int dst);
void SetBlendOp(int op);
void SetViewport(int x, int y, int w, int h);
void SetPolymostShader();
void SetSurfaceShader();
@ -411,6 +417,11 @@ public:
renderState.StateFlags |= STF_CLEARDEPTH;
}
void SetRenderStyle(FRenderStyle style)
{
renderState.Style = style;
}
void UseColorOnly(bool yes)
{
if (yes) renderState.Flags |= RF_ColorOnly;
@ -493,7 +504,7 @@ public:
renderState.AlphaThreshold = al;
}
FHardwareTexture* CreateIndexedTexture(FTexture* tex);
FHardwareTexture* CreateTrueColorTexture(FTexture* tex, int palid, bool checkfulltransparency = false, bool rgb8bit = false);
FHardwareTexture *LoadTexture(FTexture* tex, int texturetype, int palid);

View file

@ -106,7 +106,7 @@ void GLInstance::Draw2D(F2DDrawer *drawer)
EnableMultisampling(false);
EnableBlend(true);
EnableAlphaTest(true);
SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]);
auto &vertices = drawer->mVertices;
auto &indices = drawer->mIndices;
@ -138,8 +138,7 @@ void GLInstance::Draw2D(F2DDrawer *drawer)
int gltrans = -1;
SetBlendFunc(cmd.mRenderStyle.SrcAlpha, cmd.mRenderStyle.DestAlpha);
//state.SetRenderStyle(cmd.mRenderStyle);
SetRenderStyle(cmd.mRenderStyle);
//state.EnableBrightmap(!(cmd.mRenderStyle.Flags & STYLEF_ColorIsFixed));
//state.SetTextureMode(cmd.mDrawMode);
@ -243,7 +242,7 @@ void fullscreen_tint_gl(PalEntry pe)
GLInterface.EnableDepthTest(false);
GLInterface.EnableAlphaTest(false);
GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]);
GLInterface.EnableBlend(true);
GLInterface.SetColorub (pe.r, pe.g, pe.b, pe.a);
@ -275,7 +274,7 @@ void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood
GLInterface.EnableDepthTest(false);
GLInterface.EnableAlphaTest(false);
GLInterface.SetBlendFunc(STYLEALPHA_One, STYLEALPHA_One);
GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Add]);
GLInterface.EnableBlend(true);
GLInterface.UseColorOnly(true);
@ -286,7 +285,7 @@ void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood
vt[1].Set(2.5f, 1.f);
vt[2].Set(.0f, -2.5f);
GLInterface.Draw(DT_TRIANGLES, data.first, 3);
GLInterface.SetBlendOp(STYLEOP_RevSub);
GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Subtract]);
GLInterface.SetColorub(max(-tint_blood_r, 0), max(-tint_blood_g, 0), max(-tint_blood_b, 0), 255);
data = GLInterface.AllocVertices(3);
vt = data.second;
@ -294,9 +293,8 @@ void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood
vt[1].Set(2.5f, 1.f);
vt[2].Set(.0f, -2.5f);
GLInterface.Draw(DT_TRIANGLES, data.first, 3);
GLInterface.SetBlendOp(STYLEOP_Add);
GLInterface.SetColorub(255, 255, 255, 255);
GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]);
GLInterface.UseColorOnly(false);
GLInterface.SetMatrix(Matrix_Projection, &oldproj);