Monday, December 6, 2010

DDS from file on XNA 4.0

In XNA 4.0 the Texture.FromFile() and Texture.Save() methods where removed letting us with no option on directly loading or saving dds files in our applications, we can load dds files trough the content pipeline but for an editor or for any application/game where you can create or even add content dynamically this is simply not an option.
For this reason I created a small dds manipulation library for XNA 4.0, it can load almost any Directx9 dds type that containes a 2d texture a volume texture or a cube-map and it can save Texture2D,Texture3D and TextureCube with all the available XNA surface formats except for HdrBlendable.
It will load mip-maps only if the data contains a complete chain, this is for consistency with the rest of the XNA 4.0 framework, if an incomplete mip-map chain is discovered only the main surface will be loaded.


Save a Texture2D or a TextureCube as a dds to a file: 
DDSLib.DDSToFile("texture.dds", true, texture, false); 

Save a Texture2D or a TextureCube as a dds to a stream: 
DDSLib.DDSToStream(stream,0, true, texture); 

Load a dds from a file:
DDSLib.DDSFromFile("texture.dds", GraphicsDevice,true, out texture); 

Load a dds from a stream:
DDSLib.DDSFromStream(stream,0, GraphicsDevice,true, out texture); 

Get the size in bytes for a mip-map level: 
DDSLib.GetDataByteSize(texture, level)

DDS Formats it can read :
Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,
A1R5G5B5,R5G6B5,A8,FP32/Single,FP16/HalfSingle,FP32x4/Vector4,
FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,
Q8VW8V8U8/NormalizedByte4/8888QWVU,HalfVector2/G16R16F/16.16fGR,
Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,
A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64

DDS Formats it can write:
Dxt1,Dxt3,Dxt5,A8R8G8B8/Color,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,
CxV8U8/NormalizedByte2/CxVU,
Q8VW8V8U8/NormalizedByte4/8888QWVU,HalfVector2/G16R16F/16.16fGR,
Vector2/G32R32F,G16R16/RG32/1616GB,A8B8G8R8,
A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64

It has support for cube-maps.
It has support for volume textures.

It is released under the MIT/X11 license as a result you can do whatever you want with it, you just have to mention me on the credits. 
If you download it you agree with the terms of the license. 

License :
You can download it from here : 
http://cid-58baf3506093b62a.office.live.com/self.aspx/.Documents/DDSLib/DDSLib.cs

Notes :
-For the A2B10G10R10 known in XNA as Rgba1010102 there is a problem the RED and BLUE masks are backwards from the actual data ordering, this is for consistency with XNA and Directx, if you try to open such files in an editor the red and blue channel might get changed with each other, but if you open them with the Directx Texture Tool or trough the XNA Content Pipeline they will look as they should.
-At some point I will release  a standalone c# dds reader for the gdi+.

If you have something to say live a comment here or send me an email on kiki_karon@yahoo.com
I hope the gods of coding watch over you :)

7 comments:

  1. - Version 1.699.
    - Fixed many bugs, thanks for the feedback.
    -Added support for a few other kinds of dds, at this point you should be able to load/save all the available surfaces in XNA except for HdrBlendable as it has no strict definition.

    ReplyDelete
  2. -Version 1.8.
    -Added support for volume textures.

    ReplyDelete
  3. -Version 1.8.6
    -Fixed a bug regarding not loading the data in some condition if the user didn't wanted to load the mip-maps.
    -Fixed a bug regarding saving compressed files.

    ReplyDelete
  4. This is so cool. Thanks!

    I'm wondering if you've added any functions for returning a .net Bitmap/Image object so that you can manipulate DDS from win forms without XNA.

    I don't really use XNA, but I will investigate adding functions that return intrinsic .net image objects instead of XNA. You already do the hard work of parsing the dds files :)

    ReplyDelete
  5. It is doable but I was a little to lazy/busy.

    ReplyDelete
  6. Rot in hell you red pinko b@stard

    ReplyDelete