Thursday, February 28, 2008

XNA Texture2D.FromFile too slow for background texture loading?

I've been working for some time on rendering with XNA GIS data, especialy WMS tiles. I tried to do something like NASA World Wind or Virtual Earth. I downloaded png files from the WMS server and loaded them using Texture2D.FromFile method. Because there were so many textures, I had to develop a method to load/unload them at runtime.

The problem was that Texture2D.FromFile performance was very poor. I did some research, and saw that NASA World Wind caches the textures in a .dds file. Direct Draw Surface format.It had to work better with dds files than with png (which is a compressed format by the way, and of course we expect the decoding to take some time...).

So I tried to convert the received png file into a dds format, in order to increase tile cache access speed when loading/unloading images. I did something like this:

Texture = Texture2D.FromFile(device, "image.png");
Texture.Save("image.dds", ImageFileFormat.Dds);

When calling Texture2D.FromFile(XnaFactory.Device, "image.Dds"), the performance was greatly improved. When you load the texture at first, from the png file, or whatever other format, you can also specify TextureCreationParameters. For example I use
TextureCreationParameters.SurfaceFormat = SurfaceFormat.Dxt1 so the textures will be compressed.

Also, the resulting dds file would give you a good approximation about how much memory would be used in the graphic card for the texture.