D3D Texture Memory Allocations

Started by
2 comments, last by kovacsp 18 years, 8 months ago
I'm currently working on a Direct3D framework/game engine and have run into an issue regarding retrieving the memory usage of a texture. With my texture resource cache manager I need to know how many bytes each texture is currently using in RAM, the problem is: I'm not entirely sure how to do this. Taking Width*Height*Depth will of course, not always produce the proper results do to proper compression. I have thought about checking for the DDSD_LINEARSIZE flag after retrieving the DDSURFACEDESC and using the linearsize value for compressed textures, or otherwise using the WxHxD calculation. But the problem is future hardware compatibility ( more advanced compression systems ). I need a solution. Any suggestions?
Advertisement
hi,

knowing texture size and the format of it, you can have a good estimate of the memory needed (but think about DXTn compression, for example). but they say this is not exact due to internal differences.
have a look at IDirect3DDevice9::GetAvailableTextureMem() but keep in mind that this isn't exact either, moreover it doesnt not return videomem only but the agp aperture too (as far as i know), and you might not want to load so many textures so that it goes to zero due to trashing.
anyway.. you said you write a resource cache manager? in which pool do you load textures? isn't the managed pool good enough for the purpose?

kp
------------------------------------------------------------Neo, the Matrix should be 16-byte aligned for better performance!
GetAvailableTextureMem only returns the total amount of memory available, it has nothing to do with a specific texture. But I believe I have created a solution to my problem in the form of this function:

int GetTextureMemoryUsage( LPDIRECT3DTEXTURE9 pTexture, int nLevel = 0 )
{
D3DLOCKED_RECT lockedRect;
D3DSURFACE_DESC desc;

// Attempt to retrieve the texture pitch:

if (FAILED(
pTexture->LockRect( nLevel, &lockedRect, NULL, D3DLOCK_READONLY )
))
{
return 0;
}
pTexture->UnlockRect( 0 );


// Get the texture level description:

if (FAILED( pTexture->GetLevelDesc( nLevel, &desc ) ))
{
return 0;
}

return desc.Height * lockedRect.Pitch;
}


The reasoning for the creation of this caching system is to give more control to the developer. It uses the D3D managed memory pool, but has an additional layer of functionality.
hi,

if you read the free mem before and after loading a texture, then yes, it can give you some info about the size of the texture. this is why I mentioned it.

kp
------------------------------------------------------------Neo, the Matrix should be 16-byte aligned for better performance!

This topic is closed to new replies.

Advertisement