Jump to content
  • Advertisement
Sign in to follow this  
oracle1

[XNA] Texture2D load optimalization

This topic is 3166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i've compared some codes loading image (png) from file to Texture2D object And the fastest of them was:
                    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(files));
                    Texture2D customTexture = new Texture2D(graphics.GraphicsDevice, bitmap.Width, bitmap.Height, 0, TextureUsage.None, SurfaceFormat.Color);

                    System.Drawing.Imaging.BitmapData data =
                        bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
                    byte[] bytes = new byte[data.Height * data.Stride];
                    Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
                    customTexture.SetData(bytes);
                    bitmap.UnlockBits(data);

FromFile takes about 1000-2000 seconds to load 20 images, iterating through pixels takes about 900ms and function above takes 700ms. Well this is not bad but loading these files into (System.Drawing) Bitmap takes 500ms and to Image about 140ms wich is stunning result. Isn't there any way to copy textures from Image and not Bitmap?

Share this post


Link to post
Share on other sites
Advertisement
This isn't surprising, since the Texture2D file loader typically does a lot more than just load the raw image data (such as scaling to power-of-2 dimensions and mipmap generation).

Also the Bitmap class has a constructor that takes a file path.

Share this post


Link to post
Share on other sites
If you want really fast file loading use the DirectX standard .dds file format, ideally with DXT compression to make the file much smaller. That should result a minimal quantity of work to do other than reading in the file.

The texture tool that comes with the SDK can create .dds files.

Share this post


Link to post
Share on other sites
Well there is no way to use other formats, since it will be image viewer, it would be useless without support of png,bmp,jpg

EDIT:
texture won't load if image is not a bitmap, when i try to set data it gives me an error:
The size of the data passed in is too large or too small for this resource.

[Source="C#"]
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(files);
[/Source]


[Edited by - oracle1 on April 13, 2010 3:27:26 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!