Why are these textures taking up so much memory..


Someone on the newsgroups was recently asking why loading his simple 800×600 texture was taking up so much memory (more than 5 megs).  He was using code something like the following:


Texture myTexture = TextureLoader.FromFile(myDevice, somePath);


The quest seemed interesting enough for me to post my response here as well as in the newsgroup since this reinforces my belief that you should always know what the methods you’re calling ‘cost’.  It wouldn’t surprise me to find out that most people don’t realize all the things that go in with that simple line above.


So here was my response:


First, if you’re texture is really 800×600, using this overload (TextureLoader.FromFile) will ‘upscale’ the texture so that it is square and a power of two, in this case, 1024×1024..  Assuming 32bit color for each pixel, the default mip level would be:

1024x1024x4 = 4 megs of pixel data.

 

Now, this overload would also create a mipmap chain down to 1×1, so you would also have:

512x512x4 = 1 meg of pixel data

256x256x4 = 256k of pixel data

128x128x4 = 64k of pixel data

64x64x4 = 16k of pixel data

32x32x4 = 4k of pixel data

16x16x4 = 1k of pixel data

8x8x4 = 256 bytes of pixel data

4x4x4 = 64 bytes of pixel data

2x2x4 = 16 bytes of pixel data

1x1x4 = 4 bytes of pixel data

 

 

So, add it all up, and each texture you load would take (on average) 5.6 megs of pixel data..  So the numbers make sense to me..

Comments (4)

  1. Chris C. says:

    It’s hard to know what each function does Tom, when the documentation isn’t very substantial and neither is the comments. The comment for the said method is "Creates a texture from a file", there is no mention of it performing power of 2 scaling or about it creating a mipmap chain. Even in the MSDN help it doesn’t explicitly state that the method creates a mipmap chain or does any power of 2 conversion, it hints at it by making suggests about speeding up loading times. I’m guessing that users of C++/DirectX are familiar with such quirks and the C++ documentation already explains this, but MDX needs to catch up as people don’t want to rely on C++ docs when writing code for C#. Not to go on, but your book also doesn’t mention these quirks Tom.

  2. me says:

    So… is there a better way to load these big textures?