CParticleDatabase: Make use of heterogenous lookup

Resolves a TODO. (Technically this can be done with C++17 only, as
std::map allows heterogenous lookup since that standard).

Reduces number of allocations necessary for lookups.
This commit is contained in:
Lioncash 2020-05-27 06:40:16 -04:00
parent 055e4a8bec
commit a0bdb2588f
2 changed files with 57 additions and 60 deletions

View File

@ -35,11 +35,11 @@ void CParticleDatabase::CacheParticleDesc(const CCharacterInfo::CParticleResData
}
}
void CParticleDatabase::SetModulationColorAllActiveEffectsForParticleDB(
const zeus::CColor& color, std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map) {
void CParticleDatabase::SetModulationColorAllActiveEffectsForParticleDB(const zeus::CColor& color, DrawMap& map) {
for (auto& e : map) {
if (e.second)
if (e.second) {
e.second->SetModulationColor(color);
}
}
}
@ -52,8 +52,7 @@ void CParticleDatabase::SetModulationColorAllActiveEffects(const zeus::CColor& c
SetModulationColorAllActiveEffectsForParticleDB(color, xa0_lastDraw);
}
void CParticleDatabase::SuspendAllActiveEffectsForParticleDB(
CStateManager& mgr, std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map) {
void CParticleDatabase::SuspendAllActiveEffectsForParticleDB(CStateManager& mgr, DrawMap& map) {
for (auto& e : map) {
e.second->SetParticleEmission(false, mgr);
}
@ -65,8 +64,7 @@ void CParticleDatabase::SuspendAllActiveEffects(CStateManager& stateMgr) {
SuspendAllActiveEffectsForParticleDB(stateMgr, x64_lastDrawLoop);
}
void CParticleDatabase::DeleteAllLightsForParticleDB(CStateManager& mgr,
std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map) {
void CParticleDatabase::DeleteAllLightsForParticleDB(CStateManager& mgr, DrawMap& map) {
for (auto& e : map) {
e.second->DeleteLight(mgr);
}
@ -83,9 +81,7 @@ void CParticleDatabase::DeleteAllLights(CStateManager& mgr) {
void CParticleDatabase::UpdateParticleGenDB(float dt, const CPoseAsTransforms& pose, const CCharLayoutInfo& charInfo,
const zeus::CTransform& xf, const zeus::CVector3f& scale,
CStateManager& stateMgr,
std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map,
bool deleteIfDone) {
CStateManager& stateMgr, DrawMap& map, bool deleteIfDone) {
for (auto it = map.begin(); it != map.end();) {
CParticleGenInfo& info = *it->second;
if (info.GetIsActive()) {
@ -204,14 +200,13 @@ void CParticleDatabase::Update(float dt, const CPoseAsTransforms& pose, const CC
(x50_firstDrawLoop.size() || x64_lastDrawLoop.size() || x8c_firstDraw.size() || xa0_lastDraw.size());
}
void CParticleDatabase::RenderParticleGenMap(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map) {
void CParticleDatabase::RenderParticleGenMap(const DrawMap& map) {
for (const auto& e : map) {
e.second->Render();
}
}
void CParticleDatabase::RenderParticleGenMapMasked(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map,
int mask, int target) {
void CParticleDatabase::RenderParticleGenMapMasked(const DrawMap& map, int mask, int target) {
for (const auto& e : map) {
if ((e.second->GetFlags() & mask) == target) {
e.second->Render();
@ -219,8 +214,7 @@ void CParticleDatabase::RenderParticleGenMapMasked(const std::map<std::string, s
}
}
void CParticleDatabase::AddToRendererClippedParticleGenMap(
const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map, const zeus::CFrustum& frustum) {
void CParticleDatabase::AddToRendererClippedParticleGenMap(const DrawMap& map, const zeus::CFrustum& frustum) {
for (const auto& e : map) {
const auto bounds = e.second->GetBounds();
if (bounds && frustum.aabbFrustumTest(*bounds)) {
@ -229,9 +223,8 @@ void CParticleDatabase::AddToRendererClippedParticleGenMap(
}
}
void CParticleDatabase::AddToRendererClippedParticleGenMapMasked(
const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map, const zeus::CFrustum& frustum, int mask,
int target) {
void CParticleDatabase::AddToRendererClippedParticleGenMapMasked(const DrawMap& map, const zeus::CFrustum& frustum,
int mask, int target) {
for (const auto& e : map) {
if ((e.second->GetFlags() & mask) == target) {
const auto bounds = e.second->GetBounds();
@ -273,25 +266,30 @@ void CParticleDatabase::AddToRendererClipped(const zeus::CFrustum& frustum) cons
}
CParticleGenInfo* CParticleDatabase::GetParticleEffect(std::string_view name) const {
// TODO: Heterogeneous lookup when C++20 available
auto search = x3c_rendererDrawLoop.find(name.data());
if (search != x3c_rendererDrawLoop.end())
if (const auto search = x3c_rendererDrawLoop.find(name); search != x3c_rendererDrawLoop.cend()) {
return search->second.get();
search = x50_firstDrawLoop.find(name.data());
if (search != x50_firstDrawLoop.end())
}
if (const auto search = x50_firstDrawLoop.find(name); search != x50_firstDrawLoop.cend()) {
return search->second.get();
search = x64_lastDrawLoop.find(name.data());
if (search != x64_lastDrawLoop.end())
}
if (const auto search = x64_lastDrawLoop.find(name); search != x64_lastDrawLoop.cend()) {
return search->second.get();
search = x78_rendererDraw.find(name.data());
if (search != x78_rendererDraw.end())
}
if (const auto search = x78_rendererDraw.find(name); search != x78_rendererDraw.cend()) {
return search->second.get();
search = x8c_firstDraw.find(name.data());
if (search != x8c_firstDraw.end())
}
if (const auto search = x8c_firstDraw.find(name); search != x8c_firstDraw.cend()) {
return search->second.get();
search = xa0_lastDraw.find(name.data());
if (search != xa0_lastDraw.end())
}
if (const auto search = xa0_lastDraw.find(name); search != xa0_lastDraw.cend()) {
return search->second.get();
}
return nullptr;
}
@ -424,27 +422,30 @@ void CParticleDatabase::AddParticleEffect(std::string_view name, int flags, cons
void CParticleDatabase::InsertParticleGen(bool oneShot, int flags, std::string_view name,
std::unique_ptr<CParticleGenInfo>&& gen) {
std::map<std::string, std::unique_ptr<CParticleGenInfo>>* useMap;
DrawMap* useMap;
if (oneShot) {
if (flags & 0x40)
if ((flags & 0x40) != 0) {
useMap = &xa0_lastDraw;
else if (flags & 0x20)
} else if ((flags & 0x20) != 0) {
useMap = &x8c_firstDraw;
else
} else {
useMap = &x78_rendererDraw;
}
} else {
if (flags & 0x40)
if ((flags & 0x40) != 0) {
useMap = &x64_lastDrawLoop;
else if (flags & 0x20)
} else if ((flags & 0x20) != 0) {
useMap = &x50_firstDrawLoop;
else
} else {
useMap = &x3c_rendererDrawLoop;
}
}
useMap->insert(std::make_pair(std::string(name), std::move(gen)));
if (flags & 0x60)
if ((flags & 0x60) != 0) {
xb4_25_anySystemsDrawnWithModel = true;
}
}
} // namespace urde

View File

@ -19,35 +19,31 @@ class CCharLayoutInfo;
class CPoseAsTransforms;
class CParticleDatabase {
using DrawMap = std::map<std::string, std::unique_ptr<CParticleGenInfo>, std::less<>>;
std::map<CAssetId, std::shared_ptr<TLockedToken<CGenDescription>>> x0_particleDescs;
std::map<CAssetId, std::shared_ptr<TLockedToken<CSwooshDescription>>> x14_swooshDescs;
std::map<CAssetId, std::shared_ptr<TLockedToken<CElectricDescription>>> x28_electricDescs;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> x3c_rendererDrawLoop;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> x50_firstDrawLoop;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> x64_lastDrawLoop;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> x78_rendererDraw;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> x8c_firstDraw;
std::map<std::string, std::unique_ptr<CParticleGenInfo>> xa0_lastDraw;
DrawMap x3c_rendererDrawLoop;
DrawMap x50_firstDrawLoop;
DrawMap x64_lastDrawLoop;
DrawMap x78_rendererDraw;
DrawMap x8c_firstDraw;
DrawMap xa0_lastDraw;
bool xb4_24_updatesEnabled : 1 = true;
bool xb4_25_anySystemsDrawnWithModel : 1 = false;
static void SetModulationColorAllActiveEffectsForParticleDB(
const zeus::CColor& color, std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map);
static void SuspendAllActiveEffectsForParticleDB(CStateManager& mgr,
std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map);
static void DeleteAllLightsForParticleDB(CStateManager& mgr,
std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map);
static void RenderParticleGenMap(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map);
static void RenderParticleGenMapMasked(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map, int mask,
int target);
static void AddToRendererClippedParticleGenMap(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map,
const zeus::CFrustum& frustum);
static void
AddToRendererClippedParticleGenMapMasked(const std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map,
const zeus::CFrustum& frustum, int mask, int target);
static void SetModulationColorAllActiveEffectsForParticleDB(const zeus::CColor& color, DrawMap& map);
static void SuspendAllActiveEffectsForParticleDB(CStateManager& mgr, DrawMap& map);
static void DeleteAllLightsForParticleDB(CStateManager& mgr, DrawMap& map);
static void RenderParticleGenMap(const DrawMap& map);
static void RenderParticleGenMapMasked(const DrawMap& map, int mask, int target);
static void AddToRendererClippedParticleGenMap(const DrawMap& map, const zeus::CFrustum& frustum);
static void AddToRendererClippedParticleGenMapMasked(const DrawMap& map, const zeus::CFrustum& frustum, int mask,
int target);
static void UpdateParticleGenDB(float dt, const CPoseAsTransforms& pose, const CCharLayoutInfo& charInfo,
const zeus::CTransform& xf, const zeus::CVector3f& vec, CStateManager& stateMgr,
std::map<std::string, std::unique_ptr<CParticleGenInfo>>& map, bool deleteIfDone);
DrawMap& map, bool deleteIfDone);
public:
CParticleDatabase();