1
0
forked from 0ad/0ad
0ad/source/graphics/ObjectManager.cpp
wraitii 76acc4e146 Implement quality levels for actors & corresponding setting.
An actor file, as referenced by the VisualActor, can now define
different actors for different "quality level" setting.
In this initial version, the quality is handled directly by the object
manager.

Actor format impact:
- '<qualitylevels>' may be used as the root node, containing actor nodes
as children.
  - such actor nodes can refer to a file, or to an inline actor, or
simply be inlined.
  - such actor nodes may have a 'quality' attribute, specifying the
maximum quality level of this actor. By default, 255 (the maximum) is
implied.
- The actor format remains valid, but 'groups', 'variants', 'material',
'castshadow' and 'float' can be given a [minquality, maxquality[ range
via XML attributes. Outside of this range, the XML node is ignored
(making it possible to define, in a single actor file, several quality
levels).

Quality is a 0-255 value, with:
- Range 0-99 intended for lower level-of-detail actors (billboards,
etc.)
- Range 100-200 the 'normal' range for models. 100 is "low", 150
"medium", and 200 "high".
- Range 201-255 used for higher quality actors that might be used for
e.g. cinematics.
The range is wide to make it easier to add intermediate levels in the
future and it seemed easier given that an integer value of some kind was
required anyways.

Engine impacts:
- A new CActorDef class is introduced, wrapping an art/actors XML file
and its different quality levels. ObjectBase remains the definition of a
given 'actor', now at a given quality level.
- CActorDef imposes a maximal # of quality level for a particular actor
definition (5 currently).
- CUnit is made to refer to an Actor Definition explicitly, not a
particular ObjectBase.
- As a minor optimisation, variation keys are calculated on
pointer-to-sets-of-selections, instead of raw sets-of-selections, as
this reduces copying.
- some refactoring, including better const-correctness and hotloading
support via std::shared_ptr.

Differential Revision: https://code.wildfiregames.com/D3787
This was SVN commit r25210.
2021-04-08 07:22:24 +00:00

191 lines
7.0 KiB
C++

/* Copyright (C) 2021 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 "ObjectManager.h"
#include "graphics/ObjectBase.h"
#include "graphics/ObjectEntry.h"
#include "ps/CLogger.h"
#include "ps/ConfigDB.h"
#include "ps/Game.h"
#include "ps/Profile.h"
#include "ps/Filesystem.h"
#include "ps/XML/Xeromyces.h"
#include "simulation2/Simulation2.h"
#include "simulation2/components/ICmpTerrain.h"
#include "simulation2/components/ICmpVisual.h"
bool CObjectManager::ObjectKey::operator< (const CObjectManager::ObjectKey& a) const
{
if (ObjectBaseIdentifier < a.ObjectBaseIdentifier)
return true;
else if (ObjectBaseIdentifier > a.ObjectBaseIdentifier)
return false;
else
return ActorVariation < a.ActorVariation;
}
static Status ReloadChangedFileCB(void* param, const VfsPath& path)
{
return static_cast<CObjectManager*>(param)->ReloadChangedFile(path);
}
CObjectManager::CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation)
: m_MeshManager(meshManager), m_SkeletonAnimManager(skeletonAnimManager), m_Simulation(simulation)
{
RegisterFileReloadFunc(ReloadChangedFileCB, this);
m_QualityHook = std::make_unique<CConfigDBHook>(g_ConfigDB.RegisterHookAndCall("max_actor_quality", [this]() { this->ActorQualityChanged(); }));
if (!CXeromyces::AddValidator(g_VFS, "actor", "art/actors/actor.rng"))
LOGERROR("CObjectManager: failed to load actor grammar file 'art/actors/actor.rng'");
}
CObjectManager::~CObjectManager()
{
UnloadObjects();
g_ConfigDB.UnregisterHook(std::move(m_QualityHook));
UnregisterFileReloadFunc(ReloadChangedFileCB, this);
}
CActorDef* CObjectManager::FindActorDef(const CStrW& actorName)
{
ENSURE(!actorName.empty());
decltype(m_ActorDefs)::iterator it = m_ActorDefs.find(actorName);
if (it != m_ActorDefs.end())
return it->second.get();
std::unique_ptr<CActorDef> actor = std::make_unique<CActorDef>(*this);
VfsPath pathname = VfsPath("art/actors/") / actorName;
if (actor->Load(pathname))
return m_ActorDefs.emplace(actorName, std::move(actor)).first->second.get();
LOGERROR("CObjectManager::FindActorDef(): Cannot find actor '%s'", utf8_from_wstring(actorName));
return nullptr;
}
CObjectEntry* CObjectManager::FindObjectVariation(const CActorDef* actor, const std::vector<std::set<CStr>>& selections, uint32_t seed)
{
if (!actor)
return nullptr;
const std::shared_ptr<CObjectBase>& base = actor->GetBase(m_QualityLevel);
std::vector<const std::set<CStr>*> completeSelections;
for (const std::set<CStr>& selectionSet : selections)
completeSelections.emplace_back(&selectionSet);
// To maintain a consistent look between quality levels, first complete with the highest-quality variants.
// then complete again at the required quality level (since not all variants may be available).
std::set<CStr> highQualitySelections = actor->GetBase(255)->CalculateRandomRemainingSelections(seed, selections);
completeSelections.emplace_back(&highQualitySelections);
// We don't have to pass the high-quality selections here because they have higher priority anyways.
std::set<CStr> remainingSelections = base->CalculateRandomRemainingSelections(seed, selections);
completeSelections.emplace_back(&remainingSelections);
return FindObjectVariation(base, completeSelections);
}
CObjectEntry* CObjectManager::FindObjectVariation(const std::shared_ptr<CObjectBase>& base, const std::vector<const std::set<CStr>*>& completeSelections)
{
PROFILE2("FindObjectVariation");
// Look to see whether this particular variation has already been loaded
std::vector<u8> choices = base->CalculateVariationKey(completeSelections);
ObjectKey key (base->GetIdentifier(), choices);
decltype(m_Objects)::iterator it = m_Objects.find(key);
if (it != m_Objects.end() && !it->second->m_Outdated)
return it->second.get();
// If it hasn't been loaded, load it now.
std::unique_ptr<CObjectEntry> obj = std::make_unique<CObjectEntry>(base, m_Simulation);
// TODO (for some efficiency): use the pre-calculated choices for this object,
// which has already worked out what to do for props, instead of passing the
// selections into BuildVariation and having it recalculate the props' choices.
if (!obj->BuildVariation(completeSelections, choices, *this))
return nullptr;
return m_Objects.emplace(key, std::move(obj)).first->second.get();
}
CTerrain* CObjectManager::GetTerrain()
{
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
if (!cmpTerrain)
return NULL;
return cmpTerrain->GetCTerrain();
}
void CObjectManager::UnloadObjects()
{
m_Objects.clear();
m_ActorDefs.clear();
}
Status CObjectManager::ReloadChangedFile(const VfsPath& path)
{
// Mark old entries as outdated so we don't reload them from the cache
for (std::map<ObjectKey, std::unique_ptr<CObjectEntry>>::iterator it = m_Objects.begin(); it != m_Objects.end(); ++it)
if (it->second->m_Base->UsesFile(path))
it->second->m_Outdated = true;
const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual);
// Reload actors that use a changed object
for (std::unordered_map<CStrW, std::unique_ptr<CActorDef>>::iterator it = m_ActorDefs.begin(); it != m_ActorDefs.end(); ++it)
{
if (!it->second->UsesFile(path))
continue;
it->second->Reload();
// Slightly ugly hack: The graphics system doesn't preserve enough information to regenerate the
// object with all correct variations, and we don't want to waste space storing it just for the
// rare occurrence of hotloading, so we'll tell the component (which does preserve the information)
// to do the reloading itself
for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit)
static_cast<ICmpVisual*>(eit->second)->Hotload(it->first);
}
return INFO::OK;
}
void CObjectManager::ActorQualityChanged()
{
int quality;
CFG_GET_VAL("max_actor_quality", quality);
if (quality == m_QualityLevel)
return;
m_QualityLevel = quality > 255 ? 255 : quality < 0 ? 0 : quality;
// No need to reload entries or actors, but we do need to reload all units.
const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual);
for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit)
static_cast<ICmpVisual*>(eit->second)->Hotload();
// Trigger an interpolate call - needed because the game is generally paused & models disappear otherwise.
m_Simulation.Interpolate(0.f, 0.f, 0.f);
}