LCOV - code coverage report
Current view: top level - source/graphics - MaterialManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 92 14.1 %
Date: 2026-12-03 01:19:39 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "MaterialManager.h"
      21             : 
      22             : #include "graphics/Material.h"
      23             : #include "graphics/PreprocessorWrapper.h"
      24             : #include "lib/path.h"
      25             : #include "maths/MathUtil.h"
      26             : #include "maths/Vector4D.h"
      27             : #include "ps/CLogger.h"
      28             : #include "ps/CStr.h"
      29             : #include "ps/CStrIntern.h"
      30             : #include "ps/CStrInternStatic.h"
      31             : #include "ps/ConfigDB.h"
      32             : #include "ps/Errors.h"
      33             : #include "ps/Filesystem.h"
      34             : #include "ps/XMB/XMBData.h"
      35             : #include "ps/XMB/XMBStorage.h"
      36             : #include "ps/XML/Xeromyces.h"
      37             : 
      38             : #include <sstream>
      39             : #include <string>
      40             : #include <utility>
      41             : 
      42             : namespace
      43             : {
      44             : 
      45           0 : CMaterial::Pass GetMaterialPassFromStr(const std::string_view pass)
      46             : {
      47           0 :     if (pass == "shadow_caster")
      48           0 :         return CMaterial::Pass::SHADOW_CASTER;
      49           0 :     else if (pass == "reflections")
      50           0 :         return CMaterial::Pass::REFLECTIONS;
      51           0 :     else if (pass == "refractions")
      52           0 :         return CMaterial::Pass::REFRACTIONS;
      53           0 :     else if (pass == "silhouette_occluder")
      54           0 :         return CMaterial::Pass::SILHOUETTE_OCCLUDER;
      55           0 :     else if (pass == "silhouette_caster")
      56           0 :         return CMaterial::Pass::SILHOUETTE_CASTER;
      57           0 :     else if (pass == "wireframe")
      58           0 :         return CMaterial::Pass::WIREFRAME;
      59           0 :     else if (pass == "wireframe_solid")
      60           0 :         return CMaterial::Pass::WIREFRAME_SOLID;
      61             :     else
      62           0 :         return CMaterial::Pass::MAIN;
      63             : }
      64             : 
      65             : }
      66             : 
      67          10 : CMaterialManager::CMaterialManager() :
      68          10 :     qualityLevel{Clamp(g_ConfigDB.Get("materialmgr.quality", 5.0f), 0.0f, 10.0f)}
      69             : {
      70          20 :     if (VfsDirectoryExists(L"art/materials/") &&
      71          10 :         !g_Xeromyces.AddValidator(g_VFS, "material", "art/materials/material.rng"))
      72             :     {
      73           0 :         LOGERROR("CMaterialManager: failed to load grammar file 'art/materials/material.rng'");
      74             :     }
      75          10 : }
      76             : 
      77           1 : CMaterial CMaterialManager::LoadMaterial(const VfsPath& pathname)
      78             : {
      79           1 :     if (pathname.empty())
      80           0 :         return CMaterial();
      81             : 
      82           1 :     std::map<VfsPath, CMaterial>::iterator iter = m_Materials.find(pathname);
      83           1 :     if (iter != m_Materials.end())
      84           0 :         return iter->second;
      85             : 
      86           1 :     CXeromyces xeroFile;
      87           1 :     if (xeroFile.Load(g_VFS, pathname, "material") != PSRETURN_OK)
      88           1 :         return CMaterial();
      89             : 
      90             :     #define EL(x) int el_##x = xeroFile.GetElementID(#x)
      91             :     #define AT(x) int at_##x = xeroFile.GetAttributeID(#x)
      92           0 :     EL(alpha_blending);
      93           0 :     EL(alternative);
      94           0 :     EL(define);
      95           0 :     EL(shader);
      96           0 :     EL(uniform);
      97           0 :     EL(renderquery);
      98           0 :     EL(required_texture);
      99           0 :     AT(effect);
     100           0 :     AT(if);
     101           0 :     AT(quality);
     102           0 :     AT(material);
     103           0 :     AT(name);
     104           0 :     AT(pass);
     105           0 :     AT(srgb);
     106           0 :     AT(value);
     107             :     #undef AT
     108             :     #undef EL
     109             : 
     110           0 :     CPreprocessorWrapper preprocessor;
     111           0 :     CMaterial material;
     112           0 :     material.AddStaticUniform("qualityLevel", CVector4D(qualityLevel, 0, 0, 0));
     113             : 
     114           0 :     CShaderDefines defines;
     115             : 
     116           0 :     XMBElement root = xeroFile.GetRoot();
     117           0 :     for (XMBElement node : root.GetChildNodes())
     118             :     {
     119           0 :         int token = node.GetNodeName();
     120           0 :         XMBAttributeList attrs = node.GetAttributes();
     121           0 :         if (token == el_alternative)
     122             :         {
     123           0 :             CStr cond = attrs.GetNamedItem(at_if);
     124           0 :             if (cond.empty() || !preprocessor.TestConditional(cond))
     125             :             {
     126           0 :                 cond = attrs.GetNamedItem(at_quality);
     127           0 :                 if (cond.empty())
     128           0 :                     continue;
     129             :                 else
     130             :                 {
     131           0 :                     if (cond.ToFloat() <= qualityLevel)
     132           0 :                         continue;
     133             :                 }
     134             :             }
     135             : 
     136           0 :             material = LoadMaterial(VfsPath("art/materials") / attrs.GetNamedItem(at_material).FromUTF8());
     137           0 :             break;
     138           0 :         }
     139           0 :         else if (token == el_alpha_blending)
     140             :         {
     141           0 :             material.SetUsesAlphaBlending(true);
     142             :         }
     143           0 :         else if (token == el_shader)
     144             :         {
     145           0 :             const CStr pass{attrs.GetNamedItem(at_pass)};
     146           0 :             CMaterial::Shader shader{CStrIntern{attrs.GetNamedItem(at_effect)}, defines};
     147           0 :             for (XMBElement child : node.GetChildNodes())
     148             :             {
     149           0 :                 XMBAttributeList childAttrs{child.GetAttributes()};
     150           0 :                 if (child.GetNodeName() == el_define)
     151             :                 {
     152           0 :                     shader.defines.Add(CStrIntern{childAttrs.GetNamedItem(at_name)}, CStrIntern{childAttrs.GetNamedItem(at_value)});
     153             :                 }
     154             :             }
     155           0 :             material.SetShader(GetMaterialPassFromStr(pass), std::move(shader));
     156           0 :         }
     157           0 :         else if (token == el_define)
     158             :         {
     159           0 :             defines.Add(CStrIntern(attrs.GetNamedItem(at_name)), CStrIntern(attrs.GetNamedItem(at_value)));
     160             :         }
     161           0 :         else if (token == el_uniform)
     162             :         {
     163           0 :             std::stringstream str(attrs.GetNamedItem(at_value));
     164           0 :             CVector4D vec;
     165           0 :             str >> vec.X >> vec.Y >> vec.Z >> vec.W;
     166           0 :             material.AddStaticUniform(attrs.GetNamedItem(at_name).c_str(), vec);
     167           0 :         }
     168           0 :         else if (token == el_renderquery)
     169             :         {
     170           0 :             material.AddRenderQuery(attrs.GetNamedItem(at_name).c_str());
     171             :         }
     172           0 :         else if (token == el_required_texture)
     173             :         {
     174           0 :             const bool sRGB{attrs.GetNamedItem(at_srgb) == "true"};
     175           0 :             material.AddRequiredSampler({CStrIntern{attrs.GetNamedItem(at_name)}, sRGB});
     176             :         }
     177             :     }
     178             : 
     179           0 :     m_Materials[pathname] = material;
     180           0 :     return material;
     181           1 : }

Generated by: LCOV version 1.16