2020-05-24 19:19:33 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright(C) 2004-2016 Christoph Oelckers
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with this program. If not, see http://www.gnu.org/licenses/
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include "m_png.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "hw_ihwtexture.h"
|
|
|
|
#include "hw_material.h"
|
|
|
|
#include "texturemanager.h"
|
|
|
|
#include "c_cvars.h"
|
2020-05-25 21:59:07 +00:00
|
|
|
#include "v_video.h"
|
2020-05-24 19:19:33 +00:00
|
|
|
|
2021-09-18 10:20:28 +00:00
|
|
|
|
|
|
|
CVAR(Bool, gl_customshader, true, 0);
|
|
|
|
|
|
|
|
|
2020-06-15 15:37:58 +00:00
|
|
|
static IHardwareTexture* (*layercallback)(int layer, int translation);
|
2021-04-05 18:12:11 +00:00
|
|
|
TArray<UserShaderDesc> usershaders;
|
2020-06-15 15:37:58 +00:00
|
|
|
|
|
|
|
void FMaterial::SetLayerCallback(IHardwareTexture* (*cb)(int layer, int translation))
|
|
|
|
{
|
|
|
|
layercallback = cb;
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:19:33 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Constructor
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-05-25 21:59:07 +00:00
|
|
|
FMaterial::FMaterial(FGameTexture * tx, int scaleflags)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
|
|
|
mShaderIndex = SHADER_Default;
|
2020-05-25 21:59:07 +00:00
|
|
|
sourcetex = tx;
|
|
|
|
auto imgtex = tx->GetTexture();
|
2020-05-31 21:08:56 +00:00
|
|
|
mTextureLayers.Push({ imgtex, scaleflags, -1 });
|
2020-05-24 19:19:33 +00:00
|
|
|
|
2020-09-28 19:24:08 +00:00
|
|
|
if (tx->GetUseType() == ETextureType::SWCanvas && static_cast<FWrapperTexture*>(imgtex)->GetColorFormat() == 0)
|
|
|
|
{
|
|
|
|
mShaderIndex = SHADER_Paletted;
|
|
|
|
}
|
|
|
|
else if (scaleflags & CTF_Indexed)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-06-15 15:37:58 +00:00
|
|
|
mTextureLayers[0].scaleFlags |= CTF_Indexed;
|
2020-05-24 19:19:33 +00:00
|
|
|
mShaderIndex = SHADER_Paletted;
|
|
|
|
}
|
|
|
|
else if (tx->isHardwareCanvas())
|
|
|
|
{
|
2020-05-25 21:59:07 +00:00
|
|
|
if (tx->GetShaderIndex() >= FIRST_USER_SHADER)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-05-25 21:59:07 +00:00
|
|
|
mShaderIndex = tx->GetShaderIndex();
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
2020-05-31 21:08:56 +00:00
|
|
|
mTextureLayers.Last().clampflags = CLAMP_CAMTEX;
|
|
|
|
// no additional layers for cameratexture
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-27 08:39:10 +00:00
|
|
|
if (tx->isWarped())
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-09-27 08:39:10 +00:00
|
|
|
mShaderIndex = tx->isWarped(); // This picks SHADER_Warp1 or SHADER_Warp2
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
2020-09-27 08:39:10 +00:00
|
|
|
// Note that the material takes no ownership of the texture!
|
|
|
|
else if (tx->Normal.get() && tx->Specular.get())
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-09-27 08:39:10 +00:00
|
|
|
for (auto &texture : { tx->Normal.get(), tx->Specular.get() })
|
|
|
|
{
|
|
|
|
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;
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:39:10 +00:00
|
|
|
// 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 });
|
|
|
|
}
|
2020-05-24 19:19:33 +00:00
|
|
|
|
2020-09-27 08:39:10 +00:00
|
|
|
auto index = tx->GetShaderIndex();
|
2021-09-18 10:20:28 +00:00
|
|
|
if (gl_customshader)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2021-09-18 10:20:28 +00:00
|
|
|
if (index >= FIRST_USER_SHADER)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2021-09-18 10:20:28 +00:00
|
|
|
const UserShaderDesc& usershader = usershaders[index - FIRST_USER_SHADER];
|
|
|
|
if (usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material
|
2020-09-27 08:39:10 +00:00
|
|
|
{
|
2021-09-18 10:20:28 +00:00
|
|
|
for (auto& texture : tx->CustomShaderTextures)
|
|
|
|
{
|
|
|
|
if (texture == nullptr) continue;
|
|
|
|
mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable.
|
|
|
|
}
|
|
|
|
mShaderIndex = index;
|
2020-09-27 08:39:10 +00:00
|
|
|
}
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 21:59:07 +00:00
|
|
|
mScaleFlags = scaleflags;
|
2020-05-24 19:19:33 +00:00
|
|
|
|
|
|
|
mTextureLayers.ShrinkToFit();
|
2020-05-25 21:59:07 +00:00
|
|
|
tx->Material[scaleflags] = this;
|
|
|
|
if (tx->isHardwareCanvas()) tx->SetTranslucent(false);
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Destructor
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
FMaterial::~FMaterial()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-06-15 15:37:58 +00:00
|
|
|
IHardwareTexture* FMaterial::GetLayer(int i, int translation, MaterialLayerInfo** pLayer) const
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-09-28 19:24:08 +00:00
|
|
|
if ((mScaleFlags & CTF_Indexed) && i > 0 && layercallback)
|
2020-06-15 15:37:58 +00:00
|
|
|
{
|
|
|
|
static MaterialLayerInfo deflayer = { nullptr, 0, CLAMP_XY };
|
|
|
|
if (i == 1 || i == 2)
|
|
|
|
{
|
|
|
|
if (pLayer) *pLayer = &deflayer;
|
|
|
|
//This must be done with a user supplied callback because we cannot set up the rules for palette data selection here
|
|
|
|
return layercallback(i, translation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto& layer = mTextureLayers[i];
|
|
|
|
if (pLayer) *pLayer = &layer;
|
2020-09-28 19:24:08 +00:00
|
|
|
if (mScaleFlags & CTF_Indexed) translation = -1;
|
2020-06-15 15:37:58 +00:00
|
|
|
if (layer.layerTexture) return layer.layerTexture->GetHardwareTexture(translation, layer.scaleFlags);
|
|
|
|
}
|
2020-05-24 19:19:33 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Gets a texture from the texture manager and checks its validity for
|
|
|
|
// GL rendering.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-05-25 21:59:07 +00:00
|
|
|
FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool create)
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-05-25 21:59:07 +00:00
|
|
|
if (gtex && gtex->isValid())
|
2020-05-24 19:19:33 +00:00
|
|
|
{
|
2020-06-15 15:37:58 +00:00
|
|
|
if (scaleflags & CTF_Indexed) scaleflags = CTF_Indexed;
|
2020-05-25 21:59:07 +00:00
|
|
|
if (!gtex->expandSprites()) scaleflags &= ~CTF_Expand;
|
2020-05-24 19:19:33 +00:00
|
|
|
|
2020-05-25 21:59:07 +00:00
|
|
|
FMaterial *hwtex = gtex->Material[scaleflags];
|
2020-05-24 19:19:33 +00:00
|
|
|
if (hwtex == NULL && create)
|
|
|
|
{
|
2020-05-25 21:59:07 +00:00
|
|
|
hwtex = screen->CreateMaterial(gtex, scaleflags);
|
2020-05-24 19:19:33 +00:00
|
|
|
}
|
|
|
|
return hwtex;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteMaterial(FMaterial* mat)
|
|
|
|
{
|
|
|
|
delete mat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|