Skip to content

Commit

Permalink
UPBGE: Don't directly allocate on heap RAS_MeshSlot.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
panzergame committed Jun 17, 2018
1 parent 20e9ff0 commit 822bd35
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
3 changes: 1 addition & 2 deletions source/gameengine/Ketsji/KX_FontObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ void KX_FontObject::AddMeshUser()
RAS_BucketManager *bucketManager = GetScene()->GetBucketManager();
RAS_DisplayArrayBucket *arrayBucket = bucketManager->GetTextDisplayArrayBucket();

RAS_MeshSlot *ms = new RAS_MeshSlot(m_meshUser, arrayBucket);
m_meshUser->AddMeshSlot(ms);
m_meshUser->NewMeshSlot(arrayBucket);
}

void KX_FontObject::UpdateBuckets()
Expand Down
36 changes: 18 additions & 18 deletions source/gameengine/Rasterizer/RAS_BatchGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ RAS_BatchGroup *RAS_BatchGroup::RemoveMeshUser()
return this;
}

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

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

// Store original display array bucket.
batch.m_originalDisplayArrayBucketList[slot] = origArrayBucket;
batch.m_meshSlots.push_back(slot);
batch.m_originalDisplayArrayBucketList[&slot] = origArrayBucket;
batch.m_meshSlots.push_back(&slot);

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

slot->SetDisplayArrayBucket(arrayBucket);
slot.SetDisplayArrayBucket(arrayBucket);

return true;
}

bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot *slot)
bool RAS_BatchGroup::SplitMeshSlot(RAS_MeshSlot& slot)
{
RAS_MaterialBucket *bucket = slot->m_displayArrayBucket->GetBucket();
RAS_MaterialBucket *bucket = slot.m_displayArrayBucket->GetBucket();
RAS_IPolyMaterial *material = bucket->GetPolyMaterial();

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

Batch& batch = bit->second;

RAS_DisplayArrayBucket *origArrayBucket = batch.m_originalDisplayArrayBucketList[slot];
RAS_DisplayArrayBucket *origArrayBucket = batch.m_originalDisplayArrayBucketList[&slot];

if (!origArrayBucket) {
CM_Error("could not restore mesh");
return false;
}

slot->SetDisplayArrayBucket(origArrayBucket);
slot.SetDisplayArrayBucket(origArrayBucket);

batch.m_displayArray->Split(slot->m_batchPartIndex);
batch.m_displayArray->Split(slot.m_batchPartIndex);

batch.m_originalDisplayArrayBucketList.erase(slot);
batch.m_originalDisplayArrayBucketList.erase(&slot);

slot->m_batchPartIndex = -1;
slot.m_batchPartIndex = -1;

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

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

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

bool RAS_BatchGroup::SplitMeshUser(RAS_MeshUser *meshUser)
{
for (RAS_MeshSlot *meshSlot : meshUser->GetMeshSlots()) {
for (RAS_MeshSlot& meshSlot : meshUser->GetMeshSlots()) {
if (!SplitMeshSlot(meshSlot)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Rasterizer/RAS_BatchGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ class RAS_BatchGroup
* \param mat The transform matrix to apply to vertices during merging.
* \return false on failure.
*/
bool MergeMeshSlot(Batch& batch, RAS_MeshSlot *slot, const mt::mat4& mat);
bool MergeMeshSlot(Batch& batch, RAS_MeshSlot& slot, const mt::mat4& mat);

/** Split the part representing the display array containing in the passed mesh slot.
* \param slot The mesh slot using the display array to split.
* \return false on failure.
*/
bool SplitMeshSlot(RAS_MeshSlot *slot);
bool SplitMeshSlot(RAS_MeshSlot& slot);

public:
RAS_BatchGroup();
Expand Down
3 changes: 1 addition & 2 deletions source/gameengine/Rasterizer/RAS_Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ RAS_MeshUser *RAS_Mesh::AddMeshUser(void *clientobj, RAS_Deformer *deformer)
for (unsigned short i = 0, nummat = m_materials.size(); i < nummat; ++i) {
RAS_DisplayArrayBucket *arrayBucket = (deformer) ?
deformer->GetDisplayArrayBucket(i) : m_materials[i]->GetDisplayArrayBucket();
RAS_MeshSlot *ms = new RAS_MeshSlot(meshUser, arrayBucket);
meshUser->AddMeshSlot(ms);
meshUser->NewMeshSlot(arrayBucket);
}
return meshUser;
}
Expand Down
8 changes: 8 additions & 0 deletions source/gameengine/Rasterizer/RAS_MeshSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ RAS_MeshSlot::~RAS_MeshSlot()
{
}

RAS_MeshSlot::RAS_MeshSlot(const RAS_MeshSlot& other)
:m_node(this, &dummyNodeData, &RAS_MeshSlot::RunNode, nullptr),
m_displayArrayBucket(other.m_displayArrayBucket),
m_meshUser(other.m_meshUser),
m_batchPartIndex(other.m_batchPartIndex)
{
}

void RAS_MeshSlot::SetDisplayArrayBucket(RAS_DisplayArrayBucket *arrayBucket)
{
m_displayArrayBucket = arrayBucket;
Expand Down
1 change: 1 addition & 0 deletions source/gameengine/Rasterizer/RAS_MeshSlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class RAS_MeshSlot
short m_batchPartIndex;

RAS_MeshSlot(RAS_MeshUser *meshUser, RAS_DisplayArrayBucket *arrayBucket);
RAS_MeshSlot(const RAS_MeshSlot& other);
virtual ~RAS_MeshSlot();

void SetDisplayArrayBucket(RAS_DisplayArrayBucket *arrayBucket);
Expand Down
13 changes: 5 additions & 8 deletions source/gameengine/Rasterizer/RAS_MeshUser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ RAS_MeshUser::RAS_MeshUser(void *clientobj, RAS_BoundingBox *boundingBox)

RAS_MeshUser::~RAS_MeshUser()
{
for (RAS_MeshSlot *ms : m_meshSlots) {
delete ms;
}
m_meshSlots.clear();

m_boundingBox->RemoveUser();
Expand All @@ -57,9 +54,9 @@ RAS_MeshUser::~RAS_MeshUser()
}
}

void RAS_MeshUser::AddMeshSlot(RAS_MeshSlot *meshSlot)
void RAS_MeshUser::NewMeshSlot(RAS_DisplayArrayBucket *arrayBucket)
{
m_meshSlots.push_back(meshSlot);
m_meshSlots.emplace_back(this, arrayBucket);
}

bool RAS_MeshUser::GetFrontFace() const
Expand Down Expand Up @@ -87,7 +84,7 @@ void *RAS_MeshUser::GetClientObject() const
return m_clientObject;
}

RAS_MeshSlotList& RAS_MeshUser::GetMeshSlots()
std::vector<RAS_MeshSlot>& RAS_MeshUser::GetMeshSlots()
{
return m_meshSlots;
}
Expand Down Expand Up @@ -122,7 +119,7 @@ void RAS_MeshUser::SetBatchGroup(RAS_BatchGroup *batchGroup)

void RAS_MeshUser::ActivateMeshSlots()
{
for (RAS_MeshSlot *ms : m_meshSlots) {
ms->m_displayArrayBucket->ActivateMesh(ms);
for (RAS_MeshSlot& ms : m_meshSlots) {
ms.m_displayArrayBucket->ActivateMesh(&ms);
}
}
6 changes: 3 additions & 3 deletions source/gameengine/Rasterizer/RAS_MeshUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ class RAS_MeshUser : public mt::SimdClassAllocator
/// Client object owner of this mesh user.
void *m_clientObject;
/// Unique mesh slots used for render of this object.
RAS_MeshSlotList m_meshSlots;
std::vector<RAS_MeshSlot> m_meshSlots;
/// Possible batching groups shared between mesh users.
RAS_BatchGroup *m_batchGroup;

public:
RAS_MeshUser(void *clientobj, RAS_BoundingBox *boundingBox);
virtual ~RAS_MeshUser();

void AddMeshSlot(RAS_MeshSlot *meshSlot);
void NewMeshSlot(RAS_DisplayArrayBucket *arrayBucket);
bool GetFrontFace() const;
const mt::vec4& GetColor() const;
float *GetMatrix();
RAS_BoundingBox *GetBoundingBox() const;
void *GetClientObject() const;
RAS_MeshSlotList& GetMeshSlots();
std::vector<RAS_MeshSlot>& GetMeshSlots();
RAS_BatchGroup *GetBatchGroup() const;

void SetFrontFace(bool frontFace);
Expand Down

0 comments on commit 822bd35

Please sign in to comment.