Sign in to follow this  
Magnumwolf

Memory usage problem in Direct3D

Recommended Posts

Hi, Im having some problems with loading in models from .x files in Direct3D. The problem is that once ive loaded in my models my game is using 1.8GB of memory Im only loading 40~ low poly models, the models and textures take a total of 60 MB of space when not loaded in. I cant find any thing that would be causing memory leaks so i really have no idea whats causing this problem Here is my loading in code
        m_ModelMesh = NULL;
	m_MaterialBuffer = NULL;		
	m_NumMaterials = NULL;		
	m_MeshMaterials = NULL;
	m_MeshTextures = NULL;
	m_NumMaterials = NULL;
	
        
	std::string ModelPath = filepath + "/" + filename;
	D3DXLoadMeshFromX(ModelPath.c_str(), D3DXMESH_SYSTEMMEM, Direct3DDevice, NULL, &m_MaterialBuffer,NULL, &m_NumMaterials,
                       &m_ModelMesh);

	D3DXMATERIAL* d3dxMaterials;
	d3dxMaterials = (D3DXMATERIAL*)m_MaterialBuffer->GetBufferPointer();

	m_MeshMaterials = new D3DMATERIAL9[m_NumMaterials];
	m_MeshTextures  = new LPDIRECT3DTEXTURE9[m_NumMaterials];

	for (DWORD i=0; i<m_NumMaterials; i++)
	{	   
	   m_MeshMaterials[i] = d3dxMaterials[i].MatD3D;	  
	   m_MeshMaterials[i].Ambient = m_MeshMaterials[i].Diffuse;          
	  
	   m_MeshTextures[i] = NULL;
	   if (d3dxMaterials[i].pTextureFilename)
	   {		  
		   std::string TexturePath = filepath + "/" + d3dxMaterials[i].pTextureFilename;
		   D3DXCreateTextureFromFile(Direct3DDevice, TexturePath.c_str(), &m_MeshTextures[i]);
	   }
	}
        m_MaterialBuffer->Release();	
Does anyone have any idea's what could be causing this? Ive no doubt Direct3D should be able to easily load in 40 models without using this much memory. Thanks in advance, Magnumwolf

Share this post


Link to post
Share on other sites
How do you know how much memory you're using? Do you have memory leaks?

How big are the textures? Do multiple meshes use the same texture or meshes use multiple textures? Since you create a new set of textures for every mesh (even if they're duplicates):

On disk: 40 models + n textures
In memory: 40 models + 40 textures * number of textures per model

If you don't load the textures (just comment out the texture load code, leaving m_MeshTextures[i]==NULL), does memory usage (however you're determining that) go down?

Share this post


Link to post
Share on other sites
Im looking at Task manager to see how much memory is being used.

I commented out the texture loading code and i can confirm that this is the cause of the problem.

Most of the models have 2-3 textures each that are either 256x256 or 512x512
None of the models reuse the same textures.

Share this post


Link to post
Share on other sites
I think ive found the problem, a big problem with my loop is that it can load in the same texture twice if its used on more than 1 material so i was sometimes loading in the same texture over 100 times for 1 models. A few modifications to the loading process should have it fixed. (I Hope :D)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this