From 40358febc17975e51215112bc48e9a1011900d38 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 10 Nov 2020 09:08:48 +0100 Subject: [PATCH] - changed render interface to resolve textures to materials in the backend. This is needed to have a unified spot where to resolve textures for both 2D and 3D. --- source/common/engine/i_interface.h | 4 ++++ .../hwrenderer/data/hw_renderstate.h | 8 ++++++- source/glbackend/gl_texture.cpp | 5 ++-- source/glbackend/glbackend.cpp | 20 +++------------- source/glbackend/glbackend.h | 16 ++++--------- source/glbackend/pm_renderstate.h | 24 ++++++++++++++++++- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/source/common/engine/i_interface.h b/source/common/engine/i_interface.h index cd400f441..73d7fb8c8 100644 --- a/source/common/engine/i_interface.h +++ b/source/common/engine/i_interface.h @@ -4,6 +4,9 @@ #include "intrect.h" struct event_t; +class FRenderState; +class FGameTexture; +enum EUpscaleFlags; struct SystemCallbacks { @@ -27,6 +30,7 @@ struct SystemCallbacks void (*MenuClosed)(); bool (*CheckMenudefOption)(const char* opt); void (*ConsoleToggled)(int state); + bool (*PreBindTexture)(FRenderState* state, FGameTexture*& tex, EUpscaleFlags& flags, int& clampmode, int& translation, int& overrideshader); }; extern SystemCallbacks sysCallbacks; diff --git a/source/common/rendering/hwrenderer/data/hw_renderstate.h b/source/common/rendering/hwrenderer/data/hw_renderstate.h index c52ef83ab..9a90983f3 100644 --- a/source/common/rendering/hwrenderer/data/hw_renderstate.h +++ b/source/common/rendering/hwrenderer/data/hw_renderstate.h @@ -5,6 +5,7 @@ #include "hw_material.h" #include "texmanip.h" #include "version.h" +#include "i_interface.h" struct FColormap; class IVertexBuffer; @@ -569,6 +570,7 @@ public: mBias.mChanged = true; } +private: void SetMaterial(FMaterial *mat, int clampmode, int translation, int overrideshader) { mMaterial.mMaterial = mat; @@ -581,9 +583,13 @@ public: mStreamData.uDetailParms = { scale.X, scale.Y, 2, 0 }; } +public: void SetMaterial(FGameTexture* tex, EUpscaleFlags upscalemask, int scaleflags, int clampmode, int translation, int overrideshader) { - if (shouldUpscale(tex, upscalemask)) scaleflags |= CTF_Upscale; + if (sysCallbacks.PreBindTexture && !sysCallbacks.PreBindTexture(this, tex, upscalemask, clampmode, translation, overrideshader)) + { + if (shouldUpscale(tex, upscalemask)) scaleflags |= CTF_Upscale; + } SetMaterial(FMaterial::ValidateTexture(tex, scaleflags), clampmode, translation, overrideshader); } diff --git a/source/glbackend/gl_texture.cpp b/source/glbackend/gl_texture.cpp index 726ba236e..7b931af8e 100644 --- a/source/glbackend/gl_texture.cpp +++ b/source/glbackend/gl_texture.cpp @@ -78,8 +78,9 @@ bool GLInstance::SetTexture(int picnum, FGameTexture* tex, int paletteid, int sa SetBasepalTint(texpick.basepalTint); auto &mat = renderState.mMaterial; - int flags = (TextureType == TT_INDEXED) ? CTF_Indexed : 0; - mat.mMaterial = FMaterial::ValidateTexture(texpick.texture, flags); // todo allow scaling + mat.mTexture = texpick.texture; + mat.uFlags = UF_None; + mat.mScaleFlags = (TextureType == TT_INDEXED) ? CTF_Indexed : 0; mat.mClampMode = sampler; mat.mTranslation = texpick.translation; mat.mOverrideShader = -1; diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index b8ed2a324..4c262ca99 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -58,16 +58,6 @@ static int BufferLock = 0; TArray matrixArray; void Draw2D(F2DDrawer* drawer, FRenderState& state); -FileReader GetResource(const char* fn) -{ - auto fr = fileSystem.OpenFileReader(fn); - if (!fr.isOpen()) - { - I_Error("Fatal: '%s' not found", fn); - } - return fr; -} - GLInstance GLInterface; GLInstance::GLInstance() @@ -77,10 +67,6 @@ GLInstance::GLInstance() matrixArray.Push(mat); } -//void ImGui_Init_Backend(); -//ImGuiContext* im_ctx; -TArray ttf; - IHardwareTexture *setpalettelayer(int layer, int translation) { if (layer == 1) @@ -171,7 +157,7 @@ void PolymostRenderState::Apply(FRenderState& state, GLState& oldState) else { state.EnableTexture(gl_texture); - state.SetMaterial(mMaterial.mMaterial, mMaterial.mClampMode, mMaterial.mTranslation, mMaterial.mOverrideShader); + state.SetMaterial(mMaterial.mTexture, mMaterial.uFlags, mMaterial.mScaleFlags, mMaterial.mClampMode, mMaterial.mTranslation, mMaterial.mOverrideShader); } state.SetColor(Color[0], Color[1], Color[2], Color[3]); @@ -262,7 +248,6 @@ void PolymostRenderState::Apply(FRenderState& state, GLState& oldState) FVector4 blendcol(0, 0, 0, 0); int flags = 0; - if (Flags & RF_ShadeInterpolate) flags |= 16384; // hijack a free bit in here. if (fullscreenTint != 0xffffff) flags |= 16; if (hictint_flags != -1) { @@ -274,7 +259,7 @@ void PolymostRenderState::Apply(FRenderState& state, GLState& oldState) modcol.Z *= hictint.b / 64.f; } if (hictint_flags & TINTF_GRAYSCALE) - modcol.W = 1.f; + modcol.W = 1.f; if (hictint_flags & TINTF_INVERT) flags |= TextureManipulation::InvertBit; @@ -286,6 +271,7 @@ void PolymostRenderState::Apply(FRenderState& state, GLState& oldState) } } addcol.W = flags; + if (Flags & RF_ShadeInterpolate) addcol.W += 16384; // hijack a free bit in here. state.SetTextureColors(&modcol.X, &addcol.X, &blendcol.X); if (matrixIndex[Matrix_Model] != -1) diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index a3fe80ccb..36ab6cb31 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -269,22 +269,16 @@ public: SetColor(r * (1 / 255.f), g * (1 / 255.f), b * (1 / 255.f), a * (1 / 255.f)); } - void SetMaterial(FMaterial* mat, int clampmode, int translation, int overrideshader) + void SetMaterial(FGameTexture* tex, EUpscaleFlags upscalemask, int scaleflags, int clampmode, int translation, int overrideshader) { - assert(mat); - renderState.mMaterial.mMaterial = mat; + assert(tex); + renderState.mMaterial.mTexture = tex; + renderState.mMaterial.uFlags = upscalemask; + renderState.mMaterial.mScaleFlags = scaleflags; renderState.mMaterial.mClampMode = clampmode; renderState.mMaterial.mTranslation = translation; renderState.mMaterial.mOverrideShader = overrideshader; renderState.mMaterial.mChanged = true; - //mTextureModeFlags = mat->GetLayerFlags(); - } - - void SetMaterial(FGameTexture* tex, EUpscaleFlags upscalemask, int scaleflags, int clampmode, int translation, int overrideshader) - { - assert(tex); - if (shouldUpscale(tex, upscalemask)) scaleflags |= CTF_Upscale; - SetMaterial(FMaterial::ValidateTexture(tex, scaleflags), clampmode, translation, overrideshader); } void UseColorOnly(bool yes) diff --git a/source/glbackend/pm_renderstate.h b/source/glbackend/pm_renderstate.h index eee70157e..5708a1ec0 100644 --- a/source/glbackend/pm_renderstate.h +++ b/source/glbackend/pm_renderstate.h @@ -43,6 +43,28 @@ enum PRSFlags STF_SCISSORSET = 8192, }; +struct PolymostTextureState +{ + FGameTexture* mTexture = nullptr; + EUpscaleFlags uFlags; + int mScaleFlags; + int mClampMode; + int mTranslation; + int mOverrideShader; + bool mChanged; + + void Reset() + { + mTexture = nullptr; + uFlags = UF_None; + mScaleFlags = 0; + mTranslation = 0; + mClampMode = CLAMP_NONE; + mOverrideShader = -1; + mChanged = false; + } +}; + struct PolymostRenderState { int vindex, vcount, primtype; @@ -59,7 +81,7 @@ struct PolymostRenderState PalEntry fullscreenTint = 0xffffff, hictint = 0xffffff, hictint_overlay = 0xffffff; int hictint_flags = -1; FDepthBiasState mBias{ }; - FMaterialState mMaterial; + PolymostTextureState mMaterial; int StateFlags = STF_COLORMASK|STF_DEPTHMASK; FRenderStyle Style{};