From 82bd742ea3b21f8fe8fcd633d507b6008100e1ae Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 8 Dec 2018 12:42:35 +0100 Subject: [PATCH] - reworked how the software renderer manages its textures. * it's no longer the main texture objects managing the pixel buffer but FSoftwareTexture. * create proper spans for true color textures. The paletted spans only match if the image does not have any translucent pixels. * create proper warp textures instead of working off the paletted variants. As a side effect, caching of pixel buffers for texture composition is temporarily disabled, as it management of texture redirections. These things will be reimplemented once things progress further. The existing methods here had their share of serious issues that should be fixed. --- src/polyrenderer/poly_renderthread.cpp | 8 +- src/r_data/models/models_voxel.cpp | 8 +- src/r_utility.cpp | 2 +- src/swrenderer/r_renderthread.cpp | 8 +- src/swrenderer/textures/r_swtexture.cpp | 153 ++++++++++++++------- src/swrenderer/textures/r_swtexture.h | 70 +++++----- src/swrenderer/textures/warptexture.cpp | 30 ++-- src/textures/formats/automaptexture.cpp | 8 +- src/textures/formats/brightmaptexture.cpp | 8 -- src/textures/formats/buildtexture.cpp | 11 +- src/textures/formats/canvastexture.cpp | 12 +- src/textures/formats/ddstexture.cpp | 16 +-- src/textures/formats/emptytexture.cpp | 10 +- src/textures/formats/flattexture.cpp | 12 +- src/textures/formats/imgztexture.cpp | 10 +- src/textures/formats/jpegtexture.cpp | 13 +- src/textures/formats/multipatchtexture.cpp | 19 ++- src/textures/formats/patchtexture.cpp | 16 +-- src/textures/formats/pcxtexture.cpp | 23 ++-- src/textures/formats/pngtexture.cpp | 31 ++--- src/textures/formats/rawpagetexture.cpp | 10 +- src/textures/formats/shadertexture.cpp | 11 +- src/textures/formats/tgatexture.cpp | 9 +- src/textures/formats/worldtexture.cpp | 46 ------- src/textures/skyboxtexture.cpp | 27 +--- src/textures/skyboxtexture.h | 4 +- src/textures/texture.cpp | 67 ++++----- src/textures/texturemanager.cpp | 14 -- src/textures/textures.h | 35 ++--- src/v_font.cpp | 136 +++--------------- src/v_font.h | 1 + 31 files changed, 322 insertions(+), 506 deletions(-) diff --git a/src/polyrenderer/poly_renderthread.cpp b/src/polyrenderer/poly_renderthread.cpp index f9bc3831c..d5b7b6284 100644 --- a/src/polyrenderer/poly_renderthread.cpp +++ b/src/polyrenderer/poly_renderthread.cpp @@ -91,14 +91,18 @@ void PolyRenderThread::PrepareTexture(FSoftwareTexture *texture, FRenderStyle st std::unique_lock lock(loadmutex); - texture->GetPixels(style); const FSoftwareTextureSpan *spans; - texture->GetColumn(style, 0, &spans); if (PolyRenderer::Instance()->RenderTarget->IsBgra()) { texture->GetPixelsBgra(); texture->GetColumnBgra(0, &spans); } + else + { + bool alpha = !!(style.Flags & STYLEF_RedIsAlpha); + texture->GetPixels(alpha); + texture->GetColumn(alpha, 0, &spans); + } } void PolyRenderThread::PreparePolyObject(subsector_t *sub) diff --git a/src/r_data/models/models_voxel.cpp b/src/r_data/models/models_voxel.cpp index 6067e2f9e..dca6a4cd8 100644 --- a/src/r_data/models/models_voxel.cpp +++ b/src/r_data/models/models_voxel.cpp @@ -53,7 +53,7 @@ public: int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override; bool UseBasePalette() override { return false; } - uint8_t *MakeTexture(FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; protected: FVoxel *SourceVox; @@ -79,10 +79,10 @@ FVoxelTexture::FVoxelTexture(FVoxel *vox) // //=========================================================================== -uint8_t *FVoxelTexture::MakeTexture (FRenderStyle style) +TArray FVoxelTexture::Get8BitPixels(bool alphatex) { // GetPixels gets called when a translated palette is used so we still need to implement it here. - auto Pixels = new uint8_t[256]; + TArray Pixels(256, true); uint8_t *pp = SourceVox->Palette; if(pp != NULL) @@ -94,7 +94,7 @@ uint8_t *FVoxelTexture::MakeTexture (FRenderStyle style) pe.g = (pp[1] << 2) | (pp[1] >> 4); pe.b = (pp[2] << 2) | (pp[2] >> 4); // Alphatexture handling is just for completeness, but rather unlikely to be used ever. - Pixels[i] = (style.Flags & STYLEF_RedIsAlpha)? pe.r : ColorMatcher.Pick(pe); + Pixels[i] = alphatex? pe.r : ColorMatcher.Pick(pe); } } else diff --git a/src/r_utility.cpp b/src/r_utility.cpp index bce73181c..6adfbc5ac 100644 --- a/src/r_utility.cpp +++ b/src/r_utility.cpp @@ -1163,7 +1163,7 @@ void FCanvasTextureInfo::EmptyList () for (probe = List; probe != NULL; probe = next) { next = probe->Next; - probe->Texture->Unload(); + //probe->Texture->Unload(); delete probe; } List = NULL; diff --git a/src/swrenderer/r_renderthread.cpp b/src/swrenderer/r_renderthread.cpp index ae0837e6a..91c2c196c 100644 --- a/src/swrenderer/r_renderthread.cpp +++ b/src/swrenderer/r_renderthread.cpp @@ -106,14 +106,18 @@ namespace swrenderer std::unique_lock lock(loadmutex); - texture->GetPixels(style); const FSoftwareTextureSpan *spans; - texture->GetColumn(style, 0, &spans); if (Viewport->RenderTarget->IsBgra()) { texture->GetPixelsBgra(); texture->GetColumnBgra(0, &spans); } + else + { + bool alpha = !!(style.Flags & STYLEF_RedIsAlpha); + texture->GetPixels(alpha); + texture->GetColumn(alpha, 0, &spans); + } } void RenderThread::PreparePolyObject(subsector_t *sub) diff --git a/src/swrenderer/textures/r_swtexture.cpp b/src/swrenderer/textures/r_swtexture.cpp index 79560d5b2..8470227ce 100644 --- a/src/swrenderer/textures/r_swtexture.cpp +++ b/src/swrenderer/textures/r_swtexture.cpp @@ -39,7 +39,16 @@ #include "m_alloc.h" - +FSoftwareTexture *FTexture::GetSoftwareTexture() +{ + if (!SoftwareTexture) + { + if (bWarped) SoftwareTexture = new FWarpTexture(this, bWarped); + // else if (GetRedirect() != this) ... must be decided later. The current data structures make it hard to do this without creating a mess. + else SoftwareTexture = new FSoftwareTexture(this); + } + return SoftwareTexture; +} //========================================================================== // @@ -74,32 +83,31 @@ void FSoftwareTexture::CalcBitSize () HeightBits = i; } - //========================================================================== // // // //========================================================================== -const uint32_t *FSoftwareTexture::GetColumnBgra(unsigned int column, const FSoftwareTextureSpan **spans_out) +const uint8_t *FSoftwareTexture::GetPixels(int style) { - const uint32_t *pixels = GetPixelsBgra(); - if (pixels == nullptr) return nullptr; - - column %= GetWidth(); - - if (spans_out != nullptr) - GetColumn(DefaultRenderStyle(), column, spans_out); // This isn't the right way to create the spans. - return pixels + column * GetHeight(); + if (Pixels.Size() == 0 || CheckModified(style)) + { + Pixels = mSource->Get8BitPixels(style); + } + return Pixels.Data(); } +//========================================================================== +// +// +// +//========================================================================== + const uint32_t *FSoftwareTexture::GetPixelsBgra() { - if (PixelsBgra.Size() == 0 || CheckModified(DefaultRenderStyle())) + if (PixelsBgra.Size() == 0 || CheckModified(2)) { - if (!GetColumn(DefaultRenderStyle(), 0, nullptr)) - return nullptr; - FBitmap bitmap; bitmap.Create(GetWidth(), GetHeight()); mTexture->CopyTrueColorPixels(&bitmap, 0, 0); @@ -108,13 +116,86 @@ const uint32_t *FSoftwareTexture::GetPixelsBgra() return PixelsBgra.Data(); } +//========================================================================== +// +// +// +//========================================================================== + +const uint8_t *FSoftwareTexture::GetColumn(int index, unsigned int column, const FSoftwareTextureSpan **spans_out) +{ + auto Pixeldata = GetPixels(index); + if ((unsigned)column >= (unsigned)GetWidth()) + { + if (WidthMask + 1 == GetWidth()) + { + column &= WidthMask; + } + else + { + column %= GetWidth(); + } + } + if (spans_out != nullptr) + { + if (Spandata[index] == nullptr) + { + Spandata[index] = CreateSpans(Pixeldata); + } + *spans_out = Spandata[index][column]; + } + return Pixeldata + column * GetHeight(); +} + //========================================================================== // // // //========================================================================== -FSoftwareTextureSpan **FSoftwareTexture::CreateSpans (const uint8_t *pixels) +const uint32_t *FSoftwareTexture::GetColumnBgra(unsigned int column, const FSoftwareTextureSpan **spans_out) +{ + auto Pixeldata = GetPixelsBgra(); + if ((unsigned)column >= (unsigned)GetWidth()) + { + if (WidthMask + 1 == GetWidth()) + { + column &= WidthMask; + } + else + { + column %= GetWidth(); + } + } + if (spans_out != nullptr) + { + if (Spandata[2] == nullptr) + { + Spandata[2] = CreateSpans(Pixeldata); + } + *spans_out = Spandata[2][column]; + } + return Pixeldata + column * GetHeight(); +} + +//========================================================================== +// +// +// +//========================================================================== + +static bool isTranslucent(uint8_t val) +{ + return val == 0; +} + +static bool isTranslucent(uint32_t val) +{ + return (val & 0xff000000) == 0; +} + +template +FSoftwareTextureSpan **FSoftwareTexture::CreateSpans (const T *pixels) { FSoftwareTextureSpan **spans, *span; @@ -136,7 +217,7 @@ FSoftwareTextureSpan **FSoftwareTexture::CreateSpans (const uint8_t *pixels) int numcols = GetWidth(); int numrows = GetHeight(); int numspans = numcols; // One span to terminate each column - const uint8_t *data_p; + const T *data_p; bool newspan; int x, y; @@ -149,7 +230,7 @@ FSoftwareTextureSpan **FSoftwareTexture::CreateSpans (const uint8_t *pixels) for (y = numrows; y > 0; --y) { - if (*data_p++ == 0) + if (isTranslucent(*data_p++)) { if (!newspan) { @@ -174,7 +255,7 @@ FSoftwareTextureSpan **FSoftwareTexture::CreateSpans (const uint8_t *pixels) spans[x] = span; for (y = 0; y < numrows; ++y) { - if (*data_p++ == 0) + if (isTranslucent(*data_p++)) { if (!newspan) { @@ -441,41 +522,9 @@ void FSoftwareTexture::GenerateBgraMipmapsFast() // //========================================================================== -const uint8_t *FSoftwareTexture::GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out) -{ - int index = !!(style.Flags & STYLEF_RedIsAlpha); - auto Pixeldata = GetPixels(style); - if ((unsigned)column >= (unsigned)GetWidth()) - { - if (WidthMask + 1 == GetWidth()) - { - column &= WidthMask; - } - else - { - column %= GetWidth(); - } - } - if (spans_out != nullptr) - { - if (Spandata[index] == nullptr) - { - Spandata[index] = CreateSpans (Pixeldata); - } - *spans_out = Spandata[index][column]; - } - return Pixeldata + column*GetHeight(); -} - -//========================================================================== -// -// -// -//========================================================================== - void FSoftwareTexture::FreeAllSpans() { - for(int i = 0; i < 2; i++) + for(int i = 0; i < 3; i++) { if (Spandata[i] != nullptr) { diff --git a/src/swrenderer/textures/r_swtexture.h b/src/swrenderer/textures/r_swtexture.h index 273a748db..af65003bf 100644 --- a/src/swrenderer/textures/r_swtexture.h +++ b/src/swrenderer/textures/r_swtexture.h @@ -15,13 +15,17 @@ class FSoftwareTexture protected: FTexture *mTexture; FTexture *mSource; + TArray Pixels; TArray PixelsBgra; - FSoftwareTextureSpan **Spandata[2] = { nullptr, nullptr }; + FSoftwareTextureSpan **Spandata[3] = { }; uint8_t WidthBits = 0, HeightBits = 0; uint16_t WidthMask = 0; + void FreeAllSpans(); + template FSoftwareTextureSpan **CreateSpans(const T *pixels); + void FreeSpans(FSoftwareTextureSpan **spans); + void CalcBitSize(); - public: FSoftwareTexture(FTexture *tex) { @@ -34,7 +38,7 @@ public: { FreeAllSpans(); } - + FTexture *GetTexture() const { return mTexture; @@ -51,7 +55,6 @@ public: return mTexture->bMasked; } - void CalcBitSize(); bool UseBasePalette() const { return mTexture->UseBasePalette(); } int GetSkyOffset() const { return mTexture->GetSkyOffset(); } PalEntry GetSkyCapColor(bool bottom) const { return mTexture->GetSkyCapColor(bottom); } @@ -93,35 +96,25 @@ public: DVector2 GetScale() const { return mTexture->Scale; } - // Returns the whole texture, stored in column-major order - virtual const uint8_t *GetPixels(FRenderStyle style) - { - return mTexture->Get8BitPixels(style); - } - void Unload() { - mTexture->Unload(); + Pixels.Reset(); PixelsBgra.Reset(); } // Returns true if the next call to GetPixels() will return an image different from the // last call to GetPixels(). This should be considered valid only if a call to CheckModified() // is immediately followed by a call to GetPixels(). - virtual bool CheckModified (FRenderStyle style) { return false; } + virtual bool CheckModified (int which) { return false; } void GenerateBgraFromBitmap(const FBitmap &bitmap); void CreatePixelsBgraWithMipmaps(); void GenerateBgraMipmaps(); void GenerateBgraMipmapsFast(); int MipmapLevels(); - void FreeAllSpans(); - - FSoftwareTextureSpan **CreateSpans (const uint8_t *pixels); - void FreeSpans (FSoftwareTextureSpan **spans); // Returns a single column of the texture - virtual const uint8_t *GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out); + virtual const uint8_t *GetColumn(int style, unsigned int column, const FSoftwareTextureSpan **spans_out); // Returns a single column of the texture, in BGRA8 format virtual const uint32_t *GetColumnBgra(unsigned int column, const FSoftwareTextureSpan **spans_out); @@ -129,39 +122,42 @@ public: // Returns the whole texture, stored in column-major order, in BGRA8 format virtual const uint32_t *GetPixelsBgra(); - + // Returns the whole texture, stored in column-major order + virtual const uint8_t *GetPixels(int style); + + const uint8_t *GetPixels(FRenderStyle style) + { + bool alpha = !!(style.Flags & STYLEF_RedIsAlpha); + return GetPixels(alpha); + } + + const uint8_t *GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out) + { + bool alpha = !!(style.Flags & STYLEF_RedIsAlpha); + return GetColumn(alpha, column, spans_out); + } + }; // A texture that returns a wiggly version of another texture. class FWarpTexture : public FSoftwareTexture { TArray WarpedPixels[2]; + TArray WarpedPixelsRgba; + int bWarped = 0; + uint64_t GenTime[3] = { 0, 0, 0 }; + int WidthOffsetMultiplier, HeightOffsetMultiplier; // [mxd] + public: FWarpTexture (FTexture *source, int warptype); const uint32_t *GetPixelsBgra() override; - bool CheckModified (FRenderStyle) override; + const uint8_t *GetPixels(int style) override; + bool CheckModified (int which) override; - float GetSpeed() const { return mTexture->shaderspeed; } +private: - uint64_t GenTime[2] = { 0, 0 }; - uint64_t GenTimeBgra = 0; - int WidthOffsetMultiplier, HeightOffsetMultiplier; // [mxd] -protected: - - const uint8_t *GetPixels(FRenderStyle style); int NextPo2 (int v); // [mxd] void SetupMultipliers (int width, int height); // [mxd] }; - - -inline FSoftwareTexture *FTexture::GetSoftwareTexture() -{ - if (!SoftwareTexture) - { - if (bWarped) SoftwareTexture = new FWarpTexture(this, bWarped); - else SoftwareTexture = new FSoftwareTexture(this); - } - return SoftwareTexture; -} diff --git a/src/swrenderer/textures/warptexture.cpp b/src/swrenderer/textures/warptexture.cpp index 294bf694f..22cbcaf7d 100644 --- a/src/swrenderer/textures/warptexture.cpp +++ b/src/swrenderer/textures/warptexture.cpp @@ -49,38 +49,32 @@ FWarpTexture::FWarpTexture (FTexture *source, int warptype) bWarped = warptype; } -bool FWarpTexture::CheckModified (FRenderStyle style) +bool FWarpTexture::CheckModified (int style) { - return screen->FrameTime != GenTime[!!(style.Flags & STYLEF_RedIsAlpha)]; + return screen->FrameTime != GenTime[style]; } const uint32_t *FWarpTexture::GetPixelsBgra() { - auto Pixels = GetPixels(DefaultRenderStyle()); - if (PixelsBgra.Size() == 0 || GenTime[0] != GenTimeBgra) + uint64_t time = screen->FrameTime; + if (time != GenTime[2]) { - CreatePixelsBgraWithMipmaps(); - for (int i = 0; i < GetWidth() * GetHeight(); i++) - { - if (Pixels[i] != 0) - PixelsBgra[i] = 0xff000000 | GPalette.BaseColors[Pixels[i]].d; - else - PixelsBgra[i] = 0; - } - GenerateBgraMipmapsFast(); - GenTimeBgra = GenTime[0]; + auto otherpix = FSoftwareTexture::GetPixelsBgra(); + WarpedPixelsRgba.Resize(GetWidth() * GetHeight()); + WarpBuffer(WarpedPixelsRgba.Data(), otherpix, GetWidth(), GetHeight(), WidthOffsetMultiplier, HeightOffsetMultiplier, time, mTexture->shaderspeed, bWarped); + FreeAllSpans(); + GenTime[2] = time; } - return PixelsBgra.Data(); + return WarpedPixelsRgba.Data(); } -const uint8_t *FWarpTexture::GetPixels(FRenderStyle style) +const uint8_t *FWarpTexture::GetPixels(int index) { - int index = !!(style.Flags & STYLEF_RedIsAlpha); uint64_t time = screen->FrameTime; if (time != GenTime[index]) { - const uint8_t *otherpix = FSoftwareTexture::GetPixels(style); + const uint8_t *otherpix = FSoftwareTexture::GetPixels(index); WarpedPixels[index].Resize(GetWidth() * GetHeight()); WarpBuffer(WarpedPixels[index].Data(), otherpix, GetWidth(), GetHeight(), WidthOffsetMultiplier, HeightOffsetMultiplier, time, mTexture->shaderspeed, bWarped); FreeAllSpans(); diff --git a/src/textures/formats/automaptexture.cpp b/src/textures/formats/automaptexture.cpp index b779da1d9..4eb4ec353 100644 --- a/src/textures/formats/automaptexture.cpp +++ b/src/textures/formats/automaptexture.cpp @@ -50,7 +50,7 @@ class FAutomapTexture : public FWorldTexture { public: FAutomapTexture(int lumpnum); - uint8_t *MakeTexture (FRenderStyle style); + TArray Get8BitPixels(bool alphatex); }; @@ -88,15 +88,15 @@ FAutomapTexture::FAutomapTexture (int lumpnum) // //========================================================================== -uint8_t *FAutomapTexture::MakeTexture (FRenderStyle style) +TArray FAutomapTexture::Get8BitPixels(bool alphatex) { int x, y; FMemLump data = Wads.ReadLump (SourceLump); const uint8_t *indata = (const uint8_t *)data.GetMem(); - auto Pixels = new uint8_t[Width * Height]; + TArray Pixels(Width * Height, true); - const uint8_t *remap = GetRemap(style); + const uint8_t *remap = GetRemap(alphatex); for (x = 0; x < Width; ++x) { for (y = 0; y < Height; ++y) diff --git a/src/textures/formats/brightmaptexture.cpp b/src/textures/formats/brightmaptexture.cpp index c649f3e17..314ac6f74 100644 --- a/src/textures/formats/brightmaptexture.cpp +++ b/src/textures/formats/brightmaptexture.cpp @@ -45,7 +45,6 @@ class FBrightmapTexture : public FWorldTexture public: FBrightmapTexture (FTexture *source); - uint8_t *MakeTexture(FRenderStyle style) override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override; bool UseBasePalette() override { return false; } @@ -74,13 +73,6 @@ FBrightmapTexture::FBrightmapTexture (FTexture *source) SourceLump = -1; } -uint8_t *FBrightmapTexture::MakeTexture(FRenderStyle style) -{ - // This function is only necessary to satisfy the parent class's interface. - // This will never be called. - return nullptr; -} - int FBrightmapTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) { SourcePic->CopyTrueColorTranslated(bmp, x, y, rotate, TexMan.GlobalBrightmap.Palette); diff --git a/src/textures/formats/buildtexture.cpp b/src/textures/formats/buildtexture.cpp index 69ec03d2c..9d4fa2fdc 100644 --- a/src/textures/formats/buildtexture.cpp +++ b/src/textures/formats/buildtexture.cpp @@ -56,7 +56,7 @@ class FBuildTexture : public FWorldTexture { public: FBuildTexture (const FString &pathprefix, int tilenum, const uint8_t *pixels, int translation, int width, int height, int left, int top); - uint8_t *MakeTexture(FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL) override; bool UseBasePalette() override { return false; } FTextureFormat GetFormat() override { return TEX_RGB; } @@ -76,7 +76,6 @@ protected: FBuildTexture::FBuildTexture(const FString &pathprefix, int tilenum, const uint8_t *pixels, int translation, int width, int height, int left, int top) : RawPixels (pixels), Translation(translation) { - PixelsAreStatic = 3; Width = width; Height = height; _LeftOffset[1] = _LeftOffset[0] = left; @@ -85,16 +84,16 @@ FBuildTexture::FBuildTexture(const FString &pathprefix, int tilenum, const uint8 UseType = ETextureType::Override; } -uint8_t *FBuildTexture::MakeTexture(FRenderStyle style) +TArray FBuildTexture::Get8BitPixels(bool alphatex) { - auto Pixels = new uint8_t[Width * Height]; + TArray Pixels(Width * Height, true); FRemapTable *Remap = translationtables[TRANSLATION_Standard][Translation]; for (int i = 0; i < Width*Height; i++) { auto c = RawPixels[i]; - Pixels[i] = (style.Flags & STYLEF_RedIsAlpha) ? Remap->Palette[c].Luminance() : Remap->Remap[c]; + Pixels[i] = alphatex ? Remap->Palette[c].Luminance() : Remap->Remap[c]; } - return (uint8_t*)Pixels; + return Pixels; } int FBuildTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) diff --git a/src/textures/formats/canvastexture.cpp b/src/textures/formats/canvastexture.cpp index 683e9f9d6..da6358928 100644 --- a/src/textures/formats/canvastexture.cpp +++ b/src/textures/formats/canvastexture.cpp @@ -51,12 +51,9 @@ FCanvasTexture::FCanvasTexture (const char *name, int width, int height) bPixelsAllocated = false; } -FCanvasTexture::~FCanvasTexture () -{ - Unload (); -} -const uint8_t *FCanvasTexture::Get8BitPixels (FRenderStyle style) +#if 0 +const uint8_t *FCanvasTexture::Get8BitPixels(bool alphatex) { bNeedsUpdate = true; if (Canvas == NULL) @@ -66,7 +63,6 @@ const uint8_t *FCanvasTexture::Get8BitPixels (FRenderStyle style) return Pixels; } -#if 0 const uint32_t *FCanvasTexture::GetPixelsBgra() { bNeedsUpdate = true; @@ -76,7 +72,6 @@ const uint32_t *FCanvasTexture::GetPixelsBgra() } return PixelsBgra; } -#endif void FCanvasTexture::MakeTexture (FRenderStyle) // This ignores the render style because making it work as alpha texture is impractical. { @@ -97,6 +92,7 @@ void FCanvasTexture::MakeTexture (FRenderStyle) // This ignores the render style memset (Pixels, 0, Width*Height/2); memset (Pixels+Width*Height/2, 255, Width*Height/2); } +#endif void FCanvasTexture::MakeTextureBgra() { @@ -118,6 +114,7 @@ void FCanvasTexture::MakeTextureBgra() memset(PixelsBgra + Width*Height / 2, 255, Width*Height / 2 * 4); } +#if 0 void FCanvasTexture::Unload () { if (bPixelsAllocated) @@ -148,6 +145,7 @@ void FCanvasTexture::Unload () FTexture::Unload(); } +#endif bool FCanvasTexture::CheckModified (FRenderStyle) { diff --git a/src/textures/formats/ddstexture.cpp b/src/textures/formats/ddstexture.cpp index 04f36adbb..5a2007e66 100644 --- a/src/textures/formats/ddstexture.cpp +++ b/src/textures/formats/ddstexture.cpp @@ -163,7 +163,7 @@ public: FDDSTexture (FileReader &lump, int lumpnum, void *surfdesc); FTextureFormat GetFormat () override; - uint8_t *MakeTexture(FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; protected: uint32_t Format; @@ -398,30 +398,30 @@ FTextureFormat FDDSTexture::GetFormat() // //========================================================================== -uint8_t *FDDSTexture::MakeTexture (FRenderStyle style) +TArray FDDSTexture::Get8BitPixels(bool alphatex) { auto lump = Wads.OpenLumpReader (SourceLump); - auto Pixels = new uint8_t[Width*Height]; + TArray Pixels(Width*Height, true); lump.Seek (sizeof(DDSURFACEDESC2) + 4, FileReader::SeekSet); - int pmode = (style.Flags & STYLEF_RedIsAlpha) ? PIX_Alphatex : PIX_Palette; + int pmode = alphatex ? PIX_Alphatex : PIX_Palette; if (Format >= 1 && Format <= 4) // RGB: Format is # of bytes per pixel { - ReadRGB (lump, Pixels, pmode); + ReadRGB (lump, Pixels.Data(), pmode); } else if (Format == ID_DXT1) { - DecompressDXT1 (lump, Pixels, pmode); + DecompressDXT1 (lump, Pixels.Data(), pmode); } else if (Format == ID_DXT3 || Format == ID_DXT2) { - DecompressDXT3 (lump, Format == ID_DXT2, Pixels, pmode); + DecompressDXT3 (lump, Format == ID_DXT2, Pixels.Data(), pmode); } else if (Format == ID_DXT5 || Format == ID_DXT4) { - DecompressDXT5 (lump, Format == ID_DXT4, Pixels, pmode); + DecompressDXT5 (lump, Format == ID_DXT4, Pixels.Data(), pmode); } return Pixels; } diff --git a/src/textures/formats/emptytexture.cpp b/src/textures/formats/emptytexture.cpp index d49cdbf09..d793c9fb3 100644 --- a/src/textures/formats/emptytexture.cpp +++ b/src/textures/formats/emptytexture.cpp @@ -48,10 +48,9 @@ class FEmptyTexture : public FWorldTexture { - uint8_t Pixel = 0; public: FEmptyTexture (int lumpnum); - uint8_t *MakeTexture(FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; }; //========================================================================== @@ -82,7 +81,6 @@ FEmptyTexture::FEmptyTexture (int lumpnum) { bMasked = true; Width = Height = 1; - PixelsAreStatic = 3; } //========================================================================== @@ -91,8 +89,10 @@ FEmptyTexture::FEmptyTexture (int lumpnum) // //========================================================================== -uint8_t *FEmptyTexture::MakeTexture(FRenderStyle style) +TArray FEmptyTexture::Get8BitPixels(bool alphatex) { - return &Pixel; + TArray Pixel(1, true); + Pixel[0] = 0; + return Pixel; } diff --git a/src/textures/formats/flattexture.cpp b/src/textures/formats/flattexture.cpp index 0e29defc1..7f1dfbd52 100644 --- a/src/textures/formats/flattexture.cpp +++ b/src/textures/formats/flattexture.cpp @@ -48,7 +48,7 @@ class FFlatTexture : public FWorldTexture { public: FFlatTexture (int lumpnum); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; }; @@ -101,16 +101,16 @@ FFlatTexture::FFlatTexture (int lumpnum) // //========================================================================== -uint8_t *FFlatTexture::MakeTexture (FRenderStyle style) +TArray FFlatTexture::Get8BitPixels(bool alphatex) { auto lump = Wads.OpenLumpReader (SourceLump); - auto Pixels = new uint8_t[Width*Height]; - auto numread = lump.Read (Pixels, Width*Height); + TArray Pixels(Width*Height, true); + auto numread = lump.Read (Pixels.Data(), Width*Height); if (numread < Width*Height) { - memset (Pixels + numread, 0xBB, Width*Height - numread); + memset (Pixels.Data() + numread, 0xBB, Width*Height - numread); } - FTexture::FlipSquareBlockRemap(Pixels, Width, Height, GetRemap(style)); + FTexture::FlipSquareBlockRemap(Pixels.Data(), Width, Height, GetRemap(alphatex)); return Pixels; } diff --git a/src/textures/formats/imgztexture.cpp b/src/textures/formats/imgztexture.cpp index 2d0894de3..108baf4b9 100644 --- a/src/textures/formats/imgztexture.cpp +++ b/src/textures/formats/imgztexture.cpp @@ -66,7 +66,7 @@ class FIMGZTexture : public FWorldTexture public: FIMGZTexture (int lumpnum, uint16_t w, uint16_t h, int16_t l, int16_t t, bool isalpha); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override; bool UseBasePalette() override { return !isalpha; } @@ -121,7 +121,7 @@ FIMGZTexture::FIMGZTexture (int lumpnum, uint16_t w, uint16_t h, int16_t l, int1 // //========================================================================== -uint8_t *FIMGZTexture::MakeTexture (FRenderStyle style) +TArray FIMGZTexture::Get8BitPixels(bool alphatex) { FMemLump lump = Wads.ReadLump (SourceLump); const ImageHeader *imgz = (const ImageHeader *)lump.GetMem(); @@ -131,10 +131,10 @@ uint8_t *FIMGZTexture::MakeTexture (FRenderStyle style) int dest_adv = Height; int dest_rew = Width * Height - 1; - auto Pixels = new uint8_t[Width*Height]; - dest_p = Pixels; + TArray Pixels(Width*Height, true); + dest_p = Pixels.Data(); - const uint8_t *remap = GetRemap(style, isalpha); + const uint8_t *remap = GetRemap(alphatex, isalpha); // Convert the source image from row-major to column-major format and remap it if (!imgz->Compression) diff --git a/src/textures/formats/jpegtexture.cpp b/src/textures/formats/jpegtexture.cpp index 56165a731..8da8a6729 100644 --- a/src/textures/formats/jpegtexture.cpp +++ b/src/textures/formats/jpegtexture.cpp @@ -186,7 +186,7 @@ public: FTextureFormat GetFormat () override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL) override; bool UseBasePalette() override; - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; }; //========================================================================== @@ -272,17 +272,16 @@ FTextureFormat FJPEGTexture::GetFormat() // //========================================================================== -uint8_t *FJPEGTexture::MakeTexture (FRenderStyle style) +TArray FJPEGTexture::Get8BitPixels(bool doalpha) { auto lump = Wads.OpenLumpReader (SourceLump); JSAMPLE *buff = NULL; - bool doalpha = !!(style.Flags & STYLEF_RedIsAlpha); jpeg_decompress_struct cinfo; jpeg_error_mgr jerr; - auto Pixels = new uint8_t[Width * Height]; - memset (Pixels, 0xBA, Width * Height); + TArray Pixels(Width * Height, true); + memset (Pixels.Data(), 0xBA, Width * Height); cinfo.err = jpeg_std_error(&jerr); cinfo.err->output_message = JPEG_OutputMessage; @@ -311,7 +310,7 @@ uint8_t *FJPEGTexture::MakeTexture (FRenderStyle style) { int num_scanlines = jpeg_read_scanlines(&cinfo, &buff, 1); uint8_t *in = buff; - uint8_t *out = Pixels + y; + uint8_t *out = Pixels.Data() + y; switch (cinfo.out_color_space) { case JCS_RGB: @@ -325,7 +324,7 @@ uint8_t *FJPEGTexture::MakeTexture (FRenderStyle style) case JCS_GRAYSCALE: { - auto remap = GetRemap(style, true); + auto remap = GetRemap(doalpha, true); for (int x = Width; x > 0; --x) { *out = remap[in[0]]; diff --git a/src/textures/formats/multipatchtexture.cpp b/src/textures/formats/multipatchtexture.cpp index 226f85de9..b546b44ac 100644 --- a/src/textures/formats/multipatchtexture.cpp +++ b/src/textures/formats/multipatchtexture.cpp @@ -195,10 +195,10 @@ protected: bool bRedirect; bool bTranslucentPatches; - uint8_t *MakeTexture (FRenderStyle style); + TArray MakeTexture (bool alphatex); // The getters must optionally redirect if it's a simple one-patch texture. - const uint8_t *Get8BitPixels(FRenderStyle style) override { return bRedirect ? Parts->Texture->Get8BitPixels(style) : FWorldTexture::Get8BitPixels(style); } + TArray Get8BitPixels(bool alphatex) override { return bRedirect ? Parts->Texture->Get8BitPixels(alphatex) : MakeTexture(alphatex); } private: @@ -305,7 +305,6 @@ FMultiPatchTexture::FMultiPatchTexture (const void *texdef, FPatchLookup *patchl FMultiPatchTexture::~FMultiPatchTexture () { - Unload (); if (Parts != NULL) { for(int i=0; i FMultiPatchTexture::MakeTexture (bool alphatex) { int numpix = Width * Height; uint8_t blendwork[256]; bool buildrgb = bComplex; - auto Pixels = new uint8_t[numpix]; - memset (Pixels, 0, numpix); + TArray Pixels(numpix, true); + memset (Pixels.Data(), 0, numpix); - if (style.Flags & STYLEF_RedIsAlpha) + if (alphatex) { buildrgb = !UseBasePalette(); } @@ -434,7 +433,7 @@ uint8_t *FMultiPatchTexture::MakeTexture (FRenderStyle style) { trans = GetBlendMap(Parts[i].Blend, blendwork); } - Parts[i].Texture->CopyToBlock (Pixels, Width, Height, Parts[i].OriginX, Parts[i].OriginY, Parts[i].Rotate, trans, style); + Parts[i].Texture->CopyToBlock (Pixels.Data(), Width, Height, Parts[i].OriginX, Parts[i].OriginY, Parts[i].Rotate, trans, alphatex); } } } @@ -448,12 +447,12 @@ uint8_t *FMultiPatchTexture::MakeTexture (FRenderStyle style) for(int y = 0; y < Height; y++) { uint8_t *in = buffer + Width * y * 4; - uint8_t *out = Pixels + y; + uint8_t *out = Pixels.Data() + y; for (int x = 0; x < Width; x++) { if (*out == 0 && in[3] != 0) { - *out = RGBToPalette(style, in[2], in[1], in[0]); + *out = RGBToPalette(alphatex, in[2], in[1], in[0]); } out += Height; in += 4; diff --git a/src/textures/formats/patchtexture.cpp b/src/textures/formats/patchtexture.cpp index ed6630ffb..0b18f837f 100644 --- a/src/textures/formats/patchtexture.cpp +++ b/src/textures/formats/patchtexture.cpp @@ -62,7 +62,7 @@ class FPatchTexture : public FWorldTexture bool isalpha = false; public: FPatchTexture (int lumpnum, patch_t *header, bool isalphatex); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override; void DetectBadPatches(); @@ -167,7 +167,7 @@ FPatchTexture::FPatchTexture (int lumpnum, patch_t * header, bool isalphatex) // //========================================================================== -uint8_t *FPatchTexture::MakeTexture (FRenderStyle style) +TArray FPatchTexture::Get8BitPixels(bool alphatex) { uint8_t *remap, remaptable[256]; int numspans; @@ -179,7 +179,7 @@ uint8_t *FPatchTexture::MakeTexture (FRenderStyle style) maxcol = (const column_t *)((const uint8_t *)patch + Wads.LumpLength (SourceLump) - 3); - remap = GetRemap(style, isalpha); + remap = GetRemap(alphatex, isalpha); // Special case for skies if (bNoRemap0 && remap == GPalette.Remap) { @@ -190,11 +190,11 @@ uint8_t *FPatchTexture::MakeTexture (FRenderStyle style) if (badflag) { - auto Pixels = new uint8_t[Width * Height]; + TArray Pixels(Width * Height, true); uint8_t *out; // Draw the image to the buffer - for (x = 0, out = Pixels; x < Width; ++x) + for (x = 0, out = Pixels.Data(); x < Width; ++x) { const uint8_t *in = (const uint8_t *)patch + LittleLong(patch->columnofs[x]) + 3; @@ -211,13 +211,13 @@ uint8_t *FPatchTexture::MakeTexture (FRenderStyle style) numspans = Width; - auto Pixels = new uint8_t[numpix]; - memset (Pixels, 0, numpix); + TArray Pixels(numpix, true); + memset (Pixels.Data(), 0, numpix); // Draw the image to the buffer for (x = 0; x < Width; ++x) { - uint8_t *outtop = Pixels + x*Height; + uint8_t *outtop = Pixels.Data() + x*Height; const column_t *column = (const column_t *)((const uint8_t *)patch + LittleLong(patch->columnofs[x])); int top = -1; diff --git a/src/textures/formats/pcxtexture.cpp b/src/textures/formats/pcxtexture.cpp index f97cf03b8..f144e7d8f 100644 --- a/src/textures/formats/pcxtexture.cpp +++ b/src/textures/formats/pcxtexture.cpp @@ -94,7 +94,7 @@ protected: void ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr); void ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr, int planes); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; }; @@ -370,19 +370,18 @@ void FPCXTexture::ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr // //========================================================================== -uint8_t *FPCXTexture::MakeTexture(FRenderStyle style) +TArray FPCXTexture::Get8BitPixels(bool alphatex) { uint8_t PaletteMap[256]; PCXHeader header; int bitcount; - bool alphatex = !!(style.Flags & STYLEF_RedIsAlpha); auto lump = Wads.OpenLumpReader(SourceLump); lump.Read(&header, sizeof(header)); bitcount = header.bitsPerPixel * header.numColorPlanes; - auto Pixels = new uint8_t[Width*Height]; + TArray Pixels(Width*Height, true); if (bitcount < 24) { @@ -394,7 +393,7 @@ uint8_t *FPCXTexture::MakeTexture(FRenderStyle style) case 1: PaletteMap[0] = alphatex? 0 : GrayMap[0]; PaletteMap[1] = alphatex? 255 : GrayMap[255]; - ReadPCX1bit (Pixels, lump, &header); + ReadPCX1bit (Pixels.Data(), lump, &header); break; case 4: @@ -402,7 +401,7 @@ uint8_t *FPCXTexture::MakeTexture(FRenderStyle style) { PaletteMap[i] = RGBToPalettePrecise(alphatex, header.palette[i * 3], header.palette[i * 3 + 1], header.palette[i * 3 + 2]); } - ReadPCX4bits (Pixels, lump, &header); + ReadPCX4bits (Pixels.Data(), lump, &header); break; } } @@ -420,19 +419,17 @@ uint8_t *FPCXTexture::MakeTexture(FRenderStyle style) PaletteMap[i] = RGBToPalettePrecise(alphatex, r, g, b); } lump.Seek(sizeof(header), FileReader::SeekSet); - ReadPCX8bits (Pixels, lump, &header); + ReadPCX8bits (Pixels.Data(), lump, &header); } if (Width == Height) { - FlipSquareBlockRemap(Pixels, Width, Height, PaletteMap); + FlipSquareBlockRemap(Pixels.Data(), Width, Height, PaletteMap); } else { - uint8_t *newpix = new uint8_t[Width*Height]; - FlipNonSquareBlockRemap (newpix, Pixels, Width, Height, Width, PaletteMap); - uint8_t *oldpix = Pixels; - Pixels = newpix; - delete[] oldpix; + TArray newpix(Width*Height, true); + FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); + return newpix; } } else diff --git a/src/textures/formats/pngtexture.cpp b/src/textures/formats/pngtexture.cpp index 2071fedf0..5c77ce5de 100644 --- a/src/textures/formats/pngtexture.cpp +++ b/src/textures/formats/pngtexture.cpp @@ -55,7 +55,7 @@ public: FTextureFormat GetFormat () override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL) override; bool UseBasePalette() override; - uint8_t *MakeTexture(FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; protected: void ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap); @@ -389,11 +389,10 @@ void FPNGTexture::ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap) // //========================================================================== -uint8_t *FPNGTexture::MakeTexture (FRenderStyle style) +TArray FPNGTexture::Get8BitPixels(bool alphatex) { FileReader *lump; FileReader lfr; - bool alphatex = !!(style.Flags & STYLEF_RedIsAlpha); if (SourceLump >= 0) { @@ -405,10 +404,10 @@ uint8_t *FPNGTexture::MakeTexture (FRenderStyle style) lump = &fr; } - auto Pixels = new uint8_t[Width*Height]; + TArray Pixels(Width*Height, true); if (StartOfIDAT == 0) { - memset (Pixels, 0x99, Width*Height); + memset (Pixels.Data(), 0x99, Width*Height); } else { @@ -419,45 +418,43 @@ uint8_t *FPNGTexture::MakeTexture (FRenderStyle style) if (ColorType == 0 || ColorType == 3) /* Grayscale and paletted */ { - M_ReadIDAT (*lump, Pixels, Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); + M_ReadIDAT (*lump, Pixels.Data(), Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); if (Width == Height) { if (!alphatex) { - FTexture::FlipSquareBlockRemap (Pixels, Width, Height, PaletteMap); + FTexture::FlipSquareBlockRemap (Pixels.Data(), Width, Height, PaletteMap); } else if (ColorType == 0) { - FTexture::FlipSquareBlock (Pixels, Width, Height); + FTexture::FlipSquareBlock (Pixels.Data(), Width, Height); } else { uint8_t alpharemap[256]; ReadAlphaRemap(lump, alpharemap); - FTexture::FlipSquareBlockRemap(Pixels, Width, Height, alpharemap); + FTexture::FlipSquareBlockRemap(Pixels.Data(), Width, Height, alpharemap); } } else { - uint8_t *newpix = new uint8_t[Width*Height]; + TArray newpix(Width*Height, true); if (!alphatex) { - FTexture::FlipNonSquareBlockRemap (newpix, Pixels, Width, Height, Width, PaletteMap); + FTexture::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); } else if (ColorType == 0) { - FTexture::FlipNonSquareBlock (newpix, Pixels, Width, Height, Width); + FTexture::FlipNonSquareBlock (newpix.Data(), Pixels.Data(), Width, Height, Width); } else { uint8_t alpharemap[256]; ReadAlphaRemap(lump, alpharemap); - FTexture::FlipNonSquareBlockRemap(newpix, Pixels, Width, Height, Width, alpharemap); + FTexture::FlipNonSquareBlockRemap(newpix.Data(), Pixels.Data(), Width, Height, Width, alpharemap); } - uint8_t *oldpix = Pixels; - Pixels = newpix; - delete[] oldpix; + return newpix; } } else /* RGB and/or Alpha present */ @@ -469,7 +466,7 @@ uint8_t *FPNGTexture::MakeTexture (FRenderStyle style) M_ReadIDAT (*lump, tempix, Width, Height, Width*bytesPerPixel, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); in = tempix; - out = Pixels; + out = Pixels.Data(); // Convert from source format to paletted, column-major. // Formats with alpha maps are reduced to only 1 bit of alpha. diff --git a/src/textures/formats/rawpagetexture.cpp b/src/textures/formats/rawpagetexture.cpp index 9a829acc3..78b48cfac 100644 --- a/src/textures/formats/rawpagetexture.cpp +++ b/src/textures/formats/rawpagetexture.cpp @@ -52,7 +52,7 @@ class FRawPageTexture : public FWorldTexture int mPaletteLump = -1; public: FRawPageTexture (int lumpnum); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override; }; @@ -174,17 +174,17 @@ FRawPageTexture::FRawPageTexture (int lumpnum) // //========================================================================== -uint8_t *FRawPageTexture::MakeTexture (FRenderStyle style) +TArray FRawPageTexture::Get8BitPixels(bool alphatex) { FMemLump lump = Wads.ReadLump (SourceLump); const uint8_t *source = (const uint8_t *)lump.GetMem(); const uint8_t *source_p = source; uint8_t *dest_p; - auto Pixels = new uint8_t[Width*Height]; - dest_p = Pixels; + TArray Pixels(Width*Height, true); + dest_p = Pixels.Data(); - const uint8_t *remap = GetRemap(style); + const uint8_t *remap = GetRemap(alphatex); // This does not handle the custom palette. // User maps are encouraged to use a real image format when replacing E2END and the original could never be used anywhere else. diff --git a/src/textures/formats/shadertexture.cpp b/src/textures/formats/shadertexture.cpp index 123ddd223..e21bf4f24 100644 --- a/src/textures/formats/shadertexture.cpp +++ b/src/textures/formats/shadertexture.cpp @@ -53,7 +53,6 @@ public: Height = vertical ? 256 : 2; bMasked = false; bTranslucent = false; - PixelsAreStatic = 2; // The alpha buffer is static, but if this gets used as a regular texture, a separate buffer needs to be made. // Fill the column/row with shading values. // Vertical shaders have have minimum alpha at the top @@ -100,24 +99,24 @@ public: } } - uint8_t *MakeTexture(FRenderStyle style) override + TArray Get8BitPixels(bool alphatex) override { - if (style.Flags & STYLEF_RedIsAlpha) + TArray Pix(512, true); + if (alphatex) { - return Pixels; + memcpy(Pix.Data(), Pixels, 512); } else { // Since this presents itself to the game as a regular named texture // it can easily be used on walls and flats and should work as such, // even if it makes little sense. - auto Pix = new uint8_t[512]; for (int i = 0; i < 512; i++) { Pix[i] = GrayMap[Pixels[i]]; } - return Pix; } + return Pix; } int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL) override diff --git a/src/textures/formats/tgatexture.cpp b/src/textures/formats/tgatexture.cpp index 44bac1bfd..9416f6398 100644 --- a/src/textures/formats/tgatexture.cpp +++ b/src/textures/formats/tgatexture.cpp @@ -85,7 +85,7 @@ public: protected: void ReadCompressed(FileReader &lump, uint8_t * buffer, int bytesperpixel); - uint8_t *MakeTexture (FRenderStyle style) override; + TArray Get8BitPixels(bool alphatex) override; }; //========================================================================== @@ -191,7 +191,7 @@ void FTGATexture::ReadCompressed(FileReader &lump, uint8_t * buffer, int bytespe // //========================================================================== -uint8_t *FTGATexture::MakeTexture (FRenderStyle style) +TArray FTGATexture::Get8BitPixels(bool alphatex) { uint8_t PaletteMap[256]; auto lump = Wads.OpenLumpReader (SourceLump); @@ -199,9 +199,8 @@ uint8_t *FTGATexture::MakeTexture (FRenderStyle style) uint16_t w; uint8_t r,g,b,a; uint8_t * buffer; - bool alphatex = !!(style.Flags & STYLEF_RedIsAlpha); - auto Pixels = new uint8_t[Width*Height]; + TArray Pixels(Width*Height, true); lump.Read(&hdr, sizeof(hdr)); lump.Seek(hdr.id_len, FileReader::SeekCur); @@ -356,7 +355,7 @@ uint8_t *FTGATexture::MakeTexture (FRenderStyle style) case 3: // Grayscale { - auto remap = GetRemap(style, true); + auto remap = GetRemap(alphatex, true); switch (hdr.bpp) { case 8: diff --git a/src/textures/formats/worldtexture.cpp b/src/textures/formats/worldtexture.cpp index edba839c6..66b9ec3b4 100644 --- a/src/textures/formats/worldtexture.cpp +++ b/src/textures/formats/worldtexture.cpp @@ -47,49 +47,3 @@ FWorldTexture::FWorldTexture(const char *name, int lumpnum) { } -//========================================================================== -// -// -// -//========================================================================== - -FWorldTexture::~FWorldTexture() -{ - Unload(); -} - -//========================================================================== -// -// -// -//========================================================================== - -void FWorldTexture::Unload () -{ - for(int i = 0; i < 2; i++) - { - if (!(PixelsAreStatic & (1 << i))) - { - delete[] Pixeldata[i]; - } - Pixeldata[i] = nullptr; - } - FTexture::Unload(); -} - -//========================================================================== -// -// -// -//========================================================================== - -const uint8_t *FWorldTexture::Get8BitPixels (FRenderStyle style) -{ - int index = !!(style.Flags & STYLEF_RedIsAlpha); - if (Pixeldata[index] == nullptr) - { - Pixeldata[index] = MakeTexture (style); - } - return Pixeldata[index]; -} - diff --git a/src/textures/skyboxtexture.cpp b/src/textures/skyboxtexture.cpp index a1f22d9f4..6a3572b03 100644 --- a/src/textures/skyboxtexture.cpp +++ b/src/textures/skyboxtexture.cpp @@ -48,21 +48,10 @@ FSkyBox::FSkyBox(const char *name) // //----------------------------------------------------------------------------- -FSkyBox::~FSkyBox() +TArray FSkyBox::Get8BitPixels(bool alphatex) { - // The faces are only referenced but not owned so don't delete them. -} - -//----------------------------------------------------------------------------- -// -// -// -//----------------------------------------------------------------------------- - -const uint8_t *FSkyBox::Get8BitPixels (FRenderStyle style) -{ - if (faces[0]) return faces[0]->Get8BitPixels(style); - return NULL; + if (faces[0]) return faces[0]->Get8BitPixels(alphatex); + return FTexture::Get8BitPixels(alphatex); } //----------------------------------------------------------------------------- @@ -88,13 +77,3 @@ bool FSkyBox::UseBasePalette() return false; // not really but here it's not important. } -//----------------------------------------------------------------------------- -// -// -// -//----------------------------------------------------------------------------- - -void FSkyBox::Unload () -{ -} - diff --git a/src/textures/skyboxtexture.h b/src/textures/skyboxtexture.h index 08dfdd653..0dec6aa54 100644 --- a/src/textures/skyboxtexture.h +++ b/src/textures/skyboxtexture.h @@ -16,9 +16,7 @@ public: bool fliptop; FSkyBox(const char *name = nullptr); - ~FSkyBox(); - //const uint8_t *GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out); - const uint8_t *Get8BitPixels (FRenderStyle style); + TArray Get8BitPixels(bool alphatex); int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf); bool UseBasePalette(); void Unload (); diff --git a/src/textures/texture.cpp b/src/textures/texture.cpp index 61cc9ab44..da2c0de03 100644 --- a/src/textures/texture.cpp +++ b/src/textures/texture.cpp @@ -225,11 +225,11 @@ FTexture::~FTexture () if (SystemTexture[i] != nullptr) delete SystemTexture[i]; SystemTexture[i] = nullptr; } - -} - -void FTexture::Unload() -{ + if (SoftwareTexture != nullptr) + { + delete SoftwareTexture; + SoftwareTexture = nullptr; + } } //========================================================================== @@ -254,15 +254,16 @@ void FTexture::SetFrontSkyLayer () // //========================================================================== -void FTexture::CopyToBlock (uint8_t *dest, int dwidth, int dheight, int xpos, int ypos, int rotate, const uint8_t *translation, FRenderStyle style) +void FTexture::CopyToBlock (uint8_t *dest, int dwidth, int dheight, int xpos, int ypos, int rotate, const uint8_t *translation, bool style) { - const uint8_t *pixels = Get8BitPixels(style); + auto image = Get8BitPixels(style); // should use composition cache + const uint8_t *pixels = image.Data(); int srcwidth = Width; int srcheight = Height; int step_x = Height; int step_y = 1; FClipRect cr = {0, 0, dwidth, dheight}; - if (style.Flags & STYLEF_RedIsAlpha) translation = nullptr; // do not apply translations to alpha textures. + if (style) translation = nullptr; // do not apply translations to alpha textures. if (ClipCopyPixelRect(&cr, xpos, ypos, pixels, srcwidth, srcheight, step_x, step_y, rotate)) { @@ -425,7 +426,6 @@ void FTexture::FlipNonSquareBlockRemap (uint8_t *dst, const uint8_t *src, int x, void FTexture::FillBuffer(uint8_t *buff, int pitch, int height, FTextureFormat fmt) { - const uint8_t *pix; int x, y, w, h, stride; w = GetWidth(); @@ -435,7 +435,9 @@ void FTexture::FillBuffer(uint8_t *buff, int pitch, int height, FTextureFormat f { case TEX_Pal: case TEX_Gray: - pix = Get8BitPixels(fmt == TEX_Pal? DefaultRenderStyle() : LegacyRenderStyles[STYLE_Shaded]); + { + auto ppix = Get8BitPixels(fmt == TEX_Gray); // should use composition cache + auto pix = ppix.Data(); stride = pitch - w; for (y = 0; y < h; ++y) { @@ -449,6 +451,7 @@ void FTexture::FillBuffer(uint8_t *buff, int pitch, int height, FTextureFormat f buff += stride; } break; + } case TEX_RGB: { @@ -479,14 +482,16 @@ int FTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyI { PalEntry *palette = screen->GetPalette(); for(int i=1;i<256;i++) palette[i].a = 255; // set proper alpha values - bmp->CopyPixelData(x, y, Get8BitPixels(DefaultRenderStyle()), Width, Height, Height, 1, rotate, palette, inf); + auto ppix = Get8BitPixels(false); // should use composition cache + bmp->CopyPixelData(x, y, ppix.Data(), Width, Height, Height, 1, rotate, palette, inf); for(int i=1;i<256;i++) palette[i].a = 0; return 0; } int FTexture::CopyTrueColorTranslated(FBitmap *bmp, int x, int y, int rotate, PalEntry *remap, FCopyInfo *inf) { - bmp->CopyPixelData(x, y, Get8BitPixels(DefaultRenderStyle()), Width, Height, Height, 1, rotate, remap, inf); + auto ppix = Get8BitPixels(false); // should use composition cache + bmp->CopyPixelData(x, y, ppix.Data(), Width, Height, Height, 1, rotate, remap, inf); return 0; } @@ -596,7 +601,7 @@ PalEntry FTexture::GetSkyCapColor(bool bottom) int FTexture::CheckRealHeight() { - auto pixels = Get8BitPixels(DefaultRenderStyle()); + auto pixels = Get8BitPixels(false); for(int h = GetHeight()-1; h>= 0; h--) { @@ -682,12 +687,10 @@ void FTexture::CreateDefaultBrightmap() // Check for brightmaps if (UseBasePalette() && TexMan.HasGlobalBrightmap && UseType != ETextureType::Decal && UseType != ETextureType::MiscPatch && UseType != ETextureType::FontChar && - Brightmap == NULL && bWarped == 0 && - Get8BitPixels(DefaultRenderStyle()) - ) + Brightmap == NULL && bWarped == 0) { // May have one - let's check when we use this texture - const uint8_t *texbuf = Get8BitPixels(DefaultRenderStyle()); + auto texbuf = Get8BitPixels(false); const int white = ColorMatcher.Pick(255, 255, 255); int size = GetWidth() * GetHeight(); @@ -1050,13 +1053,15 @@ void FTexture::SetSpriteAdjust() //=========================================================================== // -// empty stubs to be overloaded by child classes. +// the default just returns an empty texture. // //=========================================================================== -const uint8_t *FTexture::Get8BitPixels(FRenderStyle style) +TArray FTexture::Get8BitPixels(bool alphatex) { - return nullptr; + TArray Pixels(Width * Height, true); + memset(Pixels.Data(), 0, Width * Height); + return Pixels; } //=========================================================================== @@ -1212,26 +1217,4 @@ void FTexCoordInfo::GetFromTexture(FTexture *tex, float x, float y) mWidth = tex->GetWidth(); } -///////////// - - -class TextureCache -{ - struct ItemCacheInfo - { - int palettedCount; // counts use of final paletted textures - int palettedCountCompose; // counts use of images needed for composition (can be freed after precaching) - int rgbaCount; // counts use of final true color software textures - int rawCount; // counts use of raw images needed for composition (can be freed after precaching) - int textureCount; // counts use of hardware textures - }; - TMap> pixelCachePaletted; // 8 bit column major for composition and software rendering - TMap> pixelCacheRgba; // 32 bit column major for software true color rendering - TMap> pixelCacheRaw; // 32 bit row major for composition - TMap hwTextureCache; // native system textures. - - TMap cacheMarker; - - void PrecacheLevel(); -}; diff --git a/src/textures/texturemanager.cpp b/src/textures/texturemanager.cpp index e470cff66..85221b11e 100644 --- a/src/textures/texturemanager.cpp +++ b/src/textures/texturemanager.cpp @@ -363,20 +363,6 @@ FTexture *FTextureManager::FindTexture(const char *texname, ETextureType usetype return !texnum.isValid()? NULL : Textures[texnum.GetIndex()].Texture; } -//========================================================================== -// -// FTextureManager :: UnloadAll -// -//========================================================================== - -void FTextureManager::UnloadAll () -{ - for (unsigned int i = 0; i < Textures.Size(); ++i) - { - Textures[i].Texture->Unload (); - } -} - //========================================================================== // // FTextureManager :: AddTexture diff --git a/src/textures/textures.h b/src/textures/textures.h index ec0df68ee..811c9bede 100644 --- a/src/textures/textures.h +++ b/src/textures/textures.h @@ -298,11 +298,9 @@ public: bool isFullbright() const { return bFullbright; } void CreateDefaultBrightmap(); bool FindHoles(const unsigned char * buffer, int w, int h); - uint64_t CacheID() - { - // Just a temporary placeholder. This needs to be done differently as things progress. - return (uint64_t)(intptr_t)GetRedirect(); - } + + // Returns the whole texture, stored in column-major order + virtual TArray Get8BitPixels(bool alphatex); public: static void FlipSquareBlock (uint8_t *block, int x, int y); @@ -386,9 +384,6 @@ protected: - // Returns the whole texture, stored in column-major order - virtual const uint8_t *Get8BitPixels(FRenderStyle style); - // Returns true if GetPixelsBgra includes mipmaps virtual bool Mipmapped() { return true; } @@ -397,8 +392,6 @@ protected: virtual bool UseBasePalette(); virtual FTexture *GetRedirect(); - virtual void Unload (); - // Returns the native pixel format for this image virtual FTextureFormat GetFormat(); @@ -446,7 +439,7 @@ protected: virtual void SetFrontSkyLayer(); - void CopyToBlock (uint8_t *dest, int dwidth, int dheight, int x, int y, int rotate, const uint8_t *translation, FRenderStyle style); + void CopyToBlock (uint8_t *dest, int dwidth, int dheight, int x, int y, int rotate, const uint8_t *translation, bool style); static void InitGrayMap(); @@ -467,9 +460,9 @@ protected: uint16_t Width, Height; int16_t _LeftOffset[2], _TopOffset[2]; static uint8_t GrayMap[256]; - uint8_t *GetRemap(FRenderStyle style, bool srcisgrayscale = false) + uint8_t *GetRemap(bool wantluminance, bool srcisgrayscale = false) { - if (style.Flags & STYLEF_RedIsAlpha) + if (wantluminance) { return translationtables[TRANSLATION_Standard][srcisgrayscale ? STD_Gray : STD_Grayscale]->Remap; } @@ -651,8 +644,6 @@ public: // This function can be used for such things as warping textures. void ReplaceTexture (FTextureID picnum, FTexture *newtexture, bool free); - void UnloadAll (); - int NumTextures () const { return (int)Textures.Size(); } void UpdateAnimations (uint64_t mstime); @@ -739,15 +730,7 @@ public: class FWorldTexture : public FTexture { protected: - uint8_t *Pixeldata[2] = { nullptr, nullptr }; - uint8_t PixelsAreStatic = 0; // can be set by subclasses which provide static pixel buffers. - FWorldTexture(const char *name = nullptr, int lumpnum = -1); - ~FWorldTexture(); - - void Unload() override; - const uint8_t *Get8BitPixels(FRenderStyle style) override; - virtual uint8_t *MakeTexture(FRenderStyle style) = 0; }; // A texture that doesn't really exist @@ -767,20 +750,17 @@ class FCanvasTexture : public FTexture { public: FCanvasTexture (const char *name, int width, int height); - ~FCanvasTexture (); //const uint8_t *GetColumn(FRenderStyle style, unsigned int column, const FSoftwareTextureSpan **spans_out); //const uint32_t *GetPixelsBgra() override; - const uint8_t *Get8BitPixels (FRenderStyle style); - void Unload (); + //const uint8_t *Get8BitPixels(bool alphatex); bool CheckModified (FRenderStyle) /*override*/; void NeedUpdate() { bNeedsUpdate=true; } void SetUpdated() { bNeedsUpdate = false; bDidUpdate = true; bFirstUpdate = false; } DCanvas *GetCanvas() { return Canvas; } DCanvas *GetCanvasBgra() { return CanvasBgra; } bool Mipmapped() override { return false; } - void MakeTexture (FRenderStyle style); void MakeTextureBgra (); protected: @@ -838,6 +818,7 @@ struct FTexCoordInfo }; + #endif diff --git a/src/v_font.cpp b/src/v_font.cpp index c2d56717c..bd9efebde 100644 --- a/src/v_font.cpp +++ b/src/v_font.cpp @@ -171,16 +171,12 @@ class FFontChar1 : public FTexture { public: FFontChar1 (FTexture *sourcelump); - const uint8_t *Get8BitPixels (FRenderStyle style); + TArray Get8BitPixels(bool alphatex) override; void SetSourceRemap(const uint8_t *sourceremap); - void Unload (); - ~FFontChar1 (); protected: - void MakeTexture (); FTexture *BaseTexture; - uint8_t *Pixels; const uint8_t *SourceRemap; }; @@ -189,16 +185,13 @@ class FFontChar2 : public FTexture { public: FFontChar2 (int sourcelump, int sourcepos, int width, int height, int leftofs=0, int topofs=0); - ~FFontChar2 (); - const uint8_t *Get8BitPixels (FRenderStyle style); + TArray Get8BitPixels(bool alphatex) override; void SetSourceRemap(const uint8_t *sourceremap); - void Unload (); protected: int SourceLump; int SourcePos; - uint8_t *Pixels; const uint8_t *SourceRemap; void MakeTexture (); @@ -365,6 +358,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count, FirstFont = this; Cursor = '_'; ActiveColors = 0; + translateUntranslated = false; maxyoffs = 0; @@ -530,14 +524,13 @@ void RecordTextureColors (FTexture *pic, uint8_t *usedcolors) { int x; - auto pixels = pic->Get8BitPixels(DefaultRenderStyle()); + auto pixels = pic->Get8BitPixels(false); auto size = pic->GetWidth() * pic->GetHeight(); for(x = 0;x < size; x++) { usedcolors[pixels[x]]++; } - pic->Unload(); } //========================================================================== @@ -738,6 +731,7 @@ FRemapTable *FFont::GetColorTranslation (EColorRange range, PalEntry *color) con return NULL; else if (range >= NumTextColors) range = CR_UNTRANSLATED; + //if (range == CR_UNTRANSLATED && !translateUntranslated) return nullptr; return &Ranges[range]; } @@ -913,6 +907,8 @@ void FFont::LoadTranslations() } } + // Fixme: This needs to build a translation based on the source palette, not some intermediate 'ordered' table. + ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, &luminosity); for (unsigned int i = 0; i < count; i++) @@ -1078,6 +1074,7 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data) FirstChar = 0; LastChar = 255; GlobalKerning = 0; + translateUntranslated = true; PatchRemap = new uint8_t[256]; for(unsigned int i = 0;i < 256;++i) @@ -1558,7 +1555,6 @@ FFontChar1::FFontChar1 (FTexture *sourcelump) // now copy all the properties from the base texture assert(BaseTexture != NULL); CopySize(BaseTexture); - Pixels = NULL; } //========================================================================== @@ -1569,39 +1565,21 @@ FFontChar1::FFontChar1 (FTexture *sourcelump) // //========================================================================== -const uint8_t *FFontChar1::Get8BitPixels (FRenderStyle) -{ - if (Pixels == NULL) - { - MakeTexture (); - } - return Pixels; -} - -//========================================================================== -// -// FFontChar1 :: MakeTexture -// -//========================================================================== - -void FFontChar1::MakeTexture () +TArray FFontChar1::Get8BitPixels (bool) { // Make the texture as normal, then remap it so that all the colors // are at the low end of the palette - Pixels = new uint8_t[Width*Height]; - const uint8_t *pix = BaseTexture->Get8BitPixels(DefaultRenderStyle()); + // Why? It only creates unnecessary work! + auto Pixels = BaseTexture->Get8BitPixels(false); - if (!SourceRemap) - { - memcpy(Pixels, pix, Width*Height); - } - else + if (SourceRemap) { for (int x = 0; x < Width*Height; ++x) { - Pixels[x] = SourceRemap[pix[x]]; + Pixels[x] = SourceRemap[Pixels[x]]; } } + return Pixels; } //========================================================================== @@ -1612,37 +1590,9 @@ void FFontChar1::MakeTexture () void FFontChar1::SetSourceRemap(const uint8_t *sourceremap) { - Unload(); SourceRemap = sourceremap; } -//========================================================================== -// -// FFontChar1 :: Unload -// -//========================================================================== - -void FFontChar1::Unload () -{ - if (Pixels != NULL) - { - delete[] Pixels; - Pixels = NULL; - } - FTexture::Unload(); -} - -//========================================================================== -// -// FFontChar1 :: ~FFontChar1 -// -//========================================================================== - -FFontChar1::~FFontChar1 () -{ - Unload (); -} - //========================================================================== // // FFontChar2 :: FFontChar2 @@ -1652,7 +1602,7 @@ FFontChar1::~FFontChar1 () //========================================================================== FFontChar2::FFontChar2 (int sourcelump, int sourcepos, int width, int height, int leftofs, int topofs) -: SourceLump (sourcelump), SourcePos (sourcepos), Pixels (0), SourceRemap(NULL) +: SourceLump (sourcelump), SourcePos (sourcepos), SourceRemap(NULL) { UseType = ETextureType::FontChar; Width = width; @@ -1663,29 +1613,13 @@ FFontChar2::FFontChar2 (int sourcelump, int sourcepos, int width, int height, in //========================================================================== // -// FFontChar2 :: ~FFontChar2 +// FFontChar2 :: SetSourceRemap // //========================================================================== -FFontChar2::~FFontChar2 () +void FFontChar2::SetSourceRemap(const uint8_t *sourceremap) { - Unload (); -} - -//========================================================================== -// -// FFontChar2 :: Unload -// -//========================================================================== - -void FFontChar2::Unload () -{ - if (Pixels != NULL) - { - delete[] Pixels; - Pixels = NULL; - } - FTexture::Unload(); + SourceRemap = sourceremap; } //========================================================================== @@ -1696,34 +1630,7 @@ void FFontChar2::Unload () // //========================================================================== -const uint8_t *FFontChar2::Get8BitPixels (FRenderStyle) -{ - if (Pixels == NULL) - { - MakeTexture (); - } - return Pixels; -} - -//========================================================================== -// -// FFontChar2 :: SetSourceRemap -// -//========================================================================== - -void FFontChar2::SetSourceRemap(const uint8_t *sourceremap) -{ - Unload(); - SourceRemap = sourceremap; -} - -//========================================================================== -// -// FFontChar2 :: MakeTexture -// -//========================================================================== - -void FFontChar2::MakeTexture () +TArray FFontChar2::Get8BitPixels(bool) { auto lump = Wads.OpenLumpReader (SourceLump); int destSize = Width * Height; @@ -1753,11 +1660,11 @@ void FFontChar2::MakeTexture () } } - Pixels = new uint8_t[destSize]; + TArray Pixels(destSize, true); int runlen = 0, setlen = 0; uint8_t setval = 0; // Shut up, GCC! - uint8_t *dest_p = Pixels; + uint8_t *dest_p = Pixels.Data(); int dest_adv = Height; int dest_rew = destSize - 1; @@ -1838,6 +1745,7 @@ void FFontChar2::MakeTexture () name[8] = 0; I_FatalError ("The font %s is corrupt", name); } + return Pixels; } //========================================================================== diff --git a/src/v_font.h b/src/v_font.h index 383363ce8..907c3e1f8 100644 --- a/src/v_font.h +++ b/src/v_font.h @@ -120,6 +120,7 @@ protected: int GlobalKerning; char Cursor; bool noTranslate; + bool translateUntranslated; struct CharData { FTexture *Pic;