From 7641da8b7bf56d91ec14a11150dd2dd2c9e7c39d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 17 Apr 2020 18:52:48 +0200 Subject: [PATCH] - moved several members from FTexture to FGameTexture. --- src/common/textures/hw_material.cpp | 5 +-- src/common/textures/texture.cpp | 32 +++++++------- src/common/textures/texturemanager.cpp | 4 +- src/common/textures/textures.h | 43 +++++++++---------- src/rendering/swrenderer/r_swrenderer.cpp | 4 +- .../swrenderer/textures/r_swtexture.h | 2 +- .../swrenderer/textures/swcanvastexture.cpp | 15 +++++++ 7 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/common/textures/hw_material.cpp b/src/common/textures/hw_material.cpp index 41c85fccf9..9f78f5d722 100644 --- a/src/common/textures/hw_material.cpp +++ b/src/common/textures/hw_material.cpp @@ -126,7 +126,7 @@ FMaterial::FMaterial(FGameTexture * tx, int scaleflags) mScaleFlags = scaleflags; mTextureLayers.ShrinkToFit(); - imgtex->Material[scaleflags] = this; + tx->Material[scaleflags] = this; if (tx->isHardwareCanvas()) tx->SetTranslucent(false); } @@ -167,10 +167,9 @@ FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool { if (gtex && gtex->isValid()) { - auto tex = gtex->GetTexture(); if (!gtex->ShouldExpandSprite()) scaleflags &= ~CTF_Expand; - FMaterial *hwtex = tex->Material[scaleflags]; + FMaterial *hwtex = gtex->Material[scaleflags]; if (hwtex == NULL && create) { hwtex = new FMaterial(gtex, scaleflags); diff --git a/src/common/textures/texture.cpp b/src/common/textures/texture.cpp index acdba34319..fbdc663f17 100644 --- a/src/common/textures/texture.cpp +++ b/src/common/textures/texture.cpp @@ -152,17 +152,6 @@ FTexture::~FTexture () { if (areas != nullptr) delete[] areas; areas = nullptr; - - for (int i = 0; i < 2; i++) - { - if (Material[i] != nullptr) DeleteMaterial(Material[i]); - Material[i] = nullptr; - } - if (SoftwareTexture != nullptr) - { - delete SoftwareTexture; - SoftwareTexture = nullptr; - } } //=========================================================================== @@ -861,10 +850,10 @@ void FGameTexture::SetupSpriteData() { // Since this is only needed for real sprites it gets allocated on demand. // It also allocates from the image memory arena because it has the same lifetime and to reduce maintenance. - if (Base->spi == nullptr) Base->spi = (SpritePositioningInfo*)ImageArena.Alloc(2 * sizeof(SpritePositioningInfo)); + if (spi == nullptr) spi = (SpritePositioningInfo*)ImageArena.Alloc(2 * sizeof(SpritePositioningInfo)); for (int i = 0; i < 2; i++) { - auto& spi = Base->spi[i]; + auto& spi = this->spi[i]; spi.mSpriteU[0] = spi.mSpriteV[0] = 0.f; spi.mSpriteU[1] = spi.mSpriteV[1] = 1.f; spi.spriteWidth = GetTexelWidth(); @@ -889,7 +878,7 @@ void FGameTexture::SetupSpriteData() void FGameTexture::SetSpriteRect() { - if (!Base->spi) return; + if (!spi) return; auto leftOffset = GetLeftOffsetHW(); auto topOffset = GetTopOffsetHW(); @@ -898,7 +887,7 @@ void FGameTexture::SetSpriteRect() for (int i = 0; i < 2; i++) { - auto& spi = Base->spi[i]; + auto& spi = this->spi[i]; // mSpriteRect is for positioning the sprite in the scene. spi.mSpriteRect.left = -leftOffset / fxScale; @@ -1087,8 +1076,17 @@ FGameTexture::~FGameTexture() { FGameTexture* link = fileSystem.GetLinkedTexture(GetSourceLump()); if (link == this) fileSystem.SetLinkedTexture(GetSourceLump(), nullptr); - auto str = GetName(); - Printf("Deleting texture %s\n", str.GetChars()); + if (SoftwareTexture != nullptr) + { + delete SoftwareTexture; + SoftwareTexture = nullptr; + } + for (auto &mat : Material) + { + if (mat != nullptr) DeleteMaterial(mat); + mat = nullptr; + } + } bool FGameTexture::isUserContent() const diff --git a/src/common/textures/texturemanager.cpp b/src/common/textures/texturemanager.cpp index 8265f36172..7cc959a751 100644 --- a/src/common/textures/texturemanager.cpp +++ b/src/common/textures/texturemanager.cpp @@ -119,8 +119,8 @@ void FTextureManager::FlushAll() for (int j = 0; j < 2; j++) { Textures[i].Texture->GetTexture()->CleanHardwareTextures(true); - delete Textures[i].Texture->GetTexture()->SoftwareTexture; - Textures[i].Texture->GetTexture()->SoftwareTexture = nullptr; + delete Textures[i].Texture->GetSoftwareTexture(); + Textures[i].Texture->SetSoftwareTexture(nullptr); calcShouldUpscale(Textures[i].Texture); } } diff --git a/src/common/textures/textures.h b/src/common/textures/textures.h index 6946a4d96f..459a4d0ced 100644 --- a/src/common/textures/textures.h +++ b/src/common/textures/textures.h @@ -248,8 +248,6 @@ class FTexture : public RefCountedBase public: - SpritePositioningInfo *spi = nullptr; - IHardwareTexture* GetHardwareTexture(int translation, int scaleflags); static FTexture *CreateTexture(const char *name, int lumpnum, ETextureType UseType); virtual ~FTexture (); @@ -335,16 +333,6 @@ public: static bool SmoothEdges(unsigned char * buffer,int w, int h); static PalEntry averageColor(const uint32_t *data, int size, int maxout); - - ISoftwareTexture* GetSoftwareTexture() - { - return SoftwareTexture; - } - void SetSoftwareTextue(ISoftwareTexture* swtex) - { - SoftwareTexture = swtex; - } - protected: DVector2 Scale; @@ -352,11 +340,9 @@ protected: int SourceLump; FTextureID id; - FMaterial *Material[4] = { }; public: FHardwareTextureContainer SystemTextures; protected: - ISoftwareTexture *SoftwareTexture = nullptr; protected: @@ -440,10 +426,6 @@ public: { return bTranslucent != -1 ? bTranslucent : DetermineTranslucency(); } - FMaterial* GetMaterial(int num) - { - return Material[num]; - } private: int CheckDDPK3(); @@ -610,7 +592,7 @@ class FGameTexture { friend class FMaterial; - // Material layers + // Material layers. These are shared so reference counting is used. RefCountedPtr Base; RefCountedPtr Brightmap; RefCountedPtr Detailmap; @@ -624,6 +606,10 @@ class FGameTexture int8_t shouldUpscaleFlag = 0; // Without explicit setup, scaling is disabled for a texture. ETextureType UseType = ETextureType::Wall; // This texture's primary purpose + SpritePositioningInfo* spi = nullptr; + + ISoftwareTexture* SoftwareTexture = nullptr; + FMaterial* Material[4] = { }; public: FGameTexture(FTexture* wrap) : Base(wrap) {} @@ -679,9 +665,22 @@ public: void SetSkyOffset(int ofs) { Base->SetSkyOffset(ofs); } int GetSkyOffset() const { return Base->GetSkyOffset(); } FTextureID GetID() const { return Base->GetID(); } - ISoftwareTexture* GetSoftwareTexture() { return Base->GetSoftwareTexture(); } - void SetSoftwareTexture(ISoftwareTexture* swtex) { Base->SetSoftwareTextue(swtex); } void SetScale(DVector2 vec) { Base->SetScale(vec); } + + ISoftwareTexture* GetSoftwareTexture() + { + return SoftwareTexture; + } + void SetSoftwareTexture(ISoftwareTexture* swtex) + { + SoftwareTexture = swtex; + } + + FMaterial* GetMaterial(int num) + { + return Material[num]; + } + const FString& GetName() const { return Base->GetName(); } void SetShaderSpeed(float speed) { Base->shaderspeed = speed; } void SetShaderIndex(int index) { Base->shaderindex = index; } @@ -726,7 +725,7 @@ public: void SetSize(int x, int y) { Base->SetSize(x, y); } void SetDisplaySize(float w, float h) { Base->SetSize((int)w, (int)h); } - const SpritePositioningInfo& GetSpritePositioning(int which) { if (Base->spi == nullptr) SetupSpriteData(); return Base->spi[which]; } + const SpritePositioningInfo& GetSpritePositioning(int which) { if (spi == nullptr) SetupSpriteData(); return spi[which]; } int GetAreas(FloatRect** pAreas) const { return Base->GetAreas(pAreas); } PalEntry GetSkyCapColor(bool bottom) { return Base->GetSkyCapColor(bottom); } diff --git a/src/rendering/swrenderer/r_swrenderer.cpp b/src/rendering/swrenderer/r_swrenderer.cpp index d01d89242d..24eb810498 100644 --- a/src/rendering/swrenderer/r_swrenderer.cpp +++ b/src/rendering/swrenderer/r_swrenderer.cpp @@ -227,6 +227,8 @@ void FSoftwareRenderer::SetClearColor(int color) mScene.SetClearColor(color); } +FSWCanvasTexture* GetSWCamTex(FCanvasTexture* camtex); + void FSoftwareRenderer::RenderTextureView (FCanvasTexture *camtex, AActor *viewpoint, double fov) { auto renderTarget = mScene.MainThread()->Viewport->RenderTarget; @@ -237,7 +239,7 @@ void FSoftwareRenderer::RenderTextureView (FCanvasTexture *camtex, AActor *viewp cameraViewpoint = r_viewpoint; cameraViewwindow = r_viewwindow; - auto tex = static_cast(camtex->GetSoftwareTexture()); + auto tex = GetSWCamTex(camtex); DCanvas *Canvas = renderTarget->IsBgra() ? tex->GetCanvasBgra() : tex->GetCanvas(); diff --git a/src/rendering/swrenderer/textures/r_swtexture.h b/src/rendering/swrenderer/textures/r_swtexture.h index bf9a83c5a6..cfd97c2eed 100644 --- a/src/rendering/swrenderer/textures/r_swtexture.h +++ b/src/rendering/swrenderer/textures/r_swtexture.h @@ -176,7 +176,7 @@ class FSWCanvasTexture : public FSoftwareTexture public: - FSWCanvasTexture(FGameTexture *source) : FSoftwareTexture(source) {} + FSWCanvasTexture(FGameTexture* source); ~FSWCanvasTexture(); // Returns the whole texture, stored in column-major order diff --git a/src/rendering/swrenderer/textures/swcanvastexture.cpp b/src/rendering/swrenderer/textures/swcanvastexture.cpp index a9a946fce9..db9471774b 100644 --- a/src/rendering/swrenderer/textures/swcanvastexture.cpp +++ b/src/rendering/swrenderer/textures/swcanvastexture.cpp @@ -39,6 +39,21 @@ #include "m_alloc.h" #include "imagehelpers.h" +static TMap canvasMap; + +FSWCanvasTexture* GetSWCamTex(FCanvasTexture* camtex) +{ + auto p = canvasMap.CheckKey(camtex); + return p ? *p : nullptr; +} + +FSWCanvasTexture::FSWCanvasTexture(FGameTexture* source) : FSoftwareTexture(source) +{ + // The SW renderer needs to link the canvas textures, but let's do that outside the texture manager. + auto camtex = static_cast(source->GetTexture()); + canvasMap.Insert(camtex, this); +} + FSWCanvasTexture::~FSWCanvasTexture() {