From 423f758314a8031326b42c79f2a474f822cd9ab2 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 30 May 2020 20:55:29 +0200 Subject: [PATCH] - texture code restructuring. --- source/build/src/engine.cpp | 8 +++++++- source/build/src/mdsprite.cpp | 2 +- source/build/src/voxmodel.cpp | 4 +--- source/common/textures/hw_material.cpp | 2 -- source/core/rendering/v_framebuffer.cpp | 6 ++++++ source/core/rendering/v_video.h | 14 ++++++++------ source/core/textures/buildtiles.cpp | 11 ++++++----- source/glbackend/gl_palmanager.cpp | 4 ++-- source/glbackend/gl_renderstate.h | 3 ++- source/glbackend/gl_shader.cpp | 16 ++++++++-------- source/glbackend/gl_texture.cpp | 18 +++++++++--------- source/glbackend/glbackend.cpp | 19 ++++++++++++++++--- source/glbackend/glbackend.h | 13 +++++++++++-- source/glbackend/hw_draw2d.cpp | 5 +++-- 14 files changed, 80 insertions(+), 45 deletions(-) diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 5bea04a37..a0a00b2c7 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -2104,6 +2104,7 @@ int32_t enginePostInit(void) V_LoadTranslations(); // loading the translations must be delayed until the palettes have been fully set up. lookups.postLoadTables(); TileFiles.SetupReverseTileMap(); + TileFiles.PostLoadSetup(); return 0; } @@ -3062,7 +3063,6 @@ void twod_rotatesprite(int32_t sx, int32_t sy, int32_t z, int16_t a, int16_t pic F2DDrawer::RenderCommand dg = {}; int method = 0; - dg.mTranslationId = pic? 0 : TRANSLATION(Translation_Remap + basepal, dapalnum); dg.mType = F2DDrawer::DrawTypeTriangles; if (clipx1 > 0 || clipy1 > 0 || clipx2 < screen->GetWidth() - 1 || clipy2 < screen->GetHeight() - 1) { @@ -3088,6 +3088,12 @@ void twod_rotatesprite(int32_t sx, int32_t sy, int32_t z, int16_t a, int16_t pic } dg.mTexture = pic ? pic : tileGetTexture(picnum); + if (!dg.mTexture || !dg.mTexture->isValid()) return; // empty tile. + + // todo: check for hires replacements + if (basepal == 0 && dapalnum == 0 && pic) dg.mTranslationId = 0; + else dg.mTranslationId = TRANSLATION(Translation_Remap + basepal, dapalnum); + dg.mVertCount = 4; dg.mVertIndex = (int)twod->mVertices.Reserve(4); auto ptr = &twod->mVertices[dg.mVertIndex]; diff --git a/source/build/src/mdsprite.cpp b/source/build/src/mdsprite.cpp index 8f6b138ad..dccf0826e 100644 --- a/source/build/src/mdsprite.cpp +++ b/source/build/src/mdsprite.cpp @@ -1712,7 +1712,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr) #if 0 // The data lookup here is one incredible mess. Thanks to whoever cooked this up... :( - // Todo: assign the laxers to the base textures at setup time + // Todo: assign the layers to the base textures at setup time if (!(tspr->clipdist & TSPR_FLAGS_MDHACK)) { det = tex = hw_detailmapping ? mdloadskin((md2model_t *) m, tile2model[Ptile2tile(tspr->picnum, lpal)].skinnum, DETAILPAL, surfi, nullptr) : nullptr; diff --git a/source/build/src/voxmodel.cpp b/source/build/src/voxmodel.cpp index 28cddd466..f136188bf 100644 --- a/source/build/src/voxmodel.cpp +++ b/source/build/src/voxmodel.cpp @@ -1150,6 +1150,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr) } GLInterface.SetPalswap(globalpal); + // The texture here is already translated. GLInterface.SetTexture(-1, htex, 0/*TRANSLATION(Translation_Remap + curbasepal, globalpal)*/, 0, CLAMP_XY); // This must be done after setting up the texture. @@ -1159,9 +1160,6 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr) else GLInterface.SetTinting(-1, 0xffffff, 0xffffff); - GLInterface.UseBrightmaps(false); - GLInterface.UseGlowMapping(false); - GLInterface.UseDetailMapping(false); #endif auto data = screen->mVertexData->AllocVertices(m->qcnt * 6); diff --git a/source/common/textures/hw_material.cpp b/source/common/textures/hw_material.cpp index ddedcbae1..4f5c301db 100644 --- a/source/common/textures/hw_material.cpp +++ b/source/common/textures/hw_material.cpp @@ -166,7 +166,6 @@ IHardwareTexture *FMaterial::GetLayer(int i, int translation, MaterialLayerInfo FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool create) { -#if 0 if (gtex && gtex->isValid()) { if (!gtex->expandSprites()) scaleflags &= ~CTF_Expand; @@ -178,7 +177,6 @@ FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool } return hwtex; } -#endif return NULL; } diff --git a/source/core/rendering/v_framebuffer.cpp b/source/core/rendering/v_framebuffer.cpp index 6cddade31..864edb51d 100644 --- a/source/core/rendering/v_framebuffer.cpp +++ b/source/core/rendering/v_framebuffer.cpp @@ -47,6 +47,7 @@ #include "v_2ddrawer.h" #include "build.h" #include "../glbackend/glbackend.h" +#include "hw_material.h" /* #include "hwrenderer/scene/hw_portal.h" #include "hwrenderer/utility/hw_clock.h" @@ -378,6 +379,11 @@ void DFrameBuffer::FPSLimit() #endif } +FMaterial* DFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags) +{ + return new FMaterial(tex, scaleflags); +} + void DFrameBuffer::BeginScene() { if (videoGetRenderMode() < REND_POLYMOST) return; diff --git a/source/core/rendering/v_video.h b/source/core/rendering/v_video.h index 3094fe1a5..00995509d 100644 --- a/source/core/rendering/v_video.h +++ b/source/core/rendering/v_video.h @@ -51,7 +51,6 @@ static const int VID_MIN_UI_WIDTH = 640; static const int VID_MIN_UI_HEIGHT = 400; struct sector_t; -class FTexture; struct FPortalSceneState; class FSkyVertexBuffer; class IIndexBuffer; @@ -115,7 +114,6 @@ inline bool V_IsTrueColor() } -class FTexture; class FileWriter; enum FTextureFormat : uint32_t; class FModelRenderer; @@ -148,7 +146,7 @@ protected: }; class IHardwareTexture; -class FTexture; +class FGameTexture; class DFrameBuffer @@ -233,6 +231,7 @@ public: virtual IHardwareTexture *CreateHardwareTexture(int numchannels) { return nullptr; } virtual void PrecacheMaterial(FMaterial *mat, int translation) {} virtual FModelRenderer *CreateModelRenderer(int mli) { return nullptr; } + virtual FMaterial* CreateMaterial(FGameTexture* tex, int scaleflags); virtual void BeginFrame() {} virtual void SetWindowSize(int w, int h) {} virtual void StartPrecaching() {} @@ -248,8 +247,11 @@ public: bool BuffersArePersistent() { return !!(hwcaps & RFL_BUFFER_STORAGE); } // Begin/End 2D drawing operations. - void Begin2D() { isIn2D = true; } - void End2D() { isIn2D = false; } + void Begin2D() + { + m2DDrawer.Begin(Width, Height); + } + void End2D() { m2DDrawer.End(); } void BeginScene(); void FinishScene(); @@ -257,7 +259,7 @@ public: void End2DAndUpdate() { DrawRateStuff(); - End2D(); + m2DDrawer.End(); Update(); } diff --git a/source/core/textures/buildtiles.cpp b/source/core/textures/buildtiles.cpp index c39c2d830..b95cd0471 100644 --- a/source/core/textures/buildtiles.cpp +++ b/source/core/textures/buildtiles.cpp @@ -443,9 +443,9 @@ FGameTexture* BuildTiles::ValidateCustomTile(int tilenum, ReplacementType type) replacement = new FCanvasTexture(0, 0); } else return nullptr; - auto reptex = MakeGameTexture(replacement, "", ETextureType::Any); - AddTile(tilenum, reptex); - return reptex; + auto rep = MakeGameTexture(replacement, tile->GetName(), ETextureType::Override); + AddTile(tilenum, rep); + return rep; } //========================================================================== @@ -777,7 +777,8 @@ void tileSetDummy(int tile, int width, int height) } else if (width > 0 && height > 0) { - auto dtile = MakeGameTexture(new FImageTexture(new FDummyTile(width, height)), "", ETextureType::Any); + FStringf texname("#%05d", tile); + auto dtile = MakeGameTexture(new FImageTexture(new FDummyTile(width, height)), texname, ETextureType::Any); TileFiles.AddTile(tile, dtile); } } @@ -831,7 +832,7 @@ int BuildTiles::tileCreateRotated(int tileNum) *(dst + y * width + xofs) = *(src + y + yofs); } - auto dtex = MakeGameTexture(new FImageTexture(new FLooseTile(dbuffer, tex->GetTexelHeight(), tex->GetTexelWidth())), "", ETextureType::Any); + auto dtex = MakeGameTexture(new FImageTexture(new FLooseTile(dbuffer, tex->GetTexelHeight(), tex->GetTexelWidth())), "", ETextureType::Override); int index = findUnusedTile(); bool mapart = TileFiles.tiledata[tileNum].texture != TileFiles.tiledata[tileNum].backup; TileFiles.AddTile(index, dtex, mapart); diff --git a/source/glbackend/gl_palmanager.cpp b/source/glbackend/gl_palmanager.cpp index 06d570722..9186dbea7 100644 --- a/source/glbackend/gl_palmanager.cpp +++ b/source/glbackend/gl_palmanager.cpp @@ -104,7 +104,7 @@ void PaletteManager::BindPalette(int index) p->CreateTexture((uint8_t*)palettedata->Palette, 256, 1, 15, false, "Palette"); palettetextures[index] = p; } - inst->BindTexture(2, palettetextures[index], SamplerNoFilterClampXY); + inst->SetPaletteTexture(palettetextures[index]); } } @@ -140,7 +140,7 @@ void PaletteManager::BindPalswap(int index) p->CreateTexture(lookup.Data(), 256, numshades, 15, false, "Palette"); palswaptextures[index] = p; } - inst->BindTexture(1, palswaptextures[index], SamplerNoFilterClampXY); + inst->SetLookupTexture(palswaptextures[index]); inst->SetFadeColor(lookups.getFade(index)); } } diff --git a/source/glbackend/gl_renderstate.h b/source/glbackend/gl_renderstate.h index 934da85d8..f44cbcdd9 100644 --- a/source/glbackend/gl_renderstate.h +++ b/source/glbackend/gl_renderstate.h @@ -89,6 +89,7 @@ struct PolymostRenderState PalEntry fullscreenTint = 0xffffff, hictint = 0xffffff, hictint_overlay = 0xffffff; int hictint_flags = -1; FDepthBiasState mBias{ }; + OpenGLRenderer::FHardwareTexture* PaletteTexture = nullptr, * LookupTexture = nullptr; int StateFlags = STF_COLORMASK|STF_DEPTHMASK; FRenderStyle Style{}; @@ -96,7 +97,7 @@ struct PolymostRenderState PalEntry ClearColor = 0; short vp_x, vp_y, vp_w, vp_h; short sc_x = SHRT_MIN, sc_y, sc_w, sc_h; - int texIds[6], samplerIds[6]; + int texIds[4], samplerIds[4]; PalEntry FogColor; diff --git a/source/glbackend/gl_shader.cpp b/source/glbackend/gl_shader.cpp index ea96e7e73..9023a865e 100644 --- a/source/glbackend/gl_shader.cpp +++ b/source/glbackend/gl_shader.cpp @@ -166,15 +166,15 @@ bool PolymostShader::Load(const char * name, const char * vert_prog, const char int SamplerLoc; SamplerLoc = glGetUniformLocation(hShader, "s_texture"); glUniform1i(SamplerLoc, 0); - SamplerLoc = glGetUniformLocation(hShader, "s_palswap"); - glUniform1i(SamplerLoc, 1); - SamplerLoc = glGetUniformLocation(hShader, "s_palette"); - glUniform1i(SamplerLoc, 2); - SamplerLoc = glGetUniformLocation(hShader, "s_detail"); - glUniform1i(SamplerLoc, 3); - SamplerLoc = glGetUniformLocation(hShader, "s_glow"); - glUniform1i(SamplerLoc, 4); SamplerLoc = glGetUniformLocation(hShader, "s_brightmap"); + glUniform1i(SamplerLoc, 1); + SamplerLoc = glGetUniformLocation(hShader, "s_detail"); + glUniform1i(SamplerLoc, 2); + SamplerLoc = glGetUniformLocation(hShader, "s_glow"); + glUniform1i(SamplerLoc, 3); + SamplerLoc = glGetUniformLocation(hShader, "s_palette"); + glUniform1i(SamplerLoc, 4); + SamplerLoc = glGetUniformLocation(hShader, "s_palswap"); glUniform1i(SamplerLoc, 5); glUseProgram(0); diff --git a/source/glbackend/gl_texture.cpp b/source/glbackend/gl_texture.cpp index 5da58aae4..16add7a7b 100644 --- a/source/glbackend/gl_texture.cpp +++ b/source/glbackend/gl_texture.cpp @@ -121,8 +121,8 @@ bool GLInstance::SetTexture(int picnum, FGameTexture* tex, int paletteid, int me { auto htex = LoadTexture(det, TT_TRUECOLOR, 0); UseDetailMapping(true); - htex->BindOrCreate(det, 3, CLAMP_NONE, 0, 0); - BindTexture(3, htex, SamplerRepeat); + htex->BindOrCreate(det, 2, CLAMP_NONE, 0, 0); + BindTexture(2, htex, SamplerRepeat); texbound[0] = true; /* todo: @@ -137,8 +137,8 @@ bool GLInstance::SetTexture(int picnum, FGameTexture* tex, int paletteid, int me { auto htex = LoadTexture(glow, TT_TRUECOLOR, 0); UseGlowMapping(true); - htex->BindOrCreate(glow, 4, sampler, 0, CTF_Upscale); - BindTexture(4, htex, SamplerRepeat); + htex->BindOrCreate(glow, 3, sampler, 0, CTF_Upscale); + BindTexture(3, htex, SamplerRepeat); texbound[1] = true; } } @@ -152,15 +152,15 @@ bool GLInstance::SetTexture(int picnum, FGameTexture* tex, int paletteid, int me if (htex != nullptr) { UseBrightmaps(true); - htex->BindOrCreate(btex, 5, sampler, 0, CTF_Upscale); - BindTexture(5, htex, sampler); + htex->BindOrCreate(btex, 1, sampler, 0, CTF_Upscale); + BindTexture(1, htex, sampler); texbound[2] = true; } } } - if (!texbound[0]) UnbindTexture(3); - if (!texbound[1]) UnbindTexture(4); - if (!texbound[2]) UnbindTexture(5); + if (!texbound[0]) UnbindTexture(2); + if (!texbound[1]) UnbindTexture(3); + if (!texbound[2]) UnbindTexture(1); } else return false; diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index d96a0d507..8e2b04738 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -286,6 +286,8 @@ void GLInstance::DrawImGui(ImDrawData* data) void PolymostRenderState::Apply(PolymostShader* shader, GLState &oldState) { + if (!OpenGLRenderer::GLRenderer) return; + auto sm = OpenGLRenderer::GLRenderer->mSamplerManager; bool reset = false; for (int i = 0; i < MAX_TEXTURES; i++) { @@ -297,13 +299,24 @@ void PolymostRenderState::Apply(PolymostShader* shader, GLState &oldState) reset = true; } glBindTexture(GL_TEXTURE_2D, texIds[i]); - if (OpenGLRenderer::GLRenderer) - OpenGLRenderer::GLRenderer->mSamplerManager->Bind(i, samplerIds[i], -1); + sm->Bind(i, samplerIds[i], -1); oldState.TexId[i] = texIds[i]; oldState.SamplerId[i] = samplerIds[i]; } - if (reset) glActiveTexture(GL_TEXTURE0); } + if (PaletteTexture != nullptr) + { + PaletteTexture->Bind(4, false); + sm->Bind(4, CLAMP_NOFILTER, -1); + } + if (LookupTexture != nullptr) + { + LookupTexture->Bind(5, false); + sm->Bind(5, CLAMP_NOFILTER, -1); + } + glActiveTexture(GL_TEXTURE0); + + if (StateFlags != oldState.Flags) { if ((StateFlags ^ oldState.Flags) & STF_DEPTHTEST) diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index e7bc82aa9..a0dcf4639 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -101,7 +101,7 @@ extern float shadediv[256]; enum { - MAX_TEXTURES = 6, /*15*/ // slot 15 is used internally and not available. - The renderer uses only 5, though. + MAX_TEXTURES = 4, /*15*/ // slot 15 is used internally and not available. - The renderer uses only 5, though. }; struct GLState @@ -120,7 +120,6 @@ class GLInstance int lastPalswapIndex = -1; OpenGLRenderer::FHardwareTexture* texv; FGameTexture* currentTexture = nullptr; - int TextureType; int MatrixChange = 0; // Cached GL state. @@ -481,6 +480,16 @@ public: OpenGLRenderer::FHardwareTexture *LoadTexture(FTexture* tex, int texturetype, int palid); + void SetPaletteTexture(OpenGLRenderer::FHardwareTexture* tex) + { + renderState.PaletteTexture = tex; + } + + void SetLookupTexture(OpenGLRenderer::FHardwareTexture* tex) + { + renderState.LookupTexture = tex; + } + bool SetTexture(int globalpicnum, FGameTexture* tex, int palette, int method, int sampleroverride); }; diff --git a/source/glbackend/hw_draw2d.cpp b/source/glbackend/hw_draw2d.cpp index ed87e1dc1..a44f89dda 100644 --- a/source/glbackend/hw_draw2d.cpp +++ b/source/glbackend/hw_draw2d.cpp @@ -162,14 +162,15 @@ void GLInstance::Draw2D(F2DDrawer *drawer) DisableScissor(); } - if (cmd.mTexture != nullptr) + auto tex = cmd.mTexture; + if (tex != nullptr && tex->isValid()) { auto tex = cmd.mTexture; SetFadeDisable(true); SetShade(0, numshades); - SetTexture(TileFiles.GetTileIndex(cmd.mTexture), cmd.mTexture, cmd.mTranslationId, 0, cmd.mFlags & F2DDrawer::DTF_Wrap ? CLAMP_NONE : CLAMP_XY); + SetTexture(TileFiles.GetTileIndex(tex), tex, cmd.mTranslationId, 0, cmd.mFlags & F2DDrawer::DTF_Wrap ? CLAMP_NONE : CLAMP_XY); EnableBlend(!(cmd.mRenderStyle.Flags & STYLEF_Alpha1)); UseColorOnly(false); }