Commit patch #8724:

This patch modifies the BL_ConvertMesh method from the data conversion module in order to reduce the number of polygon
material objects that are created.

Normally, there should be only one material object for each material bucket(the group of meshes that are rendered together
with a single material). However, the number of materials that are created right now in the converter is much higher
and eats a lot of memory in scenes with large polygon counts. This patch deletes those material objects(KX_BlenderMaterial)
that are used only temporarily in the converter(and are now deleted only when the converter is destroyed, at the end
of the game).

For a cube that's subdivided 7 times(90+ k polygons) I get 200 MB usage in the game engine in 2.45 and 44 MB with a
svn build with this patch applied if the "Use Blender Materials" option is activated in the Game menu.
This commit is contained in:
Benoit Bolsee 2008-03-23 23:12:40 +00:00
parent f162428bd2
commit b20f6e27ff
6 changed files with 39 additions and 19 deletions

@ -883,6 +883,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
Material* ma = 0;
bool polyvisible = true;
RAS_IPolyMaterial* polymat = NULL;
BL_Material *bl_mat;
if(converter->GetMaterials())
{
@ -891,7 +892,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
else
ma = give_current_material(blenderobj, 1);
BL_Material *bl_mat = ConvertMaterial(mesh, ma, tface, mface, mmcol, lightlayer, blenderobj, layers);
bl_mat = ConvertMaterial(mesh, ma, tface, mface, mmcol, lightlayer, blenderobj, layers);
// set the index were dealing with
bl_mat->material_index = (int)mface->mat_nr;
@ -899,7 +900,6 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
collider = ((bl_mat->ras_mode & COLLIDER)!=0);
polymat = new KX_BlenderMaterial(scene, bl_mat, skinMesh, lightlayer, blenderobj );
converter->RegisterBlenderMaterial(bl_mat);
unsigned int rgb[4];
bl_mat->GetConversionRGB(rgb);
@ -921,8 +921,6 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
if (mface->v4)
tan3 = tangent[mface->v4];
}
// this is needed to free up memory afterwards
converter->RegisterPolyMaterial(polymat);
}
else
{
@ -1029,12 +1027,27 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
polymat->m_specular = MT_Vector3(0.0f,0.0f,0.0f);
polymat->m_shininess = 35.0;
}
// this is needed to free up memory afterwards
converter->RegisterPolyMaterial(polymat);
}
RAS_MaterialBucket* bucket = scene->FindBucket(polymat);
// see if a bucket was reused or a new one was created
// this way only one KX_BlenderMaterial object has to exist per bucket
bool bucketCreated;
RAS_MaterialBucket* bucket = scene->FindBucket(polymat, bucketCreated);
if (bucketCreated) {
// this is needed to free up memory afterwards
converter->RegisterPolyMaterial(polymat);
if(converter->GetMaterials()) {
converter->RegisterBlenderMaterial(bl_mat);
}
} else {
// delete the material objects since they are no longer needed
// from now on, use the polygon material from the material bucket
delete polymat;
if(converter->GetMaterials()) {
delete bl_mat;
}
polymat = bucket->GetPolyMaterial();
}
int nverts = mface->v4?4:3;
int vtxarray = meshobj->FindVertexArray(nverts,polymat);

@ -137,17 +137,22 @@ bool SCA_KeyboardSensor::Evaluate(CValue* eventval)
{
bool justactivated = false;
bool justreleased = false;
bool active = false;
for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
{
const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
switch (inevent.m_status)
{
case SCA_InputEvent::KX_JUSTACTIVATED:
justactivated = true;
}
if (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
{
break;
case SCA_InputEvent::KX_JUSTRELEASED:
justreleased = true;
break;
case SCA_InputEvent::KX_ACTIVE:
active = true;
break;
}
}
@ -159,7 +164,7 @@ bool SCA_KeyboardSensor::Evaluate(CValue* eventval)
{
if (justreleased)
{
m_val=0;
m_val=(active)?1:0;
result = true;
}
}

@ -1212,9 +1212,9 @@ void KX_Scene::UpdateParents(double curtime)
RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat)
RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
{
return m_bucketmanager->RAS_BucketManagerFindBucket(polymat);
return m_bucketmanager->RAS_BucketManagerFindBucket(polymat, bucketCreated);
}

@ -286,7 +286,7 @@ public:
~KX_Scene();
RAS_BucketManager* GetBucketManager();
RAS_MaterialBucket* FindBucket(RAS_IPolyMaterial* polymat);
RAS_MaterialBucket* FindBucket(RAS_IPolyMaterial* polymat, bool &bucketCreated);
void RenderBuckets(const MT_Transform& cameratransform,
RAS_IRasterizer* rasty,
RAS_IRenderTools* rendertools);

@ -156,8 +156,9 @@ void RAS_BucketManager::Renderbuckets(
RAS_MaterialBucket::EndFrame();
}
RAS_MaterialBucket* RAS_BucketManager::RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material)
RAS_MaterialBucket* RAS_BucketManager::RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material, bool &bucketCreated)
{
bucketCreated = false;
BucketList::iterator it;
for (it = m_MaterialBuckets.begin(); it != m_MaterialBuckets.end(); it++)
{
@ -172,6 +173,7 @@ RAS_MaterialBucket* RAS_BucketManager::RAS_BucketManagerFindBucket(RAS_IPolyMate
}
RAS_MaterialBucket *bucket = new RAS_MaterialBucket(material);
bucketCreated = true;
if (bucket->IsTransparant())
m_AlphaBuckets.push_back(bucket);
else

@ -61,7 +61,7 @@ public:
RAS_IRasterizer* rasty,
class RAS_IRenderTools* rendertools);
RAS_MaterialBucket* RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material);
RAS_MaterialBucket* RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material, bool &bucketCreated);
private: