Skip to content

Commit b612e92

Browse files
committed
Big refactoring before switching from CMatrix44f to Transform
1 parent fd875e0 commit b612e92

File tree

12 files changed

+257
-163
lines changed

12 files changed

+257
-163
lines changed

rts/Rendering/Common/ModelDrawerData.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ class CModelDrawerDataBase : public CModelDrawerDataConcept
7070
void ClearPreviousDrawFlags() { for (auto object : unsortedObjects) object->previousDrawFlag = 0; }
7171

7272
const ScopedTransformMemAlloc& GetObjectTransformMemAlloc(const T* o) const {
73-
const auto it = matricesMemAllocs.find(const_cast<T*>(o));
74-
return (it != matricesMemAllocs.end()) ? it->second : ScopedTransformMemAlloc::Dummy();
73+
const auto it = scTransMemAllocMap.find(const_cast<T*>(o));
74+
return (it != scTransMemAllocMap.end()) ? it->second : ScopedTransformMemAlloc::Dummy();
7575
}
76-
ScopedTransformMemAlloc& GetObjectTransformMemAlloc(const T* o) { return matricesMemAllocs[const_cast<T*>(o)]; }
76+
ScopedTransformMemAlloc& GetObjectTransformMemAlloc(const T* o) { return scTransMemAllocMap[const_cast<T*>(o)]; }
7777
private:
7878
static constexpr int MMA_SIZE0 = 2 << 16;
7979
protected:
8080
std::array<ModelRenderContainer<T>, MODELTYPE_CNT> modelRenderers;
8181

8282
std::vector<T*> unsortedObjects;
83-
std::unordered_map<T*, ScopedTransformMemAlloc> matricesMemAllocs;
83+
std::unordered_map<T*, ScopedTransformMemAlloc> scTransMemAllocMap;
8484

8585
bool& mtModelDrawer;
8686
};
@@ -100,15 +100,15 @@ inline CModelDrawerDataBase<T>::CModelDrawerDataBase(const std::string& ecName,
100100
: CModelDrawerDataConcept(ecName, ecOrder)
101101
, mtModelDrawer(mtModelDrawer_)
102102
{
103-
matricesMemAllocs.reserve(MMA_SIZE0);
103+
scTransMemAllocMap.reserve(MMA_SIZE0);
104104
for (auto& mr : modelRenderers) { mr.Clear(); }
105105
}
106106

107107
template<typename T>
108108
inline CModelDrawerDataBase<T>::~CModelDrawerDataBase()
109109
{
110110
unsortedObjects.clear();
111-
matricesMemAllocs.clear();
111+
scTransMemAllocMap.clear();
112112
}
113113

114114
template<typename T>
@@ -126,7 +126,7 @@ inline void CModelDrawerDataBase<T>::AddObject(const T* co, bool add)
126126
unsortedObjects.emplace_back(o);
127127

128128
const uint32_t numMatrices = (o->model ? o->model->numPieces : 0) + 1u;
129-
matricesMemAllocs.emplace(o, ScopedTransformMemAlloc(numMatrices));
129+
scTransMemAllocMap.emplace(o, ScopedTransformMemAlloc(numMatrices));
130130

131131
modelUniformsStorage.GetObjOffset(co);
132132
}
@@ -141,7 +141,7 @@ inline void CModelDrawerDataBase<T>::DelObject(const T* co, bool del)
141141
}
142142

143143
if (del && spring::VectorErase(unsortedObjects, o)) {
144-
matricesMemAllocs.erase(o);
144+
scTransMemAllocMap.erase(o);
145145
modelUniformsStorage.GetObjOffset(co);
146146
}
147147
}
@@ -164,8 +164,7 @@ inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)
164164

165165
// from one point it doesn't worth the comparison, cause units usually move
166166
// but having not updated smma[0] allows for longer solid no-update areas in ModelUniformsUploader::UpdateDerived()
167-
if (tmNew != tmOld)
168-
smma[0] = tmNew;
167+
smma.UpdateIfChanged(0, tmNew);
169168

170169
for (int i = 0; i < o->localModel.pieces.size(); ++i) {
171170
const LocalModelPiece& lmp = o->localModel.pieces[i];
@@ -175,11 +174,13 @@ inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)
175174
continue;
176175

177176
if unlikely(!lmp.GetScriptVisible()) {
178-
smma[i + 1] = CMatrix44f::Zero();
177+
//smma[i + 1] = CMatrix44f::Zero();
178+
smma.UpdateForced(i + 1, CMatrix44f::Zero());
179179
continue;
180180
}
181181

182-
smma[i + 1] = lmp.GetModelSpaceMatrix();
182+
// UpdateIfChanged is not needed, wasCustomDirty takes that role
183+
smma.UpdateForced(i + 1, lmp.GetModelSpaceMatrix());
183184
}
184185
}
185186

rts/Rendering/Common/UpdateList.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ void UpdateList::ResetNeedUpdateAll()
2424
changed = false;
2525
}
2626

27+
void UpdateList::Trim(size_t newLessThanOrEqualSize)
28+
{
29+
RECOIL_DETAILED_TRACY_ZONE;
30+
assert(newLessThanOrEqualSize <= updateList.size());
31+
updateList.resize(newLessThanOrEqualSize);
32+
// no need to modify the update status
33+
}
34+
35+
void UpdateList::SetUpdate(size_t first, size_t count)
36+
{
37+
RECOIL_DETAILED_TRACY_ZONE;
38+
39+
auto beg = updateList.begin() + first;
40+
auto end = beg + count;
41+
42+
SetUpdate(UpdateList::IteratorPair(beg, end));
43+
}
44+
2745
void UpdateList::SetUpdate(const UpdateList::IteratorPair& it)
2846
{
2947
RECOIL_DETAILED_TRACY_ZONE;
@@ -60,7 +78,7 @@ void UpdateList::PopBack(size_t N)
6078
changed = true;
6179
}
6280

63-
std::optional<UpdateList::IteratorPair> UpdateList::GetNext(const std::optional<UpdateList::IteratorPair>& prev)
81+
std::optional<UpdateList::ConstIteratorPair> UpdateList::GetNext(const std::optional<UpdateList::ConstIteratorPair>& prev) const
6482
{
6583
RECOIL_DETAILED_TRACY_ZONE;
6684
auto beg = prev.has_value() ? prev.value().second : updateList.begin();
@@ -73,7 +91,7 @@ std::optional<UpdateList::IteratorPair> UpdateList::GetNext(const std::optional<
7391
return std::make_optional(std::make_pair(beg, end));
7492
}
7593

76-
std::pair<size_t, size_t> UpdateList::GetOffsetAndSize(const UpdateList::IteratorPair& it)
94+
std::pair<size_t, size_t> UpdateList::GetOffsetAndSize(const UpdateList::ConstIteratorPair& it) const
7795
{
7896
RECOIL_DETAILED_TRACY_ZONE;
7997
return std::make_pair(

rts/Rendering/Common/UpdateList.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88
class UpdateList {
99
CR_DECLARE_STRUCT(UpdateList)
1010
public:
11+
using ConstIteratorPair = std::pair<std::vector<bool>::const_iterator, std::vector<bool>::const_iterator>;
1112
using IteratorPair = std::pair<std::vector<bool>::iterator, std::vector<bool>::iterator>;
1213
public:
1314
UpdateList()
1415
: updateList()
15-
, changed(true)
16+
, changed(false)
17+
{}
18+
UpdateList(size_t initialSize)
19+
: updateList(initialSize)
20+
, changed(initialSize > 0)
1621
{}
1722

1823
size_t Size() const { return updateList.size(); }
1924
size_t Capacity() const { return updateList.capacity(); }
2025

26+
void Trim(size_t newLessThanOrEqualSize);
2127
void Resize(size_t newSize) { updateList.resize(newSize); SetNeedUpdateAll(); }
2228
void Reserve(size_t reservedSize) { updateList.reserve(reservedSize); }
2329

30+
void SetUpdate(size_t first, size_t count);
2431
void SetUpdate(const IteratorPair& it);
2532
void SetUpdate(size_t offset);
2633

@@ -33,8 +40,8 @@ class UpdateList {
3340

3441
bool NeedUpdate() const { return changed; }
3542

36-
std::optional<IteratorPair> GetNext(const std::optional<IteratorPair>& prev = std::nullopt);
37-
std::pair<size_t, size_t> GetOffsetAndSize(const IteratorPair& it);
43+
std::optional<ConstIteratorPair> GetNext(const std::optional<ConstIteratorPair>& prev = std::nullopt) const;
44+
std::pair<size_t, size_t> GetOffsetAndSize(const ConstIteratorPair& it) const;
3845
private:
3946
std::vector<bool> updateList;
4047
bool changed;

rts/Rendering/Models/3DModel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,15 @@ void S3DModel::SetPieceMatrices()
763763
// use this occasion and copy bpose matrices
764764
for (size_t i = 0; i < pieceObjects.size(); ++i) {
765765
const auto* po = pieceObjects[i];
766-
traAlloc[0 + i] = po->bposeTransform.ToMatrix();
766+
//traAlloc[0 + i] = po->bposeTransform.ToMatrix();
767+
traAlloc.UpdateForced((0 + i), po->bposeTransform.ToMatrix());
767768
}
768769

769770
// use this occasion and copy inverse bpose matrices
770771
// store them right after all bind pose matrices
771772
for (size_t i = 0; i < pieceObjects.size(); ++i) {
772773
const auto* po = pieceObjects[i];
773-
traAlloc[numPieces + i] = po->bposeInvTransform.ToMatrix();
774+
//traAlloc[numPieces + i] = po->bposeInvTransform.ToMatrix();
775+
traAlloc.UpdateForced((numPieces + i), po->bposeInvTransform.ToMatrix());
774776
}
775777
}

rts/Rendering/Models/ModelsMemStorage.cpp

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,31 @@ ModelUniformsStorage modelUniformsStorage;
99
ModelUniformsStorage::ModelUniformsStorage()
1010
{
1111
RECOIL_DETAILED_TRACY_ZONE;
12-
storage[0] = dummy;
13-
objectsMap.emplace(nullptr, 0);
12+
storage[AddObjects(static_cast<const CWorldObject*>(nullptr))] = dummy;
13+
}
14+
15+
ModelUniformsStorage::~ModelUniformsStorage()
16+
{
17+
// just in case
18+
DelObjects(static_cast<const CWorldObject*>(nullptr));
1419
}
1520

1621
size_t ModelUniformsStorage::AddObjects(const CWorldObject* o)
1722
{
1823
RECOIL_DETAILED_TRACY_ZONE;
1924
const size_t idx = storage.Add(ModelUniformData());
2025
objectsMap[const_cast<CWorldObject*>(o)] = idx;
26+
27+
if (idx + 1 == storage.size()) {
28+
//new item got added to the end of storage
29+
updateList.EmplaceBackUpdate();
30+
} else {
31+
// storage got updated somewhere in the middle, use updateList.SetUpdate()
32+
updateList.SetUpdate(idx);
33+
}
34+
35+
assert(storage.size() == updateList.Size());
36+
2137
return idx;
2238
}
2339

@@ -28,7 +44,18 @@ void ModelUniformsStorage::DelObjects(const CWorldObject* o)
2844
assert(it != objectsMap.end());
2945

3046
storage.Del(it->second);
47+
48+
if (storage.size() < updateList.Size()) {
49+
// storage got one element shorter, trim updateList as well
50+
updateList.Trim(it->second);
51+
} else {
52+
// storage got updated somewhere in the middle, use updateList.SetUpdate()
53+
updateList.SetUpdate(it->second);
54+
}
55+
3156
objectsMap.erase(it);
57+
58+
assert(storage.size() == updateList.Size());
3259
}
3360

3461
size_t ModelUniformsStorage::GetObjOffset(const CWorldObject* o)
@@ -49,9 +76,43 @@ ModelUniformsStorage::MyType& ModelUniformsStorage::GetObjUniformsArray(const CW
4976
return storage[offset];
5077
}
5178

52-
void TransformsMemStorage::SetAllDirty()
79+
TransformsMemStorage::TransformsMemStorage()
80+
: storage(StablePosAllocator<MyType>(INIT_NUM_ELEMS))
81+
, updateList(INIT_NUM_ELEMS)
82+
{}
83+
84+
void TransformsMemStorage::Reset()
5385
{
54-
RECOIL_DETAILED_TRACY_ZONE;
5586
assert(Threading::IsMainThread());
56-
std::fill(dirtyMap.begin(), dirtyMap.end(), BUFFERING);
87+
storage.Reset();
88+
updateList.Trim(storage.GetSize());
89+
}
90+
91+
size_t TransformsMemStorage::Allocate(size_t numElems)
92+
{
93+
auto lock = CModelsLock::GetScopedLock();
94+
95+
auto res = storage.Allocate(numElems);
96+
updateList.Resize(storage.GetSize());
97+
98+
assert(updateList.Size() == storage.GetSize());
99+
100+
return res;
101+
}
102+
103+
void TransformsMemStorage::Free(size_t firstElem, size_t numElems, const MyType* T0)
104+
{
105+
auto lock = CModelsLock::GetScopedLock();
106+
107+
storage.Free(firstElem, numElems, T0);
108+
updateList.SetUpdate(firstElem, numElems);
109+
updateList.Trim(storage.GetSize());
110+
111+
assert(updateList.Size() == storage.GetSize());
57112
}
113+
114+
const TransformsMemStorage::MyType& TransformsMemStorage::operator[](std::size_t idx) const
115+
{
116+
auto lock = CModelsLock::GetScopedLock();
117+
return storage[idx];
118+
}

0 commit comments

Comments
 (0)