Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mp3-materials'
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoalan committed Nov 17, 2019
2 parents cfea90e + 55c4a58 commit dfc9d1d
Show file tree
Hide file tree
Showing 39 changed files with 1,595 additions and 374 deletions.
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endif()

# Ensure submodules checked out
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/externals/LibCommon/CMakeLists.txt)
message(FATAL_ERROR "Please run 'git submodules update --init --recursive' to fetch submodules.")
message(FATAL_ERROR "Please run 'git submodule update --init --recursive' to fetch submodules.")
endif()

include(./dew.cmake)
Expand Down
6 changes: 3 additions & 3 deletions src/Core/GameProject/AssetNameGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void GenerateAssetNames(CGameProject *pProj)

for (uint32 iMat = 0; iMat < pSet->NumMaterials(); iMat++)
{
CMaterial *pMat = pSet->MaterialByIndex(iMat);
CMaterial *pMat = pSet->MaterialByIndex(iMat, true);

for (uint32 iPass = 0; iPass < pMat->PassCount(); iPass++)
{
Expand Down Expand Up @@ -239,7 +239,7 @@ void GenerateAssetNames(CGameProject *pProj)

for (uint32 iMat = 0; iMat < pMaterials->NumMaterials(); iMat++)
{
CMaterial *pMat = pMaterials->MaterialByIndex(iMat);
CMaterial *pMat = pMaterials->MaterialByIndex(iMat, true);
bool FoundLightmap = false;

for (uint32 iPass = 0; iPass < pMat->PassCount(); iPass++)
Expand Down Expand Up @@ -415,7 +415,7 @@ void GenerateAssetNames(CGameProject *pProj)

for (uint32 iMat = 0; iMat < pSet->NumMaterials(); iMat++)
{
CMaterial *pMat = pSet->MaterialByIndex(iMat);
CMaterial *pMat = pSet->MaterialByIndex(iMat, true);

for (uint32 iPass = 0; iPass < pMat->PassCount(); iPass++)
{
Expand Down
61 changes: 44 additions & 17 deletions src/Core/OpenGL/CShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,36 @@ bool CShaderGenerator::CreateVertexShader(const CMaterial& rkMat)
return mpShader->CompileVertexSource(ShaderCode.str().c_str());
}

static TString GetColorInputExpression(const CMaterialPass* pPass, ETevColorInput iInput)
{
if (iInput == ETevColorInput::kTextureRGB)
{
TString Ret("Tex.");
for (uint32 i = 0; i < 3; ++i)
Ret += pPass->TexSwapComp(i);
return Ret;
}
else if (iInput == ETevColorInput::kTextureAAA)
{
TString Ret("Tex.");
for (uint32 i = 0; i < 3; ++i)
Ret += pPass->TexSwapComp(3);
return Ret;
}
return gkTevColor[iInput];
}

static TString GetAlphaInputExpression(const CMaterialPass* pPass, ETevAlphaInput iInput)
{
if (iInput == ETevAlphaInput::kTextureAlpha)
{
TString Ret("Tex.");
Ret += pPass->TexSwapComp(3);
return Ret;
}
return gkTevAlpha[iInput];
}

bool CShaderGenerator::CreatePixelShader(const CMaterial& rkMat)
{
std::stringstream ShaderCode;
Expand All @@ -391,7 +421,7 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& rkMat)
<< "\n"
<< "layout(std140) uniform PixelBlock {\n"
<< " vec4 KonstColors[4];\n"
<< " vec4 TevColor;\n"
<< " vec4 TevColor[4];\n"
<< " vec4 TintColor;\n"
<< " float LightmapMultiplier;\n"
<< "};\n\n";
Expand All @@ -405,7 +435,7 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& rkMat)
ShaderCode << "void main()\n"
<< "{\n"
<< " vec4 TevInA = vec4(0, 0, 0, 0), TevInB = vec4(0, 0, 0, 0), TevInC = vec4(0, 0, 0, 0), TevInD = vec4(0, 0, 0, 0);\n"
<< " vec4 Prev = vec4(0, 0, 0, 0), C0 = TevColor, C1 = C0, C2 = C0;\n"
<< " vec4 Prev = TevColor[0], C0 = TevColor[1], C1 = TevColor[2], C2 = TevColor[3];\n"
<< " vec4 Ras = vec4(0, 0, 0, 1), Tex = vec4(0, 0, 0, 0);\n"
<< " vec4 Konst = vec4(1, 1, 1, 1);\n";

Expand All @@ -430,18 +460,13 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& rkMat)
if (pPass->TexCoordSource() != 0xFF)
ShaderCode << " TevCoord = (Tex" << iPass << ".z == 0.0 ? Tex" << iPass << ".xy : Tex" << iPass << ".xy / Tex" << iPass << ".z);\n";

if (pPass->Texture() != nullptr)
if (pPass->Texture())
ShaderCode << " Tex = texture(Texture" << iPass << ", TevCoord)";

// A couple pass types require special swizzles to access different texture color channels as alpha
if ((PassType == "TRAN") || (PassType == "INCA") || (PassType == "BLOI"))
ShaderCode << ".rgbr";
else if (PassType == "BLOL")
ShaderCode << ".rgbg";

// Apply lightmap multiplier
if ( (PassType == "DIFF") ||
(PassType == "CUST" && (rkMat.Options() & EMaterialOption::Lightmap) && iPass == 0) )
bool UseLightmapMultiplier = (PassType == "DIFF") ||
(PassType == "CUST" && (rkMat.Options() & EMaterialOption::Lightmap) && iPass == 0);
if (UseLightmapMultiplier && pPass->Texture())
ShaderCode << " * LightmapMultiplier";

ShaderCode << ";\n";
Expand All @@ -456,27 +481,29 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& rkMat)
char TevChar = iInput + 0x41; // the current stage number represented as an ASCII letter; eg 0 is 'A'

ShaderCode << " TevIn" << TevChar << " = vec4("
<< gkTevColor[pPass->ColorInput(iInput) & 0xF]
<< GetColorInputExpression(pPass, ETevColorInput(pPass->ColorInput(iInput) & 0xF))
<< ", "
<< gkTevAlpha[pPass->AlphaInput(iInput) & 0x7]
<< ");\n";
<< GetAlphaInputExpression(pPass, ETevAlphaInput(pPass->AlphaInput(iInput) & 0x7))
<< ")";
if (UseLightmapMultiplier && !pPass->Texture() && iInput == 1)
ShaderCode << " * LightmapMultiplier";
ShaderCode << ";\n";
}

ShaderCode << " // RGB Combine\n"
<< " "
<< gkTevRigid[pPass->ColorOutput()]
<< ".rgb = ";

ShaderCode << "clamp(vec3(TevInD.rgb + ((1.0 - TevInC.rgb) * TevInA.rgb + TevInC.rgb * TevInB.rgb))";
if ((PassType == "CLR ") && (Lightmap)) ShaderCode << "* (2.0 - (1.0 - LightmapMultiplier))"; // Apply tevscale 2.0 on the color pass if lightmap is present. Scale so we don't apply if lightmaps are off.
ShaderCode << "clamp(vec3(TevInD.rgb + ((1.0 - TevInC.rgb) * TevInA.rgb + TevInC.rgb * TevInB.rgb)) * " << pPass->TevColorScale();
ShaderCode << ", vec3(0, 0, 0), vec3(1.0, 1.0, 1.0));\n";

ShaderCode << " // Alpha Combine\n"
<< " "
<< gkTevRigid[pPass->AlphaOutput()]
<< ".a = ";

ShaderCode << "clamp(TevInD.a + ((1.0 - TevInC.a) * TevInA.a + TevInC.a * TevInB.a), 0.0, 1.0);\n\n";
ShaderCode << "clamp((TevInD.a + ((1.0 - TevInC.a) * TevInA.a + TevInC.a * TevInB.a)) * " << pPass->TevAlphaScale() << ", 0.0, 1.0);\n\n";
}

if (rkMat.Options() & EMaterialOption::Masked)
Expand Down
10 changes: 6 additions & 4 deletions src/Core/Render/CGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ CGraphics::SLightBlock CGraphics::sLightBlock;

CGraphics::ELightingMode CGraphics::sLightMode;
uint32 CGraphics::sNumLights;
const CColor CGraphics::skDefaultAmbientColor = CColor(0.5f, 0.5f, 0.5f, 1.f);
const CColor CGraphics::skDefaultAmbientColor = CColor(0.5f, 0.5f, 0.5f, 0.5f);
CColor CGraphics::sAreaAmbientColor = CColor::skBlack;
float CGraphics::sWorldLightMultiplier;
CLight CGraphics::sDefaultDirectionalLights[3] = {
CLight::BuildDirectional(CVector3f(0), CVector3f (0.f, -0.866025f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 1.f)),
CLight::BuildDirectional(CVector3f(0), CVector3f(-0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 1.f)),
CLight::BuildDirectional(CVector3f(0), CVector3f( 0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 1.f))
CLight::BuildDirectional(CVector3f(0), CVector3f (0.f, -0.866025f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)),
CLight::BuildDirectional(CVector3f(0), CVector3f(-0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)),
CLight::BuildDirectional(CVector3f(0), CVector3f( 0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f))
};

// ************ FUNCTIONS ************
Expand Down Expand Up @@ -167,9 +167,11 @@ void CGraphics::SetDefaultLighting()
sDefaultDirectionalLights[0].Load();
sDefaultDirectionalLights[1].Load();
sDefaultDirectionalLights[2].Load();
sNumLights = 0;
UpdateLightBlock();

sVertexBlock.COLOR0_Amb = CColor::skGray;
sVertexBlock.COLOR0_Amb.A = 0.5f;
UpdateVertexBlock();
}

Expand Down
8 changes: 7 additions & 1 deletion src/Core/Render/CGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ class CGraphics
struct SPixelBlock
{
CColor Konst[4];
CColor TevColor;
CColor TevColor[4];
CColor TintColor;
float LightmapMultiplier;
float Padding[3];

void SetAllTevColors(const CColor& color)
{
std::fill(std::begin(TevColor), std::end(TevColor), color);
}
};
static SPixelBlock sPixelBlock;

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Render/CRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void CRenderer::RenderSky(CModel *pSkyboxModel, const SViewInfo& rkViewInfo)

CGraphics::sMVPBlock.ModelMatrix = CMatrix4f::skIdentity;
CGraphics::sVertexBlock.COLOR0_Amb = CColor::skWhite;
CGraphics::sPixelBlock.TevColor = CColor::skWhite;
CGraphics::sPixelBlock.SetAllTevColors(CColor::skWhite);
CGraphics::sPixelBlock.TintColor = CColor::skWhite;
CGraphics::sNumLights = 0;
CGraphics::UpdateVertexBlock();
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Resource/Area/CGameArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void CGameArea::MergeTerrain()
for (uint32 iSurf = 0; iSurf < SubmeshCount; iSurf++)
{
SSurface *pSurf = pMdl->GetSurface(iSurf);
CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID);
CMaterial *pMat = mpMaterialSet->MaterialByIndex(pSurf->MaterialID, false);

bool NewMat = true;
for (std::vector<CStaticModel*>::iterator it = mStaticWorldModels.begin(); it != mStaticWorldModels.end(); it++)
Expand Down
61 changes: 33 additions & 28 deletions src/Core/Resource/CMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ CMaterial::CMaterial()
: mpShader(nullptr)
, mShaderStatus(EShaderStatus::NoShader)
, mRecalcHash(true)
, mEnableBloom(false)
, mVersion(EGame::Invalid)
, mOptions(EMaterialOption::None)
, mVtxDesc(EVertexAttribute::None)
Expand All @@ -27,57 +26,49 @@ CMaterial::CMaterial()
, mEchoesUnknownA(0)
, mEchoesUnknownB(0)
, mpIndirectTexture(nullptr)
{
}
, mpNextDrawPassMaterial(nullptr)
, mpBloomMaterial(nullptr)
{}

CMaterial::CMaterial(EGame Version, FVertexDescription VtxDesc)
: mpShader(nullptr)
, mShaderStatus(EShaderStatus::NoShader)
, mRecalcHash(true)
, mEnableBloom(Version == EGame::Corruption)
, mVersion(Version)
, mOptions(EMaterialOption::DepthWrite)
, mOptions(EMaterialOption::DepthWrite | EMaterialOption::ColorWrite)
, mVtxDesc(VtxDesc)
, mBlendSrcFac(GL_ONE)
, mBlendDstFac(GL_ZERO)
, mLightingEnabled(true)
, mEchoesUnknownA(0)
, mEchoesUnknownB(0)
, mpIndirectTexture(nullptr)
{
mpShader = nullptr;
mShaderStatus = EShaderStatus::NoShader;
mRecalcHash = true;
mEnableBloom = (Version == EGame::Corruption);
mVersion = Version;
mOptions = EMaterialOption::DepthWrite;
mVtxDesc = VtxDesc;
mBlendSrcFac = GL_ONE;
mBlendDstFac = GL_ZERO;
mLightingEnabled = true;
mEchoesUnknownA = 0;
mEchoesUnknownB = 0;
mpIndirectTexture = nullptr;
}
, mpNextDrawPassMaterial(nullptr)
, mpBloomMaterial(nullptr)
{}

CMaterial::~CMaterial()
{
for (uint32 iPass = 0; iPass < mPasses.size(); iPass++)
delete mPasses[iPass];

delete mpNextDrawPassMaterial;
delete mpBloomMaterial;

ClearShader();
}

CMaterial* CMaterial::Clone()
{
CMaterial *pOut = new CMaterial();
pOut->mName = mName;
pOut->mEnableBloom = mEnableBloom;
pOut->mVersion = mVersion;
pOut->mOptions = mOptions;
pOut->mVtxDesc = mVtxDesc;
for (uint32 iKonst = 0; iKonst < 4; iKonst++)
pOut->mKonstColors[iKonst] = mKonstColors[iKonst];
for (uint32 iTev = 0; iTev < 4; iTev++)
pOut->mTevColors[iTev] = mTevColors[iTev];
pOut->mBlendSrcFac = mBlendSrcFac;
pOut->mBlendDstFac = mBlendDstFac;
pOut->mLightingEnabled = mLightingEnabled;
Expand All @@ -89,6 +80,12 @@ CMaterial* CMaterial::Clone()
for (uint32 iPass = 0; iPass < mPasses.size(); iPass++)
pOut->mPasses[iPass] = mPasses[iPass]->Clone(pOut);

if (mpNextDrawPassMaterial)
pOut->mpNextDrawPassMaterial = mpNextDrawPassMaterial->Clone();

if (mpBloomMaterial)
pOut->mpBloomMaterial = mpBloomMaterial->Clone();

return pOut;
}

Expand Down Expand Up @@ -179,15 +176,12 @@ bool CMaterial::SetCurrent(FRenderOptions Options)
dstRGB = mBlendDstFac;
}

// Set alpha blend equation
bool AlphaBlended = ((mBlendSrcFac == GL_SRC_ALPHA) && (mBlendDstFac == GL_ONE_MINUS_SRC_ALPHA));

if ((mEnableBloom) && (Options & ERenderOption::EnableBloom) && (!AlphaBlended)) {
srcAlpha = mBlendSrcFac;
dstAlpha = mBlendDstFac;
} else {
if (mOptions & EMaterialOption::ZeroDestAlpha) {
srcAlpha = GL_ZERO;
dstAlpha = GL_ZERO;
} else {
srcAlpha = mBlendSrcFac;
dstAlpha = mBlendDstFac;
}

glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
Expand All @@ -196,6 +190,11 @@ bool CMaterial::SetCurrent(FRenderOptions Options)
for (uint32 iKonst = 0; iKonst < 4; iKonst++)
CGraphics::sPixelBlock.Konst[iKonst] = mKonstColors[iKonst];

// Set TEV registers
if (mVersion >= EGame::Corruption)
for (uint32 iTev = 0; iTev < 4; iTev++)
CGraphics::sPixelBlock.TevColor[iTev] = mTevColors[iTev];

// Set color channels
// COLOR0_Amb is initialized by the node instead of by the material
CGraphics::sVertexBlock.COLOR0_Mat = CColor::skWhite;
Expand All @@ -204,6 +203,11 @@ bool CMaterial::SetCurrent(FRenderOptions Options)
if ((mOptions & EMaterialOption::DepthWrite) || (Options & ERenderOption::NoAlpha)) glDepthMask(GL_TRUE);
else glDepthMask(GL_FALSE);

// Set color/alpha write
GLboolean bColorWrite = mOptions.HasFlag(EMaterialOption::ColorWrite);
GLboolean bAlphaWrite = mOptions.HasFlag(EMaterialOption::AlphaWrite);
glColorMask(bColorWrite, bColorWrite, bColorWrite, bAlphaWrite);

// Load uniforms
for (uint32 iPass = 0; iPass < mPasses.size(); iPass++)
mPasses[iPass]->SetAnimCurrent(Options, iPass);
Expand Down Expand Up @@ -249,6 +253,7 @@ uint64 CMaterial::HashParameters()
Hash.HashLong(mOptions);
Hash.HashLong(mVtxDesc);
Hash.HashData(mKonstColors, sizeof(CColor) * 4);
Hash.HashData(mTevColors, sizeof(CColor) * 4);
Hash.HashLong(mBlendSrcFac);
Hash.HashLong(mBlendDstFac);
Hash.HashByte(mLightingEnabled);
Expand Down
Loading

0 comments on commit dfc9d1d

Please sign in to comment.