Search code examples
windows-phone-8texture2dsharpdxdds-format

SharpDX load a texture in Windows Phone 8


I'm creating a Windows Phone 8 app and I'm using SharpDX to load a texture.

I've tried to load PNG using Content.Load<Texture2D>("filename.png") but I've got errors and I've realized that SharpDX in Windows Phone 8 only accepts DDS textures and doesn't accept PNG/JPEG/BMP etc. I've converted my PNG to DDS using texconv filename.png and added the file to my solution just like other files that I'm using in my project (that I can load correctly). When I run my app, I'm getting an exception:

{SharpDX.Serialization.InvalidChunkException: Unexpected chunk [DDS /0x20534444] instead of [TKFX/0x58464B54] at SharpDX.Serialization.BinarySerializer.BeginChunk(FourCC chunkId)}

Well, I've converted my valid PNG to DDS using the texconv tool. From what I see, it's a file format error, and the fourCC code corresponds to DDS, which is as it should be. But SharpDX expects TKFX format (toolkit effect?) instead of DDS. Why? I am trying to load a texture with Content.Load<Texture2D> not Content.Load<Effect> (I also have it to load a shader and it works perfectly).

If anyone finds out how to load a PNG instead of DDS, that's even better!


Solution

  • In case you still want to load PNG images, I'll just leave this code snippet here:

    public static Texture2D FromImage(BitmapImage image, GraphicsDevice device)
    {
        WriteableBitmap bitmap = new WriteableBitmap(image);
    
        return FromImageData(bitmap.Pixels, bitmap.PixelWidth, 
                                bitmap.PixelHeight, device);
    }
    
    public static Texture2D FromImageData(int[] data, int width, int height, GraphicsDevice device)
    {
        Texture2D texture = Texture2D.New(device, width, height,
                                        PixelFormat.B8G8R8A8.UNorm);
    
        texture.SetData<int>(data);
    
        return texture;
    }