From 0314cdec552e516fd1788ec3021ef1bda43e597c Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 27 Sep 2020 10:39:10 +0200 Subject: [PATCH] - texture manager fixes from GZDoom * missing null check in FPngTexture. * ignore identity translations when creating textures. --- source/common/textures/formats/anmtexture.cpp | 2 +- source/common/textures/formats/pngtexture.cpp | 3 +- source/common/textures/hw_material.cpp | 122 +++++++++--------- source/common/textures/hw_texcontainer.h | 6 +- source/common/textures/texture.cpp | 1 + 5 files changed, 68 insertions(+), 66 deletions(-) diff --git a/source/common/textures/formats/anmtexture.cpp b/source/common/textures/formats/anmtexture.cpp index 526028276..aa65dc693 100644 --- a/source/common/textures/formats/anmtexture.cpp +++ b/source/common/textures/formats/anmtexture.cpp @@ -106,7 +106,7 @@ void FAnmTexture::ReadFrame(uint8_t *pixels, uint8_t *palette) uint8_t *source = (uint8_t *)lump.GetMem(); anim_t anim; - if (ANIM_LoadAnim(&anim, source, lump.GetSize()) >= 0) + if (ANIM_LoadAnim(&anim, source, (int)lump.GetSize()) >= 0) { int numframes = ANIM_NumFrames(&anim); if (numframes >= 1) diff --git a/source/common/textures/formats/pngtexture.cpp b/source/common/textures/formats/pngtexture.cpp index 48eb2437c..fcce510d2 100644 --- a/source/common/textures/formats/pngtexture.cpp +++ b/source/common/textures/formats/pngtexture.cpp @@ -360,7 +360,8 @@ void FPNGTexture::ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap) uint8_t r = lump->ReadUInt8(); uint8_t g = lump->ReadUInt8(); uint8_t b = lump->ReadUInt8(); - alpharemap[i] = PaletteMap[i] == 0 ? 0 : Luminance(r, g, b); + int palmap = PaletteMap ? PaletteMap[i] : i; + alpharemap[i] = palmap == 0 ? 0 : Luminance(r, g, b); } lump->Seek(p, FileReader::SeekSet); } diff --git a/source/common/textures/hw_material.cpp b/source/common/textures/hw_material.cpp index a74514b87..dabe738da 100644 --- a/source/common/textures/hw_material.cpp +++ b/source/common/textures/hw_material.cpp @@ -65,73 +65,73 @@ FMaterial::FMaterial(FGameTexture * tx, int scaleflags) } else { - if (tx->isWarped()) - { - mShaderIndex = tx->isWarped(); // This picks SHADER_Warp1 or SHADER_Warp2 - } - // Note that the material takes no ownership of the texture! - else if (tx->Normal.get() && tx->Specular.get()) - { - for (auto& texture : { tx->Normal.get(), tx->Specular.get() }) + if (tx->isWarped()) { - mTextureLayers.Push({ texture, 0, -1 }); + mShaderIndex = tx->isWarped(); // This picks SHADER_Warp1 or SHADER_Warp2 } - mShaderIndex = SHADER_Specular; - } - else if (tx->Normal.get() && tx->Metallic.get() && tx->Roughness.get() && tx->AmbientOcclusion.get()) - { - for (auto& texture : { tx->Normal.get(), tx->Metallic.get(), tx->Roughness.get(), tx->AmbientOcclusion.get() }) + // Note that the material takes no ownership of the texture! + else if (tx->Normal.get() && tx->Specular.get()) { - mTextureLayers.Push({ texture, 0, -1 }); - } - mShaderIndex = SHADER_PBR; - } - - // Note that these layers must present a valid texture even if not used, because empty TMUs in the shader are an undefined condition. - tx->CreateDefaultBrightmap(); - auto placeholder = TexMan.GameByIndex(1); - if (tx->Brightmap.get()) - { - mTextureLayers.Push({ tx->Brightmap.get(), scaleflags, -1 }); - mLayerFlags |= TEXF_Brightmap; - } - else - { - mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); - } - if (tx->Detailmap.get()) - { - mTextureLayers.Push({ tx->Detailmap.get(), 0, CLAMP_NONE }); - mLayerFlags |= TEXF_Detailmap; - } - else - { - mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); - } - if (tx->Glowmap.get()) - { - mTextureLayers.Push({ tx->Glowmap.get(), scaleflags, -1 }); - mLayerFlags |= TEXF_Glowmap; - } - else - { - mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); - } - - auto index = tx->GetShaderIndex(); - if (index >= FIRST_USER_SHADER) - { - const UserShaderDesc& usershader = usershaders[index - FIRST_USER_SHADER]; - if (usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material - { - for (auto& texture : tx->CustomShaderTextures) + for (auto &texture : { tx->Normal.get(), tx->Specular.get() }) { - if (texture == nullptr) continue; - mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable. + mTextureLayers.Push({ texture, 0, -1 }); + } + mShaderIndex = SHADER_Specular; + } + else if (tx->Normal.get() && tx->Metallic.get() && tx->Roughness.get() && tx->AmbientOcclusion.get()) + { + for (auto &texture : { tx->Normal.get(), tx->Metallic.get(), tx->Roughness.get(), tx->AmbientOcclusion.get() }) + { + mTextureLayers.Push({ texture, 0, -1 }); + } + mShaderIndex = SHADER_PBR; + } + + // Note that these layers must present a valid texture even if not used, because empty TMUs in the shader are an undefined condition. + tx->CreateDefaultBrightmap(); + auto placeholder = TexMan.GameByIndex(1); + if (tx->Brightmap.get()) + { + mTextureLayers.Push({ tx->Brightmap.get(), scaleflags, -1 }); + mLayerFlags |= TEXF_Brightmap; + } + else + { + mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); + } + if (tx->Detailmap.get()) + { + mTextureLayers.Push({ tx->Detailmap.get(), 0, CLAMP_NONE }); + mLayerFlags |= TEXF_Detailmap; + } + else + { + mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); + } + if (tx->Glowmap.get()) + { + mTextureLayers.Push({ tx->Glowmap.get(), scaleflags, -1 }); + mLayerFlags |= TEXF_Glowmap; + } + else + { + mTextureLayers.Push({ placeholder->GetTexture(), 0, -1 }); + } + + auto index = tx->GetShaderIndex(); + if (index >= FIRST_USER_SHADER) + { + const UserShaderDesc &usershader = usershaders[index - FIRST_USER_SHADER]; + if (usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material + { + for (auto &texture : tx->CustomShaderTextures) + { + if (texture == nullptr) continue; + mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable. + } + mShaderIndex = index; } - mShaderIndex = index; } - } } mScaleFlags = scaleflags; diff --git a/source/common/textures/hw_texcontainer.h b/source/common/textures/hw_texcontainer.h index 9a00e848a..a29fc6396 100644 --- a/source/common/textures/hw_texcontainer.h +++ b/source/common/textures/hw_texcontainer.h @@ -12,9 +12,9 @@ enum ECreateTexBufferFlags CTF_Expand = 1, // create buffer with a one-pixel wide border CTF_Upscale = 2, // Upscale the texture CTF_CreateMask = 3, // Flags that are relevant for hardware texture creation. - CTF_Indexed = 4, // Tell the backend to create an indexed texture. - CTF_ProcessData = 8, // run postprocessing on the generated buffer. This is only needed when using the data for a hardware texture. - CTF_CheckOnly = 16, // Only runs the code to get a content ID but does not create a texture. Can be used to access a caching system for the hardware textures. + CTF_ProcessData = 4, // run postprocessing on the generated buffer. This is only needed when using the data for a hardware texture. + CTF_CheckOnly = 8, // Only runs the code to get a content ID but does not create a texture. Can be used to access a caching system for the hardware textures. + CTF_Indexed = 16 // Tell the backend to create an indexed texture. }; class FHardwareTextureContainer diff --git a/source/common/textures/texture.cpp b/source/common/textures/texture.cpp index 97ac7b20a..503657f00 100644 --- a/source/common/textures/texture.cpp +++ b/source/common/textures/texture.cpp @@ -357,6 +357,7 @@ FTextureBuffer FTexture::CreateTexBuffer(int translation, int flags) memset(buffer, 0, W * (H + 1) * 4); auto remap = translation <= 0 ? nullptr : GPalette.TranslationToTable(translation); + if (remap && remap->Inactive) remap = nullptr; if (remap) translation = remap->Index; FBitmap bmp(buffer, W * 4, W, H);