Skip to content

Commit

Permalink
UPBGE: Don't allocate polygons on heap.
Browse files Browse the repository at this point in the history
Polygons were allocated on heap without any request of this
as they are allocated only at the game conversion.
To simplify the memory managment and cache performance, the polygons
are used as value instead of pointer in RAS_MeshObject::m_polygons.
In the same time the getter functions of RAS_Polygons are qualified
const.
  • Loading branch information
panzergame committed Aug 22, 2017
1 parent 54424e1 commit a18be87
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
29 changes: 12 additions & 17 deletions source/gameengine/Rasterizer/RAS_MeshObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ RAS_MeshObject::RAS_MeshObject(Mesh *mesh, const LayersInfo& layersInfo)

RAS_MeshObject::~RAS_MeshObject()
{
std::vector<RAS_Polygon *>::iterator it;

for (it = m_polygons.begin(); it != m_polygons.end(); it++)
delete (*it);

m_sharedvertex_map.clear();
m_polygons.clear();

Expand Down Expand Up @@ -167,9 +162,9 @@ int RAS_MeshObject::NumPolygons()
return m_polygons.size();
}

RAS_Polygon *RAS_MeshObject::GetPolygon(int num) const
RAS_Polygon *RAS_MeshObject::GetPolygon(int num)
{
return m_polygons[num];
return &m_polygons[num];
}

std::string& RAS_MeshObject::GetName()
Expand Down Expand Up @@ -216,15 +211,14 @@ RAS_Polygon *RAS_MeshObject::AddPolygon(RAS_MeshMaterial *meshmat, int numverts,

// create a new polygon
RAS_IDisplayArray *darray = meshmat->GetDisplayArray();
RAS_Polygon *poly = new RAS_Polygon(bucket, darray, numverts);
m_polygons.push_back(poly);
RAS_Polygon poly(bucket, darray, numverts);

poly->SetVisible(visible);
poly->SetCollider(collider);
poly->SetTwoside(twoside);
poly.SetVisible(visible);
poly.SetCollider(collider);
poly.SetTwoside(twoside);

for (unsigned short i = 0; i < numverts; ++i) {
poly->SetVertexOffset(i, indices[i]);
poly.SetVertexOffset(i, indices[i]);
}

if (visible && !bucket->IsWire()) {
Expand All @@ -241,7 +235,8 @@ RAS_Polygon *RAS_MeshObject::AddPolygon(RAS_MeshMaterial *meshmat, int numverts,
}
}

return poly;
m_polygons.push_back(poly);
return &m_polygons.back();
}

unsigned int RAS_MeshObject::AddVertex(
Expand Down Expand Up @@ -459,10 +454,10 @@ void RAS_MeshObject::SortPolygons(RAS_IDisplayArray *array, const MT_Transform &

bool RAS_MeshObject::HasColliderPolygon()
{
int numpolys = NumPolygons();
for (int p = 0; p < numpolys; p++) {
if (m_polygons[p]->IsCollider())
for (const RAS_Polygon& poly : m_polygons) {
if (poly.IsCollider()) {
return true;
}
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Rasterizer/RAS_MeshObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RAS_MeshObject

LayersInfo m_layersInfo;

std::vector<RAS_Polygon *> m_polygons;
std::vector<RAS_Polygon> m_polygons;

/* polygon sorting */
struct polygonSlot;
Expand Down Expand Up @@ -149,7 +149,7 @@ class RAS_MeshObject
const float *GetVertexLocation(unsigned int orig_index);

int NumPolygons();
RAS_Polygon *GetPolygon(int num) const;
RAS_Polygon *GetPolygon(int num);

RAS_BoundingBox *GetBoundingBox() const;
// buckets
Expand Down
18 changes: 9 additions & 9 deletions source/gameengine/Rasterizer/RAS_Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RAS_Polygon::RAS_Polygon(RAS_MaterialBucket *bucket, RAS_IDisplayArray *darray,
m_offset[0] = m_offset[1] = m_offset[2] = m_offset[3] = 0;
}

int RAS_Polygon::VertexCount()
int RAS_Polygon::VertexCount() const
{
return m_numvert;
}
Expand All @@ -56,22 +56,22 @@ void RAS_Polygon::SetVertexOffset(int i, unsigned int offset)
m_offset[i] = offset;
}

RAS_ITexVert *RAS_Polygon::GetVertex(int i)
RAS_ITexVert *RAS_Polygon::GetVertex(int i) const
{
return m_darray->GetVertex(m_offset[i]);
}

RAS_TexVertInfo& RAS_Polygon::GetVertexInfo(unsigned int i)
const RAS_TexVertInfo& RAS_Polygon::GetVertexInfo(unsigned int i) const
{
return m_darray->GetVertexInfo(m_offset[i]);
}

unsigned int RAS_Polygon::GetVertexOffset(unsigned int i)
unsigned int RAS_Polygon::GetVertexOffset(unsigned int i) const
{
return m_offset[i];
}

bool RAS_Polygon::IsVisible()
bool RAS_Polygon::IsVisible() const
{
return (m_polyflags & VISIBLE) != 0;
}
Expand All @@ -86,7 +86,7 @@ void RAS_Polygon::SetVisible(bool visible)
}
}

bool RAS_Polygon::IsCollider()
bool RAS_Polygon::IsCollider() const
{
return (m_polyflags & COLLIDER) != 0;
}
Expand All @@ -101,7 +101,7 @@ void RAS_Polygon::SetCollider(bool visible)
}
}

bool RAS_Polygon::IsTwoside()
bool RAS_Polygon::IsTwoside() const
{
return (m_polyflags & TWOSIDE) != 0;
}
Expand All @@ -116,12 +116,12 @@ void RAS_Polygon::SetTwoside(bool twoside)
}
}

RAS_MaterialBucket *RAS_Polygon::GetMaterial()
RAS_MaterialBucket *RAS_Polygon::GetMaterial() const
{
return m_bucket;
}

RAS_IDisplayArray *RAS_Polygon::GetDisplayArray()
RAS_IDisplayArray *RAS_Polygon::GetDisplayArray() const
{
return m_darray;
}
18 changes: 9 additions & 9 deletions source/gameengine/Rasterizer/RAS_Polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ class RAS_Polygon
{
}

int VertexCount();
RAS_ITexVert *GetVertex(int i);
RAS_TexVertInfo& GetVertexInfo(unsigned int i);
int VertexCount() const;
RAS_ITexVert *GetVertex(int i) const;
const RAS_TexVertInfo& GetVertexInfo(unsigned int i) const;

void SetVertexOffset(int i, unsigned int offset);
unsigned int GetVertexOffset(unsigned int i);
unsigned int GetVertexOffset(unsigned int i) const;

bool IsVisible();
bool IsVisible() const;
void SetVisible(bool visible);

bool IsCollider();
bool IsCollider() const;
void SetCollider(bool collider);

bool IsTwoside();
bool IsTwoside() const;
void SetTwoside(bool twoside);

RAS_MaterialBucket *GetMaterial();
RAS_IDisplayArray *GetDisplayArray();
RAS_MaterialBucket *GetMaterial() const;
RAS_IDisplayArray *GetDisplayArray() const;
};

#endif
Expand Down

3 comments on commit a18be87

@DCubix
Copy link
Collaborator

@DCubix DCubix commented on a18be87 Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't that limit the amount of polygons since the stack memory size is limited?

@panzergame
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i mean on heapis allocating manually on heap, because internally std::vector allocate on heap an array.

@DCubix
Copy link
Collaborator

@DCubix DCubix commented on a18be87 Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, I forgot about that 😛

Please sign in to comment.