0ad/source/graphics/MaterialManager.cpp
Vladislav Belov f781181899
Adds sRGB support for proper lighting
Currently we're incorrectly using sRGB colors as raw inputs for lighting
instead of conversion to linear space. For proper PBR we need linear
values.
2026-07-29 01:29:09 +02:00

174 lines
4.7 KiB
C++

/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "MaterialManager.h"
#include "graphics/Material.h"
#include "graphics/PreprocessorWrapper.h"
#include "lib/path.h"
#include "maths/MathUtil.h"
#include "maths/Vector4D.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/CStrIntern.h"
#include "ps/CStrInternStatic.h"
#include "ps/ConfigDB.h"
#include "ps/Errors.h"
#include "ps/Filesystem.h"
#include "ps/XMB/XMBData.h"
#include "ps/XMB/XMBStorage.h"
#include "ps/XML/Xeromyces.h"
#include <sstream>
#include <string>
#include <utility>
namespace
{
CMaterial::Pass GetMaterialPassFromStr(const std::string_view pass)
{
if (pass == "shadow_caster")
return CMaterial::Pass::SHADOW_CASTER;
else if (pass == "reflections")
return CMaterial::Pass::REFLECTIONS;
else if (pass == "refractions")
return CMaterial::Pass::REFRACTIONS;
else if (pass == "silhouette_occluder")
return CMaterial::Pass::SILHOUETTE_OCCLUDER;
else if (pass == "silhouette_caster")
return CMaterial::Pass::SILHOUETTE_CASTER;
else if (pass == "wireframe")
return CMaterial::Pass::WIREFRAME;
else if (pass == "wireframe_solid")
return CMaterial::Pass::WIREFRAME_SOLID;
else
return CMaterial::Pass::MAIN;
}
}
CMaterialManager::CMaterialManager() :
qualityLevel{Clamp(g_ConfigDB.Get("materialmgr.quality", 5.0f), 0.0f, 10.0f)}
{
if (VfsDirectoryExists(L"art/materials/") &&
!g_Xeromyces.AddValidator(g_VFS, "material", "art/materials/material.rng"))
{
LOGERROR("CMaterialManager: failed to load grammar file 'art/materials/material.rng'");
}
}
CMaterial CMaterialManager::LoadMaterial(const VfsPath& pathname)
{
if (pathname.empty())
return CMaterial();
std::map<VfsPath, CMaterial>::iterator iter = m_Materials.find(pathname);
if (iter != m_Materials.end())
return iter->second;
CXeromyces xeroFile;
if (xeroFile.Load(g_VFS, pathname, "material") != PSRETURN_OK)
return CMaterial();
#define EL(x) int el_##x = xeroFile.GetElementID(#x)
#define AT(x) int at_##x = xeroFile.GetAttributeID(#x)
EL(alpha_blending);
EL(alternative);
EL(define);
EL(shader);
EL(uniform);
EL(renderquery);
EL(required_texture);
AT(effect);
AT(if);
AT(define);
AT(quality);
AT(material);
AT(name);
AT(pass);
AT(srgb);
AT(value);
#undef AT
#undef EL
CPreprocessorWrapper preprocessor;
CMaterial material;
material.AddStaticUniform("qualityLevel", CVector4D(qualityLevel, 0, 0, 0));
XMBElement root = xeroFile.GetRoot();
XERO_ITER_EL(root, node)
{
int token = node.GetNodeName();
XMBAttributeList attrs = node.GetAttributes();
if (token == el_alternative)
{
CStr cond = attrs.GetNamedItem(at_if);
if (cond.empty() || !preprocessor.TestConditional(cond))
{
cond = attrs.GetNamedItem(at_quality);
if (cond.empty())
continue;
else
{
if (cond.ToFloat() <= qualityLevel)
continue;
}
}
material = LoadMaterial(VfsPath("art/materials") / attrs.GetNamedItem(at_material).FromUTF8());
break;
}
else if (token == el_alpha_blending)
{
material.SetUsesAlphaBlending(true);
}
else if (token == el_shader)
{
const CStr pass{attrs.GetNamedItem(at_pass)};
const CStrIntern shaderEffect{attrs.GetNamedItem(at_effect)};
material.SetShaderEffect(GetMaterialPassFromStr(pass), shaderEffect);
}
else if (token == el_define)
{
material.AddShaderDefine(CStrIntern(attrs.GetNamedItem(at_name)), CStrIntern(attrs.GetNamedItem(at_value)));
}
else if (token == el_uniform)
{
std::stringstream str(attrs.GetNamedItem(at_value));
CVector4D vec;
str >> vec.X >> vec.Y >> vec.Z >> vec.W;
material.AddStaticUniform(attrs.GetNamedItem(at_name).c_str(), vec);
}
else if (token == el_renderquery)
{
material.AddRenderQuery(attrs.GetNamedItem(at_name).c_str());
}
else if (token == el_required_texture)
{
const bool sRGB{attrs.GetNamedItem(at_srgb) == "true"};
material.AddRequiredSampler({CStrIntern{attrs.GetNamedItem(at_name)}, sRGB});
if (!attrs.GetNamedItem(at_define).empty())
material.AddShaderDefine(CStrIntern(attrs.GetNamedItem(at_define)), str_1);
}
}
m_Materials[pathname] = material;
return material;
}