Skip to content

Commit

Permalink
Another piece of valueable scrap from BAR-quat: separate UpdateList c…
Browse files Browse the repository at this point in the history
…lass - useful to keep track of what items got updated (#1932)
  • Loading branch information
lhog authored Feb 8, 2025
1 parent 4f74ff1 commit e6f1fc4
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 100 deletions.
1 change: 1 addition & 0 deletions rts/Rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ set(sources_engine_Rendering
"${CMAKE_CURRENT_SOURCE_DIR}/Common/ModelDrawerData.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Common/ModelDrawerState.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Common/ModelDrawerHelpers.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Common/UpdateList.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Features/FeatureDrawerData.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Features/FeatureDrawer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Units/UnitDrawerData.cpp"
Expand Down
101 changes: 101 additions & 0 deletions rts/Rendering/Common/UpdateList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "UpdateList.h"
#include "System/Misc/TracyDefs.h"

#include <algorithm>

CR_BIND(UpdateList, )
CR_REG_METADATA(UpdateList, (
CR_MEMBER(updateList),
CR_MEMBER(changed)
))


void UpdateList::SetNeedUpdateAll()
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(updateList.begin(), updateList.end(), true);
changed = true;
}

void UpdateList::ResetNeedUpdateAll()
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(updateList.begin(), updateList.end(), false);
changed = false;
}

void UpdateList::Trim(size_t newLessThanOrEqualSize)
{
RECOIL_DETAILED_TRACY_ZONE;
assert(newLessThanOrEqualSize <= updateList.size());
updateList.resize(newLessThanOrEqualSize);
// no need to modify the update status
}

void UpdateList::SetUpdate(size_t first, size_t count)
{
RECOIL_DETAILED_TRACY_ZONE;

auto beg = updateList.begin() + first;
auto end = beg + count;

SetUpdate(UpdateList::IteratorPair(beg, end));
}

void UpdateList::SetUpdate(const UpdateList::IteratorPair& it)
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(it.first, it.second, true);
changed = true;
}

void UpdateList::SetUpdate(size_t offset)
{
RECOIL_DETAILED_TRACY_ZONE;
assert(offset < updateList.size());
updateList[offset] = true;
changed = true;
}

void UpdateList::EmplaceBackUpdate()
{
RECOIL_DETAILED_TRACY_ZONE;
updateList.emplace_back(true);
changed = true;
}

void UpdateList::PopBack()
{
updateList.pop_back();
changed = true;
}

void UpdateList::PopBack(size_t N)
{
while (N-- >= 0)
updateList.pop_back();

changed = true;
}

std::optional<UpdateList::ConstIteratorPair> UpdateList::GetNext(const std::optional<UpdateList::ConstIteratorPair>& prev) const
{
RECOIL_DETAILED_TRACY_ZONE;
auto beg = prev.has_value() ? prev.value().second : updateList.begin();
beg = std::find(beg, updateList.end(), true);
auto end = std::find(beg, updateList.end(), false);

if (beg == end)
return std::nullopt;

return std::make_optional(std::make_pair(beg, end));
}

std::pair<size_t, size_t> UpdateList::GetOffsetAndSize(const UpdateList::ConstIteratorPair& it) const
{
RECOIL_DETAILED_TRACY_ZONE;
return std::make_pair(
std::distance(updateList.begin(), it.first ),
std::distance(it.first , it.second)
);
}
50 changes: 50 additions & 0 deletions rts/Rendering/Common/UpdateList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <tuple>
#include <optional>
#include <vector>
#include "System/creg/creg_cond.h"

class UpdateList {
CR_DECLARE_STRUCT(UpdateList)
public:
using ConstIteratorPair = std::pair<std::vector<bool>::const_iterator, std::vector<bool>::const_iterator>;
using IteratorPair = std::pair<std::vector<bool>::iterator, std::vector<bool>::iterator>;
public:
UpdateList()
: updateList()
, changed(false)
{}
UpdateList(size_t initialSize)
: updateList(initialSize)
, changed(initialSize > 0)
{}

size_t Size() const { return updateList.size(); }
size_t Capacity() const { return updateList.capacity(); }
bool Empty() const { return updateList.empty(); }

void Trim(size_t newLessThanOrEqualSize);
void Resize(size_t newSize) { updateList.resize(newSize); SetNeedUpdateAll(); }
void Reserve(size_t reservedSize) { updateList.reserve(reservedSize); }
void Clear() { *this = std::move(UpdateList()); }

void SetUpdate(size_t first, size_t count);
void SetUpdate(const IteratorPair& it);
void SetUpdate(size_t offset);

void SetNeedUpdateAll();
void ResetNeedUpdateAll();

void EmplaceBackUpdate();
void PopBack();
void PopBack(size_t N);

bool NeedUpdate() const { return changed; }

std::optional<ConstIteratorPair> GetNext(const std::optional<ConstIteratorPair>& prev = std::nullopt) const;
std::pair<size_t, size_t> GetOffsetAndSize(const ConstIteratorPair& it) const;
private:
std::vector<bool> updateList;
bool changed;
};
69 changes: 1 addition & 68 deletions rts/Rendering/Env/Decals/GroundDecalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ CR_REG_METADATA_SUB(CGroundDecalHandler, UnitMinMaxHeight,
CR_MEMBER(max)
))

CR_BIND(CGroundDecalHandler::DecalUpdateList, )
CR_REG_METADATA_SUB(CGroundDecalHandler, DecalUpdateList,
(
CR_MEMBER(updateList),
CR_MEMBER(changed)
))

CR_BIND_DERIVED(CGroundDecalHandler, IGroundDecalDrawer, )
CR_REG_METADATA(CGroundDecalHandler, (
CR_MEMBER_UN(maxUniqueScars),
Expand Down Expand Up @@ -1673,64 +1666,4 @@ void CGroundDecalHandler::FeatureMoved(const CFeature* feature, const float3& ol
void CGroundDecalHandler::UnitLoaded(const CUnit* unit, const CUnit* transport) { ForceRemoveSolidObject(unit); }
void CGroundDecalHandler::UnitUnloaded(const CUnit* unit, const CUnit* transport) { AddSolidObject(unit); }

void CGroundDecalHandler::UnitMoved(const CUnit* unit) { AddTrack(unit, unit->pos); }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CGroundDecalHandler::DecalUpdateList::SetNeedUpdateAll()
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(updateList.begin(), updateList.end(), true);
changed = true;
}

void CGroundDecalHandler::DecalUpdateList::ResetNeedUpdateAll()
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(updateList.begin(), updateList.end(), false);
changed = false;
}

void CGroundDecalHandler::DecalUpdateList::SetUpdate(const CGroundDecalHandler::DecalUpdateList::IteratorPair& it)
{
RECOIL_DETAILED_TRACY_ZONE;
std::fill(it.first, it.second, true);
changed = true;
}

void CGroundDecalHandler::DecalUpdateList::SetUpdate(size_t offset)
{
RECOIL_DETAILED_TRACY_ZONE;
assert(offset < updateList.size());
updateList[offset] = true;
changed = true;
}

void CGroundDecalHandler::DecalUpdateList::EmplaceBackUpdate()
{
RECOIL_DETAILED_TRACY_ZONE;
updateList.emplace_back(true);
changed = true;
}

std::optional<CGroundDecalHandler::DecalUpdateList::IteratorPair> CGroundDecalHandler::DecalUpdateList::GetNext(const std::optional<CGroundDecalHandler::DecalUpdateList::IteratorPair>& prev)
{
RECOIL_DETAILED_TRACY_ZONE;
auto beg = prev.has_value() ? prev.value().second : updateList.begin();
beg = std::find(beg, updateList.end(), true);
auto end = std::find(beg, updateList.end(), false);

if (beg == end)
return std::nullopt;

return std::make_optional(std::make_pair(beg, end));
}

std::pair<size_t, size_t> CGroundDecalHandler::DecalUpdateList::GetOffsetAndSize(const CGroundDecalHandler::DecalUpdateList::IteratorPair& it)
{
RECOIL_DETAILED_TRACY_ZONE;
return std::make_pair(
std::distance(updateList.begin(), it.first ),
std::distance(it.first , it.second)
);
}
void CGroundDecalHandler::UnitMoved(const CUnit* unit) { AddTrack(unit, unit->pos); }
34 changes: 2 additions & 32 deletions rts/Rendering/Env/Decals/GroundDecalHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Rendering/GL/VAO.h"
#include "Rendering/DepthBufferCopy.h"
#include "Rendering/Textures/TextureRenderAtlas.h"
#include "Rendering/Common/UpdateList.h"
#include "System/EventClient.h"
#include "System/UnorderedMap.hpp"
#include "System/creg/creg.h"
Expand All @@ -36,37 +37,6 @@ class CGroundDecalHandler: public IGroundDecalDrawer, public CEventClient, publi
{
CR_DECLARE_DERIVED(CGroundDecalHandler)
CR_DECLARE_SUB(UnitMinMaxHeight)
CR_DECLARE_SUB(DecalUpdateList)
public:
class DecalUpdateList {
CR_DECLARE_STRUCT(DecalUpdateList)
public:
using IteratorPair = std::pair<std::vector<bool>::iterator, std::vector<bool>::iterator>;
public:
DecalUpdateList()
: updateList()
, changed(true)
{}

void Resize(size_t newSize) { updateList.resize(newSize); SetNeedUpdateAll(); }
void Reserve(size_t reservedSize) { updateList.reserve(reservedSize); }

void SetUpdate(const IteratorPair& it);
void SetUpdate(size_t offset);

void SetNeedUpdateAll();
void ResetNeedUpdateAll();

void EmplaceBackUpdate();

bool NeedUpdate() const { return changed; }

std::optional<IteratorPair> GetNext(const std::optional<IteratorPair>& prev = std::nullopt);
std::pair<size_t, size_t> GetOffsetAndSize(const IteratorPair& it);
private:
std::vector<bool> updateList;
bool changed;
};
public:
CGroundDecalHandler();
~CGroundDecalHandler() override;
Expand Down Expand Up @@ -195,7 +165,7 @@ class CGroundDecalHandler: public IGroundDecalDrawer, public CEventClient, publi
spring::unordered_map<uint32_t, size_t> idToPos;
spring::unordered_map<uint32_t, std::tuple<const CColorMap*, std::pair<size_t, size_t>>> idToCmInfo;

DecalUpdateList decalsUpdateList;
UpdateList decalsUpdateList;

uint32_t nextId;
std::vector<uint32_t> freeIds;
Expand Down

0 comments on commit e6f1fc4

Please sign in to comment.