Skip to content

Commit 822bd35

Browse files
committed
UPBGE: Don't directly allocate on heap RAS_MeshSlot.
RAS_MeshSlot are owned by RAS_MeshUser, there's no need to allocate them on heap to after hold their pointers in a list, just allocate in the list directly. Tested with sintel desert level.
1 parent 20e9ff0 commit 822bd35

File tree

8 files changed

+39
-35
lines changed

8 files changed

+39
-35
lines changed

source/gameengine/Ketsji/KX_FontObject.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ void KX_FontObject::AddMeshUser()
136136
RAS_BucketManager *bucketManager = GetScene()->GetBucketManager();
137137
RAS_DisplayArrayBucket *arrayBucket = bucketManager->GetTextDisplayArrayBucket();
138138

139-
RAS_MeshSlot *ms = new RAS_MeshSlot(m_meshUser, arrayBucket);
140-
m_meshUser->AddMeshSlot(ms);
139+
m_meshUser->NewMeshSlot(arrayBucket);
141140
}
142141

143142
void KX_FontObject::UpdateBuckets()

source/gameengine/Rasterizer/RAS_BatchGroup.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ RAS_BatchGroup *RAS_BatchGroup::RemoveMeshUser()
7070
return this;
7171
}
7272

73-
bool RAS_BatchGroup::MergeMeshSlot(RAS_BatchGroup::Batch& batch, RAS_MeshSlot *slot, const mt::mat4& mat)
73+
bool RAS_BatchGroup::MergeMeshSlot(RAS_BatchGroup::Batch& batch, RAS_MeshSlot& slot, const mt::mat4& mat)
7474
{
75-
RAS_DisplayArrayBucket *origArrayBucket = slot->m_displayArrayBucket;
75+
RAS_DisplayArrayBucket *origArrayBucket = slot.m_displayArrayBucket;
7676
RAS_IDisplayArray *origArray = origArrayBucket->GetDisplayArray();
7777
RAS_DisplayArrayBucket *arrayBucket = batch.m_displayArrayBucket;
7878
RAS_IBatchDisplayArray *array = batch.m_displayArray;
7979

80-
if (batch.m_originalDisplayArrayBucketList.find(slot) != batch.m_originalDisplayArrayBucketList.end()) {
80+
if (batch.m_originalDisplayArrayBucketList.find(&slot) != batch.m_originalDisplayArrayBucketList.end()) {
8181
CM_Error("could not merge twice a mesh");
8282
return false;
8383
}
@@ -89,21 +89,21 @@ bool RAS_BatchGroup::MergeMeshSlot(RAS_BatchGroup::Batch& batch, RAS_MeshSlot *s
8989
}
9090

9191
// Store original display array bucket.
92-
batch.m_originalDisplayArrayBucketList[slot] = origArrayBucket;
93-
batch.m_meshSlots.push_back(slot);
92+
batch.m_originalDisplayArrayBucketList[&slot] = origArrayBucket;
93+
batch.m_meshSlots.push_back(&slot);
9494

9595
// Merge display array.
9696
const unsigned int index = array->Merge(origArray, mat);
97-
slot->m_batchPartIndex = index;
97+
slot.m_batchPartIndex = index;
9898

99-
slot->SetDisplayArrayBucket(arrayBucket);
99+
slot.SetDisplayArrayBucket(arrayBucket);
100100

101101
return true;
102102
}
103103

104-
bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot *slot)
104+
bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot& slot)
105105
{
106-
RAS_MaterialBucket *bucket = slot->m_displayArrayBucket->GetBucket();
106+
RAS_MaterialBucket *bucket = slot.m_displayArrayBucket->GetBucket();
107107
RAS_IPolyMaterial *material = bucket->GetPolyMaterial();
108108

109109
std::map<RAS_IPolyMaterial *, Batch>::iterator bit = m_batchs.find(material);
@@ -114,23 +114,23 @@ bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot *slot)
114114

115115
Batch& batch = bit->second;
116116

117-
RAS_DisplayArrayBucket *origArrayBucket = batch.m_originalDisplayArrayBucketList[slot];
117+
RAS_DisplayArrayBucket *origArrayBucket = batch.m_originalDisplayArrayBucketList[&slot];
118118

119119
if (!origArrayBucket) {
120120
CM_Error("could not restore mesh");
121121
return false;
122122
}
123123

124-
slot->SetDisplayArrayBucket(origArrayBucket);
124+
slot.SetDisplayArrayBucket(origArrayBucket);
125125

126-
batch.m_displayArray->Split(slot->m_batchPartIndex);
126+
batch.m_displayArray->Split(slot.m_batchPartIndex);
127127

128-
batch.m_originalDisplayArrayBucketList.erase(slot);
128+
batch.m_originalDisplayArrayBucketList.erase(&slot);
129129

130-
slot->m_batchPartIndex = -1;
130+
slot.m_batchPartIndex = -1;
131131

132132
// One part is removed and then all the part after must use an index smaller of one.
133-
RAS_MeshSlotList::iterator mit = batch.m_meshSlots.erase(std::find(batch.m_meshSlots.begin(), batch.m_meshSlots.end(), slot));
133+
RAS_MeshSlotList::iterator mit = batch.m_meshSlots.erase(std::find(batch.m_meshSlots.begin(), batch.m_meshSlots.end(), &slot));
134134
for (RAS_MeshSlotList::iterator it = mit, end = batch.m_meshSlots.end(); it != end; ++it) {
135135
RAS_MeshSlot *meshSlot = *it;
136136
--meshSlot->m_batchPartIndex;
@@ -141,8 +141,8 @@ bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot *slot)
141141

142142
bool RAS_BatchGroup::MergeMeshUser(RAS_MeshUser *meshUser, const mt::mat4& mat)
143143
{
144-
for (RAS_MeshSlot *meshSlot : meshUser->GetMeshSlots()) {
145-
RAS_DisplayArrayBucket *arrayBucket = meshSlot->m_displayArrayBucket;
144+
for (RAS_MeshSlot& meshSlot : meshUser->GetMeshSlots()) {
145+
RAS_DisplayArrayBucket *arrayBucket = meshSlot.m_displayArrayBucket;
146146
RAS_MaterialBucket *bucket = arrayBucket->GetBucket();
147147
RAS_IPolyMaterial *material = bucket->GetPolyMaterial();
148148

@@ -167,7 +167,7 @@ bool RAS_BatchGroup::MergeMeshUser(RAS_MeshUser *meshUser, const mt::mat4& mat)
167167

168168
bool RAS_BatchGroup::SplitMeshUser(RAS_MeshUser *meshUser)
169169
{
170-
for (RAS_MeshSlot *meshSlot : meshUser->GetMeshSlots()) {
170+
for (RAS_MeshSlot& meshSlot : meshUser->GetMeshSlots()) {
171171
if (!SplitMeshSlot(meshSlot)) {
172172
return false;
173173
}

source/gameengine/Rasterizer/RAS_BatchGroup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class RAS_BatchGroup
6363
* \param mat The transform matrix to apply to vertices during merging.
6464
* \return false on failure.
6565
*/
66-
bool MergeMeshSlot(Batch& batch, RAS_MeshSlot *slot, const mt::mat4& mat);
66+
bool MergeMeshSlot(Batch& batch, RAS_MeshSlot& slot, const mt::mat4& mat);
6767

6868
/** Split the part representing the display array containing in the passed mesh slot.
6969
* \param slot The mesh slot using the display array to split.
7070
* \return false on failure.
7171
*/
72-
bool SplitMeshSlot(RAS_MeshSlot *slot);
72+
bool SplitMeshSlot(RAS_MeshSlot& slot);
7373

7474
public:
7575
RAS_BatchGroup();

source/gameengine/Rasterizer/RAS_Mesh.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ RAS_MeshUser *RAS_Mesh::AddMeshUser(void *clientobj, RAS_Deformer *deformer)
213213
for (unsigned short i = 0, nummat = m_materials.size(); i < nummat; ++i) {
214214
RAS_DisplayArrayBucket *arrayBucket = (deformer) ?
215215
deformer->GetDisplayArrayBucket(i) : m_materials[i]->GetDisplayArrayBucket();
216-
RAS_MeshSlot *ms = new RAS_MeshSlot(meshUser, arrayBucket);
217-
meshUser->AddMeshSlot(ms);
216+
meshUser->NewMeshSlot(arrayBucket);
218217
}
219218
return meshUser;
220219
}

source/gameengine/Rasterizer/RAS_MeshSlot.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ RAS_MeshSlot::~RAS_MeshSlot()
6060
{
6161
}
6262

63+
RAS_MeshSlot::RAS_MeshSlot(const RAS_MeshSlot& other)
64+
:m_node(this, &dummyNodeData, &RAS_MeshSlot::RunNode, nullptr),
65+
m_displayArrayBucket(other.m_displayArrayBucket),
66+
m_meshUser(other.m_meshUser),
67+
m_batchPartIndex(other.m_batchPartIndex)
68+
{
69+
}
70+
6371
void RAS_MeshSlot::SetDisplayArrayBucket(RAS_DisplayArrayBucket *arrayBucket)
6472
{
6573
m_displayArrayBucket = arrayBucket;

source/gameengine/Rasterizer/RAS_MeshSlot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class RAS_MeshSlot
5353
short m_batchPartIndex;
5454

5555
RAS_MeshSlot(RAS_MeshUser *meshUser, RAS_DisplayArrayBucket *arrayBucket);
56+
RAS_MeshSlot(const RAS_MeshSlot& other);
5657
virtual ~RAS_MeshSlot();
5758

5859
void SetDisplayArrayBucket(RAS_DisplayArrayBucket *arrayBucket);

source/gameengine/Rasterizer/RAS_MeshUser.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ RAS_MeshUser::RAS_MeshUser(void *clientobj, RAS_BoundingBox *boundingBox)
4444

4545
RAS_MeshUser::~RAS_MeshUser()
4646
{
47-
for (RAS_MeshSlot *ms : m_meshSlots) {
48-
delete ms;
49-
}
5047
m_meshSlots.clear();
5148

5249
m_boundingBox->RemoveUser();
@@ -57,9 +54,9 @@ RAS_MeshUser::~RAS_MeshUser()
5754
}
5855
}
5956

60-
void RAS_MeshUser::AddMeshSlot(RAS_MeshSlot *meshSlot)
57+
void RAS_MeshUser::NewMeshSlot(RAS_DisplayArrayBucket *arrayBucket)
6158
{
62-
m_meshSlots.push_back(meshSlot);
59+
m_meshSlots.emplace_back(this, arrayBucket);
6360
}
6461

6562
bool RAS_MeshUser::GetFrontFace() const
@@ -87,7 +84,7 @@ void *RAS_MeshUser::GetClientObject() const
8784
return m_clientObject;
8885
}
8986

90-
RAS_MeshSlotList& RAS_MeshUser::GetMeshSlots()
87+
std::vector<RAS_MeshSlot>& RAS_MeshUser::GetMeshSlots()
9188
{
9289
return m_meshSlots;
9390
}
@@ -122,7 +119,7 @@ void RAS_MeshUser::SetBatchGroup(RAS_BatchGroup *batchGroup)
122119

123120
void RAS_MeshUser::ActivateMeshSlots()
124121
{
125-
for (RAS_MeshSlot *ms : m_meshSlots) {
126-
ms->m_displayArrayBucket->ActivateMesh(ms);
122+
for (RAS_MeshSlot& ms : m_meshSlots) {
123+
ms.m_displayArrayBucket->ActivateMesh(&ms);
127124
}
128125
}

source/gameengine/Rasterizer/RAS_MeshUser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ class RAS_MeshUser : public mt::SimdClassAllocator
4848
/// Client object owner of this mesh user.
4949
void *m_clientObject;
5050
/// Unique mesh slots used for render of this object.
51-
RAS_MeshSlotList m_meshSlots;
51+
std::vector<RAS_MeshSlot> m_meshSlots;
5252
/// Possible batching groups shared between mesh users.
5353
RAS_BatchGroup *m_batchGroup;
5454

5555
public:
5656
RAS_MeshUser(void *clientobj, RAS_BoundingBox *boundingBox);
5757
virtual ~RAS_MeshUser();
5858

59-
void AddMeshSlot(RAS_MeshSlot *meshSlot);
59+
void NewMeshSlot(RAS_DisplayArrayBucket *arrayBucket);
6060
bool GetFrontFace() const;
6161
const mt::vec4& GetColor() const;
6262
float *GetMatrix();
6363
RAS_BoundingBox *GetBoundingBox() const;
6464
void *GetClientObject() const;
65-
RAS_MeshSlotList& GetMeshSlots();
65+
std::vector<RAS_MeshSlot>& GetMeshSlots();
6666
RAS_BatchGroup *GetBatchGroup() const;
6767

6868
void SetFrontFace(bool frontFace);

0 commit comments

Comments
 (0)