From d4b32c535a57ffd9bef5ad2a1a3f8f5d2342e1f8 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 3 Jan 2020 23:38:50 +0100 Subject: [PATCH] - moved matrices to render state. --- source/build/src/mdsprite.cpp | 2 +- source/build/src/polymost.cpp | 10 ++---- source/build/src/voxmodel.cpp | 2 +- source/glbackend/gl_renderstate.h | 12 +++++++ source/glbackend/glbackend.cpp | 59 +++++++++++-------------------- source/glbackend/glbackend.h | 19 +++------- source/glbackend/hw_draw2d.cpp | 22 +++--------- 7 files changed, 47 insertions(+), 79 deletions(-) diff --git a/source/build/src/mdsprite.cpp b/source/build/src/mdsprite.cpp index bc7bf4082..fd59c9180 100644 --- a/source/build/src/mdsprite.cpp +++ b/source/build/src/mdsprite.cpp @@ -1801,7 +1801,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr) GLInterface.SetCull(Cull_None); VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_ModelView, &identity); + GLInterface.SetIdentityMatrix(Matrix_ModelView); GLInterface.SetTinting(0, 0, PalEntry(255, 255, 255)); GLInterface.SetClamp(prevClamp); diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp index be666b3e9..7d970b165 100644 --- a/source/build/src/polymost.cpp +++ b/source/build/src/polymost.cpp @@ -348,7 +348,7 @@ static void resizeglcheck(void) m[3][2] = -(2.f * farclip * nearclip) / (farclip - nearclip); GLInterface.SetMatrix(Matrix_Projection, &m[0][0]); VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_ModelView, &identity); + GLInterface.SetIdentityMatrix(Matrix_ModelView); } } @@ -451,11 +451,7 @@ static void polymost_updaterotmat(void) static void polymost_identityrotmat(void) { - if (1) - { - VSMatrix matrix(0); - GLInterface.SetMatrix(Matrix_View, &matrix); - } + GLInterface.SetIdentityMatrix(Matrix_View); } static void polymost_flatskyrender(vec2f_t const* const dpxy, int32_t const n, int32_t method, const vec2_16_t& tilesiz); @@ -4681,7 +4677,7 @@ void polymost_dorotatespritemodel(int32_t sx, int32_t sy, int32_t z, int16_t a, GLInterface.SetMatrix(Matrix_Projection, &m[0][0]); VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_ModelView, &identity); + GLInterface.SetIdentityMatrix(Matrix_ModelView); } if (hud->flags & HUDFLAG_NODEPTH) diff --git a/source/build/src/voxmodel.cpp b/source/build/src/voxmodel.cpp index 1452c263d..6abefb81d 100644 --- a/source/build/src/voxmodel.cpp +++ b/source/build/src/voxmodel.cpp @@ -1189,7 +1189,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr) GLInterface.SetDepthFunc(Depth_Less); } VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_ModelView, &identity); + GLInterface.SetIdentityMatrix(Matrix_ModelView); GLInterface.SetFadeDisable(false); GLInterface.SetTinting(0, 0, PalEntry(255, 255, 255)); return 1; diff --git a/source/glbackend/gl_renderstate.h b/source/glbackend/gl_renderstate.h index 14dd49fcd..f4f7383a4 100644 --- a/source/glbackend/gl_renderstate.h +++ b/source/glbackend/gl_renderstate.h @@ -6,6 +6,17 @@ class PolymostShader; struct GLState; +enum EMatrixType +{ + Matrix_View, + Matrix_Projection, + Matrix_ModelView, + Matrix_Detail, + Matrix_Texture, + // These are the only ones being used. + NUMMATRICES +}; + enum PRSFlags { RF_ColorOnly = 1, @@ -56,6 +67,7 @@ struct PolymostRenderState float AlphaThreshold = 0.5f; bool AlphaTest = true; float Color[4] = { 1,1,1,1 }; + short matrixIndex[NUMMATRICES] = { -1,-1,-1,-1,-1 }; int StateFlags = STF_COLORMASK|STF_DEPTHMASK; FRenderStyle Style{}; diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index 82bce0a56..bd04412fc 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -55,6 +55,7 @@ static int blendstyles[] = { GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALP static int renderops[] = { GL_FUNC_ADD, GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT }; int depthf[] = { GL_ALWAYS, GL_LESS, GL_EQUAL, GL_LEQUAL }; +static TArray matrixArray; FileReader GetResource(const char* fn) { @@ -211,16 +212,6 @@ std::pair GLInstance::AllocVertices(size_t num) return std::make_pair((size_t)0, Buffer.data()); } -void GLInstance::RestoreTextureProps() -{ - // todo: reset everything that's needed to ensure proper functionality - VSMatrix identity(0); - if (MatrixChange & 1) GLInterface.SetMatrix(Matrix_Texture, &identity); - if (MatrixChange & 2) GLInterface.SetMatrix(Matrix_Detail, &identity); - MatrixChange = 0; -} - - static GLint primtypes[] = { GL_TRIANGLES, @@ -279,7 +270,13 @@ void GLInstance::Draw(EDrawType type, size_t start, size_t count) { glDrawArrays(primtypes[type], start, count); } - if (MatrixChange) RestoreTextureProps(); + if (MatrixChange) + { + if (MatrixChange & 1) SetIdentityMatrix(Matrix_Texture); + if (MatrixChange & 2) SetIdentityMatrix(Matrix_Detail); + MatrixChange = 0; + } + matrixArray.Resize(1); } void GLInstance::BindTexture(int texunit, FHardwareTexture *tex, int sampler) @@ -320,32 +317,8 @@ void GLInstance::UnbindAllTextures() void GLInstance::SetMatrix(int num, const VSMatrix *mat) { - matrices[num] = *mat; - switch(num) - { - default: - return; - - case Matrix_View: - polymostShader->RotMatrix.Set(mat->get()); - break; - - case Matrix_Projection: - polymostShader->ProjectionMatrix.Set(mat->get()); - break; - - case Matrix_ModelView: - polymostShader->ModelMatrix.Set(mat->get()); - break; - - case Matrix_Detail: - polymostShader->DetailMatrix.Set(mat->get()); - break; - - case Matrix_Texture: - polymostShader->TextureMatrix.Set(mat->get()); - break; - } + renderState.matrixIndex[num] = matrixArray.Size(); + matrixArray.Push(*mat); } void GLInstance::ReadPixels(int xdim, int ydim, uint8_t* buffer) @@ -517,6 +490,16 @@ void PolymostRenderState::Apply(PolymostShader* shader, GLState &oldState) shader->AlphaThreshold.Set(AlphaTest ? AlphaThreshold : -1.f); shader->Brightness.Set(Brightness); shader->FogColor.Set(FogColor); - + if (matrixIndex[Matrix_View] != -1) + shader->RotMatrix.Set(matrixArray[matrixIndex[Matrix_View]].get()); + if (matrixIndex[Matrix_Projection] != -1) + shader->ProjectionMatrix.Set(matrixArray[matrixIndex[Matrix_Projection]].get()); + if (matrixIndex[Matrix_ModelView] != -1) + shader->ModelMatrix.Set(matrixArray[matrixIndex[Matrix_ModelView]].get()); + if (matrixIndex[Matrix_Detail] != -1) + shader->DetailMatrix.Set(matrixArray[matrixIndex[Matrix_Detail]].get()); + if (matrixIndex[Matrix_Texture] != -1) + shader->TextureMatrix.Set(matrixArray[matrixIndex[Matrix_Texture]].get()); + memset(matrixIndex, -1, sizeof(matrixIndex)); } diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index 0625e668d..7369659c2 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -131,17 +131,6 @@ enum EDrawType DT_LINES }; -enum EMatrixType -{ - Matrix_View, - Matrix_Projection, - Matrix_ModelView, - Matrix_Detail, - Matrix_Texture, - // These are the only ones being used. - NUMMATRICES -}; - enum ECull { Cull_None, @@ -280,14 +269,16 @@ public: { SetMatrix(num, reinterpret_cast(mat)); } + void SetIdentityMatrix(int num) + { + renderState.matrixIndex[num] = 0; + } void SetPolymostShader(); void SetSurfaceShader(); void SetVPXShader(); void SetPalette(int palette); - bool ApplyTextureProps(FTexture *tex, int pal); - void RestoreTextureProps(); - + void ReadPixels(int w, int h, uint8_t* buffer); void SetPaletteData(int index, const uint8_t* data) diff --git a/source/glbackend/hw_draw2d.cpp b/source/glbackend/hw_draw2d.cpp index 37abbd8f2..34be26719 100644 --- a/source/glbackend/hw_draw2d.cpp +++ b/source/glbackend/hw_draw2d.cpp @@ -233,11 +233,8 @@ void GLInstance::Draw2D(F2DDrawer *drawer) void fullscreen_tint_gl(PalEntry pe) { // Todo: reroute to the 2D drawer - auto oldproj = GLInterface.GetMatrix(Matrix_Projection); - auto oldmv = GLInterface.GetMatrix(Matrix_ModelView); - VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_Projection, &identity); - GLInterface.SetMatrix(Matrix_ModelView, &identity); + GLInterface.SetIdentityMatrix(Matrix_Projection); + GLInterface.SetIdentityMatrix(Matrix_ModelView); GLInterface.EnableDepthTest(false); GLInterface.EnableAlphaTest(false); @@ -255,21 +252,14 @@ void fullscreen_tint_gl(PalEntry pe) vt[2].Set(.0f, -2.5f); GLInterface.Draw(DT_TRIANGLES, data.first, 3); GLInterface.UseColorOnly(false); - - GLInterface.SetMatrix(Matrix_Projection, &oldproj); - GLInterface.SetMatrix(Matrix_ModelView, &oldmv); } void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood_b) { if (!(tint_blood_r | tint_blood_g | tint_blood_b)) return; - auto oldproj = GLInterface.GetMatrix(Matrix_Projection); - auto oldmv = GLInterface.GetMatrix(Matrix_ModelView); - VSMatrix identity(0); - GLInterface.SetMatrix(Matrix_Projection, &identity); - GLInterface.SetMatrix(Matrix_ModelView, &identity); - + GLInterface.SetIdentityMatrix(Matrix_Projection); + GLInterface.SetIdentityMatrix(Matrix_ModelView); GLInterface.EnableDepthTest(false); GLInterface.EnableAlphaTest(false); @@ -296,10 +286,6 @@ void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood GLInterface.SetColorub(255, 255, 255, 255); GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]); GLInterface.UseColorOnly(false); - - GLInterface.SetMatrix(Matrix_Projection, &oldproj); - GLInterface.SetMatrix(Matrix_ModelView, &oldmv); - } static int32_t tint_blood_r = 0, tint_blood_g = 0, tint_blood_b = 0;